1
|
|
|
<?php |
2
|
|
|
namespace Tests\Unit; |
3
|
|
|
|
4
|
|
|
use GuzzleHttp\Client; |
5
|
|
|
use GuzzleHttp\Psr7\Response; |
6
|
|
|
use Lyal\Checkr\Entities\Resources\AdverseAction; |
7
|
|
|
use Lyal\Checkr\Exceptions\Client\BadRequest; |
8
|
|
|
use Lyal\Checkr\Exceptions\Client\Conflict; |
9
|
|
|
use Lyal\Checkr\Exceptions\Client\Forbidden; |
10
|
|
|
use Lyal\Checkr\Exceptions\Client\NotFound; |
11
|
|
|
use Lyal\Checkr\Exceptions\Client\Unauthorized; |
12
|
|
|
use Lyal\Checkr\Exceptions\Server\InternalServerError; |
13
|
|
|
use Lyal\Checkr\Exceptions\UnhandledRequestError; |
14
|
|
|
use Lyal\Checkr\Exceptions\UnknownResourceException; |
15
|
|
|
use Tests\UnitTestCase; |
16
|
|
|
|
17
|
|
|
class ClientTest extends UnitTestCase |
18
|
|
|
{ |
19
|
|
|
public function testGetGuzzle() |
20
|
|
|
{ |
21
|
|
|
$client = $this->getClient()->getHttpClient(); |
22
|
|
|
$this->assertInstanceOf(Client::class, $client); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testSetGuzzle() |
26
|
|
|
{ |
27
|
|
|
$client = $this->getClient(); |
28
|
|
|
$guzzle = $this->mockGuzzle([]); |
29
|
|
|
$client->setHttpClient($guzzle); |
30
|
|
|
$this->assertInstanceOf(\GuzzleHttp\Client::class, $client->getHttpClient()); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testValidApiCall() |
34
|
|
|
{ |
35
|
|
|
$client = $this->getClient(); |
36
|
|
|
$previousObject = new AdverseAction(NULL, $client); |
37
|
|
|
$object = $client->api('AdverseAction', uniqid('', false), $previousObject); |
38
|
|
|
$this->assertInstanceOf(get_class($previousObject), $object); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testValidMagicCall() |
42
|
|
|
{ |
43
|
|
|
$client = $this->getClient(); |
44
|
|
|
$adverseAction = new AdverseAction(NULL, $client); |
45
|
|
|
$this->assertInstanceOf(get_class($adverseAction), $client->adverse_action()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testInvalidMagicCall() |
49
|
|
|
{ |
50
|
|
|
$this->expectException(UnknownResourceException::class); |
51
|
|
|
$client = $this->getClient(); |
52
|
|
|
$client->api('ThisIsNotAValidClass', NULL, $client); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testGetKey() |
56
|
|
|
{ |
57
|
|
|
$client = $this->getClient(); |
58
|
|
|
$this->assertEquals('ourverysecretkey', $client->getKey()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testSetOption() |
62
|
|
|
{ |
63
|
|
|
$client = $this->getClient(); |
64
|
|
|
$client->setOption('test', 'test'); |
65
|
|
|
$option = $client->getOption('test'); |
66
|
|
|
$this->assertEquals('test', $option); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testSetOptions() |
70
|
|
|
{ |
71
|
|
|
$client = $this->getClient(); |
72
|
|
|
$client->setOptions(['one' => 'two', 'three' => 'four']); |
73
|
|
|
$options = $client->getOptions(); |
74
|
|
|
$this->assertEquals(['one' => 'two', 'three' => 'four'], $options); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testBadGetOption() |
78
|
|
|
{ |
79
|
|
|
$client = $this->getClient(); |
80
|
|
|
$option = $client->getOption('notset'); |
81
|
|
|
|
82
|
|
|
$this->assertFalse($option); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testApiEndPoint() |
86
|
|
|
{ |
87
|
|
|
$client = $this->getClient(); |
88
|
|
|
$url = $client->getApiEndPoint(); |
89
|
|
|
|
90
|
|
|
$this->assertEquals('https://api.checkr.com/v1/', $url); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testRequest() |
94
|
|
|
{ |
95
|
|
|
$responses = [new Response(200, [], '{"id": "57ed4ce3057e0b002adc6d93","object": "adverse_item","text": "License status: Suspended"}')]; |
96
|
|
|
$client = $this->getClient($responses); |
97
|
|
|
$response = $client->request('GET', $client->getApiEndPoint() . 'fake_request'); |
98
|
|
|
$this->assertEquals(json_decode('{"id": "57ed4ce3057e0b002adc6d93","object": "adverse_item","text": "License status: Suspended"}'), $response); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function testBadRequestException() |
102
|
|
|
{ |
103
|
|
|
$this->expectException(BadRequest::class); |
104
|
|
|
$responses = [new Response(400, [])]; |
105
|
|
|
$client = $this->getClient($responses); |
106
|
|
|
$client->request('GET', $client->getApiEndPoint() . 'fake_request'); |
107
|
|
|
$this->getClient($responses); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function testUnauthorizedException() |
111
|
|
|
{ |
112
|
|
|
$this->expectException(Unauthorized::class); |
113
|
|
|
$responses = [new Response(401, [])]; |
114
|
|
|
$client = $this->getClient($responses); |
115
|
|
|
$client->request('GET', $client->getApiEndPoint() . 'fake_request'); |
116
|
|
|
$this->getClient($responses); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function testForbiddenException() |
120
|
|
|
{ |
121
|
|
|
$this->expectException(Forbidden::class); |
122
|
|
|
$responses = [new Response(403, [])]; |
123
|
|
|
$client = $this->getClient($responses); |
124
|
|
|
$client->request('GET', $client->getApiEndPoint() . 'fake_request'); |
125
|
|
|
$this->getClient($responses); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function testNotFoundException() |
129
|
|
|
{ |
130
|
|
|
$this->expectException(NotFound::class); |
131
|
|
|
$responses = [new Response(404, [])]; |
132
|
|
|
$client = $this->getClient($responses); |
133
|
|
|
$client->request('GET', $client->getApiEndPoint() . 'fake_request'); |
134
|
|
|
$this->getClient($responses); |
135
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function testConflictException() |
139
|
|
|
{ |
140
|
|
|
$this->expectException(Conflict::class); |
141
|
|
|
$responses = [new Response(409, [])]; |
142
|
|
|
$client = $this->getClient($responses); |
143
|
|
|
$client->request('GET', $client->getApiEndPoint() . 'fake_request'); |
144
|
|
|
$this->getClient($responses); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function testInternalServerErrorException() |
148
|
|
|
{ |
149
|
|
|
$this->expectException(InternalServerError::class); |
150
|
|
|
$responses = [new Response(500, [])]; |
151
|
|
|
$client = $this->getClient($responses); |
152
|
|
|
$client->request('GET', $client->getApiEndPoint() . 'fake_request'); |
153
|
|
|
$this->getClient($responses); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function testUnhandledRequestErrorException() |
157
|
|
|
{ |
158
|
|
|
$this->expectException(UnhandledRequestError::class); |
159
|
|
|
$responses = [new Response(417, [])]; |
160
|
|
|
$client = $this->getClient($responses); |
161
|
|
|
$client->request('GET', $client->getApiEndPoint() . 'fake_request'); |
162
|
|
|
$this->getClient($responses); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
} |