-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathClientTest.php
89 lines (63 loc) · 2.98 KB
/
ClientTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
namespace Philcross\GetAddress\Tests;
use PHPUnit\Framework\TestCase;
use Philcross\GetAddress\Client;
use Philcross\GetAddress\Exceptions;
class ClientTest extends TestCase
{
use ClientMockerTrait;
public function test_the_client_can_be_instantiated()
{
$client = new Client('123456', '78910');
$this->assertInstanceOf(Client::class, $client);
}
public function test_it_correctly_sends_the_api_key_when_making_requests_to_the_api()
{
$http = $this->mockGuzzle('get', 'https://api.getAddress.io/?api-key=123456', $this->mockResponse(['worked']));
$client = new Client('123456', '78910', $http);
$response = $client->call('GET', '');
$this->assertEquals(['worked'], $response);
}
public function test_its_throws_an_exception_if_a_400_status_is_returned()
{
$this->setExpectedException(Exceptions\InvalidPostcodeException::class);
$http = $this->mockGuzzle('get', 'https://api.getAddress.io/?api-key=123456', $this->mockResponse([], 400));
$client = new Client('123456', '78910', $http);
$client->call('GET', '');
}
public function test_its_throws_an_exception_if_a_401_status_is_returned()
{
$this->setExpectedException(Exceptions\ForbiddenException::class);
$http = $this->mockGuzzle('get', 'https://api.getAddress.io/?api-key=123456', $this->mockResponse([], 401));
$client = new Client('123456', '78910', $http);
$client->call('GET', '');
}
public function test_its_throws_an_exception_if_a_404_status_is_returned()
{
$this->setExpectedException(Exceptions\PostcodeNotFoundException::class);
$http = $this->mockGuzzle('get', 'https://api.getAddress.io/?api-key=123456', $this->mockResponse([], 404));
$client = new Client('123456', '78910', $http);
$client->call('GET', '');
}
public function test_its_throws_an_exception_if_a_429_status_is_returned()
{
$this->setExpectedException(Exceptions\TooManyRequestsException::class);
$http = $this->mockGuzzle('get', 'https://api.getAddress.io/?api-key=123456', $this->mockResponse([], 429));
$client = new Client('123456', '78910', $http);
$client->call('GET', '');
}
public function test_its_throws_an_exception_if_a_500_status_is_returned()
{
$this->setExpectedException(Exceptions\ServerException::class);
$http = $this->mockGuzzle('get', 'https://api.getAddress.io/?api-key=123456', $this->mockResponse([], 500));
$client = new Client('123456', '78910', $http);
$client->call('GET', '');
}
public function test_its_throws_a_generic_exception_if_an_unknown_status_is_returned()
{
$this->setExpectedException(Exceptions\UnknownException::class);
$http = $this->mockGuzzle('get', 'https://api.getAddress.io/?api-key=123456', $this->mockResponse([], 418));
$client = new Client('123456', '78910', $http);
$client->call('GET', '');
}
}