1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\Werkspot\BingAdsApiBundle\Tests\Api\Report; |
4
|
|
|
|
5
|
|
|
use BingAds\Bulk\ReportTimePeriod; |
6
|
|
|
use BingAds\Proxy\ClientProxy; |
7
|
|
|
use BingAds\Reporting\AccountThroughAdGroupReportScope; |
8
|
|
|
use BingAds\Reporting\GeoLocationPerformanceReportRequest; |
9
|
|
|
use BingAds\Reporting\NonHourlyReportAggregation; |
10
|
|
|
use BingAds\Reporting\ReportFormat; |
11
|
|
|
use BingAds\Reporting\ReportTime; |
12
|
|
|
use Mockery; |
13
|
|
|
use PHPUnit_Framework_TestCase; |
14
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
15
|
|
|
use stdClass; |
16
|
|
|
use SoapFault; |
17
|
|
|
use Werkspot\BingAdsApiBundle\Api\Client; |
18
|
|
|
use Werkspot\BingAdsApiBundle\Api\Exceptions; |
19
|
|
|
use Werkspot\BingAdsApiBundle\Api\Helper; |
20
|
|
|
use Werkspot\BingAdsApiBundle\Api\Report\GeoLocationPerformanceReport; |
21
|
|
|
use Werkspot\BingAdsApiBundle\Guzzle\OauthTokenService; |
22
|
|
|
use Werkspot\BingAdsApiBundle\Model\AccessToken; |
23
|
|
|
use Werkspot\BingAdsApiBundle\Model\ApiDetails; |
24
|
|
|
|
25
|
|
|
class GeoLocationPerformanceReportTest extends PHPUnit_Framework_TestCase |
26
|
|
|
{ |
27
|
|
|
const YESTERDAY = 'Yesterday'; |
28
|
|
|
const ACCESS_TOKEN = '2ec09aeccaf634d982eec793037e37fe'; |
29
|
|
|
const REFRESH_TOKEN = '0c59f7e609b0cc467067e39d523116ce'; |
30
|
|
|
|
31
|
|
|
public function testGetRequest() |
32
|
|
|
{ |
33
|
|
|
$expected = new GeoLocationPerformanceReportRequest(); |
34
|
|
|
$expected->Format = ReportFormat::Csv; |
35
|
|
|
$expected->ReportName = GeoLocationPerformanceReport::NAME; |
36
|
|
|
$expected->ReturnOnlyCompleteData = true; |
37
|
|
|
$expected->Aggregation = NonHourlyReportAggregation::Daily; |
38
|
|
|
$expected->Scope = new AccountThroughAdGroupReportScope(); |
39
|
|
|
$expected->Time = new ReportTime(); |
40
|
|
|
$expected->Time->PredefinedTime = self::YESTERDAY; |
|
|
|
|
41
|
|
|
$expected->Columns = []; |
42
|
|
|
|
43
|
|
|
$report = new GeoLocationPerformanceReport(); |
44
|
|
|
$result = $report->getRequest([], self::YESTERDAY); |
45
|
|
|
|
46
|
|
|
$this->assertEquals($expected, $result); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testSetAggregation() |
50
|
|
|
{ |
51
|
|
|
$report = new GeoLocationPerformanceReport(); |
52
|
|
|
|
53
|
|
|
$result = $report->getRequest([], self::YESTERDAY); |
54
|
|
|
$this->assertEquals(NonHourlyReportAggregation::Daily, $result->Aggregation); |
|
|
|
|
55
|
|
|
|
56
|
|
|
$report->setAggregation(NonHourlyReportAggregation::Monthly); |
57
|
|
|
$result = $report->getRequest([], self::YESTERDAY); |
58
|
|
|
|
59
|
|
|
$this->assertEquals(NonHourlyReportAggregation::Monthly, $result->Aggregation); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testGeoLocationPerformanceReportReturnsArrayWithCsv() |
63
|
|
|
{ |
64
|
|
|
$apiClient = $this->getApiClient( |
65
|
|
|
$this->getRequestNewAccessTokenMock(), |
66
|
|
|
new ApiDetails('refreshToken', 'clientId', 'clientSecret', 'redirectUri', 'devToken'), |
67
|
|
|
$this->getClientProxyMock(), |
68
|
|
|
$this->getFileHelperMock(), |
69
|
|
|
$this->getCsvHelperMock(), |
70
|
|
|
$this->getTimeHelperMock() |
71
|
|
|
); |
72
|
|
|
$result = $apiClient->get(['TimePeriod', 'AccountName', 'AdGroupId'], 'GeoLocationPerformanceReport', ReportTimePeriod::LastWeek); |
73
|
|
|
$this->assertEquals([ASSETS_DIR . 'report.csv'], $result); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testGeoLocationPerformanceReportMoveFile() |
77
|
|
|
{ |
78
|
|
|
$apiClient = $this->getApiClient( |
79
|
|
|
$this->getRequestNewAccessTokenMock(), |
80
|
|
|
new ApiDetails('refreshToken', 'clientId', 'clientSecret', 'redirectUri', 'devToken'), |
81
|
|
|
$this->getClientProxyMock(), |
82
|
|
|
$this->getFileHelperMock(), |
83
|
|
|
$this->getCsvHelperMock(), |
84
|
|
|
$this->getTimeHelperMock() |
85
|
|
|
); |
86
|
|
|
$result = $apiClient->get(['TimePeriod', 'AccountName', 'AdGroupId'], 'GeoLocationPerformanceReport', ReportTimePeriod::LastWeek, ASSETS_DIR . 'test.csv'); |
87
|
|
|
$this->assertEquals(ASSETS_DIR . 'test.csv', $result); |
88
|
|
|
|
89
|
|
|
//--Move File back |
90
|
|
|
$fileSystem = new Filesystem(); |
91
|
|
|
$fileSystem->rename(ASSETS_DIR . 'test.csv', ASSETS_DIR . 'report.csv'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testGetRefreshToken() |
95
|
|
|
{ |
96
|
|
|
$apiClient = $this->getApiClient( |
97
|
|
|
$this->getRequestNewAccessTokenMock(), |
98
|
|
|
new ApiDetails('refreshToken', 'clientId', 'clientSecret', 'redirectUri', 'devToken'), |
99
|
|
|
$this->getClientProxyMock(), |
100
|
|
|
$this->getFileHelperMock(), |
101
|
|
|
$this->getCsvHelperMock(), |
102
|
|
|
$this->getTimeHelperMock() |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
$this->assertEquals('refreshToken', $apiClient->getRefreshToken()); |
106
|
|
|
|
107
|
|
|
$apiClient->get(['TimePeriod', 'AccountName', 'AdGroupId'], 'GeoLocationPerformanceReport', ReportTimePeriod::LastWeek); |
108
|
|
|
$this->assertEquals(self::REFRESH_TOKEN, $apiClient->getRefreshToken()); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
View Code Duplication |
public function testGeoLocationPerformanceReportTimeoutException() |
|
|
|
|
112
|
|
|
{ |
113
|
|
|
$this->expectException(Exceptions\RequestTimeoutException::class); |
114
|
|
|
$apiClient = $this->getApiClient( |
115
|
|
|
$this->getRequestNewAccessTokenMock(), |
116
|
|
|
new ApiDetails('refreshToken', 'clientId', 'clientSecret', 'redirectUri', 'devToken'), |
117
|
|
|
$this->getClientProxyMock('Pending'), |
118
|
|
|
new Helper\File(), |
119
|
|
|
new Helper\Csv(), |
120
|
|
|
$this->getTimeHelperMock() |
121
|
|
|
); |
122
|
|
|
$apiClient->get(['TimePeriod', 'AccountName', 'AdGroupId'], 'GeoLocationPerformanceReport'); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
View Code Duplication |
public function testGeoLocationPerformanceReportRequestErrorException() |
|
|
|
|
126
|
|
|
{ |
127
|
|
|
$this->expectException(Exceptions\ReportRequestErrorException::class); |
128
|
|
|
$apiClient = $this->getApiClient( |
129
|
|
|
$this->getRequestNewAccessTokenMock(), |
130
|
|
|
new ApiDetails('refreshToken', 'clientId', 'clientSecret', 'redirectUri', 'devToken'), |
131
|
|
|
$this->getClientProxyMock('Error'), |
132
|
|
|
new Helper\File(), |
133
|
|
|
new Helper\Csv(), |
134
|
|
|
$this->getTimeHelperMock() |
135
|
|
|
); |
136
|
|
|
$apiClient->get(['TimePeriod', 'AccountName', 'AdGroupId'], 'GeoLocationPerformanceReport'); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function testPollGenerateReportSoapException() |
140
|
|
|
{ |
141
|
|
|
$clientProxyMock = Mockery::mock(ClientProxy::class); |
142
|
|
|
$clientProxyMock->ReportRequestId = 'reportID'; |
|
|
|
|
143
|
|
|
$clientProxyMock |
|
|
|
|
144
|
|
|
->shouldReceive('ConstructWithCredentials') |
145
|
|
|
->andReturnSelf() |
146
|
|
|
->once() |
147
|
|
|
->shouldReceive('GetNamespace') |
148
|
|
|
->once() |
149
|
|
|
->andReturn('Namespace') |
150
|
|
|
->shouldReceive('GetService') |
151
|
|
|
->twice() |
152
|
|
|
->andReturnSelf() |
153
|
|
|
->shouldReceive('SubmitGenerateReport') |
154
|
|
|
->once() |
155
|
|
|
->andReturnSelf() |
156
|
|
|
->shouldReceive('PollGenerateReport') |
157
|
|
|
->andThrow($this->generateSoapFault(0)); |
158
|
|
|
$this->expectException(Exceptions\SoapInternalErrorException::class); |
159
|
|
|
$apiClient = $this->getApiClient( |
160
|
|
|
$this->getRequestNewAccessTokenMock(), |
161
|
|
|
new ApiDetails('refreshToken', 'clientId', 'clientSecret', 'redirectUri', 'devToken'), |
162
|
|
|
$clientProxyMock, |
163
|
|
|
new Helper\File(), |
164
|
|
|
new Helper\Csv(), |
165
|
|
|
$this->getTimeHelperMock() |
166
|
|
|
); |
167
|
|
|
$apiClient->get(['TimePeriod', 'AccountName', 'AdGroupId'], 'GeoLocationPerformanceReport'); |
168
|
|
|
} |
169
|
|
|
/** |
170
|
|
|
* @return Mockery\MockInterface |
171
|
|
|
*/ |
172
|
|
|
private function getClientProxyMock($reportStatus = 'Success') |
173
|
|
|
{ |
174
|
|
|
$clientProxyMock = Mockery::mock(ClientProxy::class); |
175
|
|
|
$clientProxyMock |
|
|
|
|
176
|
|
|
->shouldReceive('ConstructWithCredentials') |
177
|
|
|
->andReturnSelf() |
178
|
|
|
->once() |
179
|
|
|
->shouldReceive('GetNamespace') |
180
|
|
|
->between(1, 48) |
181
|
|
|
->andReturn('Namespace') |
182
|
|
|
->shouldReceive('GetService') |
183
|
|
|
->between(2, 49) |
184
|
|
|
->andReturnSelf() |
185
|
|
|
->shouldReceive('SubmitGenerateReport') |
186
|
|
|
->between(1, 48) |
187
|
|
|
->andReturnSelf() |
188
|
|
|
->shouldReceive('PollGenerateReport') |
189
|
|
|
->between(1, 48) |
190
|
|
|
->andReturnSelf(); |
191
|
|
|
|
192
|
|
|
$status = new \stdClass(); |
193
|
|
|
$status->Status = $reportStatus; |
194
|
|
|
$status->ReportDownloadUrl = 'http://example.com/download.zip'; |
195
|
|
|
|
196
|
|
|
$clientProxyMock->ReportRequestId = 'reportID'; |
|
|
|
|
197
|
|
|
$clientProxyMock->ReportRequestStatus = $status; |
|
|
|
|
198
|
|
|
|
199
|
|
|
return $clientProxyMock; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @return Mockery\MockInterface |
204
|
|
|
*/ |
205
|
|
View Code Duplication |
private function getRequestNewAccessTokenMock() |
|
|
|
|
206
|
|
|
{ |
207
|
|
|
$requestNewAccessTokenMock = Mockery::mock(OauthTokenService::class); |
208
|
|
|
$requestNewAccessTokenMock |
209
|
|
|
->shouldReceive('refreshToken') |
210
|
|
|
->with('clientId', 'clientSecret', 'redirectUri', AccessToken::class) |
211
|
|
|
->once() |
212
|
|
|
->andReturn(new AccessToken(self::ACCESS_TOKEN, self::REFRESH_TOKEN)); |
213
|
|
|
|
214
|
|
|
return $requestNewAccessTokenMock; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @return Mockery\MockInterface |
219
|
|
|
*/ |
220
|
|
|
private function getFileHelperMock() |
221
|
|
|
{ |
222
|
|
|
$zipHelperMock = Mockery::mock(Helper\File::class); |
223
|
|
|
$zipHelperMock |
|
|
|
|
224
|
|
|
->shouldReceive('getFile') |
225
|
|
|
->andReturn('/tmp/report.zip') |
226
|
|
|
->once() |
227
|
|
|
->shouldReceive('unZip') |
228
|
|
|
->with('/tmp/report.zip') |
229
|
|
|
->andReturn([ASSETS_DIR . 'report.csv']) |
230
|
|
|
->once(); |
231
|
|
|
|
232
|
|
|
return $zipHelperMock; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @return Mockery\MockInterface |
237
|
|
|
*/ |
238
|
|
|
private function getCsvHelperMock() |
239
|
|
|
{ |
240
|
|
|
$lines = file(ASSETS_DIR . 'report.csv'); |
241
|
|
|
$csvHelperMock = Mockery::mock(Helper\Csv::class); |
242
|
|
|
$csvHelperMock |
|
|
|
|
243
|
|
|
->shouldReceive('removeHeaders') |
244
|
|
|
->andReturn($lines) |
245
|
|
|
->once() |
246
|
|
|
->shouldReceive('removeLastLines') |
247
|
|
|
->andReturn($lines) |
248
|
|
|
->once() |
249
|
|
|
->shouldReceive('convertDateMDYtoYMD') |
250
|
|
|
->andReturn($lines) |
251
|
|
|
->once(); |
252
|
|
|
|
253
|
|
|
return $csvHelperMock; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
private function getTimeHelperMock() |
257
|
|
|
{ |
258
|
|
|
$timeHelperMock = Mockery::mock(Helper\Time::class); |
259
|
|
|
$timeHelperMock->shouldReceive('sleep')->andReturnNull(); |
260
|
|
|
|
261
|
|
|
return $timeHelperMock; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @param OauthTokenService $requestNewAccessToken |
266
|
|
|
* @param ClientProxy $clientProxy |
267
|
|
|
* @param Helper\File $fileHelper |
268
|
|
|
* @param Helper\Csv $csvHelper |
269
|
|
|
* @param Helper\Time $timeHelper |
270
|
|
|
* |
271
|
|
|
* @return Client |
272
|
|
|
*/ |
273
|
|
View Code Duplication |
private function getApiClient(OauthTokenService $requestNewAccessToken, ApiDetails $apiDetails, ClientProxy $clientProxy, Helper\File $fileHelper, Helper\Csv $csvHelper, Helper\Time $timeHelper) |
|
|
|
|
274
|
|
|
{ |
275
|
|
|
$apiClient = new Client($requestNewAccessToken, $apiDetails, $clientProxy, $fileHelper, $csvHelper, $timeHelper); |
276
|
|
|
$apiClient->setConfig(['cache_dir' => '/tmp']); |
277
|
|
|
|
278
|
|
|
return $apiClient; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* @param $code |
283
|
|
|
* @return SoapFault |
284
|
|
|
*/ |
285
|
|
View Code Duplication |
private function generateSoapFault($code) |
|
|
|
|
286
|
|
|
{ |
287
|
|
|
$message = "an error message {$code}"; |
288
|
|
|
$error = new stdClass(); |
289
|
|
|
$error->Code = $code; |
290
|
|
|
$error->Message = $message; |
291
|
|
|
$exception = new SoapFault('Server', ''); |
292
|
|
|
$exception->detail = new stdClass(); |
|
|
|
|
293
|
|
|
$exception->detail->AdApiFaultDetail = new stdClass(); |
294
|
|
|
$exception->detail->AdApiFaultDetail->Errors = new stdClass(); |
295
|
|
|
$exception->detail->AdApiFaultDetail->Errors->AdApiError = [$error]; |
296
|
|
|
|
297
|
|
|
return $exception; |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..