Skip to content

Commit

Permalink
tests: added Weather tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrepimpao committed May 8, 2024
1 parent 8538f4b commit 84a5862
Show file tree
Hide file tree
Showing 17 changed files with 453 additions and 380 deletions.
69 changes: 0 additions & 69 deletions src/Endpoint/WeatherEndpoint.php

This file was deleted.

92 changes: 0 additions & 92 deletions src/Entity/WeatherCondition.php

This file was deleted.

27 changes: 27 additions & 0 deletions src/Test/Util/TestCollectionResponseTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace ProgrammatorDev\OpenWeatherMap\Test\Util;

use Nyholm\Psr7\Response;
use PHPUnit\Framework\Attributes\DataProvider;

trait TestCollectionResponseTrait
{
#[DataProvider(methodName: 'provideCollectionResponseData')]
public function testCollectionResponse(
string $responseClass,
string $responseBody,
string $resource,
string $method,
?array $args = null
): void
{
$this->mockClient->addResponse(new Response(
status: 200,
body: $responseBody
));

$response = $this->api->$resource()->$method(...$args);
$this->assertContainsOnlyInstancesOf($responseClass, $response);
}
}
27 changes: 27 additions & 0 deletions src/Test/Util/TestItemResponseTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace ProgrammatorDev\OpenWeatherMap\Test\Util;

use Nyholm\Psr7\Response;
use PHPUnit\Framework\Attributes\DataProvider;

trait TestItemResponseTrait
{
#[DataProvider(methodName: 'provideItemResponseData')]
public function testItemResponse(
string $responseClass,
string $responseBody,
string $resource,
string $method,
?array $args = null
): void
{
$this->mockClient->addResponse(new Response(
status: 200,
body: $responseBody
));

$response = $this->api->$resource()->$method(...$args);
$this->assertInstanceOf($responseClass, $response);
}
}
16 changes: 16 additions & 0 deletions src/Test/Util/TestValidationExceptionTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace ProgrammatorDev\OpenWeatherMap\Test\Util;

use PHPUnit\Framework\Attributes\DataProvider;
use ProgrammatorDev\Validator\Exception\ValidationException;

trait TestValidationExceptionTrait
{
#[DataProvider(methodName: 'provideValidationExceptionData')]
public function testValidationException(string $resource, string $method, ?array $args = null): void
{
$this->expectException(ValidationException::class);
$this->api->$resource()->$method(...$args);
}
}
32 changes: 17 additions & 15 deletions tests/Integration/GeocodingResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,26 @@
use ProgrammatorDev\OpenWeatherMap\Entity\Location;
use ProgrammatorDev\OpenWeatherMap\Test\AbstractTest;
use ProgrammatorDev\OpenWeatherMap\Test\MockResponse;
use ProgrammatorDev\OpenWeatherMap\Test\Util\TestExceptionsTrait;
use ProgrammatorDev\OpenWeatherMap\Test\Util\TestResponsesTrait;
use ProgrammatorDev\OpenWeatherMap\Test\Util\TestCollectionResponseTrait;
use ProgrammatorDev\OpenWeatherMap\Test\Util\TestValidationExceptionTrait;
use ProgrammatorDev\OpenWeatherMap\Test\Util\TestItemResponseTrait;

class GeocodingResourceTest extends AbstractTest
{
use TestResponsesTrait;
use TestExceptionsTrait;
use TestItemResponseTrait;
use TestCollectionResponseTrait;
use TestValidationExceptionTrait;

public static function provideItemResponseData(): \Generator
{
yield 'get by zip code' => [
ZipLocation::class,
MockResponse::GEOCODING_ZIP,
'geocoding',
'getByZipCode',
['1000-001', 'pt']
];
}

public static function provideCollectionResponseData(): \Generator
{
Expand All @@ -32,17 +45,6 @@ public static function provideCollectionResponseData(): \Generator
];
}

public static function provideItemResponseData(): \Generator
{
yield 'get by zip code' => [
ZipLocation::class,
MockResponse::GEOCODING_ZIP,
'geocoding',
'getByZipCode',
['1000-001', 'pt']
];
}

public static function provideValidationExceptionData(): \Generator
{
yield 'get by location name, blank value' => ['geocoding', 'getByLocationName', ['']];
Expand Down
2 changes: 2 additions & 0 deletions tests/Integration/OpenWeatherMapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
namespace ProgrammatorDev\OpenWeatherMap\Test\Integration;

use ProgrammatorDev\OpenWeatherMap\Resource\GeocodingResource;
use ProgrammatorDev\OpenWeatherMap\Resource\WeatherResource;
use ProgrammatorDev\OpenWeatherMap\Test\AbstractTest;

class OpenWeatherMapTest extends AbstractTest
{
public function testMethods()
{
$this->assertInstanceOf(GeocodingResource::class, $this->api->geocoding());
$this->assertInstanceOf(WeatherResource::class, $this->api->weather());
}
}
47 changes: 47 additions & 0 deletions tests/Integration/WeatherResourceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Integration;

use ProgrammatorDev\OpenWeatherMap\Entity\Weather\Weather;
use ProgrammatorDev\OpenWeatherMap\Entity\Weather\WeatherCollection;
use ProgrammatorDev\OpenWeatherMap\Test\AbstractTest;
use ProgrammatorDev\OpenWeatherMap\Test\MockResponse;
use ProgrammatorDev\OpenWeatherMap\Test\Util\TestValidationExceptionTrait;
use ProgrammatorDev\OpenWeatherMap\Test\Util\TestItemResponseTrait;

class WeatherResourceTest extends AbstractTest
{
use TestItemResponseTrait;
use TestValidationExceptionTrait;

public static function provideItemResponseData(): \Generator
{
yield 'get current' => [
Weather::class,
MockResponse::WEATHER_CURRENT,
'weather',
'getCurrent',
[50, 50]
];
yield 'get forecast' => [
WeatherCollection::class,
MockResponse::WEATHER_FORECAST,
'weather',
'getForecast',
[50, 50]
];
}

public static function provideValidationExceptionData(): \Generator
{
yield 'get current, latitude lower than -90' => ['weather', 'getCurrent', [-91, 50]];
yield 'get current, latitude greater than 90' => ['weather', 'getCurrent', [91, 50]];
yield 'get current, longitude lower than -180' => ['weather', 'getCurrent', [50, -181]];
yield 'get current, longitude greater than 180' => ['weather', 'getCurrent', [50, 181]];
yield 'get forecast, latitude lower than -90' => ['weather', 'getForecast', [-91, 50]];
yield 'get forecast, latitude greater than 90' => ['weather', 'getForecast', [91, 50]];
yield 'get forecast, longitude lower than -180' => ['weather', 'getForecast', [50, -181]];
yield 'get forecast, longitude greater than 180' => ['weather', 'getForecast', [50, 181]];
yield 'get forecast, zero num results' => ['weather', 'getForecast', [50, 50, 0]];
}
}
26 changes: 26 additions & 0 deletions tests/Unit/ConditionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace ProgrammatorDev\OpenWeatherMap\Test\Unit;

use ProgrammatorDev\OpenWeatherMap\Entity\Condition;
use ProgrammatorDev\OpenWeatherMap\Entity\Icon;
use ProgrammatorDev\OpenWeatherMap\Test\AbstractTest;

class ConditionTest extends AbstractTest
{
public function testMethods()
{
$entity = new Condition([
'id' => 200,
'main' => 'name',
'description' => 'description',
'icon' => '01d'
]);

$this->assertSame(200, $entity->getId());
$this->assertSame('name', $entity->getName());
$this->assertSame('description', $entity->getDescription());
$this->assertInstanceOf(Icon::class, $entity->getIcon());
$this->assertSame('THUNDERSTORM', $entity->getSystemName());
}
}
Loading

0 comments on commit 84a5862

Please sign in to comment.