ExperimentsTest::testGet_NotIntegerExperimentId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 11
loc 11
rs 9.9
c 0
b 0
f 0
1
<?php
2
namespace OptimizelyPHPTest\Service\v2;
3
4
use PHPUnit_Framework_TestCase;
5
use WebMarketingROI\OptimizelyPHP\OptimizelyApiClient;
6
use WebMarketingROI\OptimizelyPHP\Result;
7
use WebMarketingROI\OptimizelyPHP\Service\v2\Experiments;
8
use WebMarketingROI\OptimizelyPHP\Resource\v2\Experiment;
9
use WebMarketingROI\OptimizelyPHP\Resource\v2\ExperimentResults;
10
use WebMarketingROI\OptimizelyPHP\Resource\v2\Project;
11
use WebMarketingROI\OptimizelyPHP\Resource\v2\Page;
12
use WebMarketingROI\OptimizelyPHP\Resource\v2\ClickEvent;
13
use WebMarketingROI\OptimizelyPHP\Resource\v2\Audience;
14
15
class ExperimentsTest extends BaseServiceTest
16
{
17
    public function testListAll()
18
    {
19
        // Mock 'OptimizelyApiClient' object to avoid real API calls
20
        $optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient')
21
                            ->disableOriginalConstructor()
22
                            ->getMock();
23
24
        $curDate = date('Y-m-d H:i:s');
0 ignored issues
show
Unused Code introduced by
$curDate is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
25
        
26
        $result = new Result(array(
27
                        array(
28
                            "project_id" => 1000,
29
                            "audience_ids" => array(
30
                              1234,
31
                              1212,
32
                              1432
33
                            ),
34
                            "campaign_id" => 2000,
35
                            "changes" => array(
36
                              array(
37
                                "type" => "custom_code",
38
                                "allow_additional_redirect" => true,
39
                                "async" => true,
40
                                "selector" => "a[href*=\"optimizely\"]",
41
                                "dependencies" => array(
42
                                  24,
43
                                  26
44
                                ),
45
                                "destination" => "https://app.optimizely.com/",
46
                                "extension_id" => 1234,
47
                                "preserve_parameters" => true,
48
                                "src" => 524,
49
                                "value" => "window.someGlobalFunction();",
50
                                "id" => "string"
51
                              )
52
                            ),
53
                            "description" => "string",
54
                            "holdback" => 5000,
55
                            "key" => "home_page_experiment",
56
                            "metrics" => array(
57
                              array(
58
                                "aggregator" => "unique",
59
                                "event_id" => 0,
60
                                "field" => "revenue",
61
                                "scope" => "visitor"
62
                              )
63
                            ),
64
                            "name" => "Blue Button Experiment",
65
                            "schedule" => array(
66
                              "start_time" => "2016-10-17T07:05:00.070Z",
67
                              "stop_time" => "2016-10-17T07:05:00.070Z",
68
                              "time_zone" => "UTC"
69
                            ),
70
                            "status" => "active",
71
                            "variations" => array(
72
                              array(
73
                                "actions" => array(
74
                                  array(
75
                                    "changes" => array(
76
                                      array(
77
                                        "type" => "custom_code",
78
                                        "allow_additional_redirect" => true, 
79
                                        "async" => true,
80
                                        "selector" => "a[href*=\"optimizely\"]",
81
                                        "dependencies" => array(
82
                                          24,
83
                                          26
84
                                        ),
85
                                        "destination" => "https://app.optimizely.com/",
86
                                        "extension_id" => 1234,
87
                                        "preserve_parameters" => true,
88
                                        "src" => 524,
89
                                        "value" => "window.someGlobalFunction();",
90
                                        "id" => "string"
91
                                      )
92
                                    ),
93
                                    "page_id" => 0
94
                                  )
95
                                ),
96
                                "archived"=> true,
97
                                "key" => "blue_button_variation",
98
                                "name" => "Blue Button",
99
                                "variation_id" => 0,
100
                                "weight" => 0
101
                              )
102
                            ),
103
                            "created" => "2016-10-17T07:05:00.070Z",
104
                            "id" => 3000,
105
                            "is_classic" => false,
106
                            "last_modified" => "2016-10-17T07:05:00.070Z"
107
                          )
108
                        ), 200);
109
        
110
        $optimizelyApiClientMock->method('sendApiRequest')
111
                    ->willReturn($result);
112
        
113
        $experimentsService = new Experiments($optimizelyApiClientMock);
114
        
115
        $result = $experimentsService->listAll(1000);
116
        $experiments = $result->getPayload();
117
        
118
        $this->assertTrue(count($experiments)==1);
119
        $this->assertTrue($experiments[0] instanceOf Experiment);
120
        $this->assertTrue($experiments[0]->getName()=='Blue Button Experiment');        
121
    }
122
    
123
    /**
124
     * @expectedException Exception
125
     */
126 View Code Duplication
    public function testListAll_BothProjectIdAndCampaignIdAreNull()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
127
    {
128
        // Mock 'OptimizelyApiClient' object to avoid real API calls
129
        $optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient')
130
                            ->disableOriginalConstructor()
131
                            ->getMock();
132
        
133
        $experimentsService = new Experiments($optimizelyApiClientMock);
134
        
135
        $result = $experimentsService->listAll(null, null, false, 1, 25);
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
136
    }
137
    
138
    /**
139
     * @expectedException Exception
140
     */
141 View Code Duplication
    public function testListAll_InvalidPage()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
142
    {
143
        // Mock 'OptimizelyApiClient' object to avoid real API calls
144
        $optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient')
145
                            ->disableOriginalConstructor()
146
                            ->getMock();
147
        
148
        $experimentsService = new Experiments($optimizelyApiClientMock);
149
        
150
        $result = $experimentsService->listAll(1000, null, false, -1, 25);
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
151
    }
152
    
153
    /**
154
     * @expectedException Exception
155
     */
156 View Code Duplication
    public function testListAll_InvalidPerPage()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
157
    {
158
        // Mock 'OptimizelyApiClient' object to avoid real API calls
159
        $optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient')
160
                            ->disableOriginalConstructor()
161
                            ->getMock();
162
        
163
        $experimentsService = new Experiments($optimizelyApiClientMock);
164
        
165
        $result = $experimentsService->listAll(1000, null, false, 1, 10000);
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
166
    }
167
    
168
    public function testGet()
169
    {
170
        // Mock 'OptimizelyApiClient' object to avoid real API calls
171
        $optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient')
172
                            ->disableOriginalConstructor()
173
                            ->getMock();
174
175
        $result = new Result(array(
176
                            "project_id" => 1000,
177
                            "audience_ids" => array(
178
                              1234,
179
                              1212,
180
                              1432
181
                            ),
182
                            "campaign_id" => 2000,
183
                            "changes" => array(
184
                              array(
185
                                "type" => "custom_code",
186
                                "allow_additional_redirect" => true,
187
                                "async" => true,
188
                                "selector" => "a[href*=\"optimizely\"]",
189
                                "dependencies" => array(
190
                                  24,
191
                                  26
192
                                ),
193
                                "destination" => "https://app.optimizely.com/",
194
                                "extension_id" => 1234,
195
                                "preserve_parameters" => true,
196
                                "src" => 524,
197
                                "value" => "window.someGlobalFunction();",
198
                                "id" => "string"
199
                              )
200
                            ),
201
                            "description" => "string",
202
                            "holdback" => 5000,
203
                            "key" => "home_page_experiment",
204
                            "metrics" => array(
205
                              array(
206
                                "aggregator" => "unique",
207
                                "event_id" => 0,
208
                                "field" => "revenue",
209
                                "scope" => "session"
210
                              )
211
                            ),
212
                            "name" => "Blue Button Experiment",
213
                            "schedule" => array(
214
                              "start_time" => "2016-10-17T07:05:00.070Z",
215
                              "stop_time" => "2016-10-17T07:05:00.070Z",
216
                              "time_zone" => "UTC"
217
                            ),
218
                            "status" => "active",
219
                            "variations" => array(
220
                              array(
221
                                "actions" => array(
222
                                  array(
223
                                    "changes" => array(
224
                                      array(
225
                                        "type" => "custom_code",
226
                                        "allow_additional_redirect" => true, 
227
                                        "async" => true,
228
                                        "selector" => "a[href*=\"optimizely\"]",
229
                                        "dependencies" => array(
230
                                          24,
231
                                          26
232
                                        ),
233
                                        "destination" => "https://app.optimizely.com/",
234
                                        "extension_id" => 1234,
235
                                        "preserve_parameters" => true,
236
                                        "src" => 524,
237
                                        "value" => "window.someGlobalFunction();",
238
                                        "id" => "string"
239
                                      )
240
                                    ),
241
                                    "page_id" => 0
242
                                  )
243
                                ),
244
                                "archived"=> true,
245
                                "key" => "blue_button_variation",
246
                                "name" => "Blue Button",
247
                                "variation_id" => 0,
248
                                "weight" => 0
249
                              )
250
                            ),
251
                            "created" => "2016-10-17T07:05:00.070Z",
252
                            "id" => 3000,
253
                            "is_classic" => false,
254
                            "last_modified" => "2016-10-17T07:05:00.070Z"
255
                          ), 200);
256
        
257
        $optimizelyApiClientMock->method('sendApiRequest')
258
                    ->willReturn($result);
259
        
260
        $experimentsService = new Experiments($optimizelyApiClientMock);
261
        
262
        $result = $experimentsService->get(3000);
263
        $experiment = $result->getPayload();
264
        
265
        $this->assertTrue($experiment instanceOf Experiment);
266
        $this->assertTrue($experiment->getName()=='Blue Button Experiment');        
267
    }
268
    
269
    /**
270
     * @expectedException Exception
271
     */
272 View Code Duplication
    public function testGet_NotIntegerExperimentId()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
273
    {
274
        // Mock 'OptimizelyApiClient' object to avoid real API calls
275
        $optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient')
276
                            ->disableOriginalConstructor()
277
                            ->getMock();
278
        
279
        $experimentsService = new Experiments($optimizelyApiClientMock);
280
        
281
        $result = $experimentsService->get('1');
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
282
    }
283
    
284
    /**
285
     * @expectedException Exception
286
     */
287 View Code Duplication
    public function testGet_NegativeExperimentId()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
288
    {
289
        // Mock 'OptimizelyApiClient' object to avoid real API calls
290
        $optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient')
291
                            ->disableOriginalConstructor()
292
                            ->getMock();
293
        
294
        $experimentsService = new Experiments($optimizelyApiClientMock);
295
        
296
        $result = $experimentsService->get(-1);
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
297
    }
298
    
299
    public function testGetResults()
300
    {
301
        // Mock 'OptimizelyApiClient' object to avoid real API calls
302
        $optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient')
303
                            ->disableOriginalConstructor()
304
                            ->getMock();
305
306
        $result = new Result(array(
307
                            "confidence_threshold" => 0.9,
308
                            "end_time" => "2016-10-17T07:05:00.089Z",
309
                            "experiment_id" => 3000,
310
                            "metrics" => array(
311
                              array(
312
                                "event_id" => "string",
313
                                "event_name" => "string",
314
                                "measure" => "conversions",
315
                                "metric_id" => "string",
316
                                "priority" => 1,
317
                                "unit" => "session",
318
                                "results" => array(
319
                                  "9000" => array(
320
                                    "experiment_id" => 0,
321
                                    "is_baseline" => true,
322
                                    "lift" => array(
323
                                      "confidence_interval" => array(
324
                                        0.010399560300730457,
325
                                        0.0850821459929161
326
                                      ),
327
                                      "is_most_conclusive" => true,
328
                                      "is_significant" => true,
329
                                      "significance" => 0,
330
                                      "value" => 0,
331
                                      "visitors_remaining" => 0
332
                                    ),
333
                                    "name" => "Blue Button",
334
                                    "rate" => 0,
335
                                    "scope" => "variation",
336
                                    "total_increase" => array(
337
                                      "confidence_interval" => array(
338
                                        0.010399560300730457,
339
                                        0.0850821459929161
340
                                      ),
341
                                      "is_most_conclusive" => true,
342
                                      "is_significant" => true,
343
                                      "significance" => 0,
344
                                      "value" => 0,
345
                                      "visitors_remaining" => 0
346
                                    ),
347
                                    "value" => 0,
348
                                    "variation_id" => "string"
349
                                  )
350
                                )
351
                              )
352
                            ),
353
                            "reach" => array(
354
                              "baseline_count" => 0,
355
                              "baseline_reach" => 0,
356
                              "total_count" => 0,
357
                              "treatment_count" => 0,
358
                              "treatment_reach" => 0,
359
                              "variations" => array(
360
                                "9000" => array(
361
                                  "count" => 0,
362
                                  "name" => "Blue Button",
363
                                  "variation_id" => "string",
364
                                  "variation_reach" => 0
365
                                )
366
                              )
367
                            ),
368
                            "start_time" => "2016-10-17T07:05:00.090Z"
369
                        ), 200);
370
        
371
        $optimizelyApiClientMock->method('sendApiRequest')
372
                    ->willReturn($result);
373
        
374
        $experimentsService = new Experiments($optimizelyApiClientMock);
375
        
376
        $result = $experimentsService->getResults(3000);
377
        $experimentResults = $result->getPayload();
378
        
379
        $this->assertTrue($experimentResults instanceOf ExperimentResults);
380
        $this->assertTrue($experimentResults->getConfidenceThreshold()==0.9);        
381
    }
382
    
383
    public function testCreate()
384
    {
385
        // Mock 'OptimizelyApiClient' object to avoid real API calls
386
        $optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient')
387
                            ->disableOriginalConstructor()
388
                            ->getMock();
389
390
        $result = new Result(array(
391
                            "project_id" => 1000,
392
                            "audience_ids" => array(
393
                              1234,
394
                              1212,
395
                              1432
396
                            ),
397
                            "campaign_id" => 2000,
398
                            "changes" => array(
399
                              array(
400
                                "type" => "custom_code",
401
                                "allow_additional_redirect" => true,
402
                                "async" => true,
403
                                "selector" => "a[href*=\"optimizely\"]",
404
                                "dependencies" => array(
405
                                  24,
406
                                  26
407
                                ),
408
                                "destination" => "https://app.optimizely.com/",
409
                                "extension_id" => 1234,
410
                                "preserve_parameters" => true,
411
                                "src" => 524,
412
                                "value" => "window.someGlobalFunction();",
413
                                "id" => "string"
414
                              )
415
                            ),
416
                            "description" => "string",
417
                            "holdback" => 5000,
418
                            "key" => "home_page_experiment",
419
                            "metrics" => array(
420
                              array(
421
                                "aggregator" => "unique",
422
                                "event_id" => 0,
423
                                "field" => "revenue",
424
                                "scope" => "session"
425
                              )
426
                            ),
427
                            "name" => "Blue Button Experiment",
428
                            "schedule" => array(
429
                              "start_time" => "2016-10-17T07:05:00.099Z",
430
                              "stop_time" => "2016-10-17T07:05:00.099Z",
431
                              "time_zone" => "UTC"
432
                            ),
433
                            "status" => "active",
434
                            "variations" => array(
435
                              array(
436
                                "actions" => array(
437
                                  array(
438
                                    "changes" => array(
439
                                      array(
440
                                        "type" => "custom_code",
441
                                        "allow_additional_redirect" => true,
442
                                        "async" => true,
443
                                        "selector" => "a[href*=\"optimizely\"]",
444
                                        "dependencies" => array(
445
                                          24,
446
                                          26
447
                                        ),
448
                                        "destination" => "https://app.optimizely.com/",
449
                                        "extension_id" => 1234,
450
                                        "preserve_parameters" => true,
451
                                        "src" => 524,
452
                                        "value" => "window.someGlobalFunction();",
453
                                        "id" => "string"
454
                                      )
455
                                    ),
456
                                    "page_id" => 0
457
                                  )
458
                                ),
459
                                "archived" => true,
460
                                "key" => "blue_button_variation",
461
                                "name" => "Blue Button",
462
                                "variation_id" => 0,
463
                                "weight" => 0
464
                              )
465
                            ),
466
                            "created" => "2016-10-17T07:05:00.099Z",
467
                            "id" => 3000,
468
                            "is_classic" => false,
469
                            "last_modified" => "2016-10-17T07:05:00.099Z"
470
                        ), 201);
471
        
472
        $optimizelyApiClientMock->method('sendApiRequest')
473
                    ->willReturn($result);
474
        
475
        $experimentsService = new Experiments($optimizelyApiClientMock);
476
        
477
        $experiment = new Experiment(array(
478
            "project_id" => 1000,
479
            "audience_ids" => array(
480
              1234,
481
              1212,
482
              1432
483
            ),
484
            "campaign_id" => 2000,
485
            "changes" => array(
486
              array(
487
                "type" => "custom_code",
488
                "allow_additional_redirect" => true,
489
                "async" => true,
490
                "selector" => "a[href*=\"optimizely\"]",
491
                "dependencies" => array(
492
                  24,
493
                  26
494
                ),
495
                "destination" => "https://app.optimizely.com/",
496
                "extension_id" => 1234,
497
                "preserve_parameters" => true,
498
                "src" => 524,
499
                "value" => "window.someGlobalFunction();"
500
              )
501
            ),
502
            "description" => "string",
503
            "holdback" => 5000,
504
            "key" => "home_page_experiment",
505
            "metrics" => array(
506
              array(
507
                "aggregator" => "unique",
508
                "event_id" => 0,
509
                "field" => "revenue",
510
                "scope" => "session"
511
              )
512
            ),
513
            "name" => "Blue Button Experiment",
514
            "schedule" => array(
515
              "start_time" => "2016-10-17T07:04:59.724Z",
516
              "stop_time" => "2016-10-17T07:04:59.724Z",
517
              "time_zone" => "UTC"
518
            ),
519
            "status" => "active",
520
            "variations" => array(
521
              array(
522
                "actions" => array(
523
                  array(
524
                    "changes" => array(
525
                      array(
526
                        "type" => "custom_code",
527
                        "allow_additional_redirect" => true,
528
                        "async" => true,
529
                        "selector" => "a[href*=\"optimizely\"]",
530
                        "dependencies" => array(
531
                          24,
532
                          26
533
                        ),
534
                        "destination" => "https://app.optimizely.com/",
535
                        "extension_id" => 1234,
536
                        "preserve_parameters" => true,
537
                        "src" => 524,
538
                        "value" => "window.someGlobalFunction();"
539
                      )
540
                    ),
541
                    "page_id" => 0
542
                  )
543
                ),
544
                "archived" => true,
545
                "key" => "blue_button_variation",
546
                "name" => "Blue Button",
547
                "variation_id" => 0,
548
                "weight" => 0
549
              )
550
            )
551
        ));
552
        
553
        $result = $experimentsService->create($experiment);
554
        $createdExperiment = $result->getPayload();
555
        
556
        $this->assertTrue($createdExperiment instanceOf Experiment);
557
        $this->assertTrue($createdExperiment->getName()=='Blue Button Experiment');                
558
    }
559
    
560
    public function testUpdate()
561
    {
562
        // Mock 'OptimizelyApiClient' object to avoid real API calls
563
        $optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient')
564
                            ->disableOriginalConstructor()
565
                            ->getMock();
566
567
        $result = new Result(array(
568
                        "project_id" => 1000,
569
                        "audience_ids" => array(
570
                          1234,
571
                          1212,
572
                          1432
573
                        ),
574
                        "campaign_id" => 2000,
575
                        "changes" => array(
576
                          array(
577
                            "type" => "custom_code",
578
                            "allow_additional_redirect" => true,
579
                            "async" => true,
580
                            "selector" => "a[href*=\"optimizely\"]",
581
                            "dependencies" => array(
582
                              24,
583
                              26
584
                            ),
585
                            "destination" => "https://app.optimizely.com/",
586
                            "extension_id" => 1234,
587
                            "preserve_parameters" => true,
588
                            "src" => 524,
589
                            "value" => "window.someGlobalFunction();",
590
                            "id" => "string"
591
                          )
592
                        ),
593
                        "description" => "string",
594
                        "holdback" => 5000,
595
                        "key" => "home_page_experiment",
596
                        "metrics" => array(
597
                          array(
598
                            "aggregator" => "unique",
599
                            "event_id" => 0,
600
                            "field" => "revenue",
601
                            "scope" => "session"
602
                          )
603
                        ),
604
                        "name" => "Blue Button Experiment",
605
                        "schedule" => array(
606
                          "start_time" => "2016-10-17T07:05:00.109Z",
607
                          "stop_time" => "2016-10-17T07:05:00.109Z",
608
                          "time_zone" => "UTC"
609
                        ),
610
                        "status" => "active",
611
                        "variations" => array(
612
                          array(
613
                            "actions" => array(
614
                              array(
615
                                "changes" => array(
616
                                  array(
617
                                    "type" => "custom_code",
618
                                    "allow_additional_redirect" => true,
619
                                    "async" => true,
620
                                    "selector" => "a[href*=\"optimizely\"]",
621
                                    "dependencies" => array(
622
                                      24,
623
                                      26
624
                                    ),
625
                                    "destination" => "https://app.optimizely.com/",
626
                                    "extension_id" => 1234,
627
                                    "preserve_parameters" => true,
628
                                    "src" => 524,
629
                                    "value" => "window.someGlobalFunction();",
630
                                    "id" => "string"
631
                                  )
632
                                ),
633
                                "page_id" => 0
634
                              )
635
                            ),
636
                            "archived" => true,
637
                            "key" => "blue_button_variation",
638
                            "name" => "Blue Button",
639
                            "variation_id" => 0,
640
                            "weight" => 0
641
                          )
642
                        ),
643
                        "created" => "2016-10-17T07:05:00.109Z",
644
                        "id" => 3000,
645
                        "is_classic" => false,
646
                        "last_modified" => "2016-10-17T07:05:00.109Z"
647
                        ), 200);
648
        
649
        $optimizelyApiClientMock->method('sendApiRequest')
650
                    ->willReturn($result);
651
        
652
        $experimentsService = new Experiments($optimizelyApiClientMock);
653
        
654
        $experiment = new Experiment(array(
655
              "audience_ids" => array(
656
                0
657
              ),
658
              "changes" => array(
659
                array(
660
                  "type" => "custom_code",
661
                  "allow_additional_redirect" => true,
662
                  "async" => true,
663
                  "selector" => "a[href*=\"optimizely\"]",
664
                  "dependencies" => array(
665
                    24,
666
                    26
667
                  ),
668
                  "destination" => "https://app.optimizely.com/",
669
                  "extension_id" => 1234,
670
                  "preserve_parameters" => true,
671
                  "src" => 524,
672
                  "value" => "window.someGlobalFunction();"
673
                )
674
              ),
675
              "description" => "AB Test to see if the Blue Button converts more visitors",
676
              "holdback" => 0,
677
              "key" => "home_page_experiment",
678
              "metrics" => array(
679
                array(
680
                  "aggregator" => "unique",
681
                  "event_id" => 0,
682
                  "field" => "revenue",
683
                  "scope" => "session"
684
                )
685
              ),
686
              "name" => "Blue Button Experiment",
687
              "schedule" => array(
688
                "start_time" => "2016-10-17T07:04:59.731Z",
689
                "stop_time" => "2016-10-17T07:04:59.731Z",
690
                "time_zone" => "UTC"
691
              ),
692
              "status" => "active",
693
              "variations" => array(
694
                array(
695
                  "actions" => array(
696
                    array(
697
                      "changes" => array(
698
                        array(
699
                          "type" => "custom_code",
700
                          "allow_additional_redirect" => true,
701
                          "async" => true,
702
                          "selector" => "a[href*=\"optimizely\"]",
703
                          "dependencies" => array(
704
                            24,
705
                            26
706
                          ),
707
                          "destination" => "https://app.optimizely.com/",
708
                          "extension_id" => 1234,
709
                          "preserve_parameters" => true,
710
                          "src" => 524,
711
                          "value" => "window.someGlobalFunction();"
712
                        )
713
                      ),
714
                      "page_id" => 0
715
                    )
716
                  ),
717
                  "archived" => true,
718
                  "key" => "blue_button_variation",
719
                  "name" => "Blue Button",
720
                  "variation_id" => 0,
721
                  "weight" => 0
722
                )
723
              )
724
        ));
725
        
726
        $result = $experimentsService->update(1000, $experiment, 'start');
727
        $updatedExperiment = $result->getPayload();
728
        
729
        $this->assertTrue($updatedExperiment instanceOf Experiment);
730
        $this->assertTrue($updatedExperiment->getName()=='Blue Button Experiment');                
731
    }
732
    
733 View Code Duplication
    public function testDelete()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
734
    {
735
        // Mock 'OptimizelyApiClient' object to avoid real API calls
736
        $optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient')
737
                            ->disableOriginalConstructor()
738
                            ->getMock();
739
740
        $result = new Result(array(), 200);
741
        
742
        $optimizelyApiClientMock->method('sendApiRequest')
743
                    ->willReturn($result);
744
        
745
        $experimentsService = new Experiments($optimizelyApiClientMock);
746
     
747
        $result = $experimentsService->delete(1000);
748
        
749
        $this->assertEquals(200, $result->getHttpCode());        
750
    }
751
    
752
    public function testIntegration()
753
    {
754
        if (!getenv('OPTIMIZELY_PHP_TEST_INTEGRATION')) 
755
            $this->markTestSkipped('OPTIMIZELY_PHP_TEST_INTEGRATION env var is not set');
756
        
757
        $credentials = $this->loadCredentialsFromFile();
758
        
759
        $optimizelyClient = new OptimizelyApiClient($credentials, 'v2');
0 ignored issues
show
Bug introduced by
It seems like $credentials defined by $this->loadCredentialsFromFile() on line 757 can also be of type null; however, WebMarketingROI\Optimize...piClient::__construct() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
760
        $this->assertTrue($optimizelyClient!=null);
761
        
762
        // Create new project        
763
        $curDate = date('Y-m-d H:i:s');
764
        $newProject = new Project(array(
765
            "name" => "Test Project $curDate",
766
            "account_id" => 12345,
767
            "confidence_threshold" => 0.9,
768
            "platform" => "web",
769
            "status" => "active",
770
            "web_snippet" => array(
771
              "enable_force_variation" => false,
772
              "exclude_disabled_experiments" => false,
773
              "exclude_names" => true,
774
              "include_jquery" => true,
775
              "ip_anonymization" => false,
776
              "ip_filter" => "^206\\.23\\.100\\.([5-9][0-9]|1([0-4][0-9]|50))$",
777
              "library" => "jquery-1.11.3-trim",
778
              "project_javascript" => "alert(\"Active Experiment\")"
779
            )
780
        ));
781
        
782
        $result = $optimizelyClient->projects()->create($newProject);
0 ignored issues
show
Documentation Bug introduced by
The method projects does not exist on object<WebMarketingROI\O...HP\OptimizelyApiClient>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
783
        $createdProject = $result->getPayload();
784
        
785
        // Create new page in the project
786
        $page = new Page(array(
787
            "edit_url" => "https://www.optimizely.com",
788
            "name" => "Home Page",
789
            "project_id" => $createdProject->getId(),
790
            "activation_code" => "function callbackFn(activate, options) { activate(); }",
791
            "activation_type" => "immediate",
792
            "archived" => false,
793
            "category" => "article",
794
            "conditions" => "[\"and\", {\"type\": \"url\", \"match_type\": \"substring\", \"value\": \"optimize\"}]",
795
            "key" => "home_page",
796
            "page_type" => "single_url",
797
            "created" => "2016-10-18T05:07:04.113Z",
798
            "id" => 4000,
799
            "last_modified" => "2016-10-18T05:07:04.113Z"
800
        ));
801
        
802
        $result = $optimizelyClient->pages()->create($page);
0 ignored issues
show
Documentation Bug introduced by
The method pages does not exist on object<WebMarketingROI\O...HP\OptimizelyApiClient>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
803
        $createdPage = $result->getPayload();
804
        
805
        // Create new event in the project
806
        $event = new ClickEvent(array(
807
              "event_filter" => array(
808
                "filter_type" => "target_selector",
809
                "selector" => ".menu-options"
810
              ),
811
              "name" => "Add to Cart",
812
              "archived" => true,
813
              "category" => "add_to_cart",
814
              "description" => "Some simple event",
815
              "event_type" => "click",
816
              "key" => "add_to_cart",
817
              "created" => "2016-10-18T05:07:04.153Z",
818
              "id" => 0,
819
              "is_classic" => false,
820
              //"is_editable" => true,
821
              "page_id" => $createdPage->getId(),
822
              "project_id" => $createdProject->getId()
823
        ));
824
        
825
        $result = $optimizelyClient->events()->createClickEvent($createdPage->getId(), $event);
0 ignored issues
show
Documentation Bug introduced by
The method events does not exist on object<WebMarketingROI\O...HP\OptimizelyApiClient>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
826
        $createdEvent = $result->getPayload();
827
        
828
        // Create new audience in the project
829
        $audience = new Audience(array(
830
            "project_id" => $createdProject->getId(),
831
            "archived" => false,
832
            "conditions" => "[\"and\", {\"type\": \"language\", \"value\": \"es\"}, {\"type\": \"location\", \"value\": \"US\"}]",
833
            "description" => "People that speak spanish and are in San Francisco",
834
            "name" => "Spanish speaking San Franciscans",
835
            "segmentation" => true
836
        ));
837
        
838
        $result = $optimizelyClient->audiences()->create($audience);
0 ignored issues
show
Documentation Bug introduced by
The method audiences does not exist on object<WebMarketingROI\O...HP\OptimizelyApiClient>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
839
        $createdAudience = $result->getPayload();
0 ignored issues
show
Unused Code introduced by
$createdAudience is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
840
        
841
        // Create new experiment in the project
842
        $experiment = new Experiment(array(
843
            "project_id" => $createdProject->getId(),
844
            "variations" => array(
845
            array(
846
              "weight" => 10000,
847
              "actions" => array(
848
                array(
849
                  "changes" => array(
850
                    array(
851
                      "type" => "attribute",
852
                      //"allow_additional_redirect" => true,
853
                      "async" => true,
854
                      "attributes" => array(
855
                        "class" => "intro",
856
                        "hide" => true,
857
                        "href" => "example.com",
858
                        "html" => "New Title",
859
                        "remove" => true,
860
                        "src" => "song.mp3",
861
                        "style" => "background-color:blue;",
862
                        "text" => "Some nice message"
863
                      ),
864
                      //"config" => ["name" => "Flash Sale Today!", "color" => "blue"],
865
                      "css" => array(
866
                        "background-color" => "string",
867
                        "background-image" => "string",
868
                        "border-color" => "string",
869
                        "border-style" => "string",
870
                        "border-width" => "string",
871
                        "color" => "string",
872
                        "font-size" => "string",
873
                        "font-weight" => "string",
874
                        "height" => "string",
875
                        "position" => "string",
876
                        "width" => "string"
877
                      ),
878
                      /*"dependencies" => [
879
                        '24',
880
                        '26'
881
                      ],*/
882
                      //"destination" => "https://app.optimizely.com/",
883
                      "name" => "Setting button text",
884
                      //"operator" => "after",
885
                      //"preserve_parameters" => true,
886
                      "rearrange" => "{\"insertSelector\": \".greyBox\", \"operator\": \"before\"}",
887
                      "selector" => "a[href*=\"optimizely\"]",
888
                      //"value" => "window.someGlobalFunction();"
889
                    )
890
                  ),
891
                  "page_id" => $createdPage->getId(),
892
                )
893
              ),
894
              "archived" => false,
895
              "key" => "blue_button_variation",
896
              "name" => "Blue Button"
897
            )
898
          ),
899
            "description" => "string",
900
            "holdback" => 5000,
901
            "key" => "home_page_experiment",
902
            "metrics" => array(
903
              array(
904
                "aggregator" => "sum",
905
                "event_id" => $createdEvent->getId(),
906
                "field" => "revenue",
907
                "scope" => "visitor"
908
              )
909
            ),
910
            "name" => "Blue Button Experiment",
911
            /*"schedule" => array(
912
              "start_time" => "string",
913
              "stop_time" => "string",
914
              "time_zone" => "GMT-01:00"
915
            ),*/
916
            "type" => "a/b"    
917
        ));
918
                
919
        
920
        $result = $optimizelyClient->experiments()->create($experiment);
0 ignored issues
show
Documentation Bug introduced by
The method experiments does not exist on object<WebMarketingROI\O...HP\OptimizelyApiClient>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
921
        $createdExperiment = $result->getPayload();
922
        
923
        $this->assertTrue($createdExperiment instanceOf Experiment);
924
        $this->assertTrue($createdExperiment->getName()=='Blue Button Experiment');  
925
        
926
        // List all existing experiments and try to find the created experiment
927
        $experimentFound = false;        
928
        try {
929
            $page = 1;
930
            for (;;) {                            
931
                $result = $optimizelyClient->experiments()->listAll($createdProject->getId(), null, false, $page);
0 ignored issues
show
Documentation Bug introduced by
The method experiments does not exist on object<WebMarketingROI\O...HP\OptimizelyApiClient>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
932
933
                $experiments = $result->getPayload();
934
935
                foreach ($experiments as $experiment) {
936
                    if ($experiment->getName()=="Blue Button Experiment") {
937
                        $experimentFound = true;
938
                        break;
939
                    }
940
                }
941
942
                if ($result->getNextPage()==null)
943
                    break;
944
945
                $page ++;
946
            }
947
        }
948
        catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The class OptimizelyPHPTest\Service\v2\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
949
            // Handle error.
950
            $code = $e->getCode();
951
            $httpCode = $e->getHttpCode();
952
            $message = $e->getMessage();
953
            $uuid = $e->getUuid();
954
            echo "Exception caught: $message (code=$code http_code=$httpCode uuid=$uuid)\n";
955
        }
956
        
957
        $this->assertTrue($experimentFound);
958
        
959
        // Update experiment
960
        $createdExperiment->setName('Some new experiment name');
961
        $result = $optimizelyClient->experiments()->update($createdExperiment->getId(), $createdExperiment, 'start');
0 ignored issues
show
Documentation Bug introduced by
The method experiments does not exist on object<WebMarketingROI\O...HP\OptimizelyApiClient>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
962
        $updatedExperiment = $result->getPayload();                
963
        
964
        $this->assertTrue($updatedExperiment instanceOf Experiment);
965
        $this->assertTrue($updatedExperiment->getName()=='Some new experiment name');  
966
        
967
        // Delete experiment        
968
        $result = $optimizelyClient->experiments()->delete($createdExperiment->getId());
0 ignored issues
show
Documentation Bug introduced by
The method experiments does not exist on object<WebMarketingROI\O...HP\OptimizelyApiClient>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
969
    }
970
}
971