|
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() |
|
|
|
|
|
|
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() |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @expectedException Exception |
|
64
|
|
|
*/ |
|
65
|
|
View Code Duplication |
public function testListAll_InvalidPerPage() |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
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() |
|
|
|
|
|
|
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() |
|
|
|
|
|
|
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'); |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
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 (;;) { |
|
|
|
|
|
|
245
|
|
|
$result = $optimizelyClient->attributes()->listAll($createdAttribute->getId(), $page); |
|
|
|
|
|
|
246
|
|
|
|
|
247
|
|
|
$attributes = $result->getPayload(); |
|
248
|
|
|
|
|
249
|
|
|
foreach ($attributes as $attribute) { |
|
250
|
|
|
if ($audience->getName()=="Subscriber Status") { |
|
|
|
|
|
|
251
|
|
|
$attributeFound = true; |
|
252
|
|
|
break; |
|
253
|
|
|
} |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
if ($result->getNextPage()==null) |
|
257
|
|
|
break; |
|
258
|
|
|
|
|
259
|
|
|
$page ++; |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
catch (Exception $e) { |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
285
|
|
|
$updatedProject = $result->getPayload(); |
|
286
|
|
|
|
|
287
|
|
|
$this->assertEquals('archived', $updatedProject->getStatus()); |
|
288
|
|
|
} |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
|
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.