1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the SlotMachine library. |
5
|
|
|
* |
6
|
|
|
* (c) Adam Elsodaney <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace test\SlotMachine; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
use Symfony\Component\Yaml\Yaml; |
16
|
|
|
|
17
|
|
|
class SlotMachineTest extends \PHPUnit_Framework_TestCase |
18
|
|
|
{ |
19
|
|
|
private $page; |
20
|
|
|
private static $slotsConfig; |
21
|
|
|
private static $slotsConfigWithOptions; |
22
|
|
|
|
23
|
|
|
public static function setUpBeforeClass() |
24
|
|
|
{ |
25
|
|
|
self::$slotsConfig = Yaml::parse(file_get_contents(__DIR__.'/../fixtures/slots.config.yml')); |
26
|
|
|
self::$slotsConfigWithOptions = Yaml::parse(file_get_contents(__DIR__.'/../fixtures/slots_with_options.config.yml')); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function setUp() |
30
|
|
|
{ |
31
|
|
|
$this->page = new SlotMachine(self::$slotsConfig); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @covers SlotMachine\SlotMachine::count |
36
|
|
|
*/ |
37
|
|
|
public function testCountable() |
38
|
|
|
{ |
39
|
|
|
$this->assertEquals(10, count($this->page)); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @covers SlotMachine\SlotMachine::initialize |
44
|
|
|
*/ |
45
|
|
|
public function testInitializeWithOptions() |
46
|
|
|
{ |
47
|
|
|
$s = new SlotMachine(self::$slotsConfigWithOptions); |
48
|
|
|
$this->assertEquals('Welcome back, Guest!', $s->get('headline', 3)); |
49
|
|
|
$this->assertEquals(2, count($s)); |
50
|
|
|
|
51
|
|
|
$t = new SlotMachine(self::$slotsConfigWithOptions, Request::create('?uid=8000')); |
52
|
|
|
|
53
|
|
|
$this->assertEquals('See you again, Admin!', $t->get('headline', 8000)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @covers SlotMachine\SlotMachine::get |
58
|
|
|
*/ |
59
|
|
|
public function testGet() |
60
|
|
|
{ |
61
|
|
|
$this->assertEquals('h', $this->page['headline']->getKey()); |
62
|
|
|
|
63
|
|
|
$this->assertEquals('Howdy, stranger. Please take a moment to register.', $this->page->get('headline')); |
64
|
|
|
|
65
|
|
|
// This slot has a custom default that should be used. |
66
|
|
|
$this->assertEquals('penguin.png', $this->page->get('featured_image')); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @covers SlotMachine\SlotMachine::get |
71
|
|
|
* @covers SlotMachine\SlotMachine::all |
72
|
|
|
* @covers SlotMachine\SlotMachine::toJson |
73
|
|
|
* @covers SlotMachine\SlotMachine::__toString |
74
|
|
|
*/ |
75
|
|
|
public function testAll() |
76
|
|
|
{ |
77
|
|
|
$slots = $this->page->all(); |
78
|
|
|
|
79
|
|
|
$this->assertEquals('Howdy, stranger. Please take a moment to register.', $slots['headline']); |
80
|
|
|
$this->assertEquals('penguin.png', $slots['featured_image']); |
81
|
|
|
|
82
|
|
|
$json = json_decode($this->page); |
83
|
|
|
$this->assertEquals('penguin.png', $json->featured_image); |
84
|
|
|
|
85
|
|
|
// Now try with a custom request |
86
|
|
|
$slots = new SlotMachine(self::$slotsConfig, Request::create('?app_data[h]=4&app_data[uid]=11&app_data[i]=2')); |
87
|
|
|
$data = json_decode($slots); |
88
|
|
|
$this->assertEquals('See you again, Claus!', $data->headline); |
89
|
|
|
$this->assertEquals('<img src="parrot.png" alt="Featured Image" />', $data->featured_image_html); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @covers SlotMachine\SlotMachine::get |
94
|
|
|
*/ |
95
|
|
|
public function testGetUndefinedCard() |
96
|
|
|
{ |
97
|
|
|
// Return the default card |
98
|
|
|
$this->assertEquals('Howdy, stranger. Please take a moment to register.', $this->page->get('headline', 9001)); |
99
|
|
|
$this->assertEquals('penguin.png', $this->page->get('featured_image', 9001)); |
100
|
|
|
|
101
|
|
|
// Return the fallback card |
102
|
|
|
$this->assertEquals('Dubstep', $this->page->get('music_genre', 9001)); |
103
|
|
|
|
104
|
|
|
$this->assertEquals('', $this->page->get('music_genre_optional', 9001)); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @covers SlotMachine\SlotMachine::get |
109
|
|
|
* @expectedException SlotMachine\Exception\NoCardFoundException |
110
|
|
|
*/ |
111
|
|
|
public function testGetUndefinedCardThrowsException() |
112
|
|
|
{ |
113
|
|
|
$this->setExpectedException('SlotMachine\Exception\NoCardFoundException'); |
114
|
|
|
$this->assertEquals('Splittercore', $this->page->get('music_genre_required', 9001)); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @covers SlotMachine\SlotMachine::get |
119
|
|
|
*/ |
120
|
|
|
public function testGetDefaultViaObjectMethod() |
121
|
|
|
{ |
122
|
|
|
$this->assertEquals('Sign up now to begin your free download.', $this->page->get('headline', 2)); |
123
|
|
|
|
124
|
|
|
// This slot has a custom default and should be overridden. |
125
|
|
|
$this->assertEquals('parrot.png', $this->page->get('featured_image', 2)); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @covers SlotMachine\SlotMachine::get |
130
|
|
|
*/ |
131
|
|
|
public function testGetViaRequest() |
132
|
|
|
{ |
133
|
|
|
// Test from passed parameters |
134
|
|
|
$slots = new SlotMachine(self::$slotsConfig, Request::create('/', 'GET', array('h' => '2'))); |
135
|
|
|
$this->assertEquals('Sign up now to begin your free download.', $slots->get('headline')); |
136
|
|
|
|
137
|
|
|
// Test from query string |
138
|
|
|
$slots = new SlotMachine(self::$slotsConfig, Request::create('?h=2', 'GET')); |
139
|
|
|
$this->assertEquals('Sign up now to begin your free download.', $slots->get('headline')); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @covers SlotMachine\SlotMachine::get |
144
|
|
|
*/ |
145
|
|
|
public function testGetFromArrayViaRequest() |
146
|
|
|
{ |
147
|
|
|
// Test from passed array parameters |
148
|
|
|
$slots = new SlotMachine(self::$slotsConfig, |
149
|
|
|
Request::create('/', 'GET', array( |
150
|
|
|
'app_data' => array('fb' => 1) |
151
|
|
|
)) |
152
|
|
|
); |
153
|
|
|
$this->assertEquals('product_page', $slots->get('facebook_page')); |
154
|
|
|
|
155
|
|
|
// Test from array query string |
156
|
|
|
$slots = new SlotMachine(self::$slotsConfig, Request::create('?app_data[fb]=2', 'GET')); |
157
|
|
|
$this->assertEquals('promotional_page', $slots->get('facebook_page')); |
158
|
|
|
|
159
|
|
|
$slots = new SlotMachine(self::$slotsConfig, Request::create('?app_data[i]=5', 'GET')); |
160
|
|
|
$this->assertEquals('elephant.png', $slots->get('featured_image')); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @covers SlotMachine\SlotMachine::get |
165
|
|
|
*/ |
166
|
|
|
public function testGetAndResolveParameters() |
167
|
|
|
{ |
168
|
|
|
// Test from array query string |
169
|
|
|
$slots = new SlotMachine(self::$slotsConfig, Request::create('?app_data[h]=1', 'GET')); |
170
|
|
|
$this->assertEquals('Register today for your free gift.', $slots->get('headline')); |
171
|
|
|
|
172
|
|
|
// Test from passed array parameters |
173
|
|
|
$slots = new SlotMachine(self::$slotsConfig, Request::create('/', 'GET', array('app_data' => array('h' => 2)))); |
174
|
|
|
$this->assertEquals('Sign up now to begin your free download.', $slots->get('headline')); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @covers SlotMachine\SlotMachine::initialize |
179
|
|
|
*/ |
180
|
|
|
public function testAssignReel() |
181
|
|
|
{ |
182
|
|
|
$this->assertEquals('London', $this->page->get('city')); |
183
|
|
|
$this->assertEquals('Cologne', $this->page->get('city', 9)); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Not sure if this test is really needed, but it's here anyway. |
188
|
|
|
* @covers SlotMachine\SlotMachine::get |
189
|
|
|
*/ |
190
|
|
|
public function testGetReturnsUtf8() |
191
|
|
|
{ |
192
|
|
|
// Cyrillic |
193
|
|
|
$this->assertEquals('Москва', $this->page->get('city_l10n', 8)); |
194
|
|
|
|
195
|
|
|
// Arabic |
196
|
|
|
$this->assertEquals('القاهرة', $this->page->get('city_l10n', 4)); |
197
|
|
|
|
198
|
|
|
// Chinese |
199
|
|
|
$this->assertEquals('上海', $this->page->get('city_l10n', 1)); |
200
|
|
|
|
201
|
|
|
// Japanese |
202
|
|
|
$this->assertEquals('東京', $this->page->get('city_l10n', 3)); |
203
|
|
|
|
204
|
|
|
// Thai |
205
|
|
|
$this->assertEquals('กรุงเทพมหานคร', $this->page->get('city_l10n', 6)); |
206
|
|
|
|
207
|
|
|
// Latin Extended |
208
|
|
|
$this->assertEquals('Köln', $this->page->get('city_l10n', 9)); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @covers SlotMachine\SlotMachine::initialize |
213
|
|
|
*/ |
214
|
|
|
public function testWithNestedSlots() |
215
|
|
|
{ |
216
|
|
|
$slots = new SlotMachine(self::$slotsConfig, Request::create('?uid=7&h=3')); |
217
|
|
|
$this->assertEquals('Welcome back, Stan!', $slots->get('headline')); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @covers SlotMachine\SlotMachine::initialize |
222
|
|
|
*/ |
223
|
|
|
public function testWithNestedSlotsAndArrayParameters() |
224
|
|
|
{ |
225
|
|
|
$slots = new SlotMachine(self::$slotsConfig, Request::create('?app_data[uid]=6&app_data[h]=4')); |
226
|
|
|
$this->assertEquals('See you again, Lois!', $slots->get('headline')); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @covers SlotMachine\SlotMachine::initialize |
231
|
|
|
*/ |
232
|
|
|
public function testWithNestedSlotsAndCustomDefaults() |
233
|
|
|
{ |
234
|
|
|
$this->assertEquals('<img src="penguin.png" alt="Featured Image" />', $this->page->get('featured_image_html')); |
235
|
|
|
|
236
|
|
|
$slots = new SlotMachine(self::$slotsConfig, Request::create('?app_data[i]=6')); |
237
|
|
|
$this->assertEquals('<img src="tiger.png" alt="Featured Image" />', $slots->get('featured_image_html')); |
238
|
|
|
|
239
|
|
|
$slots = new SlotMachine(self::$slotsConfig, Request::create('?app_data[i]=6&app_data[ih]=0')); |
240
|
|
|
$this->assertEquals('<img src="tiger.png" />', $slots->get('featured_image_html')); |
241
|
|
|
|
242
|
|
|
$slots = new SlotMachine(self::$slotsConfig, Request::create('?app_data[i]=6&app_data[ih]=43')); |
243
|
|
|
$this->assertEquals('<img src="tiger.png" alt="Featured Image" />', $slots->get('featured_image_html')); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @covers SlotMachine\SlotMachine::initialize |
248
|
|
|
*/ |
249
|
|
|
public function testInitialize() |
250
|
|
|
{ |
251
|
|
|
// Test by calling getCard directly on the Slot injected into the container. |
252
|
|
|
// That way we know that it has been setup. |
253
|
|
|
$this->assertEquals('Howdy, stranger. Please take a moment to register.', $this->page['headline']->getCard()); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @covers SlotMachine\SlotMachine::getConfig |
258
|
|
|
*/ |
259
|
|
|
public function testGetConfig() |
260
|
|
|
{ |
261
|
|
|
$this->assertTrue(is_array($this->page->getConfig())); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @covers SlotMachine\SlotMachine::getRequest |
266
|
|
|
*/ |
267
|
|
|
public function testGetRequest() |
268
|
|
|
{ |
269
|
|
|
$this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\Request', $this->page->getRequest()); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @covers SlotMachine\SlotMachine::setRequest |
274
|
|
|
*/ |
275
|
|
|
public function testSetRequest() |
276
|
|
|
{ |
277
|
|
|
$c = clone $this->page; |
278
|
|
|
$c->setRequest(Request::create('?msg=hello')); |
279
|
|
|
$this->assertEquals('hello', $c->getRequest()->query->get('msg')); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @covers SlotMachine\SlotMachine::interpolate |
284
|
|
|
*/ |
285
|
|
|
public function testInterpolate() |
286
|
|
|
{ |
287
|
|
|
$card = 'I used to {verb} {article} {noun}, but then I took an arrow to the knee.'; |
288
|
|
|
$interpolated = SlotMachine::interpolate($card, array( |
289
|
|
|
'verb' => 'be', |
290
|
|
|
'article' => 'an', |
291
|
|
|
'noun' => 'adventurer' |
292
|
|
|
)); |
293
|
|
|
|
294
|
|
|
$this->assertEquals('I used to be an adventurer, but then I took an arrow to the knee.', $interpolated); |
295
|
|
|
|
296
|
|
|
// try with custom delimiters |
297
|
|
|
$card = 'I used to %verb% %article% %noun%, but then I took an arrow to the knee.'; |
298
|
|
|
$interpolated = SlotMachine::interpolate($card, array( |
299
|
|
|
'verb' => 'listen', |
300
|
|
|
'article' => 'to', |
301
|
|
|
'noun' => 'dubstep' |
302
|
|
|
), array('%', '%')); |
303
|
|
|
|
304
|
|
|
$this->assertEquals('I used to listen to dubstep, but then I took an arrow to the knee.', $interpolated); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* @covers SlotMachine\SlotMachine::interpolate |
309
|
|
|
* @expectedException LengthException |
310
|
|
|
*/ |
311
|
|
|
public function testInterpolateThrowsException() |
312
|
|
|
{ |
313
|
|
|
$this->setExpectedException('LengthException'); |
314
|
|
|
|
315
|
|
|
$card = 'Yo <target>, I\'m real happy for you, Imma let you finish, but <subject> is one of the best <product> of all time!'; |
316
|
|
|
$interpolated = SlotMachine::interpolate($card, array( |
|
|
|
|
317
|
|
|
'target' => 'Zend', |
318
|
|
|
'subject' => 'Symfony', |
319
|
|
|
'product' => 'PHP frameworks' |
320
|
|
|
), array('<')); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* @covers SlotMachine\SlotMachine::interpolate |
325
|
|
|
* @expectedException PHPUnit_Framework_Error |
326
|
|
|
*/ |
327
|
|
|
public function testInterpolateEmitsWarning() |
328
|
|
|
{ |
329
|
|
|
$card = '"<quote>", said no one ever!'; |
330
|
|
|
|
331
|
|
|
$interpolated = SlotMachine::interpolate($card, array( |
|
|
|
|
332
|
|
|
'quote' => 'PHP is a solid language', |
333
|
|
|
), array('<', '>', '*')); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* @covers SlotMachine\SlotMachine::interpolate |
338
|
|
|
*/ |
339
|
|
|
public function testInterpolateWhileEmittingWarning() |
340
|
|
|
{ |
341
|
|
|
$card = '"<quote>", said no one ever!'; |
342
|
|
|
|
343
|
|
|
$interpolated = @SlotMachine::interpolate($card, array( |
344
|
|
|
'quote' => "I won't stay longer than 4 hours in Starbucks for I need to be elsewhere", |
345
|
|
|
), array('<', '>', '*')); |
346
|
|
|
|
347
|
|
|
$this->assertEquals( |
348
|
|
|
"\"I won't stay longer than 4 hours in Starbucks for I need to be elsewhere\", said no one ever!", |
349
|
|
|
$interpolated |
350
|
|
|
); |
351
|
|
|
} |
352
|
|
|
} |
353
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.