1
|
|
|
<?php |
2
|
|
|
namespace OptimizelyPHPTest\Service\v2; |
3
|
|
|
|
4
|
|
|
use OptimizelyPHPTest\Service\v2\BaseServiceTest; |
5
|
|
|
use WebMarketingROI\OptimizelyPHP\OptimizelyApiClient; |
6
|
|
|
use WebMarketingROI\OptimizelyPHP\Result; |
7
|
|
|
use WebMarketingROI\OptimizelyPHP\Service\v2\Events; |
8
|
|
|
use WebMarketingROI\OptimizelyPHP\Resource\v2\Event; |
9
|
|
|
use WebMarketingROI\OptimizelyPHP\Resource\v2\ClickEvent; |
10
|
|
|
use WebMarketingROI\OptimizelyPHP\Resource\v2\CustomEvent; |
11
|
|
|
use WebMarketingROI\OptimizelyPHP\Resource\v2\Project; |
12
|
|
|
use WebMarketingROI\OptimizelyPHP\Resource\v2\Page; |
13
|
|
|
|
14
|
|
|
class EventsTest extends BaseServiceTest |
15
|
|
|
{ |
16
|
|
|
public function testListAll() |
17
|
|
|
{ |
18
|
|
|
// Mock 'OptimizelyApiClient' object to avoid real API calls |
19
|
|
|
$optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient') |
20
|
|
|
->disableOriginalConstructor() |
21
|
|
|
->getMock(); |
22
|
|
|
|
23
|
|
|
$result = new Result(array( |
24
|
|
|
array( |
25
|
|
|
"archived" => true, |
26
|
|
|
"category" => "add_to_cart", |
27
|
|
|
"description" => "Item added to cart", |
28
|
|
|
"event_filter" => array( |
29
|
|
|
"filter_type" => "target_selector", |
30
|
|
|
"selector" => ".menu-options" |
31
|
|
|
), |
32
|
|
|
"event_type" => "custom", |
33
|
|
|
"key" => "add_to_cart", |
34
|
|
|
"name" => "Add to Cart", |
35
|
|
|
"page_id" => 5000, |
36
|
|
|
"project_id" => 1000, |
37
|
|
|
"created" => "2016-10-18T05:07:04.136Z", |
38
|
|
|
"id" => 0, |
39
|
|
|
"is_classic" => false, |
40
|
|
|
"is_editable" => true |
41
|
|
|
) |
42
|
|
|
), 200); |
43
|
|
|
|
44
|
|
|
$optimizelyApiClientMock->method('sendApiRequest') |
45
|
|
|
->willReturn($result); |
46
|
|
|
|
47
|
|
|
$eventsService = new Events($optimizelyApiClientMock); |
48
|
|
|
|
49
|
|
|
$result = $eventsService->listAll(1000, true); |
|
|
|
|
50
|
|
|
$events = $result->getPayload(); |
51
|
|
|
|
52
|
|
|
$this->assertTrue(count($events)==1); |
53
|
|
|
$this->assertTrue($events[0] instanceOf Event); |
54
|
|
|
$this->assertTrue($events[0]->getName()=='Add to Cart'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testGet() |
58
|
|
|
{ |
59
|
|
|
// Mock 'OptimizelyApiClient' object to avoid real API calls |
60
|
|
|
$optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient') |
61
|
|
|
->disableOriginalConstructor() |
62
|
|
|
->getMock(); |
63
|
|
|
|
64
|
|
|
$result = new Result(array( |
65
|
|
|
"archived" => true, |
66
|
|
|
"category" => "add_to_cart", |
67
|
|
|
"description" => "Item added to cart", |
68
|
|
|
"event_filter" => array( |
69
|
|
|
"filter_type" => "target_selector", |
70
|
|
|
"selector" => ".menu-options" |
71
|
|
|
), |
72
|
|
|
"event_type" => "custom", |
73
|
|
|
"key" => "add_to_cart", |
74
|
|
|
"name" => "Add to Cart", |
75
|
|
|
"page_id" => 5000, |
76
|
|
|
"project_id" => 1000, |
77
|
|
|
"created" => "2016-10-18T05:07:04.146Z", |
78
|
|
|
"id" => 0, |
79
|
|
|
"is_classic" => false, |
80
|
|
|
"is_editable" => true |
81
|
|
|
), 200); |
82
|
|
|
|
83
|
|
|
$optimizelyApiClientMock->method('sendApiRequest') |
84
|
|
|
->willReturn($result); |
85
|
|
|
|
86
|
|
|
$eventsService = new Events($optimizelyApiClientMock); |
87
|
|
|
|
88
|
|
|
$result = $eventsService->get(5000); |
|
|
|
|
89
|
|
|
$event = $result->getPayload(); |
90
|
|
|
|
91
|
|
|
$this->assertTrue($event instanceOf Event); |
92
|
|
|
$this->assertTrue($event->getName()=='Add to Cart'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
View Code Duplication |
public function testCreateClickEvent() |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
// Mock 'OptimizelyApiClient' object to avoid real API calls |
98
|
|
|
$optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient') |
99
|
|
|
->disableOriginalConstructor() |
100
|
|
|
->getMock(); |
101
|
|
|
|
102
|
|
|
$result = new Result(array( |
103
|
|
|
"event_filter" => array( |
104
|
|
|
"filter_type" => "target_selector", |
105
|
|
|
"selector" => ".menu-options" |
106
|
|
|
), |
107
|
|
|
"name" => "Add to Cart", |
108
|
|
|
"archived" => true, |
109
|
|
|
"category" => "add_to_cart", |
110
|
|
|
"description" => "string", |
111
|
|
|
"event_type" => "click", |
112
|
|
|
"key" => "add_to_cart", |
113
|
|
|
"created" => "2016-10-18T05:07:04.153Z", |
114
|
|
|
"id" => 0, |
115
|
|
|
"is_classic" => false, |
116
|
|
|
"is_editable" => true, |
117
|
|
|
"page_id" => 0, |
118
|
|
|
"project_id" => 1000 |
119
|
|
|
), 201); |
120
|
|
|
|
121
|
|
|
$optimizelyApiClientMock->method('sendApiRequest') |
122
|
|
|
->willReturn($result); |
123
|
|
|
|
124
|
|
|
$eventsService = new Events($optimizelyApiClientMock); |
125
|
|
|
|
126
|
|
|
$event = new ClickEvent(array( |
127
|
|
|
"event_filter" => array( |
128
|
|
|
"filter_type" => "target_selector", |
129
|
|
|
"selector" => ".menu-options" |
130
|
|
|
), |
131
|
|
|
"name" => "Add to Cart", |
132
|
|
|
"archived" => true, |
133
|
|
|
"category" => "add_to_cart", |
134
|
|
|
"description" => "string", |
135
|
|
|
"event_type" => "click", |
136
|
|
|
"key" => "add_to_cart" |
137
|
|
|
)); |
138
|
|
|
|
139
|
|
|
$result = $eventsService->createClickEvent(0, $event); |
140
|
|
|
$createdEvent = $result->getPayload(); |
141
|
|
|
|
142
|
|
|
$this->assertTrue($createdEvent instanceOf ClickEvent); |
143
|
|
|
$this->assertTrue($createdEvent->getName()=='Add to Cart'); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
View Code Duplication |
public function testCreateCustomEvent() |
|
|
|
|
147
|
|
|
{ |
148
|
|
|
// Mock 'OptimizelyApiClient' object to avoid real API calls |
149
|
|
|
$optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient') |
150
|
|
|
->disableOriginalConstructor() |
151
|
|
|
->getMock(); |
152
|
|
|
|
153
|
|
|
$result = new Result(array( |
154
|
|
|
"archived" => true, |
155
|
|
|
"category" => "add_to_cart", |
156
|
|
|
"description" => "string", |
157
|
|
|
"event_type" => "custom", |
158
|
|
|
"key" => "loaded_new_app", |
159
|
|
|
"name" => "Loaded New App", |
160
|
|
|
"created" => "2016-10-18T05:07:04.163Z", |
161
|
|
|
"id" => 0, |
162
|
|
|
"is_classic" => false, |
163
|
|
|
"is_editable" => true, |
164
|
|
|
"page_id" => 0, |
165
|
|
|
"project_id" => 1000 |
166
|
|
|
), 201); |
167
|
|
|
|
168
|
|
|
$optimizelyApiClientMock->method('sendApiRequest') |
169
|
|
|
->willReturn($result); |
170
|
|
|
|
171
|
|
|
$eventsService = new Events($optimizelyApiClientMock); |
172
|
|
|
|
173
|
|
|
$event = new CustomEvent(array( |
174
|
|
|
"archived" => true, |
175
|
|
|
"category" => "add_to_cart", |
176
|
|
|
"description" => "string", |
177
|
|
|
"event_type" => "custom", |
178
|
|
|
"key" => "loaded_new_app", |
179
|
|
|
"name" => "Loaded New App" |
180
|
|
|
)); |
181
|
|
|
|
182
|
|
|
$result = $eventsService->createCustomEvent(0, $event); |
183
|
|
|
$createdEvent = $result->getPayload(); |
184
|
|
|
|
185
|
|
|
$this->assertTrue($createdEvent instanceOf CustomEvent); |
186
|
|
|
$this->assertTrue($createdEvent->getName()=='Loaded New App'); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
View Code Duplication |
public function testUpdateClickEvent() |
|
|
|
|
190
|
|
|
{ |
191
|
|
|
// Mock 'OptimizelyApiClient' object to avoid real API calls |
192
|
|
|
$optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient') |
193
|
|
|
->disableOriginalConstructor() |
194
|
|
|
->getMock(); |
195
|
|
|
|
196
|
|
|
$result = new Result(array( |
197
|
|
|
"event_filter" => array( |
198
|
|
|
"filter_type" => "target_selector", |
199
|
|
|
"selector" => ".menu-options" |
200
|
|
|
), |
201
|
|
|
"name" => "Add to Cart", |
202
|
|
|
"archived" => true, |
203
|
|
|
"category" => "add_to_cart", |
204
|
|
|
"description" => "string", |
205
|
|
|
"event_type" => "click", |
206
|
|
|
"key" => "add_to_cart", |
207
|
|
|
"created" => "2016-10-18T05:07:04.153Z", |
208
|
|
|
"id" => 0, |
209
|
|
|
"is_classic" => false, |
210
|
|
|
"is_editable" => true, |
211
|
|
|
"page_id" => 0, |
212
|
|
|
"project_id" => 1000 |
213
|
|
|
), 200); |
214
|
|
|
|
215
|
|
|
$optimizelyApiClientMock->method('sendApiRequest') |
216
|
|
|
->willReturn($result); |
217
|
|
|
|
218
|
|
|
$eventsService = new Events($optimizelyApiClientMock); |
219
|
|
|
|
220
|
|
|
$event = new ClickEvent(array( |
221
|
|
|
"event_filter" => array( |
222
|
|
|
"filter_type" => "target_selector", |
223
|
|
|
"selector" => ".menu-options" |
224
|
|
|
), |
225
|
|
|
"name" => "Add to Cart", |
226
|
|
|
"archived" => true, |
227
|
|
|
"category" => "add_to_cart", |
228
|
|
|
"description" => "string", |
229
|
|
|
"event_type" => "click", |
230
|
|
|
"key" => "add_to_cart" |
231
|
|
|
)); |
232
|
|
|
|
233
|
|
|
$result = $eventsService->updateClickEvent(0, 0, $event); |
234
|
|
|
$updatedEvent = $result->getPayload(); |
235
|
|
|
|
236
|
|
|
$this->assertTrue($updatedEvent instanceOf ClickEvent); |
237
|
|
|
$this->assertTrue($updatedEvent->getName()=='Add to Cart'); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
View Code Duplication |
public function testUpdateCustomEvent() |
|
|
|
|
241
|
|
|
{ |
242
|
|
|
// Mock 'OptimizelyApiClient' object to avoid real API calls |
243
|
|
|
$optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient') |
244
|
|
|
->disableOriginalConstructor() |
245
|
|
|
->getMock(); |
246
|
|
|
|
247
|
|
|
$result = new Result(array( |
248
|
|
|
"archived" => true, |
249
|
|
|
"category" => "add_to_cart", |
250
|
|
|
"description" => "string", |
251
|
|
|
"event_type" => "custom", |
252
|
|
|
"key" => "loaded_new_app", |
253
|
|
|
"name" => "Loaded New App", |
254
|
|
|
"created" => "2016-10-18T05:07:04.163Z", |
255
|
|
|
"id" => 0, |
256
|
|
|
"is_classic" => false, |
257
|
|
|
"is_editable" => true, |
258
|
|
|
"page_id" => 0, |
259
|
|
|
"project_id" => 1000 |
260
|
|
|
), 200); |
261
|
|
|
|
262
|
|
|
$optimizelyApiClientMock->method('sendApiRequest') |
263
|
|
|
->willReturn($result); |
264
|
|
|
|
265
|
|
|
$eventsService = new Events($optimizelyApiClientMock); |
266
|
|
|
|
267
|
|
|
$event = new CustomEvent(array( |
268
|
|
|
"archived" => true, |
269
|
|
|
"category" => "add_to_cart", |
270
|
|
|
"description" => "string", |
271
|
|
|
"event_type" => "custom", |
272
|
|
|
"key" => "loaded_new_app", |
273
|
|
|
"name" => "Loaded New App" |
274
|
|
|
)); |
275
|
|
|
|
276
|
|
|
$result = $eventsService->updateCustomEvent(0, 0, $event); |
277
|
|
|
$updatedEvent = $result->getPayload(); |
278
|
|
|
|
279
|
|
|
$this->assertTrue($updatedEvent instanceOf CustomEvent); |
280
|
|
|
$this->assertTrue($updatedEvent->getName()=='Loaded New App'); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
public function testIntegration() |
284
|
|
|
{ |
285
|
|
|
if (!getenv('OPTIMIZELY_PHP_TEST_INTEGRATION')) |
286
|
|
|
$this->markTestSkipped('OPTIMIZELY_PHP_TEST_INTEGRATION env var is not set'); |
287
|
|
|
|
288
|
|
|
$credentials = $this->loadCredentialsFromFile(); |
289
|
|
|
|
290
|
|
|
$optimizelyClient = new OptimizelyApiClient($credentials, 'v2'); |
|
|
|
|
291
|
|
|
$this->assertTrue($optimizelyClient!=null); |
292
|
|
|
|
293
|
|
|
// Create new project |
294
|
|
|
$curDate = date('Y-m-d H:i:s'); |
295
|
|
|
$newProject = new Project(array( |
296
|
|
|
"name" => "Test Project $curDate", |
297
|
|
|
"account_id" => 12345, |
298
|
|
|
"confidence_threshold" => 0.9, |
299
|
|
|
"platform" => "web", |
300
|
|
|
"status" => "active", |
301
|
|
|
"web_snippet" => array( |
302
|
|
|
"enable_force_variation" => false, |
303
|
|
|
"exclude_disabled_experiments" => false, |
304
|
|
|
"exclude_names" => true, |
305
|
|
|
"include_jquery" => true, |
306
|
|
|
"ip_anonymization" => false, |
307
|
|
|
"ip_filter" => "^206\\.23\\.100\\.([5-9][0-9]|1([0-4][0-9]|50))$", |
308
|
|
|
"library" => "jquery-1.11.3-trim", |
309
|
|
|
"project_javascript" => "alert(\"Active Experiment\")" |
310
|
|
|
) |
311
|
|
|
)); |
312
|
|
|
|
313
|
|
|
$result = $optimizelyClient->projects()->create($newProject); |
|
|
|
|
314
|
|
|
$createdProject = $result->getPayload(); |
315
|
|
|
|
316
|
|
|
// Create new page in the project |
317
|
|
|
$page = new Page(array( |
318
|
|
|
"edit_url" => "https://www.optimizely.com", |
319
|
|
|
"name" => "Home Page", |
320
|
|
|
"project_id" => $createdProject->getId(), |
321
|
|
|
"activation_code" => "string", |
322
|
|
|
"activation_type" => "immediate", |
323
|
|
|
"archived" => false, |
324
|
|
|
"category" => "article", |
325
|
|
|
"conditions" => "[\"and\", {\"type\": \"url\", \"match_type\": \"substring\", \"value\": \"optimize\"}]", |
326
|
|
|
"key" => "home_page", |
327
|
|
|
"page_type" => "single_url", |
328
|
|
|
"created" => "2016-10-18T05:07:04.113Z", |
329
|
|
|
"id" => 4000, |
330
|
|
|
"last_modified" => "2016-10-18T05:07:04.113Z" |
331
|
|
|
)); |
332
|
|
|
|
333
|
|
|
$result = $optimizelyClient->pages()->create($page); |
|
|
|
|
334
|
|
|
$createdPage = $result->getPayload(); |
335
|
|
|
|
336
|
|
|
$this->assertTrue($createdPage instanceOf Page); |
337
|
|
|
$this->assertTrue($createdPage->getName()=='Home Page'); |
338
|
|
|
|
339
|
|
|
// Create new event in the project |
340
|
|
|
$event = new ClickEvent(array( |
341
|
|
|
"event_filter" => array( |
342
|
|
|
"filter_type" => "target_selector", |
343
|
|
|
"selector" => ".menu-options" |
344
|
|
|
), |
345
|
|
|
"name" => "Add to Cart", |
346
|
|
|
"archived" => true, |
347
|
|
|
"category" => "add_to_cart", |
348
|
|
|
"description" => "Some simple event", |
349
|
|
|
"event_type" => "click", |
350
|
|
|
"key" => "add_to_cart", |
351
|
|
|
"created" => "2016-10-18T05:07:04.153Z", |
352
|
|
|
"id" => 0, |
353
|
|
|
"is_classic" => false, |
354
|
|
|
"is_editable" => true, |
355
|
|
|
"page_id" => $createdPage->getId(), |
356
|
|
|
"project_id" => $createdProject->getId() |
357
|
|
|
)); |
358
|
|
|
|
359
|
|
|
$result = $optimizelyClient->events()->createClickEvent($createdPage->getId(), $event); |
|
|
|
|
360
|
|
|
$createdEvent = $result->getPayload(); |
361
|
|
|
|
362
|
|
|
$this->assertTrue($createdEvent instanceOf ClickEvent); |
363
|
|
|
$this->assertTrue($createdEvent->getName()=='Add to Cart'); |
364
|
|
|
|
365
|
|
|
// List all existing events and try to find the created event |
366
|
|
|
$eventFound = false; |
367
|
|
|
try { |
368
|
|
|
$page = 1; |
369
|
|
View Code Duplication |
for (;;) { |
|
|
|
|
370
|
|
|
$result = $optimizelyClient->events()->listAll($createdEvent->getId(), $page); |
|
|
|
|
371
|
|
|
|
372
|
|
|
$events = $result->getPayload(); |
373
|
|
|
|
374
|
|
|
foreach ($events as $event) { |
375
|
|
|
if ($event->getName()=="Add to Cart") { |
376
|
|
|
$eventFound = true; |
377
|
|
|
break; |
378
|
|
|
} |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
if ($result->getNextPage()==null) |
382
|
|
|
break; |
383
|
|
|
|
384
|
|
|
$page ++; |
385
|
|
|
} |
386
|
|
|
} |
387
|
|
|
catch (Exception $e) { |
|
|
|
|
388
|
|
|
// Handle error. |
389
|
|
|
$code = $e->getCode(); |
390
|
|
|
$httpCode = $e->getHttpCode(); |
391
|
|
|
$message = $e->getMessage(); |
392
|
|
|
$uuid = $e->getUuid(); |
393
|
|
|
echo "Exception caught: $message (code=$code http_code=$httpCode uuid=$uuid)\n"; |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
$this->assertTrue($eventFound); |
397
|
|
|
|
398
|
|
|
// Update page |
399
|
|
|
$createdEvent->setName('Add to Cart 2'); |
400
|
|
|
$result = $optimizelyClient->events()->update($createdEvent->getId(), $createdEvent); |
|
|
|
|
401
|
|
|
$updatedEvent = $result->getPayload(); |
402
|
|
|
|
403
|
|
|
$this->assertTrue($updatedEvent instanceOf Event); |
404
|
|
|
$this->assertTrue($updatedEvent->getName()=='Add to Cart 2'); |
405
|
|
|
|
406
|
|
|
// Make project archived |
407
|
|
|
|
408
|
|
|
$createdProject->setStatus('archived'); |
409
|
|
|
$result = $optimizelyClient->projects()->update($createdProject->getId(), $createdProject); |
|
|
|
|
410
|
|
|
$updatedProject = $result->getPayload(); |
411
|
|
|
|
412
|
|
|
$this->assertEquals('archived', $updatedProject->getStatus()); |
413
|
|
|
} |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: