Skip to content

Commit 84a5862

Browse files
committed
tests: added Weather tests
1 parent 8538f4b commit 84a5862

17 files changed

+453
-380
lines changed

src/Endpoint/WeatherEndpoint.php

Lines changed: 0 additions & 69 deletions
This file was deleted.

src/Entity/WeatherCondition.php

Lines changed: 0 additions & 92 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\OpenWeatherMap\Test\Util;
4+
5+
use Nyholm\Psr7\Response;
6+
use PHPUnit\Framework\Attributes\DataProvider;
7+
8+
trait TestCollectionResponseTrait
9+
{
10+
#[DataProvider(methodName: 'provideCollectionResponseData')]
11+
public function testCollectionResponse(
12+
string $responseClass,
13+
string $responseBody,
14+
string $resource,
15+
string $method,
16+
?array $args = null
17+
): void
18+
{
19+
$this->mockClient->addResponse(new Response(
20+
status: 200,
21+
body: $responseBody
22+
));
23+
24+
$response = $this->api->$resource()->$method(...$args);
25+
$this->assertContainsOnlyInstancesOf($responseClass, $response);
26+
}
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\OpenWeatherMap\Test\Util;
4+
5+
use Nyholm\Psr7\Response;
6+
use PHPUnit\Framework\Attributes\DataProvider;
7+
8+
trait TestItemResponseTrait
9+
{
10+
#[DataProvider(methodName: 'provideItemResponseData')]
11+
public function testItemResponse(
12+
string $responseClass,
13+
string $responseBody,
14+
string $resource,
15+
string $method,
16+
?array $args = null
17+
): void
18+
{
19+
$this->mockClient->addResponse(new Response(
20+
status: 200,
21+
body: $responseBody
22+
));
23+
24+
$response = $this->api->$resource()->$method(...$args);
25+
$this->assertInstanceOf($responseClass, $response);
26+
}
27+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\OpenWeatherMap\Test\Util;
4+
5+
use PHPUnit\Framework\Attributes\DataProvider;
6+
use ProgrammatorDev\Validator\Exception\ValidationException;
7+
8+
trait TestValidationExceptionTrait
9+
{
10+
#[DataProvider(methodName: 'provideValidationExceptionData')]
11+
public function testValidationException(string $resource, string $method, ?array $args = null): void
12+
{
13+
$this->expectException(ValidationException::class);
14+
$this->api->$resource()->$method(...$args);
15+
}
16+
}

tests/Integration/GeocodingResourceTest.php

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,26 @@
66
use ProgrammatorDev\OpenWeatherMap\Entity\Location;
77
use ProgrammatorDev\OpenWeatherMap\Test\AbstractTest;
88
use ProgrammatorDev\OpenWeatherMap\Test\MockResponse;
9-
use ProgrammatorDev\OpenWeatherMap\Test\Util\TestExceptionsTrait;
10-
use ProgrammatorDev\OpenWeatherMap\Test\Util\TestResponsesTrait;
9+
use ProgrammatorDev\OpenWeatherMap\Test\Util\TestCollectionResponseTrait;
10+
use ProgrammatorDev\OpenWeatherMap\Test\Util\TestValidationExceptionTrait;
11+
use ProgrammatorDev\OpenWeatherMap\Test\Util\TestItemResponseTrait;
1112

1213
class GeocodingResourceTest extends AbstractTest
1314
{
14-
use TestResponsesTrait;
15-
use TestExceptionsTrait;
15+
use TestItemResponseTrait;
16+
use TestCollectionResponseTrait;
17+
use TestValidationExceptionTrait;
18+
19+
public static function provideItemResponseData(): \Generator
20+
{
21+
yield 'get by zip code' => [
22+
ZipLocation::class,
23+
MockResponse::GEOCODING_ZIP,
24+
'geocoding',
25+
'getByZipCode',
26+
['1000-001', 'pt']
27+
];
28+
}
1629

1730
public static function provideCollectionResponseData(): \Generator
1831
{
@@ -32,17 +45,6 @@ public static function provideCollectionResponseData(): \Generator
3245
];
3346
}
3447

35-
public static function provideItemResponseData(): \Generator
36-
{
37-
yield 'get by zip code' => [
38-
ZipLocation::class,
39-
MockResponse::GEOCODING_ZIP,
40-
'geocoding',
41-
'getByZipCode',
42-
['1000-001', 'pt']
43-
];
44-
}
45-
4648
public static function provideValidationExceptionData(): \Generator
4749
{
4850
yield 'get by location name, blank value' => ['geocoding', 'getByLocationName', ['']];

tests/Integration/OpenWeatherMapTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
namespace ProgrammatorDev\OpenWeatherMap\Test\Integration;
44

55
use ProgrammatorDev\OpenWeatherMap\Resource\GeocodingResource;
6+
use ProgrammatorDev\OpenWeatherMap\Resource\WeatherResource;
67
use ProgrammatorDev\OpenWeatherMap\Test\AbstractTest;
78

89
class OpenWeatherMapTest extends AbstractTest
910
{
1011
public function testMethods()
1112
{
1213
$this->assertInstanceOf(GeocodingResource::class, $this->api->geocoding());
14+
$this->assertInstanceOf(WeatherResource::class, $this->api->weather());
1315
}
1416
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Integration;
4+
5+
use ProgrammatorDev\OpenWeatherMap\Entity\Weather\Weather;
6+
use ProgrammatorDev\OpenWeatherMap\Entity\Weather\WeatherCollection;
7+
use ProgrammatorDev\OpenWeatherMap\Test\AbstractTest;
8+
use ProgrammatorDev\OpenWeatherMap\Test\MockResponse;
9+
use ProgrammatorDev\OpenWeatherMap\Test\Util\TestValidationExceptionTrait;
10+
use ProgrammatorDev\OpenWeatherMap\Test\Util\TestItemResponseTrait;
11+
12+
class WeatherResourceTest extends AbstractTest
13+
{
14+
use TestItemResponseTrait;
15+
use TestValidationExceptionTrait;
16+
17+
public static function provideItemResponseData(): \Generator
18+
{
19+
yield 'get current' => [
20+
Weather::class,
21+
MockResponse::WEATHER_CURRENT,
22+
'weather',
23+
'getCurrent',
24+
[50, 50]
25+
];
26+
yield 'get forecast' => [
27+
WeatherCollection::class,
28+
MockResponse::WEATHER_FORECAST,
29+
'weather',
30+
'getForecast',
31+
[50, 50]
32+
];
33+
}
34+
35+
public static function provideValidationExceptionData(): \Generator
36+
{
37+
yield 'get current, latitude lower than -90' => ['weather', 'getCurrent', [-91, 50]];
38+
yield 'get current, latitude greater than 90' => ['weather', 'getCurrent', [91, 50]];
39+
yield 'get current, longitude lower than -180' => ['weather', 'getCurrent', [50, -181]];
40+
yield 'get current, longitude greater than 180' => ['weather', 'getCurrent', [50, 181]];
41+
yield 'get forecast, latitude lower than -90' => ['weather', 'getForecast', [-91, 50]];
42+
yield 'get forecast, latitude greater than 90' => ['weather', 'getForecast', [91, 50]];
43+
yield 'get forecast, longitude lower than -180' => ['weather', 'getForecast', [50, -181]];
44+
yield 'get forecast, longitude greater than 180' => ['weather', 'getForecast', [50, 181]];
45+
yield 'get forecast, zero num results' => ['weather', 'getForecast', [50, 50, 0]];
46+
}
47+
}

tests/Unit/ConditionTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\OpenWeatherMap\Test\Unit;
4+
5+
use ProgrammatorDev\OpenWeatherMap\Entity\Condition;
6+
use ProgrammatorDev\OpenWeatherMap\Entity\Icon;
7+
use ProgrammatorDev\OpenWeatherMap\Test\AbstractTest;
8+
9+
class ConditionTest extends AbstractTest
10+
{
11+
public function testMethods()
12+
{
13+
$entity = new Condition([
14+
'id' => 200,
15+
'main' => 'name',
16+
'description' => 'description',
17+
'icon' => '01d'
18+
]);
19+
20+
$this->assertSame(200, $entity->getId());
21+
$this->assertSame('name', $entity->getName());
22+
$this->assertSame('description', $entity->getDescription());
23+
$this->assertInstanceOf(Icon::class, $entity->getIcon());
24+
$this->assertSame('THUNDERSTORM', $entity->getSystemName());
25+
}
26+
}

0 commit comments

Comments
 (0)