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() |
|
|
|
|
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() |
|
|
|
|
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); |
|
|
|
|
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() |
|
|
|
|
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'); |
|
|
|
|
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); |
|
|
|
|
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); |
|
|
|
|
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 (;;) { |
|
|
|
|
262
|
|
|
$result = $optimizelyClient->pages()->listAll($createdProject->getId(), $page); |
|
|
|
|
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) { |
|
|
|
|
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); |
|
|
|
|
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); |
|
|
|
|
302
|
|
|
$updatedProject = $result->getPayload(); |
303
|
|
|
|
304
|
|
|
$this->assertEquals('archived', $updatedProject->getStatus()); |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
|
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.