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

PagesTest::testIntegration()   C

Complexity

Conditions 7
Paths 34

Size

Total Lines 105
Code Lines 71

Duplication

Lines 17
Ratio 16.19 %

Importance

Changes 0
Metric Value
cc 7
eloc 71
nc 34
nop 0
dl 17
loc 105
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 OptimizelyPHPTest\Service\v2\BaseServiceTest;
5
use WebMarketingROI\OptimizelyPHP\OptimizelyApiClient;
6
use WebMarketingROI\OptimizelyPHP\Result;
7
use WebMarketingROI\OptimizelyPHP\Service\v2\Pages;
8
use WebMarketingROI\OptimizelyPHP\Resource\v2\Page;
9
use WebMarketingROI\OptimizelyPHP\Resource\v2\Project;
10
11
class PagesTest extends BaseServiceTest
12
{
13
    public function testListAll()
14
    {
15
        // Mock 'OptimizelyApiClient' object to avoid real API calls
16
        $optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient')
17
                            ->disableOriginalConstructor()
18
                            ->getMock();
19
20
        $result = new Result(array(
21
                        array(
22
                            "edit_url" => "https://www.optimizely.com",
23
                            "name" => "Home Page",
24
                            "project_id" => 1000,
25
                            "activation_code" => "string",
26
                            "activation_type" => "immediate",
27
                            "archived" => false,
28
                            "category" => "article",
29
                            "conditions" => "string",
30
                            "key" => "home_page",
31
                            "page_type" => "single_url",
32
                            "created" => "2016-10-18T05:07:04.096Z",
33
                            "id" => 4000,
34
                            "last_modified" => "2016-10-18T05:07:04.096Z"
35
                        )
36
                    ), 200);
37
        
38
        $optimizelyApiClientMock->method('sendApiRequest')
39
                    ->willReturn($result);
40
        
41
        $pagesService = new Pages($optimizelyApiClientMock);
42
        
43
        $result = $pagesService->listAll(1000);
44
        $pages = $result->getPayload();
45
        
46
        $this->assertTrue(count($pages)==1);
47
        $this->assertTrue($pages[0] instanceOf Page);
48
        $this->assertTrue($pages[0]->getName()=='Home Page');        
49
    }
50
    
51
    public function testGet()
52
    {
53
        // Mock 'OptimizelyApiClient' object to avoid real API calls
54
        $optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient')
55
                            ->disableOriginalConstructor()
56
                            ->getMock();
57
58
        $result = new Result(array(
59
                            "edit_url" => "https://www.optimizely.com",
60
                            "name" => "Home Page",
61
                            "project_id" => 1000,
62
                            "activation_code" => "string",
63
                            "activation_type" => "immediate",
64
                            "archived" => false,
65
                            "category" => "article",
66
                            "conditions" => "string",
67
                            "key" => "home_page",
68
                            "page_type" => "single_url",
69
                            "created" => "2016-10-18T05:07:04.104Z",
70
                            "id" => 4000,
71
                            "last_modified" => "2016-10-18T05:07:04.104Z"
72
                        ), 200);
73
        
74
        $optimizelyApiClientMock->method('sendApiRequest')
75
                    ->willReturn($result);
76
        
77
        $pagesService = new Pages($optimizelyApiClientMock);
78
        
79
        $result = $pagesService->get(5000);
80
        $page = $result->getPayload();
81
                
82
        $this->assertTrue($page instanceOf Page);
83
        $this->assertEquals('Home Page', $page->getName());        
84
    }
85
    
86 View Code Duplication
    public function testCreate()
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...
87
    {
88
        // Mock 'OptimizelyApiClient' object to avoid real API calls
89
        $optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient')
90
                            ->disableOriginalConstructor()
91
                            ->getMock();
92
93
        $result = new Result(array(
94
                            "edit_url" => "https://www.optimizely.com",
95
                            "name" => "Home Page",
96
                            "project_id" => 1000,
97
                            "activation_code" => "string",
98
                            "activation_type" => "immediate",
99
                            "archived" => false,
100
                            "category" => "article",
101
                            "conditions" => "string",
102
                            "key" => "home_page",
103
                            "page_type" => "single_url",
104
                            "created" => "2016-10-18T05:07:04.113Z",
105
                            "id" => 4000,
106
                            "last_modified" => "2016-10-18T05:07:04.113Z"
107
                        ), 201);
108
        
109
        $optimizelyApiClientMock->method('sendApiRequest')
110
                    ->willReturn($result);
111
        
112
        $pagesService = new Pages($optimizelyApiClientMock);
113
        
114
        $page = new Page(array(
115
            "edit_url" => "https://www.optimizely.com",
116
            "name" => "Home Page",
117
            "project_id" => 1000,
118
            "activation_code" => "string",
119
            "activation_type" => "immediate",
120
            "archived" => false,
121
            "category" => "article",
122
            "conditions" => "string",
123
            "key" => "home_page",
124
            "page_type" => "single_url"
125
        ));
126
        
127
        $result = $pagesService->create($page);
128
        $createdPage = $result->getPayload();
129
        
130
        $this->assertTrue($createdPage instanceOf Page);
131
        $this->assertTrue($createdPage->getName()=='Home Page');                
132
    }
133
    
134 View Code Duplication
    public function testUpdate()
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...
135
    {
136
        // Mock 'OptimizelyApiClient' object to avoid real API calls
137
        $optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient')
138
                            ->disableOriginalConstructor()
139
                            ->getMock();
140
141
        $result = new Result(array(
142
                            "edit_url" => "https://www.optimizely.com",
143
                            "name" => "Home Page",
144
                            "project_id" => 1000,
145
                            "activation_code" => "string",
146
                            "activation_type" => "immediate",
147
                            "archived" => false,
148
                            "category" => "article",
149
                            "conditions" => "string",
150
                            "key" => "home_page",
151
                            "page_type" => "single_url",
152
                            "created" => "2016-10-18T05:07:04.113Z",
153
                            "id" => 4000,
154
                            "last_modified" => "2016-10-18T05:07:04.113Z"
155
                        ), 200);
156
        
157
        $optimizelyApiClientMock->method('sendApiRequest')
158
                    ->willReturn($result);
159
        
160
        $pagesService = new Pages($optimizelyApiClientMock);
161
        
162
        $page = new Page(array(
163
            "edit_url" => "https://www.optimizely.com",
164
            "name" => "Home Page",
165
            "project_id" => 1000,
166
            "activation_code" => "string",
167
            "activation_type" => "immediate",
168
            "archived" => false,
169
            "category" => "article",
170
            "conditions" => "string",
171
            "key" => "home_page",
172
            "page_type" => "single_url"
173
        ));
174
        
175
        $result = $pagesService->update(1000, $page);
0 ignored issues
show
Documentation introduced by
$page is of type object<WebMarketingROI\O...lyPHP\Resource\v2\Page>, but the function expects a object<WebMarketingROI\O...HP\Service\v2\Audience>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
176
        $updatedPage = $result->getPayload();
177
        
178
        $this->assertTrue($updatedPage instanceOf Page);
179
        $this->assertTrue($updatedPage->getName()=='Home Page');                  
180
    }
181
    
182 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...
183
    {
184
        // Mock 'OptimizelyApiClient' object to avoid real API calls
185
        $optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient')
186
                            ->disableOriginalConstructor()
187
                            ->getMock();
188
189
        $result = new Result(array(), 200);
190
        
191
        $optimizelyApiClientMock->method('sendApiRequest')
192
                    ->willReturn($result);
193
        
194
        $pagesService = new Pages($optimizelyApiClientMock);
195
     
196
        $result = $pagesService->delete(1000);
197
        
198
        $this->assertEquals(200, $result->getHttpCode());
199
    }
200
    
201
    public function testIntegration()
202
    {
203
        if (!getenv('OPTIMIZELY_PHP_TEST_INTEGRATION')) 
204
            $this->markTestSkipped('OPTIMIZELY_PHP_TEST_INTEGRATION env var is not set');
205
        
206
        $credentials = $this->loadCredentialsFromFile();
207
        
208
        $optimizelyClient = new OptimizelyApiClient($credentials, 'v2');
0 ignored issues
show
Bug introduced by
It seems like $credentials defined by $this->loadCredentialsFromFile() on line 206 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...
209
        $this->assertTrue($optimizelyClient!=null);
210
        
211
        // Create new project        
212
        $curDate = date('Y-m-d H:i:s');
213
        $newProject = new Project(array(
214
            "name" => "Test Project $curDate",
215
            "account_id" => 12345,
216
            "confidence_threshold" => 0.9,
217
            "platform" => "web",
218
            "status" => "active",
219
            "web_snippet" => array(
220
              "enable_force_variation" => false,
221
              "exclude_disabled_experiments" => false,
222
              "exclude_names" => true,
223
              "include_jquery" => true,
224
              "ip_anonymization" => false,
225
              "ip_filter" => "^206\\.23\\.100\\.([5-9][0-9]|1([0-4][0-9]|50))$",
226
              "library" => "jquery-1.11.3-trim",
227
              "project_javascript" => "alert(\"Active Experiment\")"
228
            )
229
        ));
230
        
231
        $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...
232
        $createdProject = $result->getPayload();
233
        
234
        // Create new page in the project
235
        $page = new Page(array(
236
            "edit_url" => "https://www.optimizely.com",
237
            "name" => "Home Page",
238
            "project_id" => $createdProject->getId(),
239
            "activation_code" => "string",
240
            "activation_type" => "immediate",
241
            "archived" => false,
242
            "category" => "article",
243
            "conditions" => "[\"and\", {\"type\": \"url\", \"match_type\": \"substring\", \"value\": \"optimize\"}]",
244
            "key" => "home_page",
245
            "page_type" => "single_url",
246
            "created" => "2016-10-18T05:07:04.113Z",
247
            "id" => 4000,
248
            "last_modified" => "2016-10-18T05:07:04.113Z"
249
        ));
250
        
251
        $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...
252
        $createdPage = $result->getPayload();
253
        
254
        $this->assertTrue($createdPage instanceOf Page);
255
        $this->assertTrue($createdPage->getName()=='Home Page');  
256
        
257
        // List all existing pages and try to find the created page
258
        $pageFound = false;        
259
        try {
260
            $page = 1;
261 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...
262
                $result = $optimizelyClient->pages()->listAll($createdProject->getId(), $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...
263
264
                $pages = $result->getPayload();
265
266
                foreach ($pages as $pageObject) {
267
                    if ($pageObject->getName()=="Home Page") {
268
                        $pageFound = true;
269
                        break;
270
                    }
271
                }
272
273
                if ($result->getNextPage()==null)
274
                    break;
275
276
                $page ++;
277
            }
278
        }
279
        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...
280
            // Handle error.
281
            $code = $e->getCode();
282
            $httpCode = $e->getHttpCode();
283
            $message = $e->getMessage();
284
            $uuid = $e->getUuid();
285
            echo "Exception caught: $message (code=$code http_code=$httpCode uuid=$uuid)\n";
286
        }
287
        
288
        $this->assertTrue($pageFound);
289
        
290
        // Update page
291
        $createdPage->setName('Some new page name');
292
        $result = $optimizelyClient->pages()->update($createdPage->getId(), $createdPage);
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...
293
        $updatedPage = $result->getPayload();                
294
        
295
        $this->assertTrue($updatedPage instanceOf Page);
296
        $this->assertTrue($updatedPage->getName()=='Some new page name');  
297
        
298
        // Make project archived
299
        
300
        $createdProject->setStatus('archived');
301
        $result = $optimizelyClient->projects()->update($createdProject->getId(), $createdProject);
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...
302
        $updatedProject = $result->getPayload();
303
        
304
        $this->assertEquals('archived', $updatedProject->getStatus());
305
    }
306
}
307
308