|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Kosv\RandomUser\Tests\Client; |
|
6
|
|
|
|
|
7
|
|
|
use Kosv\RandomUser\Client\Client; |
|
8
|
|
|
use Kosv\RandomUser\Client\QueryBuilder; |
|
9
|
|
|
use Kosv\RandomUser\Client\Response as ClientResponse; |
|
10
|
|
|
use Kosv\RandomUser\Exceptions\UnexpectedResponseException; |
|
11
|
|
|
use Kosv\RandomUser\Interfaces\TransportInterface; |
|
12
|
|
|
use Kosv\RandomUser\Transport\Response as TransportResponse; |
|
13
|
|
|
use PHPUnit\Framework\TestCase; |
|
14
|
|
|
|
|
15
|
|
|
final class ClientTest extends TestCase |
|
16
|
|
|
{ |
|
17
|
|
|
public function testRequestWhenApiReturnErr(): void |
|
18
|
|
|
{ |
|
19
|
|
|
$this->expectException(UnexpectedResponseException::class); |
|
20
|
|
|
$this->expectExceptionMessage('Service returned error. Error text: "test error".'); |
|
21
|
|
|
|
|
22
|
|
|
$transport = $this->createMock(TransportInterface::class); |
|
23
|
|
|
$transport->method('get')->willReturn(new TransportResponse(200, '{"error": "test error"}')); |
|
24
|
|
|
|
|
25
|
|
|
$client = new Client($transport); |
|
26
|
|
|
$client->request(new QueryBuilder()); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function testRequestWhenNotJson(): void |
|
30
|
|
|
{ |
|
31
|
|
|
$this->expectException(UnexpectedResponseException::class); |
|
32
|
|
|
$this->expectExceptionMessage('The response came in an unsupported format, must be JSON.'); |
|
33
|
|
|
|
|
34
|
|
|
$transport = $this->createMock(TransportInterface::class); |
|
35
|
|
|
$transport->method('get')->willReturn(new TransportResponse(200, '<p>test</p>')); |
|
36
|
|
|
|
|
37
|
|
|
$client = new Client($transport); |
|
38
|
|
|
$client->request(new QueryBuilder()); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function testRequestWhenNotValidResponse(): void |
|
42
|
|
|
{ |
|
43
|
|
|
$this->expectException(UnexpectedResponseException::class); |
|
44
|
|
|
$this->expectExceptionMessage('The response does not contain required data.'); |
|
45
|
|
|
|
|
46
|
|
|
$transport = $this->createMock(TransportInterface::class); |
|
47
|
|
|
$transport->method('get')->willReturn(new TransportResponse(200, '{"info": {}}')); |
|
48
|
|
|
|
|
49
|
|
|
$client = new Client($transport); |
|
50
|
|
|
$client->request(new QueryBuilder()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function testRequestWhenOk(): void |
|
54
|
|
|
{ |
|
55
|
|
|
$transport = $this->createMock(TransportInterface::class); |
|
56
|
|
|
$transport->method('get')->willReturn(new TransportResponse(200, '{"info": {}, "results": []}')); |
|
57
|
|
|
|
|
58
|
|
|
$client = new Client($transport); |
|
59
|
|
|
$this->assertInstanceOf(ClientResponse::class, $client->request(new QueryBuilder())); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function testRequestWhenResponse500(): void |
|
63
|
|
|
{ |
|
64
|
|
|
$this->expectException(UnexpectedResponseException::class); |
|
65
|
|
|
$this->expectExceptionMessage('The http-server returned an unexpected http-code.'); |
|
66
|
|
|
|
|
67
|
|
|
$transport = $this->createMock(TransportInterface::class); |
|
68
|
|
|
$transport->method('get')->willReturn(new TransportResponse(500, '')); |
|
69
|
|
|
|
|
70
|
|
|
$client = new Client($transport); |
|
71
|
|
|
$client->request(new QueryBuilder()); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|