Completed
Push — master ( 4d3008...03e605 )
by Oleg
02:22
created

PagesTest::testCreate()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 45
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 35
nc 1
nop 0
dl 0
loc 45
rs 8.8571
c 0
b 0
f 0
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_type" => "immediate",
26
                            "archived" => false,
27
                            "category" => "article",
28
                            "conditions" => "string",
29
                            "key" => "home_page",
30
                            "page_type" => "single_url",
31
                            "created" => "2016-10-18T05:07:04.096Z",
32
                            "id" => 4000,
33
                            "last_modified" => "2016-10-18T05:07:04.096Z"
34
                        )
35
                    ), 200);
36
        
37
        $optimizelyApiClientMock->method('sendApiRequest')
38
                    ->willReturn($result);
39
        
40
        $pagesService = new Pages($optimizelyApiClientMock);
41
        
42
        $result = $pagesService->listAll(1000);
43
        $pages = $result->getPayload();
44
        
45
        $this->assertTrue(count($pages)==1);
46
        $this->assertTrue($pages[0] instanceOf Page);
47
        $this->assertTrue($pages[0]->getName()=='Home Page');        
48
    }
49
    
50
    public function testGet()
51
    {
52
        // Mock 'OptimizelyApiClient' object to avoid real API calls
53
        $optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient')
54
                            ->disableOriginalConstructor()
55
                            ->getMock();
56
57
        $result = new Result(array(
58
                            "edit_url" => "https://www.optimizely.com",
59
                            "name" => "Home Page",
60
                            "project_id" => 1000,
61
                            "activation_type" => "immediate",
62
                            "archived" => false,
63
                            "category" => "article",
64
                            "conditions" => "string",
65
                            "key" => "home_page",
66
                            "page_type" => "single_url",
67
                            "created" => "2016-10-18T05:07:04.104Z",
68
                            "id" => 4000,
69
                            "last_modified" => "2016-10-18T05:07:04.104Z"
70
                        ), 200);
71
        
72
        $optimizelyApiClientMock->method('sendApiRequest')
73
                    ->willReturn($result);
74
        
75
        $pagesService = new Pages($optimizelyApiClientMock);
76
        
77
        $result = $pagesService->get(5000);
78
        $page = $result->getPayload();
79
                
80
        $this->assertTrue($page instanceOf Page);
81
        $this->assertEquals('Home Page', $page->getName());        
82
    }
83
    
84
    public function testCreate()
85
    {
86
        // Mock 'OptimizelyApiClient' object to avoid real API calls
87
        $optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient')
88
                            ->disableOriginalConstructor()
89
                            ->getMock();
90
91
        $result = new Result(array(
92
                            "edit_url" => "https://www.optimizely.com",
93
                            "name" => "Home Page",
94
                            "project_id" => 1000,
95
                            "activation_type" => "immediate",
96
                            "archived" => false,
97
                            "category" => "article",
98
                            "conditions" => "string",
99
                            "key" => "home_page",
100
                            "page_type" => "single_url",
101
                            "created" => "2016-10-18T05:07:04.113Z",
102
                            "id" => 4000,
103
                            "last_modified" => "2016-10-18T05:07:04.113Z"
104
                        ), 201);
105
        
106
        $optimizelyApiClientMock->method('sendApiRequest')
107
                    ->willReturn($result);
108
        
109
        $pagesService = new Pages($optimizelyApiClientMock);
110
        
111
        $page = new Page(array(
112
            "edit_url" => "https://www.optimizely.com",
113
            "name" => "Home Page",
114
            "project_id" => 1000,
115
            "activation_type" => "immediate",
116
            "archived" => false,
117
            "category" => "article",
118
            "conditions" => "string",
119
            "key" => "home_page",
120
            "page_type" => "single_url"
121
        ));
122
        
123
        $result = $pagesService->create($page);
124
        $createdPage = $result->getPayload();
125
        
126
        $this->assertTrue($createdPage instanceOf Page);
127
        $this->assertTrue($createdPage->getName()=='Home Page');                
128
    }
129
    
130
    public function testUpdate()
131
    {
132
        // Mock 'OptimizelyApiClient' object to avoid real API calls
133
        $optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient')
134
                            ->disableOriginalConstructor()
135
                            ->getMock();
136
137
        $result = new Result(array(
138
                            "edit_url" => "https://www.optimizely.com",
139
                            "name" => "Home Page",
140
                            "project_id" => 1000,
141
                            "activation_code" => "string",
142
                            "activation_type" => "immediate",
143
                            "archived" => false,
144
                            "category" => "article",
145
                            "conditions" => "string",
146
                            "key" => "home_page",
147
                            "page_type" => "single_url",
148
                            "created" => "2016-10-18T05:07:04.113Z",
149
                            "id" => 4000,
150
                            "last_modified" => "2016-10-18T05:07:04.113Z"
151
                        ), 200);
152
        
153
        $optimizelyApiClientMock->method('sendApiRequest')
154
                    ->willReturn($result);
155
        
156
        $pagesService = new Pages($optimizelyApiClientMock);
157
        
158
        $page = new Page(array(
159
            "edit_url" => "https://www.optimizely.com",
160
            "name" => "Home Page",
161
            "project_id" => 1000,
162
            "activation_code" => "string",
163
            "activation_type" => "immediate",
164
            "archived" => false,
165
            "category" => "article",
166
            "conditions" => "string",
167
            "key" => "home_page",
168
            "page_type" => "single_url"
169
        ));
170
        
171
        $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...
172
        $updatedPage = $result->getPayload();
173
        
174
        $this->assertTrue($updatedPage instanceOf Page);
175
        $this->assertTrue($updatedPage->getName()=='Home Page');                  
176
    }
177
    
178 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...
179
    {
180
        // Mock 'OptimizelyApiClient' object to avoid real API calls
181
        $optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient')
182
                            ->disableOriginalConstructor()
183
                            ->getMock();
184
185
        $result = new Result(array(), 200);
186
        
187
        $optimizelyApiClientMock->method('sendApiRequest')
188
                    ->willReturn($result);
189
        
190
        $pagesService = new Pages($optimizelyApiClientMock);
191
     
192
        $result = $pagesService->delete(1000);
193
        
194
        $this->assertEquals(200, $result->getHttpCode());
195
    }
196
    
197
    public function testIntegration()
198
    {
199
        if (!getenv('OPTIMIZELY_PHP_TEST_INTEGRATION')) 
200
            $this->markTestSkipped('OPTIMIZELY_PHP_TEST_INTEGRATION env var is not set');
201
        
202
        $credentials = $this->loadCredentialsFromFile();
203
        
204
        $optimizelyClient = new OptimizelyApiClient($credentials, 'v2');
0 ignored issues
show
Bug introduced by
It seems like $credentials defined by $this->loadCredentialsFromFile() on line 202 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...
205
        $this->assertTrue($optimizelyClient!=null);
206
        
207
        // Create new project        
208
        $curDate = date('Y-m-d H:i:s');
209
        $newProject = new Project(array(
210
            "name" => "Test Project $curDate",
211
            "account_id" => 12345,
212
            "confidence_threshold" => 0.9,
213
            "platform" => "web",
214
            "status" => "active",
215
            "web_snippet" => array(
216
              "enable_force_variation" => false,
217
              "exclude_disabled_experiments" => false,
218
              "exclude_names" => true,
219
              "include_jquery" => true,
220
              "ip_anonymization" => false,
221
              "ip_filter" => "^206\\.23\\.100\\.([5-9][0-9]|1([0-4][0-9]|50))$",
222
              "library" => "jquery-1.11.3-trim",
223
              "project_javascript" => "alert(\"Active Experiment\")"
224
            )
225
        ));
226
        
227
        $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...
228
        $createdProject = $result->getPayload();
229
        
230
        // Create new page in the project
231
        $page = new Page(array(
232
            "edit_url" => "https://www.optimizely.com",
233
            "name" => "Home Page",
234
            "project_id" => $createdProject->getId(),
235
            "activation_type" => "immediate",
236
            "archived" => false,
237
            "category" => "article",
238
            "conditions" => "[\"and\", {\"type\": \"url\", \"match_type\": \"substring\", \"value\": \"optimize\"}]",
239
            "key" => "home_page",
240
            "page_type" => "single_url",
241
            "created" => "2016-10-18T05:07:04.113Z",
242
            "id" => 4000,
243
            "last_modified" => "2016-10-18T05:07:04.113Z"
244
        ));
245
        
246
        $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...
247
        $createdPage = $result->getPayload();
248
        
249
        $this->assertTrue($createdPage instanceOf Page);
250
        $this->assertTrue($createdPage->getName()=='Home Page');  
251
        
252
        // List all existing pages and try to find the created page
253
        $pageFound = false;        
254
        try {
255
            $page = 1;
256
            for (;;) {                            
257
                $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...
258
259
                $pages = $result->getPayload();
260
261
                foreach ($pages as $pageObject) {
262
                    if ($pageObject->getName()=="Home Page") {
263
                        $pageFound = true;
264
                        break;
265
                    }
266
                }
267
268
                if ($result->getNextPage()==null)
269
                    break;
270
271
                $page ++;
272
            }
273
        }
274
        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...
275
            // Handle error.
276
            $code = $e->getCode();
277
            $httpCode = $e->getHttpCode();
278
            $message = $e->getMessage();
279
            $uuid = $e->getUuid();
280
            echo "Exception caught: $message (code=$code http_code=$httpCode uuid=$uuid)\n";
281
        }
282
        
283
        $this->assertTrue($pageFound);
284
        
285
        // Update page
286
        $createdPage->setName('Some new page name');
287
        $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...
288
        $updatedPage = $result->getPayload();                
289
        
290
        $this->assertTrue($updatedPage instanceOf Page);
291
        $this->assertTrue($updatedPage->getName()=='Some new page name');  
292
        
293
        // Make project archived
294
        
295
        $createdProject->setStatus('archived');
296
        $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...
297
        $updatedProject = $result->getPayload();
298
        
299
        $this->assertEquals('archived', $updatedProject->getStatus());
300
    }
301
}
302
303