Completed
Push — master ( fe2892...6427ed )
by Oleg
03:03
created

ExperimentsTest::testIntegration()   C

Complexity

Conditions 7
Paths 34

Size

Total Lines 206
Code Lines 84

Duplication

Lines 17
Ratio 8.25 %

Importance

Changes 0
Metric Value
cc 7
eloc 84
nc 34
nop 0
dl 17
loc 206
rs 6.4589
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
12
class ExperimentsTest extends BaseServiceTest
13
{
14
    public function testListAll()
15
    {
16
        // Mock 'OptimizelyApiClient' object to avoid real API calls
17
        $optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient')
18
                            ->disableOriginalConstructor()
19
                            ->getMock();
20
21
        $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...
22
        
23
        $result = new Result(array(
24
                        array(
25
                            "project_id" => 1000,
26
                            "audience_ids" => array(
27
                              1234,
28
                              1212,
29
                              1432
30
                            ),
31
                            "campaign_id" => 2000,
32
                            "changes" => array(
33
                              array(
34
                                "type" => "custom_code",
35
                                "allow_additional_redirect" => true,
36
                                "async" => true,
37
                                "selector" => "a[href*=\"optimizely\"]",
38
                                "dependencies" => array(
39
                                  24,
40
                                  26
41
                                ),
42
                                "destination" => "https://app.optimizely.com/",
43
                                "extension_id" => 1234,
44
                                "preserve_parameters" => true,
45
                                "src" => 524,
46
                                "value" => "window.someGlobalFunction();",
47
                                "id" => "string"
48
                              )
49
                            ),
50
                            "description" => "string",
51
                            "holdback" => 5000,
52
                            "key" => "home_page_experiment",
53
                            "metrics" => array(
54
                              array(
55
                                "aggregator" => "unique",
56
                                "event_id" => 0,
57
                                "field" => "revenue",
58
                                "scope" => "session"
59
                              )
60
                            ),
61
                            "name" => "Blue Button Experiment",
62
                            "schedule" => array(
63
                              "start_time" => "2016-10-17T07:05:00.070Z",
64
                              "stop_time" => "2016-10-17T07:05:00.070Z",
65
                              "time_zone" => "UTC"
66
                            ),
67
                            "status" => "active",
68
                            "variations" => array(
69
                              array(
70
                                "actions" => array(
71
                                  array(
72
                                    "changes" => array(
73
                                      array(
74
                                        "type" => "custom_code",
75
                                        "allow_additional_redirect" => true, 
76
                                        "async" => true,
77
                                        "selector" => "a[href*=\"optimizely\"]",
78
                                        "dependencies" => array(
79
                                          24,
80
                                          26
81
                                        ),
82
                                        "destination" => "https://app.optimizely.com/",
83
                                        "extension_id" => 1234,
84
                                        "preserve_parameters" => true,
85
                                        "src" => 524,
86
                                        "value" => "window.someGlobalFunction();",
87
                                        "id" => "string"
88
                                      )
89
                                    ),
90
                                    "page_id" => 0
91
                                  )
92
                                ),
93
                                "archived"=> true,
94
                                "key" => "blue_button_variation",
95
                                "name" => "Blue Button",
96
                                "variation_id" => 0,
97
                                "weight" => 0
98
                              )
99
                            ),
100
                            "created" => "2016-10-17T07:05:00.070Z",
101
                            "id" => 3000,
102
                            "is_classic" => false,
103
                            "last_modified" => "2016-10-17T07:05:00.070Z"
104
                          )
105
                        ), 200);
106
        
107
        $optimizelyApiClientMock->method('sendApiRequest')
108
                    ->willReturn($result);
109
        
110
        $experimentsService = new Experiments($optimizelyApiClientMock);
111
        
112
        $result = $experimentsService->listAll(1000);
113
        $experiments = $result->getPayload();
114
        
115
        $this->assertTrue(count($experiments)==1);
116
        $this->assertTrue($experiments[0] instanceOf Experiment);
117
        $this->assertTrue($experiments[0]->getName()=='Blue Button Experiment');        
118
    }
119
    
120
    /**
121
     * @expectedException Exception
122
     */
123 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...
124
    {
125
        // Mock 'OptimizelyApiClient' object to avoid real API calls
126
        $optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient')
127
                            ->disableOriginalConstructor()
128
                            ->getMock();
129
        
130
        $experimentsService = new Experiments($optimizelyApiClientMock);
131
        
132
        $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...
133
    }
134
    
135
    /**
136
     * @expectedException Exception
137
     */
138 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...
139
    {
140
        // Mock 'OptimizelyApiClient' object to avoid real API calls
141
        $optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient')
142
                            ->disableOriginalConstructor()
143
                            ->getMock();
144
        
145
        $experimentsService = new Experiments($optimizelyApiClientMock);
146
        
147
        $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...
148
    }
149
    
150
    /**
151
     * @expectedException Exception
152
     */
153 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...
154
    {
155
        // Mock 'OptimizelyApiClient' object to avoid real API calls
156
        $optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient')
157
                            ->disableOriginalConstructor()
158
                            ->getMock();
159
        
160
        $experimentsService = new Experiments($optimizelyApiClientMock);
161
        
162
        $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...
163
    }
164
    
165
    public function testGet()
166
    {
167
        // Mock 'OptimizelyApiClient' object to avoid real API calls
168
        $optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient')
169
                            ->disableOriginalConstructor()
170
                            ->getMock();
171
172
        $result = new Result(array(
173
                            "project_id" => 1000,
174
                            "audience_ids" => array(
175
                              1234,
176
                              1212,
177
                              1432
178
                            ),
179
                            "campaign_id" => 2000,
180
                            "changes" => array(
181
                              array(
182
                                "type" => "custom_code",
183
                                "allow_additional_redirect" => true,
184
                                "async" => true,
185
                                "selector" => "a[href*=\"optimizely\"]",
186
                                "dependencies" => array(
187
                                  24,
188
                                  26
189
                                ),
190
                                "destination" => "https://app.optimizely.com/",
191
                                "extension_id" => 1234,
192
                                "preserve_parameters" => true,
193
                                "src" => 524,
194
                                "value" => "window.someGlobalFunction();",
195
                                "id" => "string"
196
                              )
197
                            ),
198
                            "description" => "string",
199
                            "holdback" => 5000,
200
                            "key" => "home_page_experiment",
201
                            "metrics" => array(
202
                              array(
203
                                "aggregator" => "unique",
204
                                "event_id" => 0,
205
                                "field" => "revenue",
206
                                "scope" => "session"
207
                              )
208
                            ),
209
                            "name" => "Blue Button Experiment",
210
                            "schedule" => array(
211
                              "start_time" => "2016-10-17T07:05:00.070Z",
212
                              "stop_time" => "2016-10-17T07:05:00.070Z",
213
                              "time_zone" => "UTC"
214
                            ),
215
                            "status" => "active",
216
                            "variations" => array(
217
                              array(
218
                                "actions" => array(
219
                                  array(
220
                                    "changes" => array(
221
                                      array(
222
                                        "type" => "custom_code",
223
                                        "allow_additional_redirect" => true, 
224
                                        "async" => true,
225
                                        "selector" => "a[href*=\"optimizely\"]",
226
                                        "dependencies" => array(
227
                                          24,
228
                                          26
229
                                        ),
230
                                        "destination" => "https://app.optimizely.com/",
231
                                        "extension_id" => 1234,
232
                                        "preserve_parameters" => true,
233
                                        "src" => 524,
234
                                        "value" => "window.someGlobalFunction();",
235
                                        "id" => "string"
236
                                      )
237
                                    ),
238
                                    "page_id" => 0
239
                                  )
240
                                ),
241
                                "archived"=> true,
242
                                "key" => "blue_button_variation",
243
                                "name" => "Blue Button",
244
                                "variation_id" => 0,
245
                                "weight" => 0
246
                              )
247
                            ),
248
                            "created" => "2016-10-17T07:05:00.070Z",
249
                            "id" => 3000,
250
                            "is_classic" => false,
251
                            "last_modified" => "2016-10-17T07:05:00.070Z"
252
                          ), 200);
253
        
254
        $optimizelyApiClientMock->method('sendApiRequest')
255
                    ->willReturn($result);
256
        
257
        $experimentsService = new Experiments($optimizelyApiClientMock);
258
        
259
        $result = $experimentsService->get(3000);
260
        $experiment = $result->getPayload();
261
        
262
        $this->assertTrue($experiment instanceOf Experiment);
263
        $this->assertTrue($experiment->getName()=='Blue Button Experiment');        
264
    }
265
    
266
    /**
267
     * @expectedException Exception
268
     */
269 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...
270
    {
271
        // Mock 'OptimizelyApiClient' object to avoid real API calls
272
        $optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient')
273
                            ->disableOriginalConstructor()
274
                            ->getMock();
275
        
276
        $experimentsService = new Experiments($optimizelyApiClientMock);
277
        
278
        $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...
279
    }
280
    
281
    /**
282
     * @expectedException Exception
283
     */
284 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...
285
    {
286
        // Mock 'OptimizelyApiClient' object to avoid real API calls
287
        $optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient')
288
                            ->disableOriginalConstructor()
289
                            ->getMock();
290
        
291
        $experimentsService = new Experiments($optimizelyApiClientMock);
292
        
293
        $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...
294
    }
295
    
296
    public function testGetResults()
297
    {
298
        // Mock 'OptimizelyApiClient' object to avoid real API calls
299
        $optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient')
300
                            ->disableOriginalConstructor()
301
                            ->getMock();
302
303
        $result = new Result(array(
304
                            "confidence_threshold" => 0.9,
305
                            "end_time" => "2016-10-17T07:05:00.089Z",
306
                            "experiment_id" => 3000,
307
                            "metrics" => array(
308
                              array(
309
                                "event" => "string",
310
                                "event_name" => "string",
311
                                "measure" => "conversions",
312
                                "metric_id" => "string",
313
                                "priority" => 1,
314
                                "unit" => "session",
315
                                "variation_results" => array(
316
                                  "9000" => array(
317
                                    "experiment_id" => 0,
318
                                    "is_baseline" => true,
319
                                    "lift" => array(
320
                                      "confidence_interval" => array(
321
                                        0.010399560300730457,
322
                                        0.0850821459929161
323
                                      ),
324
                                      "is_most_conclusive" => true,
325
                                      "is_significant" => true,
326
                                      "significance" => 0,
327
                                      "value" => 0,
328
                                      "visitors_remaining" => 0
329
                                    ),
330
                                    "name" => "Blue Button",
331
                                    "rate" => 0,
332
                                    "scope" => "variation",
333
                                    "total_increase" => array(
334
                                      "confidence_interval" => array(
335
                                        0.010399560300730457,
336
                                        0.0850821459929161
337
                                      ),
338
                                      "is_most_conclusive" => true,
339
                                      "is_significant" => true,
340
                                      "significance" => 0,
341
                                      "value" => 0,
342
                                      "visitors_remaining" => 0
343
                                    ),
344
                                    "value" => 0,
345
                                    "variation_id" => "string"
346
                                  )
347
                                )
348
                              )
349
                            ),
350
                            "reach" => array(
351
                              "baseline_count" => 0,
352
                              "baseline_reach" => 0,
353
                              "total_count" => 0,
354
                              "treatment_count" => 0,
355
                              "treatment_reach" => 0,
356
                              "variations" => array(
357
                                "9000" => array(
358
                                  "count" => 0,
359
                                  "name" => "Blue Button",
360
                                  "variation_id" => "string",
361
                                  "variation_reach" => 0
362
                                )
363
                              )
364
                            ),
365
                            "start_time" => "2016-10-17T07:05:00.090Z"
366
                        ), 200);
367
        
368
        $optimizelyApiClientMock->method('sendApiRequest')
369
                    ->willReturn($result);
370
        
371
        $experimentsService = new Experiments($optimizelyApiClientMock);
372
        
373
        $result = $experimentsService->getResults(3000);
374
        $experimentResults = $result->getPayload();
375
        
376
        $this->assertTrue($experimentResults instanceOf ExperimentResults);
377
        $this->assertTrue($experimentResults->getConfidenceThreshold()==0.9);        
378
    }
379
    
380
    public function testCreate()
381
    {
382
        // Mock 'OptimizelyApiClient' object to avoid real API calls
383
        $optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient')
384
                            ->disableOriginalConstructor()
385
                            ->getMock();
386
387
        $result = new Result(array(
388
                            "project_id" => 1000,
389
                            "audience_ids" => array(
390
                              1234,
391
                              1212,
392
                              1432
393
                            ),
394
                            "campaign_id" => 2000,
395
                            "changes" => array(
396
                              array(
397
                                "type" => "custom_code",
398
                                "allow_additional_redirect" => true,
399
                                "async" => true,
400
                                "selector" => "a[href*=\"optimizely\"]",
401
                                "dependencies" => array(
402
                                  24,
403
                                  26
404
                                ),
405
                                "destination" => "https://app.optimizely.com/",
406
                                "extension_id" => 1234,
407
                                "preserve_parameters" => true,
408
                                "src" => 524,
409
                                "value" => "window.someGlobalFunction();",
410
                                "id" => "string"
411
                              )
412
                            ),
413
                            "description" => "string",
414
                            "holdback" => 5000,
415
                            "key" => "home_page_experiment",
416
                            "metrics" => array(
417
                              array(
418
                                "aggregator" => "unique",
419
                                "event_id" => 0,
420
                                "field" => "revenue",
421
                                "scope" => "session"
422
                              )
423
                            ),
424
                            "name" => "Blue Button Experiment",
425
                            "schedule" => array(
426
                              "start_time" => "2016-10-17T07:05:00.099Z",
427
                              "stop_time" => "2016-10-17T07:05:00.099Z",
428
                              "time_zone" => "UTC"
429
                            ),
430
                            "status" => "active",
431
                            "variations" => array(
432
                              array(
433
                                "actions" => array(
434
                                  array(
435
                                    "changes" => array(
436
                                      array(
437
                                        "type" => "custom_code",
438
                                        "allow_additional_redirect" => true,
439
                                        "async" => true,
440
                                        "selector" => "a[href*=\"optimizely\"]",
441
                                        "dependencies" => array(
442
                                          24,
443
                                          26
444
                                        ),
445
                                        "destination" => "https://app.optimizely.com/",
446
                                        "extension_id" => 1234,
447
                                        "preserve_parameters" => true,
448
                                        "src" => 524,
449
                                        "value" => "window.someGlobalFunction();",
450
                                        "id" => "string"
451
                                      )
452
                                    ),
453
                                    "page_id" => 0
454
                                  )
455
                                ),
456
                                "archived" => true,
457
                                "key" => "blue_button_variation",
458
                                "name" => "Blue Button",
459
                                "variation_id" => 0,
460
                                "weight" => 0
461
                              )
462
                            ),
463
                            "created" => "2016-10-17T07:05:00.099Z",
464
                            "id" => 3000,
465
                            "is_classic" => false,
466
                            "last_modified" => "2016-10-17T07:05:00.099Z"
467
                        ), 201);
468
        
469
        $optimizelyApiClientMock->method('sendApiRequest')
470
                    ->willReturn($result);
471
        
472
        $experimentsService = new Experiments($optimizelyApiClientMock);
473
        
474
        $experiment = new Experiment(array(
475
            "project_id" => 1000,
476
            "audience_ids" => array(
477
              1234,
478
              1212,
479
              1432
480
            ),
481
            "campaign_id" => 2000,
482
            "changes" => array(
483
              array(
484
                "type" => "custom_code",
485
                "allow_additional_redirect" => true,
486
                "async" => true,
487
                "selector" => "a[href*=\"optimizely\"]",
488
                "dependencies" => array(
489
                  24,
490
                  26
491
                ),
492
                "destination" => "https://app.optimizely.com/",
493
                "extension_id" => 1234,
494
                "preserve_parameters" => true,
495
                "src" => 524,
496
                "value" => "window.someGlobalFunction();"
497
              )
498
            ),
499
            "description" => "string",
500
            "holdback" => 5000,
501
            "key" => "home_page_experiment",
502
            "metrics" => array(
503
              array(
504
                "aggregator" => "unique",
505
                "event_id" => 0,
506
                "field" => "revenue",
507
                "scope" => "session"
508
              )
509
            ),
510
            "name" => "Blue Button Experiment",
511
            "schedule" => array(
512
              "start_time" => "2016-10-17T07:04:59.724Z",
513
              "stop_time" => "2016-10-17T07:04:59.724Z",
514
              "time_zone" => "UTC"
515
            ),
516
            "status" => "active",
517
            "variations" => array(
518
              array(
519
                "actions" => array(
520
                  array(
521
                    "changes" => array(
522
                      array(
523
                        "type" => "custom_code",
524
                        "allow_additional_redirect" => true,
525
                        "async" => true,
526
                        "selector" => "a[href*=\"optimizely\"]",
527
                        "dependencies" => array(
528
                          24,
529
                          26
530
                        ),
531
                        "destination" => "https://app.optimizely.com/",
532
                        "extension_id" => 1234,
533
                        "preserve_parameters" => true,
534
                        "src" => 524,
535
                        "value" => "window.someGlobalFunction();"
536
                      )
537
                    ),
538
                    "page_id" => 0
539
                  )
540
                ),
541
                "archived" => true,
542
                "key" => "blue_button_variation",
543
                "name" => "Blue Button",
544
                "variation_id" => 0,
545
                "weight" => 0
546
              )
547
            )
548
        ));
549
        
550
        $result = $experimentsService->create($experiment);
551
        $createdExperiment = $result->getPayload();
552
        
553
        $this->assertTrue($createdExperiment instanceOf Experiment);
554
        $this->assertTrue($createdExperiment->getName()=='Blue Button Experiment');                
555
    }
556
    
557
    public function testUpdate()
558
    {
559
        // Mock 'OptimizelyApiClient' object to avoid real API calls
560
        $optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient')
561
                            ->disableOriginalConstructor()
562
                            ->getMock();
563
564
        $result = new Result(array(
565
                        "project_id" => 1000,
566
                        "audience_ids" => array(
567
                          1234,
568
                          1212,
569
                          1432
570
                        ),
571
                        "campaign_id" => 2000,
572
                        "changes" => array(
573
                          array(
574
                            "type" => "custom_code",
575
                            "allow_additional_redirect" => true,
576
                            "async" => true,
577
                            "selector" => "a[href*=\"optimizely\"]",
578
                            "dependencies" => array(
579
                              24,
580
                              26
581
                            ),
582
                            "destination" => "https://app.optimizely.com/",
583
                            "extension_id" => 1234,
584
                            "preserve_parameters" => true,
585
                            "src" => 524,
586
                            "value" => "window.someGlobalFunction();",
587
                            "id" => "string"
588
                          )
589
                        ),
590
                        "description" => "string",
591
                        "holdback" => 5000,
592
                        "key" => "home_page_experiment",
593
                        "metrics" => array(
594
                          array(
595
                            "aggregator" => "unique",
596
                            "event_id" => 0,
597
                            "field" => "revenue",
598
                            "scope" => "session"
599
                          )
600
                        ),
601
                        "name" => "Blue Button Experiment",
602
                        "schedule" => array(
603
                          "start_time" => "2016-10-17T07:05:00.109Z",
604
                          "stop_time" => "2016-10-17T07:05:00.109Z",
605
                          "time_zone" => "UTC"
606
                        ),
607
                        "status" => "active",
608
                        "variations" => array(
609
                          array(
610
                            "actions" => array(
611
                              array(
612
                                "changes" => array(
613
                                  array(
614
                                    "type" => "custom_code",
615
                                    "allow_additional_redirect" => true,
616
                                    "async" => true,
617
                                    "selector" => "a[href*=\"optimizely\"]",
618
                                    "dependencies" => array(
619
                                      24,
620
                                      26
621
                                    ),
622
                                    "destination" => "https://app.optimizely.com/",
623
                                    "extension_id" => 1234,
624
                                    "preserve_parameters" => true,
625
                                    "src" => 524,
626
                                    "value" => "window.someGlobalFunction();",
627
                                    "id" => "string"
628
                                  )
629
                                ),
630
                                "page_id" => 0
631
                              )
632
                            ),
633
                            "archived" => true,
634
                            "key" => "blue_button_variation",
635
                            "name" => "Blue Button",
636
                            "variation_id" => 0,
637
                            "weight" => 0
638
                          )
639
                        ),
640
                        "created" => "2016-10-17T07:05:00.109Z",
641
                        "id" => 3000,
642
                        "is_classic" => false,
643
                        "last_modified" => "2016-10-17T07:05:00.109Z"
644
                        ), 200);
645
        
646
        $optimizelyApiClientMock->method('sendApiRequest')
647
                    ->willReturn($result);
648
        
649
        $experimentsService = new Experiments($optimizelyApiClientMock);
650
        
651
        $experiment = new Experiment(array(
652
              "audience_ids" => array(
653
                0
654
              ),
655
              "changes" => array(
656
                array(
657
                  "type" => "custom_code",
658
                  "allow_additional_redirect" => true,
659
                  "async" => true,
660
                  "selector" => "a[href*=\"optimizely\"]",
661
                  "dependencies" => array(
662
                    24,
663
                    26
664
                  ),
665
                  "destination" => "https://app.optimizely.com/",
666
                  "extension_id" => 1234,
667
                  "preserve_parameters" => true,
668
                  "src" => 524,
669
                  "value" => "window.someGlobalFunction();"
670
                )
671
              ),
672
              "description" => "AB Test to see if the Blue Button converts more visitors",
673
              "holdback" => 0,
674
              "key" => "home_page_experiment",
675
              "metrics" => array(
676
                array(
677
                  "aggregator" => "unique",
678
                  "event_id" => 0,
679
                  "field" => "revenue",
680
                  "scope" => "session"
681
                )
682
              ),
683
              "name" => "Blue Button Experiment",
684
              "schedule" => array(
685
                "start_time" => "2016-10-17T07:04:59.731Z",
686
                "stop_time" => "2016-10-17T07:04:59.731Z",
687
                "time_zone" => "UTC"
688
              ),
689
              "status" => "active",
690
              "variations" => array(
691
                array(
692
                  "actions" => array(
693
                    array(
694
                      "changes" => array(
695
                        array(
696
                          "type" => "custom_code",
697
                          "allow_additional_redirect" => true,
698
                          "async" => true,
699
                          "selector" => "a[href*=\"optimizely\"]",
700
                          "dependencies" => array(
701
                            24,
702
                            26
703
                          ),
704
                          "destination" => "https://app.optimizely.com/",
705
                          "extension_id" => 1234,
706
                          "preserve_parameters" => true,
707
                          "src" => 524,
708
                          "value" => "window.someGlobalFunction();"
709
                        )
710
                      ),
711
                      "page_id" => 0
712
                    )
713
                  ),
714
                  "archived" => true,
715
                  "key" => "blue_button_variation",
716
                  "name" => "Blue Button",
717
                  "variation_id" => 0,
718
                  "weight" => 0
719
                )
720
              )
721
        ));
722
        
723
        $result = $experimentsService->update(1000, $experiment, true, true);
724
        $updatedExperiment = $result->getPayload();
725
        
726
        $this->assertTrue($updatedExperiment instanceOf Experiment);
727
        $this->assertTrue($updatedExperiment->getName()=='Blue Button Experiment');                
728
    }
729
    
730 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...
731
    {
732
        // Mock 'OptimizelyApiClient' object to avoid real API calls
733
        $optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient')
734
                            ->disableOriginalConstructor()
735
                            ->getMock();
736
737
        $result = new Result(array(), 200);
738
        
739
        $optimizelyApiClientMock->method('sendApiRequest')
740
                    ->willReturn($result);
741
        
742
        $experimentsService = new Experiments($optimizelyApiClientMock);
743
     
744
        $result = $experimentsService->delete(1000);
745
        
746
        $this->assertEquals(200, $result->getHttpCode());        
747
    }
748
    
749
    public function testIntegration()
750
    {
751
        if (!getenv('OPTIMIZELY_PHP_TEST_INTEGRATION')) 
752
            $this->markTestSkipped('OPTIMIZELY_PHP_TEST_INTEGRATION env var is not set');
753
        
754
        $credentials = $this->loadCredentialsFromFile();
755
        
756
        $optimizelyClient = new OptimizelyApiClient($credentials, 'v2');
0 ignored issues
show
Bug introduced by
It seems like $credentials defined by $this->loadCredentialsFromFile() on line 754 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...
757
        $this->assertTrue($optimizelyClient!=null);
758
        
759
        // Create new project        
760
        $curDate = date('Y-m-d H:i:s');
761
        $newProject = new Project(array(
762
            "name" => "Test Project $curDate",
763
            "account_id" => 12345,
764
            "confidence_threshold" => 0.9,
765
            "platform" => "web",
766
            "status" => "active",
767
            "web_snippet" => array(
768
              "enable_force_variation" => false,
769
              "exclude_disabled_experiments" => false,
770
              "exclude_names" => true,
771
              "include_jquery" => true,
772
              "ip_anonymization" => false,
773
              "ip_filter" => "^206\\.23\\.100\\.([5-9][0-9]|1([0-4][0-9]|50))$",
774
              "library" => "jquery-1.11.3-trim",
775
              "project_javascript" => "alert(\"Active Experiment\")"
776
            )
777
        ));
778
        
779
        $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...
780
        $createdProject = $result->getPayload();
781
        
782
        // Create new experiment in the project
783
        $experiment = new Experiment(array(
784
            "project_id" => $createdProject->getId(),
785
            "variations" => array(
786
              array(
787
                "weight" => 0,
788
                "actions" => array(
789
                  array(
790
                    "changes" => array(
791
                      /*array(
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
792
                        "type" => "attribute",
793
                        "allow_additional_redirect" => true,
794
                        "async" => true,
795
                        "attributes" => array(
796
                          "class" => "intro",
797
                          "hide" => true,
798
                          "href" => "example.com",
799
                          "html" => "New Title",
800
                          "remove" => true,
801
                          "src" => "song.mp3",
802
                          "style" => "background-color:blue;",
803
                          "text" => "Some nice message"
804
                        ),
805
                        "config" => array("name" => "Flash Sale Today!", "color" => "blue"),
806
                        "css" => array(
807
                          "background-color" => "string",
808
                          "background-image" => "string",
809
                          "border-color" => "string",
810
                          "border-style" => "string",
811
                          "border-width" => "string",
812
                          "color" => "string",
813
                          "font-size" => "string",
814
                          "font-weight" => "string",
815
                          "height" => "string",
816
                          "position" => "string",
817
                          "width" => "string"
818
                        ),
819
                        "dependencies" => array(
820
                          '24',
821
                          '26'
822
                        ),
823
                        "destination" => "https://app.optimizely.com/",
824
                        "name" => "Setting button text",
825
                        "operator" => "after",
826
                        "preserve_parameters" => true,
827
                        "rearrange" => "{\"insertSelector\": \".greyBox\", \"operator\": \"before\"}",
828
                        "selector" => "a[href*=\"optimizely\"]",
829
                        "value" => "window.someGlobalFunction();"
830
                      )*/
831
                    ),
832
                    "page_id" => 0
833
                  )
834
                ),
835
                "archived" => true,
836
                "key" => "blue_button_variation",
837
                "name" => "Blue Button"
838
              )
839
            ),
840
            "audience_conditions" => "[\"and\", {\"audience_id\": 7000}, {\"audience_id\":7001}]",
841
            "campaign_id" => 2000,
842
            "changes" => array(
843
              /*array(
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
844
                "type" => "attribute",
845
                "allow_additional_redirect" => true,
846
                "async" => true,
847
                "attributes" => array(
848
                  "class" => "intro",
849
                  "hide" => true,
850
                  "href" => "example.com",
851
                  "html" => "New Title",
852
                  "remove" => true,
853
                  "src" => "song.mp3",
854
                  "style" => "background-color:blue;",
855
                  "text" => "Some nice message"
856
                ),
857
                "config" => array("name" => "Flash Sale Today!", "color" => "blue"),
858
                "css" => array(
859
                  "background-color" => "string",
860
                  "background-image" => "string",
861
                  "border-color" => "string",
862
                  "border-style" => "string",
863
                  "border-width" => "string",
864
                  "color" => "string",
865
                  "font-size" => "string",
866
                  "font-weight" => "string",
867
                  "height" => "string",
868
                  "position" => "string",
869
                  "width" => "string"
870
                ),
871
                "dependencies" => array(
872
                  '24',
873
                  '26'
874
                ),
875
                "destination" => "https://app.optimizely.com/",
876
                "name" => "Setting button text",
877
                "operator" => "after",
878
                "preserve_parameters" => true,
879
                "rearrange" => array("insertSelector" => ".greyBox", "operator" => "before"),
880
                "selector" => "a[href*=\"optimizely\"]",
881
                "value" => "window.someGlobalFunction();"
882
              )*/
883
            ),
884
            "description" => "string",
885
            "holdback" => 5000,
886
            "key" => "home_page_experiment",
887
            "metrics" => array(
888
              array(
889
                "aggregator" => "unique",
890
                "event_id" => 0,
891
                "field" => "revenue",
892
                "scope" => "session"
893
              )
894
            ),
895
            "name" => "Blue Button Experiment",
896
            "schedule" => array(
897
              "start_time" => "string",
898
              "stop_time" => "string",
899
              "time_zone" => "GMT-01:00"
900
            ),
901
            "type" => "a/b"    
902
        ));
903
                
904
        
905
        $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...
906
        $createdExperiment = $result->getPayload();
907
        
908
        $this->assertTrue($createdExperiment instanceOf Experiment);
909
        $this->assertTrue($createdExperiment->getName()=='Blue Button Experiment');  
910
        
911
        // List all existing experiments and try to find the created experiment
912
        $experimentFound = false;        
913
        try {
914
            $page = 1;
915 View Code Duplication
            for (;;) {                            
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
916
                $result = $optimizelyClient->experiments()->listAll($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...
917
918
                $experiments = $result->getPayload();
919
920
                foreach ($experiments as $experiment) {
921
                    if ($experiment->getName()=="Blue Button Experiment") {
922
                        $experimentFound = true;
923
                        break;
924
                    }
925
                }
926
927
                if ($result->getNextPage()==null)
928
                    break;
929
930
                $page ++;
931
            }
932
        }
933
        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...
934
            // Handle error.
935
            $code = $e->getCode();
936
            $httpCode = $e->getHttpCode();
937
            $message = $e->getMessage();
938
            $uuid = $e->getUuid();
939
            echo "Exception caught: $message (code=$code http_code=$httpCode uuid=$uuid)\n";
940
        }
941
        
942
        $this->assertTrue($experimentFound);
943
        
944
        // Update experiment
945
        $createdExperiment->setName('Some new experiment name');
946
        $result = $optimizelyClient->experiments()->update($createdExperiment->getId(), $createdExperiment);
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...
947
        $updatedExperiment = $result->getPayload();                
948
        
949
        $this->assertTrue($updatedExperiment instanceOf Experiment);
950
        $this->assertTrue($updatedExperiment->getName()=='Some new experiment name');  
951
        
952
        // Delete experiment        
953
        $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...
954
    }
955
}
956