Skip to content

Commit e52d3df

Browse files
committed
tests: added api error tests
1 parent 71aa3a3 commit e52d3df

File tree

2 files changed

+56
-38
lines changed

2 files changed

+56
-38
lines changed

tests/ApiErrorTest_.php

-38
This file was deleted.

tests/Integration/ResourceTest.php

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\OpenWeatherMap\Test\Integration;
4+
5+
use Nyholm\Psr7\Response;
6+
use PHPUnit\Framework\Attributes\DataProvider;
7+
use ProgrammatorDev\Api\Method;
8+
use ProgrammatorDev\OpenWeatherMap\Exception\BadRequestException;
9+
use ProgrammatorDev\OpenWeatherMap\Exception\NotFoundException;
10+
use ProgrammatorDev\OpenWeatherMap\Exception\TooManyRequestsException;
11+
use ProgrammatorDev\OpenWeatherMap\Exception\UnauthorizedException;
12+
use ProgrammatorDev\OpenWeatherMap\Exception\UnexpectedErrorException;
13+
use ProgrammatorDev\OpenWeatherMap\Resource\Resource;
14+
use ProgrammatorDev\OpenWeatherMap\Test\AbstractTest;
15+
use ProgrammatorDev\OpenWeatherMap\Test\MockResponse;
16+
17+
class ResourceTest extends AbstractTest
18+
{
19+
private Resource $resource;
20+
21+
protected function setUp(): void
22+
{
23+
parent::setUp();
24+
25+
$this->resource = new class($this->api) extends Resource {
26+
public function request(): void
27+
{
28+
$this->api->request(
29+
method: Method::GET,
30+
path: '/test'
31+
);
32+
}
33+
};
34+
}
35+
36+
#[DataProvider(methodName: 'provideApiErrorData')]
37+
public function testApiError(int $statusCode, string $exception): void
38+
{
39+
$this->mockClient->addResponse(new Response(
40+
status: $statusCode,
41+
body: MockResponse::API_ERROR
42+
));
43+
44+
$this->expectException($exception);
45+
$this->resource->request();
46+
}
47+
48+
public static function provideApiErrorData(): \Generator
49+
{
50+
yield 'bad request' => [400, BadRequestException::class];
51+
yield 'unauthorized' => [401, UnauthorizedException::class];
52+
yield 'not found' => [404, NotFoundException::class];
53+
yield 'too many requests' => [429, TooManyRequestsException::class];
54+
yield 'unexpected error' => [500, UnexpectedErrorException::class];
55+
}
56+
}

0 commit comments

Comments
 (0)