Skip to content

Commit

Permalink
Implement get one charge by tbnr
Browse files Browse the repository at this point in the history
  • Loading branch information
Art4 committed Mar 22, 2024
1 parent d1dbdc9 commit 0a413f7
Show file tree
Hide file tree
Showing 2 changed files with 270 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,23 @@ public function listCharges(): array
return $this->parseJsonResponseToArray($response, 200);
}

/**
* Get one charge by tbnr using the endpoint `GET /api/charges/<tbnr>`
*
* @link https://www.weg.li/api
*
* @throws \Psr\Http\Client\ClientExceptionInterface If an error happens while processing the request.
* @throws UnexpectedResponseException If an error happens while processing the response.
*
* @return mixed[]
*/
public function getChargeByTbnr(string $tbnr): array
{
$response = $this->sendJsonRequest('GET', '/api/charges/' . $tbnr);

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

/**
* List all notices for the authorized user using the endpoint `GET /api/notices`
*
Expand Down
253 changes: 253 additions & 0 deletions tests/Unit/Client/GetChargeByTbnrTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
<?php

declare(strict_types=1);

namespace Tests\Art4\Wegliphant\Client;

use Art4\Wegliphant\Client;
use Art4\Wegliphant\Exception\UnexpectedResponseException;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;

#[CoversClass(Client::class)]
final class GetChargeByTbnrTest extends TestCase
{
public function testGetChargeByTbnrReturnsArray(): void
{
$expected = [
'tbnr' => '141312',
'description' => 'Sie parkten im absoluten Haltverbot (Zeichen 283).',
'fine' => '25.0',
'bkat' => '§ 41 Abs. 1 iVm Anlage 2, § 49 StVO; § 24 Abs. 1, 3 Nr. 5 StVG; 52 BKat',
'penalty' => null,
'fap' => null,
'points' => 0,
'valid_from' => '2021-11-09T00:00:00.000+01:00',
'valid_to' => null,
'implementation' => null,
'classification' => 5,
'variant_table_id' => 741017,
'rule_id' => 39,
'table_id' => null,
'required_refinements' => '00000000000000000000000000000000',
'number_required_refinements' => 0,
'max_fine' => '0.0',
'created_at' => '2023-09-18T15:30:43.312+02:00',
'updated_at' => '2023-09-18T15:30:43.312+02:00',
];

$apiKey = 'c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2';

$request = $this->createMock(RequestInterface::class);
$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/api/charges/141312')->willReturn($request);

$stream = $this->createConfiguredMock(
StreamInterface::class,
[
'__toString' => json_encode($expected),
],
);

$response = $this->createConfiguredMock(
ResponseInterface::class,
[
'getStatusCode' => 200,
'getHeaderLine' => 'application/json',
'getBody' => $stream,
]
);

$httpClient = $this->createMock(ClientInterface::class);
$httpClient->expects($this->exactly(1))->method('sendRequest')->willReturn($response);

$client = Client::create(
$httpClient,
$requestFactory,
);
$client->authenticate($apiKey);

$response = $client->getChargeByTbnr('141312');

$this->assertSame(
$expected,
$response,
);
}

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

$request = $this->createMock(RequestInterface::class);
$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/api/charges/141312')->willReturn($request);

$httpClient = $this->createMock(ClientInterface::class);
$httpClient->expects($this->exactly(1))->method('sendRequest')->willThrowException(
$this->createMock(ClientExceptionInterface::class),
);

$client = Client::create(
$httpClient,
$requestFactory,
);
$client->authenticate($apiKey);

$this->expectException(ClientExceptionInterface::class);
$this->expectExceptionMessage('');

$client->getChargeByTbnr('141312');
}

public function testGetChargeByTbnrThrowsUnexpectedResponseExceptionOnWrongStatusCode(): void
{
$request = $this->createMock(RequestInterface::class);
$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/api/charges/000000')->willReturn($request);

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

$httpClient = $this->createMock(ClientInterface::class);
$httpClient->expects($this->exactly(1))->method('sendRequest')->willReturn($response);

$client = Client::create(
$httpClient,
$requestFactory,
);

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

$client->getChargeByTbnr('000000');
}

public function testGetChargeByTbnrThrowsUnexpectedResponseExceptionOnWrongContentTypeHeader(): void
{
$request = $this->createMock(RequestInterface::class);
$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/api/charges/141312')->willReturn($request);

$response = $this->createConfiguredMock(
ResponseInterface::class,
[
'getStatusCode' => 200,
'getHeaderLine' => 'text/html',
]
);

$httpClient = $this->createMock(ClientInterface::class);
$httpClient->expects($this->exactly(1))->method('sendRequest')->willReturn($response);

$client = Client::create(
$httpClient,
$requestFactory,
);

$this->expectException(UnexpectedResponseException::class);
$this->expectExceptionMessage('Server replied not with JSON content.');

$client->getChargeByTbnr('141312');
}

public function testGetChargeByTbnrThrowsUnexpectedResponseExceptionOnInvalidJsonBody(): void
{
$request = $this->createMock(RequestInterface::class);
$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/api/charges/141312')->willReturn($request);

$stream = $this->createConfiguredMock(
StreamInterface::class,
[
'__toString' => 'invalid json',
],
);

$response = $this->createConfiguredMock(
ResponseInterface::class,
[
'getStatusCode' => 200,
'getHeaderLine' => 'application/json',
'getBody' => $stream,
]
);

$httpClient = $this->createMock(ClientInterface::class);
$httpClient->expects($this->exactly(1))->method('sendRequest')->willReturn($response);

$client = Client::create(
$httpClient,
$requestFactory,
);

$this->expectException(UnexpectedResponseException::class);
$this->expectExceptionMessage('Response body contains no valid JSON: invalid json');

$client->getChargeByTbnr('141312');
}

public function testGetChargeByTbnrThrowsUnexpectedResponseExceptionOnJsonBodyWithoutArray(): void
{
$request = $this->createMock(RequestInterface::class);
$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/api/charges/141312')->willReturn($request);

$stream = $this->createConfiguredMock(
StreamInterface::class,
[
'__toString' => '"this is not an array"',
],
);

$response = $this->createConfiguredMock(
ResponseInterface::class,
[
'getStatusCode' => 200,
'getHeaderLine' => 'application/json',
'getBody' => $stream,
]
);

$httpClient = $this->createMock(ClientInterface::class);
$httpClient->expects($this->exactly(1))->method('sendRequest')->willReturn($response);

$client = Client::create(
$httpClient,
$requestFactory,
);

$this->expectException(UnexpectedResponseException::class);
$this->expectExceptionMessage('Response JSON does not contain an array: "this is not an array"');

$client->getChargeByTbnr('141312');
}
}

0 comments on commit 0a413f7

Please sign in to comment.