1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace MaxBeckers\AmazonAlexa\Test\Helper; |
6
|
|
|
|
7
|
|
|
use GuzzleHttp\Client; |
8
|
|
|
use MaxBeckers\AmazonAlexa\Exception\DeviceApiCallException; |
9
|
|
|
use MaxBeckers\AmazonAlexa\Exception\MissingRequestDataException; |
10
|
|
|
use MaxBeckers\AmazonAlexa\Helper\DeviceAddressInformationHelper; |
11
|
|
|
use MaxBeckers\AmazonAlexa\Request\Context; |
12
|
|
|
use MaxBeckers\AmazonAlexa\Request\Device; |
13
|
|
|
use MaxBeckers\AmazonAlexa\Request\Request; |
14
|
|
|
use MaxBeckers\AmazonAlexa\Request\System; |
15
|
|
|
use PHPUnit\Framework\TestCase; |
16
|
|
|
use Psr\Http\Message\ResponseInterface; |
17
|
|
|
use Psr\Http\Message\StreamInterface; |
18
|
|
|
|
19
|
|
|
class DeviceAddressInformationHelperTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
public function testGetCountryAndPostalCodeSuccess(): void |
22
|
|
|
{ |
23
|
|
|
$responseData = [ |
24
|
|
|
'countryCode' => 'US', |
25
|
|
|
'postalCode' => '98109', |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
$client = $this->createMock(Client::class); |
29
|
|
|
$apiResponse = $this->createMock(ResponseInterface::class); |
30
|
|
|
$apiResponseBody = $this->createMock(StreamInterface::class); |
31
|
|
|
|
32
|
|
|
$client->method('request') |
33
|
|
|
->willReturn($apiResponse); |
34
|
|
|
$apiResponse->method('getStatusCode') |
35
|
|
|
->willReturn(200); |
36
|
|
|
$apiResponse->method('getBody') |
37
|
|
|
->willReturn($apiResponseBody); |
38
|
|
|
$apiResponseBody->method('getContents') |
39
|
|
|
->willReturn(json_encode($responseData)); |
40
|
|
|
|
41
|
|
|
$deviceAddressInformationHelper = new DeviceAddressInformationHelper($client); |
42
|
|
|
$this->assertEquals( |
43
|
|
|
Device\DeviceAddressInformation::fromApiResponse($responseData), |
44
|
|
|
$deviceAddressInformationHelper->getCountryAndPostalCode($this->createDummyRequest('id', 'https://test.com', 'test')) |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testGetCountryAndPostalCodeError(): void |
49
|
|
|
{ |
50
|
|
|
$client = $this->createMock(Client::class); |
51
|
|
|
$apiResponse = $this->createMock(ResponseInterface::class); |
52
|
|
|
|
53
|
|
|
$client->method('request') |
54
|
|
|
->willReturn($apiResponse); |
55
|
|
|
$apiResponse->method('getStatusCode') |
56
|
|
|
->willReturn(403); |
57
|
|
|
|
58
|
|
|
$deviceAddressInformationHelper = new DeviceAddressInformationHelper($client); |
59
|
|
|
$this->expectException(DeviceApiCallException::class); |
60
|
|
|
$deviceAddressInformationHelper->getCountryAndPostalCode($this->createDummyRequest('id', 'https://test.com', 'test')); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testGetCountryAndPostalCodeMissingData(): void |
64
|
|
|
{ |
65
|
|
|
$client = $this->createMock(Client::class); |
66
|
|
|
|
67
|
|
|
$deviceAddressInformationHelper = new DeviceAddressInformationHelper($client); |
68
|
|
|
$this->expectException(MissingRequestDataException::class); |
69
|
|
|
$deviceAddressInformationHelper->getCountryAndPostalCode($this->createDummyRequest()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testGetAddressSuccess(): void |
73
|
|
|
{ |
74
|
|
|
$responseData = [ |
75
|
|
|
'stateOrRegion' => 'WA', |
76
|
|
|
'city' => 'Seattle', |
77
|
|
|
'countryCode' => 'US', |
78
|
|
|
'postalCode' => '98109', |
79
|
|
|
'addressLine1' => '410 Terry Ave North', |
80
|
|
|
'addressLine2' => '', |
81
|
|
|
'addressLine3' => 'aeiou', |
82
|
|
|
'districtOrCounty' => '', |
83
|
|
|
]; |
84
|
|
|
|
85
|
|
|
$client = $this->createMock(Client::class); |
86
|
|
|
$apiResponse = $this->createMock(ResponseInterface::class); |
87
|
|
|
$apiResponseBody = $this->createMock(StreamInterface::class); |
88
|
|
|
|
89
|
|
|
$client->method('request') |
90
|
|
|
->willReturn($apiResponse); |
91
|
|
|
$apiResponse->method('getStatusCode') |
92
|
|
|
->willReturn(200); |
93
|
|
|
$apiResponse->method('getBody') |
94
|
|
|
->willReturn($apiResponseBody); |
95
|
|
|
$apiResponseBody->method('getContents') |
96
|
|
|
->willReturn(json_encode($responseData)); |
97
|
|
|
|
98
|
|
|
$deviceAddressInformationHelper = new DeviceAddressInformationHelper($client); |
99
|
|
|
$this->assertEquals( |
100
|
|
|
Device\DeviceAddressInformation::fromApiResponse($responseData), |
101
|
|
|
$deviceAddressInformationHelper->getAddress($this->createDummyRequest('id', 'https://test.com', 'test')) |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function testGetAddressError(): void |
106
|
|
|
{ |
107
|
|
|
$client = $this->createMock(Client::class); |
108
|
|
|
$apiResponse = $this->createMock(ResponseInterface::class); |
109
|
|
|
|
110
|
|
|
$client->method('request') |
111
|
|
|
->willReturn($apiResponse); |
112
|
|
|
$apiResponse->method('getStatusCode') |
113
|
|
|
->willReturn(500); |
114
|
|
|
|
115
|
|
|
$deviceAddressInformationHelper = new DeviceAddressInformationHelper($client); |
116
|
|
|
$this->expectException(DeviceApiCallException::class); |
117
|
|
|
$deviceAddressInformationHelper->getAddress($this->createDummyRequest('id', 'https://test.com', 'test')); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function testGetAddressMissingData(): void |
121
|
|
|
{ |
122
|
|
|
$client = $this->createMock(Client::class); |
123
|
|
|
|
124
|
|
|
$deviceAddressInformationHelper = new DeviceAddressInformationHelper($client); |
125
|
|
|
$this->expectException(MissingRequestDataException::class); |
126
|
|
|
$deviceAddressInformationHelper->getCountryAndPostalCode(new Request()); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function testGetAddressOnInvalidRequestInstance(): void |
130
|
|
|
{ |
131
|
|
|
$client = $this->createMock(Client::class); |
132
|
|
|
|
133
|
|
|
$deviceAddressInformationHelper = new DeviceAddressInformationHelper($client); |
134
|
|
|
$this->expectException(MissingRequestDataException::class); |
135
|
|
|
$deviceAddressInformationHelper->getAddress(new Request()); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
private function createDummyRequest(?string $deviceId = null, ?string $apiEndpoint = null, ?string $apiAccessToken = null): Request |
139
|
|
|
{ |
140
|
|
|
$device = new Device(); |
141
|
|
|
$device->deviceId = $deviceId; |
142
|
|
|
|
143
|
|
|
$system = new System(); |
144
|
|
|
$system->device = $device; |
145
|
|
|
$system->apiEndpoint = $apiEndpoint; |
146
|
|
|
$system->apiAccessToken = $apiAccessToken; |
147
|
|
|
|
148
|
|
|
$context = new Context(); |
149
|
|
|
$context->system = $system; |
150
|
|
|
|
151
|
|
|
$request = new Request(); |
152
|
|
|
$request->context = $context; |
153
|
|
|
|
154
|
|
|
return $request; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|