scratch – Blame information for rev 87

Subversion Repositories:
Rev:
Rev Author Line No. Line
87 office 1 <?php
2  
3 namespace GuzzleHttp\Tests;
4  
5 use GuzzleHttp\Collection;
6  
7 class CollectionTest extends \PHPUnit_Framework_TestCase
8 {
9 /** @var Collection */
10 protected $coll;
11  
12 protected function setUp()
13 {
14 $this->coll = new Collection();
15 }
16  
17 public function testConstructorCanBeCalledWithNoParams()
18 {
19 $this->coll = new Collection();
20 $p = $this->coll->toArray();
21 $this->assertEmpty($p, '-> Collection must be empty when no data is passed');
22 }
23  
24 public function testConstructorCanBeCalledWithParams()
25 {
26 $testData = array(
27 'test' => 'value',
28 'test_2' => 'value2'
29 );
30 $this->coll = new Collection($testData);
31 $this->assertEquals($this->coll->toArray(), $testData);
32 $this->assertEquals($this->coll->toArray(), $this->coll->toArray());
33 }
34  
35 public function testImplementsIteratorAggregate()
36 {
37 $this->coll->set('key', 'value');
38 $this->assertInstanceOf('ArrayIterator', $this->coll->getIterator());
39 $this->assertEquals(1, count($this->coll));
40 $total = 0;
41 foreach ($this->coll as $key => $value) {
42 $this->assertEquals('key', $key);
43 $this->assertEquals('value', $value);
44 $total++;
45 }
46 $this->assertEquals(1, $total);
47 }
48  
49 public function testCanAddValuesToExistingKeysByUsingArray()
50 {
51 $this->coll->add('test', 'value1');
52 $this->assertEquals($this->coll->toArray(), array('test' => 'value1'));
53 $this->coll->add('test', 'value2');
54 $this->assertEquals($this->coll->toArray(), array('test' => array('value1', 'value2')));
55 $this->coll->add('test', 'value3');
56 $this->assertEquals($this->coll->toArray(), array('test' => array('value1', 'value2', 'value3')));
57 }
58  
59 public function testHandlesMergingInDisparateDataSources()
60 {
61 $params = array(
62 'test' => 'value1',
63 'test2' => 'value2',
64 'test3' => array('value3', 'value4')
65 );
66 $this->coll->merge($params);
67 $this->assertEquals($this->coll->toArray(), $params);
68  
69 // Pass the same object to itself
70 $this->assertEquals($this->coll->merge($this->coll), $this->coll);
71 }
72  
73 public function testCanClearAllDataOrSpecificKeys()
74 {
75 $this->coll->merge(array(
76 'test' => 'value1',
77 'test2' => 'value2'
78 ));
79  
80 // Clear a specific parameter by name
81 $this->coll->remove('test');
82  
83 $this->assertEquals($this->coll->toArray(), array(
84 'test2' => 'value2'
85 ));
86  
87 // Clear all parameters
88 $this->coll->clear();
89  
90 $this->assertEquals($this->coll->toArray(), array());
91 }
92  
93 public function testProvidesKeys()
94 {
95 $this->assertEquals(array(), $this->coll->getKeys());
96 $this->coll->merge(array(
97 'test1' => 'value1',
98 'test2' => 'value2'
99 ));
100 $this->assertEquals(array('test1', 'test2'), $this->coll->getKeys());
101 // Returns the cached array previously returned
102 $this->assertEquals(array('test1', 'test2'), $this->coll->getKeys());
103 $this->coll->remove('test1');
104 $this->assertEquals(array('test2'), $this->coll->getKeys());
105 $this->coll->add('test3', 'value3');
106 $this->assertEquals(array('test2', 'test3'), $this->coll->getKeys());
107 }
108  
109 public function testChecksIfHasKey()
110 {
111 $this->assertFalse($this->coll->hasKey('test'));
112 $this->coll->add('test', 'value');
113 $this->assertEquals(true, $this->coll->hasKey('test'));
114 $this->coll->add('test2', 'value2');
115 $this->assertEquals(true, $this->coll->hasKey('test'));
116 $this->assertEquals(true, $this->coll->hasKey('test2'));
117 $this->assertFalse($this->coll->hasKey('testing'));
118 $this->assertEquals(false, $this->coll->hasKey('AB-C', 'junk'));
119 }
120  
121 public function testChecksIfHasValue()
122 {
123 $this->assertFalse($this->coll->hasValue('value'));
124 $this->coll->add('test', 'value');
125 $this->assertEquals('test', $this->coll->hasValue('value'));
126 $this->coll->add('test2', 'value2');
127 $this->assertEquals('test', $this->coll->hasValue('value'));
128 $this->assertEquals('test2', $this->coll->hasValue('value2'));
129 $this->assertFalse($this->coll->hasValue('val'));
130 }
131  
132 public function testImplementsCount()
133 {
134 $data = new Collection();
135 $this->assertEquals(0, $data->count());
136 $data->add('key', 'value');
137 $this->assertEquals(1, count($data));
138 $data->add('key', 'value2');
139 $this->assertEquals(1, count($data));
140 $data->add('key_2', 'value3');
141 $this->assertEquals(2, count($data));
142 }
143  
144 public function testAddParamsByMerging()
145 {
146 $params = array(
147 'test' => 'value1',
148 'test2' => 'value2',
149 'test3' => array('value3', 'value4')
150 );
151  
152 // Add some parameters
153 $this->coll->merge($params);
154  
155 // Add more parameters by merging them in
156 $this->coll->merge(array(
157 'test' => 'another',
158 'different_key' => 'new value'
159 ));
160  
161 $this->assertEquals(array(
162 'test' => array('value1', 'another'),
163 'test2' => 'value2',
164 'test3' => array('value3', 'value4'),
165 'different_key' => 'new value'
166 ), $this->coll->toArray());
167 }
168  
169 public function testAllowsFunctionalFilter()
170 {
171 $this->coll->merge(array(
172 'fruit' => 'apple',
173 'number' => 'ten',
174 'prepositions' => array('about', 'above', 'across', 'after'),
175 'same_number' => 'ten'
176 ));
177  
178 $filtered = $this->coll->filter(function ($key, $value) {
179 return $value == 'ten';
180 });
181  
182 $this->assertNotSame($filtered, $this->coll);
183  
184 $this->assertEquals(array(
185 'number' => 'ten',
186 'same_number' => 'ten'
187 ), $filtered->toArray());
188 }
189  
190 public function testAllowsFunctionalMapping()
191 {
192 $this->coll->merge(array(
193 'number_1' => 1,
194 'number_2' => 2,
195 'number_3' => 3
196 ));
197  
198 $mapped = $this->coll->map(function ($key, $value) {
199 return $value * $value;
200 });
201  
202 $this->assertNotSame($mapped, $this->coll);
203  
204 $this->assertEquals(array(
205 'number_1' => 1,
206 'number_2' => 4,
207 'number_3' => 9
208 ), $mapped->toArray());
209 }
210  
211 public function testImplementsArrayAccess()
212 {
213 $this->coll->merge(array(
214 'k1' => 'v1',
215 'k2' => 'v2'
216 ));
217  
218 $this->assertTrue($this->coll->offsetExists('k1'));
219 $this->assertFalse($this->coll->offsetExists('Krull'));
220  
221 $this->coll->offsetSet('k3', 'v3');
222 $this->assertEquals('v3', $this->coll->offsetGet('k3'));
223 $this->assertEquals('v3', $this->coll->get('k3'));
224  
225 $this->coll->offsetUnset('k1');
226 $this->assertFalse($this->coll->offsetExists('k1'));
227 }
228  
229 public function testCanReplaceAllData()
230 {
231 $this->assertSame($this->coll, $this->coll->replace(array(
232 'a' => '123'
233 )));
234  
235 $this->assertEquals(array(
236 'a' => '123'
237 ), $this->coll->toArray());
238 }
239  
240 public function testPreparesFromConfig()
241 {
242 $c = Collection::fromConfig(array(
243 'a' => '123',
244 'base_url' => 'http://www.test.com/'
245 ), array(
246 'a' => 'xyz',
247 'b' => 'lol'
248 ), array('a'));
249  
250 $this->assertInstanceOf('GuzzleHttp\Collection', $c);
251 $this->assertEquals(array(
252 'a' => '123',
253 'b' => 'lol',
254 'base_url' => 'http://www.test.com/'
255 ), $c->toArray());
256  
257 try {
258 $c = Collection::fromConfig(array(), array(), array('a'));
259 $this->fail('Exception not throw when missing config');
260 } catch (\InvalidArgumentException $e) {
261 }
262 }
263  
264 function falseyDataProvider()
265 {
266 return array(
267 array(false, false),
268 array(null, null),
269 array('', ''),
270 array(array(), array()),
271 array(0, 0),
272 );
273 }
274  
275 /**
276 * @dataProvider falseyDataProvider
277 */
278 public function testReturnsCorrectData($a, $b)
279 {
280 $c = new Collection(array('value' => $a));
281 $this->assertSame($b, $c->get('value'));
282 }
283  
284 public function testRetrievesNestedKeysUsingPath()
285 {
286 $data = array(
287 'foo' => 'bar',
288 'baz' => array(
289 'mesa' => array(
290 'jar' => 'jar'
291 )
292 )
293 );
294 $collection = new Collection($data);
295 $this->assertEquals('bar', $collection->getPath('foo'));
296 $this->assertEquals('jar', $collection->getPath('baz/mesa/jar'));
297 $this->assertNull($collection->getPath('wewewf'));
298 $this->assertNull($collection->getPath('baz/mesa/jar/jar'));
299 }
300  
301 public function testFalseyKeysStillDescend()
302 {
303 $collection = new Collection(array(
304 '0' => array(
305 'a' => 'jar'
306 ),
307 1 => 'other'
308 ));
309 $this->assertEquals('jar', $collection->getPath('0/a'));
310 $this->assertEquals('other', $collection->getPath('1'));
311 }
312  
313 public function getPathProvider()
314 {
315 $data = array(
316 'foo' => 'bar',
317 'baz' => array(
318 'mesa' => array(
319 'jar' => 'jar',
320 'array' => array('a', 'b', 'c')
321 ),
322 'bar' => array(
323 'baz' => 'bam',
324 'array' => array('d', 'e', 'f')
325 )
326 ),
327 'bam' => array(
328 array('foo' => 1),
329 array('foo' => 2),
330 array('array' => array('h', 'i'))
331 )
332 );
333 $c = new Collection($data);
334  
335 return array(
336 // Simple path selectors
337 array($c, 'foo', 'bar'),
338 array($c, 'baz', $data['baz']),
339 array($c, 'bam', $data['bam']),
340 array($c, 'baz/mesa', $data['baz']['mesa']),
341 array($c, 'baz/mesa/jar', 'jar'),
342 // Does not barf on missing keys
343 array($c, 'fefwfw', null),
344 array($c, 'baz/mesa/array', $data['baz']['mesa']['array'])
345 );
346 }
347  
348 /**
349 * @dataProvider getPathProvider
350 */
351 public function testGetPath(Collection $c, $path, $expected, $separator = '/')
352 {
353 $this->assertEquals($expected, $c->getPath($path, $separator));
354 }
355  
356 public function testOverridesSettings()
357 {
358 $c = new Collection(array('foo' => 1, 'baz' => 2, 'bar' => 3));
359 $c->overwriteWith(array('foo' => 10, 'bar' => 300));
360 $this->assertEquals(array('foo' => 10, 'baz' => 2, 'bar' => 300), $c->toArray());
361 }
362  
363 public function testOverwriteWithCollection()
364 {
365 $c = new Collection(array('foo' => 1, 'baz' => 2, 'bar' => 3));
366 $b = new Collection(array('foo' => 10, 'bar' => 300));
367 $c->overwriteWith($b);
368 $this->assertEquals(array('foo' => 10, 'baz' => 2, 'bar' => 300), $c->toArray());
369 }
370  
371 public function testOverwriteWithTraversable()
372 {
373 $c = new Collection(array('foo' => 1, 'baz' => 2, 'bar' => 3));
374 $b = new Collection(array('foo' => 10, 'bar' => 300));
375 $c->overwriteWith($b->getIterator());
376 $this->assertEquals(array('foo' => 10, 'baz' => 2, 'bar' => 300), $c->toArray());
377 }
378  
379 public function testCanSetNestedPathValueThatDoesNotExist()
380 {
381 $c = new Collection(array());
382 $c->setPath('foo/bar/baz/123', 'hi');
383 $this->assertEquals('hi', $c['foo']['bar']['baz']['123']);
384 }
385  
386 public function testCanSetNestedPathValueThatExists()
387 {
388 $c = new Collection(array('foo' => array('bar' => 'test')));
389 $c->setPath('foo/bar', 'hi');
390 $this->assertEquals('hi', $c['foo']['bar']);
391 }
392  
393 /**
394 * @expectedException \RuntimeException
395 */
396 public function testVerifiesNestedPathIsValidAtExactLevel()
397 {
398 $c = new Collection(array('foo' => 'bar'));
399 $c->setPath('foo/bar', 'hi');
400 $this->assertEquals('hi', $c['foo']['bar']);
401 }
402  
403 /**
404 * @expectedException \RuntimeException
405 */
406 public function testVerifiesThatNestedPathIsValidAtAnyLevel()
407 {
408 $c = new Collection(array('foo' => 'bar'));
409 $c->setPath('foo/bar/baz', 'test');
410 }
411  
412 public function testCanAppendToNestedPathValues()
413 {
414 $c = new Collection();
415 $c->setPath('foo/bar/[]', 'a');
416 $c->setPath('foo/bar/[]', 'b');
417 $this->assertEquals(['a', 'b'], $c['foo']['bar']);
418 }
419 }