Skip to content

Commit c88e365

Browse files
committed
tests: added geocoding resource tests
1 parent 17d3153 commit c88e365

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

src/Test/Util/TestExceptionsTrait.php

+16
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 TestExceptionsTrait
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+
}

src/Test/Util/TestResponsesTrait.php

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\OpenWeatherMap\Test\Util;
4+
5+
use Nyholm\Psr7\Response;
6+
use PHPUnit\Framework\Attributes\DataProvider;
7+
8+
trait TestResponsesTrait
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+
28+
#[DataProvider(methodName: 'provideItemResponseData')]
29+
public function testItemResponse(
30+
string $responseClass,
31+
string $responseBody,
32+
string $resource,
33+
string $method,
34+
?array $args = null
35+
): void
36+
{
37+
$this->mockClient->addResponse(new Response(
38+
status: 200,
39+
body: $responseBody
40+
));
41+
42+
$response = $this->api->$resource()->$method(...$args);
43+
$this->assertInstanceOf($responseClass, $response);
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\OpenWeatherMap\Test\Integration;
4+
5+
use ProgrammatorDev\OpenWeatherMap\Entity\Location;
6+
use ProgrammatorDev\OpenWeatherMap\Test\AbstractTest;
7+
use ProgrammatorDev\OpenWeatherMap\Test\MockResponse;
8+
use ProgrammatorDev\OpenWeatherMap\Test\Util\TestExceptionsTrait;
9+
use ProgrammatorDev\OpenWeatherMap\Test\Util\TestResponsesTrait;
10+
11+
class GeocodingResourceTest extends AbstractTest
12+
{
13+
use TestResponsesTrait;
14+
use TestExceptionsTrait;
15+
16+
public static function provideCollectionResponseData(): \Generator
17+
{
18+
yield 'get by location name' => [
19+
Location::class,
20+
MockResponse::GEOCODING_DIRECT,
21+
'geocoding',
22+
'getByLocationName',
23+
['test']
24+
];
25+
yield 'get by coordinate' => [
26+
Location::class,
27+
MockResponse::GEOCODING_REVERSE,
28+
'geocoding',
29+
'getByCoordinate',
30+
[50, 50]
31+
];
32+
}
33+
34+
public static function provideItemResponseData(): \Generator
35+
{
36+
yield 'get by zip code' => [
37+
Location::class,
38+
MockResponse::GEOCODING_ZIP,
39+
'geocoding',
40+
'getByZipCode',
41+
['1000-001', 'pt']
42+
];
43+
}
44+
45+
public static function provideValidationExceptionData(): \Generator
46+
{
47+
yield 'get by location name, blank value' => ['geocoding', 'getByLocationName', ['']];
48+
yield 'get by location name, zero num results' => ['geocoding', 'getByLocationName', ['test', 0]];
49+
yield 'get by coordinate, latitude lower than -90' => ['geocoding', 'getByCoordinate', [-91, 50]];
50+
yield 'get by coordinate, latitude greater than 90' => ['geocoding', 'getByCoordinate', [91, 50]];
51+
yield 'get by coordinate, longitude lower than -180' => ['geocoding', 'getByCoordinate', [50, -181]];
52+
yield 'get by coordinate, longitude greater than 180' => ['geocoding', 'getByCoordinate', [50, 181]];
53+
yield 'get by coordinate, zero num results' => ['geocoding', 'getByCoordinate', [50, 50, 0]];
54+
yield 'get by zip code, blank zip code' => ['geocoding', 'getByZipCode', ['', 'pt']];
55+
yield 'get by zip code, blank country code' => ['geocoding', 'getByZipCode', ['1000-001', '']];
56+
yield 'get by zip code, invalid country code' => ['geocoding', 'getByZipCode', ['1000-001', 'invalid']];
57+
}
58+
}

0 commit comments

Comments
 (0)