Skip to content

Commit

Permalink
Merge pull request #27 from Art4/update-swagger-spec
Browse files Browse the repository at this point in the history
Update swagger spec
  • Loading branch information
Art4 authored Mar 22, 2024
2 parents 9676a86 + a270457 commit d1dbdc9
Show file tree
Hide file tree
Showing 8 changed files with 303 additions and 40 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- The supported weg.li-API spec as Swagger 2.0 json format was added.

### Changed

- The endpoints for listing charges and districs and getting a district by ZIP were changed to reflect the latest API changes.

## [0.3.0 - 2024-03-07](https://github.com/Art4/wegliphant/compare/0.2.0...0.3.0)

### Added
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

- API Docs: https://www.weg.li/api
- API Source: https://www.weg.li/apidocs.json
- API Version: https://github.com/weg-li/weg-li/tree/c1b13d731cba72b18a221c9b6c71324d78614bd2
- API Datetime: 2024-03-06T09:21:36Z
- API Version: https://github.com/weg-li/weg-li/tree/9247d97012486a052e0a326bec83e754ace750a6
- API Datetime: 2024-03-22T08:19:09Z

Requires: PHP ^8.1

Expand Down
12 changes: 6 additions & 6 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function authenticate(string $apiKey): void
}

/**
* List all districts using the endpoint `GET /districts.json`
* List all districts using the endpoint `GET /api/districts`
*
* @link https://www.weg.li/api
*
Expand All @@ -45,13 +45,13 @@ public function authenticate(string $apiKey): void
*/
public function listDistricts(): array
{
$response = $this->sendJsonRequest('GET', '/districts.json');
$response = $this->sendJsonRequest('GET', '/api/districts');

return $this->parseJsonResponseToArray($response, 200);
}

/**
* Get one district by ZIP using the endpoint `GET /districts/<zip>.json`
* Get one district by ZIP using the endpoint `GET /api/districts/<zip>`
*
* @link https://www.weg.li/api
*
Expand All @@ -62,13 +62,13 @@ public function listDistricts(): array
*/
public function getDistrictByZip(string $zip): array
{
$response = $this->sendJsonRequest('GET', '/districts/' . $zip . '.json');
$response = $this->sendJsonRequest('GET', '/api/districts/' . $zip);

return $this->parseJsonResponseToArray($response, 200);
}

/**
* List all charges using the endpoint `GET /charges.json`
* List all charges using the endpoint `GET /api/charges`
*
* @link https://www.weg.li/api
*
Expand All @@ -79,7 +79,7 @@ public function getDistrictByZip(string $zip): array
*/
public function listCharges(): array
{
$response = $this->sendJsonRequest('GET', '/charges.json');
$response = $this->sendJsonRequest('GET', '/api/charges');

return $this->parseJsonResponseToArray($response, 200);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Client/AuthenticateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function testAuthenticateSetsCorrectHeader(): void
]);

$requestFactory = $this->createMock(RequestFactoryInterface::class);
$requestFactory->expects($this->exactly(1))->method('createRequest')->with('GET', 'https://www.weg.li/districts.json')->willReturn($request);
$requestFactory->expects($this->exactly(1))->method('createRequest')->with('GET', 'https://www.weg.li/api/districts')->willReturn($request);

$stream = $this->createConfiguredMock(
StreamInterface::class,
Expand Down
32 changes: 22 additions & 10 deletions tests/Unit/Client/GetDistrictByZipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,16 @@ public function testGetDistrictByZipReturnsArray(): void
'updated_at' => '2020-03-06T17:53:09.034+01:00',
];

$apiKey = 'c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2';

$request = $this->createMock(RequestInterface::class);
$request->expects($this->exactly(1))->method('withHeader')->willReturn($request);
$request->expects($this->exactly(2))->method('withHeader')->willReturnMap([
['Accept', 'application/json', $request],
['X-API-KEY', $apiKey, $request],
]);

$requestFactory = $this->createMock(RequestFactoryInterface::class);
$requestFactory->expects($this->exactly(1))->method('createRequest')->with('GET', 'https://www.weg.li/districts/12305.json')->willReturn($request);
$requestFactory->expects($this->exactly(1))->method('createRequest')->with('GET', 'https://www.weg.li/api/districts/12305')->willReturn($request);

$stream = $this->createConfiguredMock(
StreamInterface::class,
Expand All @@ -64,6 +69,7 @@ public function testGetDistrictByZipReturnsArray(): void
$httpClient,
$requestFactory,
);
$client->authenticate($apiKey);

$response = $client->getDistrictByZip('12305');

Expand All @@ -75,11 +81,16 @@ public function testGetDistrictByZipReturnsArray(): void

public function testGetDistrictByZipThrowsClientException(): void
{
$apiKey = 'c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2';

$request = $this->createMock(RequestInterface::class);
$request->expects($this->exactly(1))->method('withHeader')->willReturn($request);
$request->expects($this->exactly(2))->method('withHeader')->willReturnMap([
['Accept', 'application/json', $request],
['X-API-KEY', $apiKey, $request],
]);

$requestFactory = $this->createMock(RequestFactoryInterface::class);
$requestFactory->expects($this->exactly(1))->method('createRequest')->with('GET', 'https://www.weg.li/districts/12305.json')->willReturn($request);
$requestFactory->expects($this->exactly(1))->method('createRequest')->with('GET', 'https://www.weg.li/api/districts/12305')->willReturn($request);

$httpClient = $this->createMock(ClientInterface::class);
$httpClient->expects($this->exactly(1))->method('sendRequest')->willThrowException(
Expand All @@ -90,6 +101,7 @@ public function testGetDistrictByZipThrowsClientException(): void
$httpClient,
$requestFactory,
);
$client->authenticate($apiKey);

$this->expectException(ClientExceptionInterface::class);
$this->expectExceptionMessage('');
Expand All @@ -103,12 +115,12 @@ public function testGetDistrictByZipThrowsUnexpectedResponseExceptionOnWrongStat
$request->expects($this->exactly(1))->method('withHeader')->willReturn($request);

$requestFactory = $this->createMock(RequestFactoryInterface::class);
$requestFactory->expects($this->exactly(1))->method('createRequest')->with('GET', 'https://www.weg.li/districts/00000.json')->willReturn($request);
$requestFactory->expects($this->exactly(1))->method('createRequest')->with('GET', 'https://www.weg.li/api/districts/00000')->willReturn($request);

$response = $this->createConfiguredMock(
ResponseInterface::class,
[
'getStatusCode' => 500,
'getStatusCode' => 401,
]
);

Expand All @@ -121,7 +133,7 @@ public function testGetDistrictByZipThrowsUnexpectedResponseExceptionOnWrongStat
);

$this->expectException(UnexpectedResponseException::class);
$this->expectExceptionMessage('Server replied with the status code 500, but 200 was expected.');
$this->expectExceptionMessage('Server replied with the status code 401, but 200 was expected.');

$client->getDistrictByZip('00000');
}
Expand All @@ -132,7 +144,7 @@ public function testGetDistrictByZipThrowsUnexpectedResponseExceptionOnWrongCont
$request->expects($this->exactly(1))->method('withHeader')->willReturn($request);

$requestFactory = $this->createMock(RequestFactoryInterface::class);
$requestFactory->expects($this->exactly(1))->method('createRequest')->with('GET', 'https://www.weg.li/districts/12305.json')->willReturn($request);
$requestFactory->expects($this->exactly(1))->method('createRequest')->with('GET', 'https://www.weg.li/api/districts/12305')->willReturn($request);

$response = $this->createConfiguredMock(
ResponseInterface::class,
Expand Down Expand Up @@ -162,7 +174,7 @@ public function testGetDistrictByZipThrowsUnexpectedResponseExceptionOnInvalidJs
$request->expects($this->exactly(1))->method('withHeader')->willReturn($request);

$requestFactory = $this->createMock(RequestFactoryInterface::class);
$requestFactory->expects($this->exactly(1))->method('createRequest')->with('GET', 'https://www.weg.li/districts/12305.json')->willReturn($request);
$requestFactory->expects($this->exactly(1))->method('createRequest')->with('GET', 'https://www.weg.li/api/districts/12305')->willReturn($request);

$stream = $this->createConfiguredMock(
StreamInterface::class,
Expand Down Expand Up @@ -200,7 +212,7 @@ public function testGetDistrictByZipThrowsUnexpectedResponseExceptionOnJsonBodyW
$request->expects($this->exactly(1))->method('withHeader')->willReturn($request);

$requestFactory = $this->createMock(RequestFactoryInterface::class);
$requestFactory->expects($this->exactly(1))->method('createRequest')->with('GET', 'https://www.weg.li/districts/12305.json')->willReturn($request);
$requestFactory->expects($this->exactly(1))->method('createRequest')->with('GET', 'https://www.weg.li/api/districts/12305')->willReturn($request);

$stream = $this->createConfiguredMock(
StreamInterface::class,
Expand Down
32 changes: 22 additions & 10 deletions tests/Unit/Client/ListChargesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,16 @@ public function testListChargesReturnsArray(): void
],
];

$apiKey = 'c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2';

$request = $this->createMock(RequestInterface::class);
$request->expects($this->exactly(1))->method('withHeader')->willReturn($request);
$request->expects($this->exactly(2))->method('withHeader')->willReturnMap([
['Accept', 'application/json', $request],
['X-API-KEY', $apiKey, $request],
]);

$requestFactory = $this->createMock(RequestFactoryInterface::class);
$requestFactory->expects($this->exactly(1))->method('createRequest')->with('GET', 'https://www.weg.li/charges.json')->willReturn($request);
$requestFactory->expects($this->exactly(1))->method('createRequest')->with('GET', 'https://www.weg.li/api/charges')->willReturn($request);

$stream = $this->createConfiguredMock(
StreamInterface::class,
Expand All @@ -136,6 +141,7 @@ public function testListChargesReturnsArray(): void
$httpClient,
$requestFactory,
);
$client->authenticate($apiKey);

$response = $client->listCharges();

Expand All @@ -147,11 +153,16 @@ public function testListChargesReturnsArray(): void

public function testListChargesThrowsClientException(): void
{
$apiKey = 'c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2';

$request = $this->createMock(RequestInterface::class);
$request->expects($this->exactly(1))->method('withHeader')->willReturn($request);
$request->expects($this->exactly(2))->method('withHeader')->willReturnMap([
['Accept', 'application/json', $request],
['X-API-KEY', $apiKey, $request],
]);

$requestFactory = $this->createMock(RequestFactoryInterface::class);
$requestFactory->expects($this->exactly(1))->method('createRequest')->with('GET', 'https://www.weg.li/charges.json')->willReturn($request);
$requestFactory->expects($this->exactly(1))->method('createRequest')->with('GET', 'https://www.weg.li/api/charges')->willReturn($request);

$httpClient = $this->createMock(ClientInterface::class);
$httpClient->expects($this->exactly(1))->method('sendRequest')->willThrowException(
Expand All @@ -162,6 +173,7 @@ public function testListChargesThrowsClientException(): void
$httpClient,
$requestFactory,
);
$client->authenticate($apiKey);

$this->expectException(ClientExceptionInterface::class);
$this->expectExceptionMessage('');
Expand All @@ -175,12 +187,12 @@ public function testListChargesThrowsUnexpectedResponseExceptionOnWrongStatusCod
$request->expects($this->exactly(1))->method('withHeader')->willReturn($request);

$requestFactory = $this->createMock(RequestFactoryInterface::class);
$requestFactory->expects($this->exactly(1))->method('createRequest')->with('GET', 'https://www.weg.li/charges.json')->willReturn($request);
$requestFactory->expects($this->exactly(1))->method('createRequest')->with('GET', 'https://www.weg.li/api/charges')->willReturn($request);

$response = $this->createConfiguredMock(
ResponseInterface::class,
[
'getStatusCode' => 500,
'getStatusCode' => 401,
]
);

Expand All @@ -193,7 +205,7 @@ public function testListChargesThrowsUnexpectedResponseExceptionOnWrongStatusCod
);

$this->expectException(UnexpectedResponseException::class);
$this->expectExceptionMessage('Server replied with the status code 500, but 200 was expected.');
$this->expectExceptionMessage('Server replied with the status code 401, but 200 was expected.');

$client->listCharges();
}
Expand All @@ -204,7 +216,7 @@ public function testListChargesThrowsUnexpectedResponseExceptionOnWrongContentTy
$request->expects($this->exactly(1))->method('withHeader')->willReturn($request);

$requestFactory = $this->createMock(RequestFactoryInterface::class);
$requestFactory->expects($this->exactly(1))->method('createRequest')->with('GET', 'https://www.weg.li/charges.json')->willReturn($request);
$requestFactory->expects($this->exactly(1))->method('createRequest')->with('GET', 'https://www.weg.li/api/charges')->willReturn($request);

$response = $this->createConfiguredMock(
ResponseInterface::class,
Expand Down Expand Up @@ -234,7 +246,7 @@ public function testListChargesThrowsUnexpectedResponseExceptionOnInvalidJsonBod
$request->expects($this->exactly(1))->method('withHeader')->willReturn($request);

$requestFactory = $this->createMock(RequestFactoryInterface::class);
$requestFactory->expects($this->exactly(1))->method('createRequest')->with('GET', 'https://www.weg.li/charges.json')->willReturn($request);
$requestFactory->expects($this->exactly(1))->method('createRequest')->with('GET', 'https://www.weg.li/api/charges')->willReturn($request);

$stream = $this->createConfiguredMock(
StreamInterface::class,
Expand Down Expand Up @@ -272,7 +284,7 @@ public function testListChargesThrowsUnexpectedResponseExceptionOnJsonBodyWithou
$request->expects($this->exactly(1))->method('withHeader')->willReturn($request);

$requestFactory = $this->createMock(RequestFactoryInterface::class);
$requestFactory->expects($this->exactly(1))->method('createRequest')->with('GET', 'https://www.weg.li/charges.json')->willReturn($request);
$requestFactory->expects($this->exactly(1))->method('createRequest')->with('GET', 'https://www.weg.li/api/charges')->willReturn($request);

$stream = $this->createConfiguredMock(
StreamInterface::class,
Expand Down
Loading

0 comments on commit d1dbdc9

Please sign in to comment.