1 | <?php |
||
18 | use Werkspot\BingAdsApiBundle\Model\AccessToken; |
||
19 | use Werkspot\BingAdsApiBundle\Model\ApiDetails; |
||
20 | |||
21 | class ClientTest extends PHPUnit_Framework_TestCase |
||
22 | { |
||
23 | const ACCESS_TOKEN = '2ec09aeccaf634d982eec793037e37fe'; |
||
24 | const REFRESH_TOKEN = '0c59f7e609b0cc467067e39d523116ce'; |
||
25 | |||
26 | public function testSetApiDetails() |
||
27 | { |
||
28 | $newApiDetails = new ApiDetails('1', '2', '3', '4', '5'); |
||
29 | |||
30 | $expected = $this->getApiClient( |
||
31 | new OauthTokenService(new \GuzzleHttp\Client()), |
||
32 | $newApiDetails, |
||
33 | new ClientProxy('example.com'), |
||
34 | $this->getFileHelper(), |
||
35 | new Helper\Csv(), |
||
36 | $this->getTimeHelperMock() |
||
37 | ); |
||
38 | |||
39 | $api = $this->getApiClient( |
||
40 | new OauthTokenService(new \GuzzleHttp\Client()), |
||
41 | new ApiDetails(null, null, null, null, null), |
||
42 | new ClientProxy('example.com'), |
||
43 | $this->getFileHelper(), |
||
44 | new Helper\Csv(), |
||
45 | $this->getTimeHelperMock() |
||
46 | ); |
||
47 | |||
48 | $this->assertNotEquals($expected, $api); |
||
49 | $api->setApiDetails($newApiDetails); |
||
50 | $this->assertEquals($expected, $api); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * @dataProvider getTestSoapExceptionData |
||
55 | * |
||
56 | * @param int $code |
||
57 | * @param string $exceptionClassName |
||
58 | */ |
||
59 | public function testSoapExceptions($code, $exceptionClassName) |
||
60 | { |
||
61 | $this->expectException($exceptionClassName); |
||
62 | $this->runClientSoapException($code); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @dataProvider getTestSoapExceptionData |
||
67 | * |
||
68 | * @param int $code |
||
69 | * @param string $exceptionClassName |
||
70 | */ |
||
71 | public function testSoapOperationErrorExceptions($code, $exceptionClassName) |
||
72 | { |
||
73 | $this->expectException($exceptionClassName); |
||
74 | $this->runClientSoapException($code, 'OperationError'); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @dataProvider getTestSoapExceptionData |
||
79 | * |
||
80 | * @param int $code |
||
81 | * @param string $exceptionClassName |
||
82 | */ |
||
83 | public function testSoapBatchErrorExceptions($code, $exceptionClassName) |
||
84 | { |
||
85 | $this->expectException($exceptionClassName); |
||
86 | $this->runClientSoapException($code, 'BatchErrors'); |
||
87 | } |
||
88 | |||
89 | public function getTestSoapExceptionData() |
||
90 | { |
||
91 | return [ |
||
92 | 0 => [ |
||
93 | 'errorCode' => 0, |
||
94 | 'exceptionClassName' => Exceptions\SoapInternalErrorException::class |
||
95 | ], |
||
96 | 105 => [ |
||
97 | 'errorCode' => 105, |
||
98 | 'exceptionClassName' => Exceptions\SoapInvalidCredentialsException::class, |
||
99 | ], |
||
100 | 106 => [ |
||
101 | 'errorCode' => 106, |
||
102 | 'exceptionClassName' => Exceptions\SoapUserIsNotAuthorizedException::class, |
||
103 | ], |
||
104 | 2004 => [ |
||
105 | 'errorCode' => 2004, |
||
106 | 'exceptionClassName' => Exceptions\SoapNoCompleteDataAvailableException::class, |
||
107 | ], |
||
108 | 2100 => [ |
||
109 | 'errorCode' => 2100, |
||
110 | 'exceptionClassName' => Exceptions\SoapReportingServiceInvalidReportIdException::class, |
||
111 | ], |
||
112 | 9999 => [ |
||
113 | 'errorCode' => 9999, |
||
114 | 'exceptionClassName' => Exceptions\SoapUnknownErrorException::class, |
||
115 | ], |
||
116 | ]; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @param int $code |
||
121 | * @param null|string $type |
||
122 | * |
||
123 | * @return MockInterface |
||
124 | */ |
||
125 | private function runClientSoapException($code, $type = null) |
||
126 | { |
||
127 | $clientProxyMock = $this->getClientProxyMock(); |
||
128 | $clientProxyMock |
||
129 | ->shouldReceive('GetService') |
||
130 | ->andThrow($this->generateSoapFault($code, $type)); |
||
131 | |||
132 | $apiClient = $this->getApiClient( |
||
133 | $this->getOauthTokenServiceMock(), |
||
134 | new ApiDetails('refreshToken', 'clientId', 'clientSecret', 'redirectUri', 'devToken'), |
||
135 | $clientProxyMock, |
||
136 | $this->getFileHelper(), |
||
137 | new Helper\Csv(), |
||
138 | $this->getTimeHelperMock() |
||
139 | ); |
||
140 | |||
141 | $apiClient->getReport('GeoLocationPerformanceReport', [], ReportTimePeriod::LastWeek, 'test.csv'); |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * @return Mockery\MockInterface |
||
146 | */ |
||
147 | private function getOauthTokenServiceMock() |
||
148 | { |
||
149 | $oauthTokenServiceMock = Mockery::mock(OauthTokenService::class); |
||
150 | $oauthTokenServiceMock |
||
151 | ->shouldReceive('refreshToken') |
||
152 | ->with('clientId', 'clientSecret', 'redirectUri', AccessToken::class) |
||
153 | ->once() |
||
154 | ->andReturn(new AccessToken(self::ACCESS_TOKEN, self::REFRESH_TOKEN)); |
||
155 | |||
156 | return $oauthTokenServiceMock; |
||
157 | } |
||
158 | |||
159 | private function getTimeHelperMock() |
||
160 | { |
||
161 | $timeHelperMock = Mockery::mock(Helper\Time::class); |
||
162 | $timeHelperMock->shouldReceive('sleep')->andReturnNull(); |
||
163 | |||
164 | return $timeHelperMock; |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * @param OauthTokenService $oauthTokenService |
||
169 | * @param ApiDetails $apiDetails |
||
170 | * @param ClientProxy $clientProxy |
||
171 | * @param Helper\File $fileHelper |
||
172 | * @param Helper\Csv $csvHelper |
||
173 | * @param Helper\Time $timeHelper |
||
174 | * |
||
175 | * @return Client |
||
176 | */ |
||
177 | private function getApiClient(OauthTokenService $oauthTokenService, ApiDetails $apiDetails, ClientProxy $clientProxy, Helper\File $fileHelper, Helper\Csv $csvHelper, Helper\Time $timeHelper) |
||
178 | { |
||
179 | $apiClient = new Client($oauthTokenService, $apiDetails, $clientProxy, $fileHelper, $csvHelper, $timeHelper); |
||
180 | $apiClient->setConfig(['cache_dir' => '/tmp']); |
||
181 | |||
182 | return $apiClient; |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * @param int $code |
||
187 | * @param null|string $type |
||
188 | * |
||
189 | * @return \SoapFault |
||
190 | */ |
||
191 | private function generateSoapFault($code, $type = null) |
||
192 | { |
||
193 | $message = "an error message {$code}"; |
||
194 | $error = new stdClass(); |
||
195 | $error->Code = $code; |
||
196 | $error->Message = $message; |
||
197 | $exception = new SoapFault('Server', ''); |
||
198 | $exception->detail = new stdClass(); |
||
199 | if ($type === 'BatchErrors') { |
||
200 | $exception->detail->ApiFaultDetail = new stdClass(); |
||
201 | $exception->detail->ApiFaultDetail->BatchErrors = new stdClass(); |
||
202 | $exception->detail->ApiFaultDetail->BatchErrors->BatchError = [$error]; |
||
203 | } elseif ($type === 'OperationError') { |
||
204 | $exception->detail->ApiFaultDetail = new stdClass(); |
||
205 | $exception->detail->ApiFaultDetail->OperationErrors = new stdClass(); |
||
206 | $exception->detail->ApiFaultDetail->OperationErrors->OperationError = [$error]; |
||
207 | } else { |
||
208 | $exception->detail->AdApiFaultDetail = new stdClass(); |
||
209 | $exception->detail->AdApiFaultDetail->Errors = new stdClass(); |
||
210 | $exception->detail->AdApiFaultDetail->Errors->AdApiError = [$error]; |
||
211 | } |
||
212 | |||
213 | return $exception; |
||
214 | } |
||
215 | |||
216 | private function getClientProxyMock() |
||
217 | { |
||
218 | $clientProxyMock = Mockery::mock(ClientProxy::class); |
||
219 | $clientProxyMock |
||
220 | ->shouldReceive('ConstructWithCredentials') |
||
237 |