Completed
Push — master ( 0ca99d...7c70a3 )
by Oleg
02:52
created

ProjectsTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 282
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 282
rs 10
c 1
b 0
f 0
wmc 11
lcom 1
cbo 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
B testListAll() 0 42 1
B testGet() 0 39 1
A testCreate() 0 63 1
A testUpdate() 0 61 1
C testIntegration() 0 70 7
1
<?php
2
namespace OptimizelyPHPTest\Service\v2;
3
4
use OptimizelyPHPTest\Service\v2\BaseServiceTest;
5
use WebMarketingROI\OptimizelyPHP\OptimizelyApiClient;
6
use WebMarketingROI\OptimizelyPHP\Service\v2\Projects;
7
use WebMarketingROI\OptimizelyPHP\Resource\v2\Project;
8
9
class ProjectsTest extends BaseServiceTest
10
{
11
    public function testListAll()
12
    {
13
        // Mock 'OptimizelyApiClient' object to avoid real API calls
14
        $optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient')
15
                            ->disableOriginalConstructor()
16
                            ->getMock();
17
18
        $curDate = date('Y-m-d H:i:s');
19
        
20
        $optimizelyApiClientMock->method('sendApiRequest')
21
                    ->willReturn(array(
22
                        array(
23
                            'id' => '1523456',
24
                            'account_id' => '54321',
25
                            'name' => 'Some Optimizely Project',
26
                            'created' => $curDate,
27
                            'last_modified' => $curDate,
28
                            'is_classic' => true,
29
                            'socket_token' => 'fwerw',            
30
                            'web_snippet' => array(
31
                                "enable_force_variation" => false,
32
                                "exclude_disabled_experiments" => false,
33
                                "exclude_names" => true,
34
                                "include_jquery" => true,
35
                                "ip_anonymization" => false,
36
                                "ip_filter" => "^206\\.23\\.100\\.([5-9][0-9]|1([0-4][0-9]|50))$",
37
                                "library" => "jquery-1.11.3-trim",
38
                                "project_javascript" => "alert(\"Active Experiment\")",
39
                                "code_revision" => '123456',
40
                                "js_file_size" => 5004
41
                            )
42
                        )
43
                    ));
44
        
45
        $projectsService = new Projects($optimizelyApiClientMock);
46
        
47
        $projects = $projectsService->listAll();
48
        
49
        $this->assertTrue(count($projects)==1);
50
        $this->assertTrue($projects[0] instanceOf Project);
51
        $this->assertTrue($projects[0]->getName()=='Some Optimizely Project');        
52
    }
53
    
54
    public function testGet()
55
    {
56
        // Mock 'OptimizelyApiClient' object to avoid real API calls
57
        $optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient')
58
                            ->disableOriginalConstructor()
59
                            ->getMock();
60
61
        $curDate = date('Y-m-d H:i:s');
62
        
63
        $optimizelyApiClientMock->method('sendApiRequest')
64
                    ->willReturn(array(
65
                            'id' => '1523456',
66
                            'account_id' => '54321',
67
                            'name' => 'Some Optimizely Project',
68
                            'created' => $curDate,
69
                            'last_modified' => $curDate,
70
                            'is_classic' => true,
71
                            'socket_token' => 'fwerw',            
72
                            'web_snippet' => array(
73
                                "enable_force_variation" => false,
74
                                "exclude_disabled_experiments" => false,
75
                                "exclude_names" => true,
76
                                "include_jquery" => true,
77
                                "ip_anonymization" => false,
78
                                "ip_filter" => "^206\\.23\\.100\\.([5-9][0-9]|1([0-4][0-9]|50))$",
79
                                "library" => "jquery-1.11.3-trim",
80
                                "project_javascript" => "alert(\"Active Experiment\")",
81
                                "code_revision" => '123456',
82
                                "js_file_size" => 5004
83
                            )                        
84
                        ));
85
        
86
        $projectsService = new Projects($optimizelyApiClientMock);
87
        
88
        $project = $projectsService->get(1523456);
89
        
90
        $this->assertTrue($project instanceOf Project);
91
        $this->assertTrue($project->getName()=='Some Optimizely Project');        
92
    }
93
    
94
    public function testCreate()
95
    {
96
        // Mock 'OptimizelyApiClient' object to avoid real API calls
97
        $optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient')
98
                            ->disableOriginalConstructor()
99
                            ->getMock();
100
101
        $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...
102
        
103
        $optimizelyApiClientMock->method('sendApiRequest')
104
                    ->willReturn(array(
105
                            "name" => "Test Project",
106
                            "account_id" => 12345,
107
                            "confidence_threshold" => 0.9,
108
                            "dcp_service_id" => 121234,
109
                            "platform" => "web",
110
                            "status" => "active",
111
                            "web_snippet" => array(
112
                              "enable_force_variation" => false,
113
                              "exclude_disabled_experiments" => false,
114
                              "exclude_names" => true,
115
                              "include_jquery" => true,
116
                              "ip_anonymization" => false,
117
                              "ip_filter" => "^206\\.23\\.100\\.([5-9][0-9]|1([0-4][0-9]|50))$",
118
                              "library" => "jquery-1.11.3-trim",
119
                              "project_javascript" => "alert(\"Active Experiment\")",
120
                              "code_revision" => 0,
121
                              "js_file_size" => 63495
122
                            ),
123
                            "created" => "2016-10-17T07:04:59.991Z",
124
                            "id" => 1000,
125
                            "is_classic" => true,
126
                            "last_modified" => "2016-10-17T07:04:59.991Z",
127
                            "socket_token" => "AABBCCDD~123456789"                   
128
                        ));
129
        
130
        $projectsService = new Projects($optimizelyApiClientMock);
131
        
132
        $project = new Project(array(
133
            "name" => "Test Project",
134
            "account_id" => 12345,
135
            "confidence_threshold" => 0.9,
136
            "dcp_service_id" => 121234,
137
            "platform" => "web",
138
            "status" => "active",
139
            "web_snippet" => array(
140
              "enable_force_variation" => false,
141
              "exclude_disabled_experiments" => false,
142
              "exclude_names" => true,
143
              "include_jquery" => true,
144
              "ip_anonymization" => false,
145
              "ip_filter" => "^206\\.23\\.100\\.([5-9][0-9]|1([0-4][0-9]|50))$",
146
              "library" => "jquery-1.11.3-trim",
147
              "project_javascript" => "alert(\"Active Experiment\")"
148
            )
149
        ));
150
        
151
        $createdProject = $projectsService->create($project);
152
        
153
        $this->assertTrue($createdProject instanceOf Project);
154
        $this->assertTrue($createdProject->getName()=='Test Project');        
155
        $this->assertTrue($createdProject->getAccountId()==12345);        
156
    }
157
    
158
    public function testUpdate()
159
    {
160
        // Mock 'OptimizelyApiClient' object to avoid real API calls
161
        $optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient')
162
                            ->disableOriginalConstructor()
163
                            ->getMock();
164
165
        $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...
166
        
167
        $optimizelyApiClientMock->method('sendApiRequest')
168
                    ->willReturn(array(
169
                            "name" => "Test Project",
170
                            "account_id" => 12345,
171
                            "confidence_threshold" => 0.9,
172
                            "dcp_service_id" => 121234,
173
                            "platform" => "web",
174
                            "status" => "active",
175
                            "web_snippet" => array(
176
                              "enable_force_variation" => false,
177
                              "exclude_disabled_experiments" => false,
178
                              "exclude_names" => true,
179
                              "include_jquery" => true,
180
                              "ip_anonymization" => false,
181
                              "ip_filter" => "^206\\.23\\.100\\.([5-9][0-9]|1([0-4][0-9]|50))$",
182
                              "library" => "jquery-1.11.3-trim",
183
                              "project_javascript" => "alert(\"Active Experiment\")",
184
                              "code_revision" => 0,
185
                              "js_file_size" => 63495
186
                            ),
187
                            "created" => "2016-10-17T07:04:59.999Z",
188
                            "id" => 1000,
189
                            "is_classic" => true,
190
                            "last_modified" => "2016-10-17T07:04:59.999Z",
191
                            "socket_token" => "AABBCCDD~123456789"
192
                        ));
193
        
194
        $projectsService = new Projects($optimizelyApiClientMock);
195
        
196
        $project = new Project(array(
197
            "confidence_threshold" => 0.9,
198
            "dcp_service_id" => 121234,
199
            "name" => "Test Project",
200
            "status" => "active",
201
            "web_snippet" => array(
202
              "enable_force_variation" => false,
203
              "exclude_disabled_experiments" => false,
204
              "exclude_names" => true,
205
              "include_jquery" => true,
206
              "ip_anonymization" => false,
207
              "ip_filter" => "^206\\.23\\.100\\.([5-9][0-9]|1([0-4][0-9]|50))$",
208
              "library" => "jquery-1.11.3-trim",
209
              "project_javascript" => "alert(\"Active Experiment\")"
210
            )
211
        ));
212
        
213
        $updatedProject = $projectsService->update(121234, $project);
214
        
215
        $this->assertTrue($updatedProject instanceOf Project);
216
        $this->assertTrue($updatedProject->getName()=='Test Project');        
217
        $this->assertTrue($updatedProject->getAccountId()==12345);        
218
    }
219
    
220
    public function testIntegration()
221
    {
222
        if (!getenv('OPTIMIZELY_PHP_TEST_INTEGRATION')) 
223
            $this->markTestSkipped('OPTIMIZELY_PHP_TEST_INTEGRATION env var is not set');
224
        
225
        $credentials = $this->loadCredentialsFromFile();
226
        
227
        $optimizelyClient = new OptimizelyApiClient($credentials, 'v2');
0 ignored issues
show
Bug introduced by
It seems like $credentials defined by $this->loadCredentialsFromFile() on line 225 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...
228
        $this->assertTrue($optimizelyClient!=null);
229
        
230
        // Create new project
231
        $curDate = date('Y-m-d H:i:s');
232
        $newProject = new Project(array(
233
            "name" => "Test Project $curDate",
234
            "account_id" => 12345,
235
            "confidence_threshold" => 0.9,
236
            "platform" => "web",
237
            "status" => "active",
238
            "web_snippet" => array(
239
              "enable_force_variation" => false,
240
              "exclude_disabled_experiments" => false,
241
              "exclude_names" => true,
242
              "include_jquery" => true,
243
              "ip_anonymization" => false,
244
              "ip_filter" => "^206\\.23\\.100\\.([5-9][0-9]|1([0-4][0-9]|50))$",
245
              "library" => "jquery-1.11.3-trim",
246
              "project_javascript" => "alert(\"Active Experiment\")"
247
            )
248
        ));
249
        
250
        $createdProject = $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...
251
        $this->assertEquals("Test Project $curDate", $createdProject->getName());
252
        
253
        // List all existing projects and try to find the created project
254
        $projectFound = false;
255
        $projectId = null;
256
        $page = 0;
257
        for (;;) {
258
            try {
259
                $projects = $optimizelyClient->projects()->listAll($page);
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...
260
                foreach ($projects as $project) {
261
                    if ($project->getName()=="Test Project $curDate") {
262
                        $projectId = $project->getId();
263
                        $found = true;
0 ignored issues
show
Unused Code introduced by
$found 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...
264
                        break;
265
                    }
266
                }
267
                
268
            } catch (\Exception $e) {
269
                if ($e->getCode()!=502)
270
                    throw $e;
271
                break;
272
            }
273
            $page ++;
274
        } 
275
        
276
        $this->assertTrue($projectFound);
277
        
278
        // Retrieve project by ID
279
        
280
        $project = $optimizelyClient->projects()->get($projectId);
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...
281
        $this->assertEquals("Test Project $curDate", $project->getName());
282
        
283
        // Make project archived
284
        
285
        $project->setStatus('archived');
286
        $updatedProject = $optimizelyClient->projects()->update($projectId, $project);
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...
287
        
288
        $this->assertEquals('archived', $updatedProject->getStatus());
289
    }
290
}
291
292
293