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

AttributesTest::testIntegration()   C

Complexity

Conditions 7
Paths 34

Size

Total Lines 100
Code Lines 66

Duplication

Lines 17
Ratio 17 %

Importance

Changes 0
Metric Value
cc 7
eloc 66
nc 34
nop 0
dl 17
loc 100
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\Attributes;
8
use WebMarketingROI\OptimizelyPHP\Resource\v2\Attribute;
9
use WebMarketingROI\OptimizelyPHP\Resource\v2\Project;
10
11
class AttributesTest extends BaseServiceTest
12
{
13 View Code Duplication
    public function testListAll()
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...
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
                            "key" => "subscriber_status",
23
                            "project_id" => 0,
24
                            "archived" => false,
25
                            "description" => "string",
26
                            "name" => "Subscriber Status",
27
                            "condition_type" => "custom_attribute",
28
                            "id" => 0,
29
                            "last_modified" => "2017-05-08T03:34:01.035Z"
30
                        )
31
                    ), 200);
32
        
33
        
34
        $optimizelyApiClientMock->method('sendApiRequest')
35
                    ->willReturn($result);
36
        
37
        $attributesService = new Attributes($optimizelyApiClientMock);
38
        
39
        $result = $attributesService->listAll(0);
40
        $attributes = $result->getPayload();
41
        
42
        $this->assertTrue(count($attributes)==1);
43
        $this->assertTrue($attributes[0] instanceOf Attribute);
44
        $this->assertTrue($attributes[0]->getName()=='Subscriber Status');        
45
    }
46
    
47
    /**
48
     * @expectedException Exception
49
     */
50 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...
51
    {
52
        // Mock 'OptimizelyApiClient' object to avoid real API calls
53
        $optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient')
54
                            ->disableOriginalConstructor()
55
                            ->getMock();
56
        
57
        $attributesService = new Attributes($optimizelyApiClientMock);
58
        
59
        $result = $attributesService->listAll(1000, -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...
60
    }
61
    
62
    /**
63
     * @expectedException Exception
64
     */
65 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...
66
    {
67
        // Mock 'OptimizelyApiClient' object to avoid real API calls
68
        $optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient')
69
                            ->disableOriginalConstructor()
70
                            ->getMock();
71
        
72
        $attributesService = new Attributes($optimizelyApiClientMock);
73
        
74
        $result = $attributesService->listAll(1000, 1, 1000);
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...
75
    }
76
    
77
    public function testGet()
78
    {
79
        // Mock 'OptimizelyApiClient' object to avoid real API calls
80
        $optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient')
81
                            ->disableOriginalConstructor()
82
                            ->getMock();
83
84
        $result = new Result(array(
85
                            "key" => "subscriber_status",
86
                            "project_id" => 0,
87
                            "archived" => false,
88
                            "description" => "string",
89
                            "name" => "Subscriber Status",
90
                            "condition_type" => "custom_attribute",
91
                            "id" => 0,
92
                            "last_modified" => "2017-05-08T03:34:01.035Z"
93
                        ), 200);
94
        
95
        $optimizelyApiClientMock->method('sendApiRequest')
96
                    ->willReturn($result);
97
        
98
        $attributesService = new Attributes($optimizelyApiClientMock);
99
        
100
        $result = $attributesService->get(0);
0 ignored issues
show
Documentation introduced by
0 is of type integer, but the function expects a object<WebMarketingROI\O...elyPHP\Service\v2\type>.

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...
101
        $attribute = $result->getPayload();
102
        
103
        $this->assertTrue($attribute instanceOf Attribute);
104
        $this->assertTrue($attribute->getName()=='Subscriber Status');        
105
    }
106
    
107 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...
108
    {
109
        // Mock 'OptimizelyApiClient' object to avoid real API calls
110
        $optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient')
111
                            ->disableOriginalConstructor()
112
                            ->getMock();
113
114
        $result = new Result(array(
115
                            "key" => "subscriber_status",
116
                            "project_id" => 0,
117
                            "archived" => false,
118
                            "description" => "string",
119
                            "name" => "Subscriber Status",
120
                            "condition_type" => "custom_attribute",
121
                            "id" => 0,
122
                            "last_modified" => "2017-05-08T03:34:01.035Z"
123
                        ), 201);
124
        
125
        $optimizelyApiClientMock->method('sendApiRequest')
126
                    ->willReturn($result);
127
        
128
        $attributesService = new Attributes($optimizelyApiClientMock);
129
        
130
        $attribute = new Attribute(array(
131
                "key" => "subscriber_status",
132
                "project_id" => 0,
133
                "archived" => false,
134
                "description" => "string",
135
                "name" => "Subscriber Status",
136
                "condition_type" => "custom_attribute",
137
                "id" => 0,
138
                "last_modified" => "2017-05-08T03:34:01.035Z"
139
        ));
140
        
141
        $result = $attributesService->create($attribute);
142
        $createdAttribute = $result->getPayload();
143
        
144
        $this->assertTrue($createdAttribute instanceOf Attribute);
145
        $this->assertTrue($createdAttribute->getName()=='Subscriber Status');                
146
    }
147
    
148 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...
149
    {
150
        // Mock 'OptimizelyApiClient' object to avoid real API calls
151
        $optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient')
152
                            ->disableOriginalConstructor()
153
                            ->getMock();
154
155
        $result = new Result(array(
156
                            "key" => "subscriber_status",
157
                            "project_id" => 0,
158
                            "archived" => false,
159
                            "description" => "string",
160
                            "name" => "Subscriber Status",
161
                            "condition_type" => "custom_attribute",
162
                            "id" => 0,
163
                            "last_modified" => "2017-05-08T03:34:01.035Z"
164
                        ), 200);
165
        
166
        $optimizelyApiClientMock->method('sendApiRequest')
167
                    ->willReturn($result);
168
        
169
        $attributesService = new Attributes($optimizelyApiClientMock);
170
        
171
        $attribute = new Attribute(array(
172
                "key" => "subscriber_status",
173
                "project_id" => 0,
174
                "archived" => false,
175
                "description" => "string",
176
                "name" => "Subscriber Status",
177
                "condition_type" => "custom_attribute",
178
                "id" => 0,
179
                "last_modified" => "2017-05-08T03:34:01.035Z"
180
        ));
181
        
182
        $result = $attributesService->update(0, $attribute);
183
        $createdAttribute = $result->getPayload();
184
        
185
        $this->assertTrue($createdAttribute instanceOf Attribute);
186
        $this->assertTrue($createdAttribute->getName()=='Subscriber Status');                
187
    }
188
    
189
    public function testIntegration()
190
    {
191
        if (!getenv('OPTIMIZELY_PHP_TEST_INTEGRATION')) 
192
            $this->markTestSkipped('OPTIMIZELY_PHP_TEST_INTEGRATION env var is not set');
193
        
194
        $credentials = $this->loadCredentialsFromFile();
195
        
196
        $optimizelyClient = new OptimizelyApiClient($credentials, 'v2');
0 ignored issues
show
Bug introduced by
It seems like $credentials defined by $this->loadCredentialsFromFile() on line 194 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...
197
        $this->assertTrue($optimizelyClient!=null);
198
        
199
        // Create new project        
200
        $curDate = date('Y-m-d H:i:s');
201
        $newProject = new Project(array(
202
            "name" => "Test Project $curDate",
203
            "account_id" => 12345,
204
            "confidence_threshold" => 0.9,
205
            "platform" => "web",
206
            "status" => "active",
207
            "web_snippet" => array(
208
              "enable_force_variation" => false,
209
              "exclude_disabled_experiments" => false,
210
              "exclude_names" => true,
211
              "include_jquery" => true,
212
              "ip_anonymization" => false,
213
              "ip_filter" => "^206\\.23\\.100\\.([5-9][0-9]|1([0-4][0-9]|50))$",
214
              "library" => "jquery-1.11.3-trim",
215
              "project_javascript" => "alert(\"Active Experiment\")"
216
            )
217
        ));
218
        
219
        $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...
220
        $createdProject = $result->getPayload();
221
        
222
        // Create new attribute in the project
223
        $attribute = new Attribute(array(
224
            "key" => "subscriber_status",
225
            "project_id" => 0,
226
            "archived" => false,
227
            "description" => "string",
228
            "name" => "Subscriber Status",
229
            "condition_type" => "custom_attribute",
230
            "id" => 0,
231
            "last_modified" => "2017-05-08T03:34:01.035Z"
232
        ));
233
        
234
        $result = $optimizelyClient->attributes()->create($attribute);
0 ignored issues
show
Documentation Bug introduced by
The method attributes 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...
235
        $createdAttribute = $result->getPayload();
236
        
237
        $this->assertTrue($createdAttribute instanceOf Attribute);
238
        $this->assertTrue($createdAttribute->getName()=='Subscriber Status');  
239
        
240
        // List all existing attributes and try to find the created attribute
241
        $attributeFound = false;        
242
        try {
243
            $page = 1;
244 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...
245
                $result = $optimizelyClient->attributes()->listAll($createdAttribute->getId(), $page);
0 ignored issues
show
Documentation Bug introduced by
The method attributes 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...
246
247
                $attributes = $result->getPayload();
248
249
                foreach ($attributes as $attribute) {
250
                    if ($audience->getName()=="Subscriber Status") {
0 ignored issues
show
Bug introduced by
The variable $audience does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
251
                        $attributeFound = true;
252
                        break;
253
                    }
254
                }
255
256
                if ($result->getNextPage()==null)
257
                    break;
258
259
                $page ++;
260
            }
261
        }
262
        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...
263
            // Handle error.
264
            $code = $e->getCode();
265
            $httpCode = $e->getHttpCode();
266
            $message = $e->getMessage();
267
            $uuid = $e->getUuid();
268
            echo "Exception caught: $message (code=$code http_code=$httpCode uuid=$uuid)\n";
269
        }
270
        
271
        $this->assertTrue($attributeFound);
272
        
273
        // Update attribute
274
        $createdAttribute->setName('Some new attribute name');
275
        $result = $optimizelyClient->attributes()->update($createdAttribute->getId(), $createdAttribute);
0 ignored issues
show
Documentation Bug introduced by
The method attributes 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...
276
        $updatedAttribute = $result->getPayload();                
277
        
278
        $this->assertTrue($updatedAttribute instanceOf Attribute);
279
        $this->assertTrue($updatedAttribute->getName()=='Some new attribute name');  
280
        
281
        // Make project archived
282
        
283
        $createdProject->setStatus('archived');
284
        $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...
285
        $updatedProject = $result->getPayload();
286
        
287
        $this->assertEquals('archived', $updatedProject->getStatus());
288
    }
289
}
290
291