Open Journal Systems  3.3.0
CollectionTest.php
1 <?php
2 
3 namespace Guzzle\Tests\Common;
4 
8 
13 {
15  protected $coll;
16 
17  protected function setUp()
18  {
19  $this->coll = new Collection();
20  }
21 
23  {
24  $this->coll = new Collection();
25  $p = $this->coll->getAll();
26  $this->assertEmpty($p, '-> Collection must be empty when no data is passed');
27  }
28 
29  public function testConstructorCanBeCalledWithParams()
30  {
31  $testData = array(
32  'test' => 'value',
33  'test_2' => 'value2'
34  );
35  $this->coll = new Collection($testData);
36  $this->assertEquals($this->coll->getAll(), $testData, '-> getAll() must return the data passed in the constructor');
37  $this->assertEquals($this->coll->getAll(), $this->coll->toArray());
38  }
39 
40  public function testImplementsIteratorAggregate()
41  {
42  $this->coll->set('key', 'value');
43  $this->assertInstanceOf('ArrayIterator', $this->coll->getIterator());
44  $this->assertEquals(1, count($this->coll));
45  $total = 0;
46  foreach ($this->coll as $key => $value) {
47  $this->assertEquals('key', $key);
48  $this->assertEquals('value', $value);
49  $total++;
50  }
51  $this->assertEquals(1, $total);
52  }
53 
55  {
56  $this->coll->add('test', 'value1');
57  $this->assertEquals($this->coll->getAll(), array('test' => 'value1'));
58  $this->coll->add('test', 'value2');
59  $this->assertEquals($this->coll->getAll(), array('test' => array('value1', 'value2')));
60  $this->coll->add('test', 'value3');
61  $this->assertEquals($this->coll->getAll(), array('test' => array('value1', 'value2', 'value3')));
62  }
63 
65  {
66  $params = array(
67  'test' => 'value1',
68  'test2' => 'value2',
69  'test3' => array('value3', 'value4')
70  );
71  $this->coll->merge($params);
72  $this->assertEquals($this->coll->getAll(), $params);
73 
74  // Pass the same object to itself
75  $this->assertEquals($this->coll->merge($this->coll), $this->coll);
76  }
77 
78  public function testCanClearAllDataOrSpecificKeys()
79  {
80  $this->coll->merge(array(
81  'test' => 'value1',
82  'test2' => 'value2'
83  ));
84 
85  // Clear a specific parameter by name
86  $this->coll->remove('test');
87 
88  $this->assertEquals($this->coll->getAll(), array(
89  'test2' => 'value2'
90  ));
91 
92  // Clear all parameters
93  $this->coll->clear();
94 
95  $this->assertEquals($this->coll->getAll(), array());
96  }
97 
98  public function testGetsValuesByKey()
99  {
100  $this->assertNull($this->coll->get('test'));
101  $this->coll->add('test', 'value');
102  $this->assertEquals('value', $this->coll->get('test'));
103  $this->coll->set('test2', 'v2');
104  $this->coll->set('test3', 'v3');
105  $this->assertEquals(array(
106  'test' => 'value',
107  'test2' => 'v2'
108  ), $this->coll->getAll(array('test', 'test2')));
109  }
110 
111  public function testProvidesKeys()
112  {
113  $this->assertEquals(array(), $this->coll->getKeys());
114  $this->coll->merge(array(
115  'test1' => 'value1',
116  'test2' => 'value2'
117  ));
118  $this->assertEquals(array('test1', 'test2'), $this->coll->getKeys());
119  // Returns the cached array previously returned
120  $this->assertEquals(array('test1', 'test2'), $this->coll->getKeys());
121  $this->coll->remove('test1');
122  $this->assertEquals(array('test2'), $this->coll->getKeys());
123  $this->coll->add('test3', 'value3');
124  $this->assertEquals(array('test2', 'test3'), $this->coll->getKeys());
125  }
126 
127  public function testChecksIfHasKey()
128  {
129  $this->assertFalse($this->coll->hasKey('test'));
130  $this->coll->add('test', 'value');
131  $this->assertEquals(true, $this->coll->hasKey('test'));
132  $this->coll->add('test2', 'value2');
133  $this->assertEquals(true, $this->coll->hasKey('test'));
134  $this->assertEquals(true, $this->coll->hasKey('test2'));
135  $this->assertFalse($this->coll->hasKey('testing'));
136  $this->assertEquals(false, $this->coll->hasKey('AB-C', 'junk'));
137  }
138 
139  public function testChecksIfHasValue()
140  {
141  $this->assertFalse($this->coll->hasValue('value'));
142  $this->coll->add('test', 'value');
143  $this->assertEquals('test', $this->coll->hasValue('value'));
144  $this->coll->add('test2', 'value2');
145  $this->assertEquals('test', $this->coll->hasValue('value'));
146  $this->assertEquals('test2', $this->coll->hasValue('value2'));
147  $this->assertFalse($this->coll->hasValue('val'));
148  }
149 
150  public function testCanGetAllValuesByArray()
151  {
152  $this->coll->add('foo', 'bar');
153  $this->coll->add('tEsT', 'value');
154  $this->coll->add('tesTing', 'v2');
155  $this->coll->add('key', 'v3');
156  $this->assertNull($this->coll->get('test'));
157  $this->assertEquals(array(
158  'foo' => 'bar',
159  'tEsT' => 'value',
160  'tesTing' => 'v2'
161  ), $this->coll->getAll(array(
162  'foo', 'tesTing', 'tEsT'
163  )));
164  }
165 
166  public function testImplementsCount()
167  {
168  $data = new Collection();
169  $this->assertEquals(0, $data->count());
170  $data->add('key', 'value');
171  $this->assertEquals(1, count($data));
172  $data->add('key', 'value2');
173  $this->assertEquals(1, count($data));
174  $data->add('key_2', 'value3');
175  $this->assertEquals(2, count($data));
176  }
177 
178  public function testAddParamsByMerging()
179  {
180  $params = array(
181  'test' => 'value1',
182  'test2' => 'value2',
183  'test3' => array('value3', 'value4')
184  );
185 
186  // Add some parameters
187  $this->coll->merge($params);
188 
189  // Add more parameters by merging them in
190  $this->coll->merge(array(
191  'test' => 'another',
192  'different_key' => 'new value'
193  ));
194 
195  $this->assertEquals(array(
196  'test' => array('value1', 'another'),
197  'test2' => 'value2',
198  'test3' => array('value3', 'value4'),
199  'different_key' => 'new value'
200  ), $this->coll->getAll());
201  }
202 
203  public function testAllowsFunctionalFilter()
204  {
205  $this->coll->merge(array(
206  'fruit' => 'apple',
207  'number' => 'ten',
208  'prepositions' => array('about', 'above', 'across', 'after'),
209  'same_number' => 'ten'
210  ));
211 
212  $filtered = $this->coll->filter(function($key, $value) {
213  return $value == 'ten';
214  });
215 
216  $this->assertNotEquals($filtered, $this->coll);
217 
218  $this->assertEquals(array(
219  'number' => 'ten',
220  'same_number' => 'ten'
221  ), $filtered->getAll());
222  }
223 
224  public function testAllowsFunctionalMapping()
225  {
226  $this->coll->merge(array(
227  'number_1' => 1,
228  'number_2' => 2,
229  'number_3' => 3
230  ));
231 
232  $mapped = $this->coll->map(function($key, $value) {
233  return $value * $value;
234  });
235 
236  $this->assertNotEquals($mapped, $this->coll);
237 
238  $this->assertEquals(array(
239  'number_1' => 1,
240  'number_2' => 4,
241  'number_3' => 9
242  ), $mapped->getAll());
243  }
244 
245  public function testImplementsArrayAccess()
246  {
247  $this->coll->merge(array(
248  'k1' => 'v1',
249  'k2' => 'v2'
250  ));
251 
252  $this->assertTrue($this->coll->offsetExists('k1'));
253  $this->assertFalse($this->coll->offsetExists('Krull'));
254 
255  $this->coll->offsetSet('k3', 'v3');
256  $this->assertEquals('v3', $this->coll->offsetGet('k3'));
257  $this->assertEquals('v3', $this->coll->get('k3'));
258 
259  $this->coll->offsetUnset('k1');
260  $this->assertFalse($this->coll->offsetExists('k1'));
261  }
262 
263  public function testUsesStaticWhenCreatingNew()
264  {
265  $qs = new QueryString(array(
266  'a' => 'b',
267  'c' => 'd'
268  ));
269 
270  $this->assertInstanceOf('Guzzle\\Http\\QueryString', $qs->map(function($a, $b) {}));
271  $this->assertInstanceOf('Guzzle\\Common\\Collection', $qs->map(function($a, $b) {}, array(), false));
272 
273  $this->assertInstanceOf('Guzzle\\Http\\QueryString', $qs->filter(function($a, $b) {}));
274  $this->assertInstanceOf('Guzzle\\Common\\Collection', $qs->filter(function($a, $b) {}, false));
275  }
276 
277  public function testCanReplaceAllData()
278  {
279  $this->assertSame($this->coll, $this->coll->replace(array(
280  'a' => '123'
281  )));
282 
283  $this->assertEquals(array(
284  'a' => '123'
285  ), $this->coll->getAll());
286  }
287 
288  public function dataProvider()
289  {
290  return array(
291  array('this_is_a_test', '{a}_is_a_{b}', array(
292  'a' => 'this',
293  'b' => 'test'
294  )),
295  array('this_is_a_test', '{abc}_is_a_{0}', array(
296  'abc' => 'this',
297  0 => 'test'
298  )),
299  array('this_is_a_test', '{abc}_is_a_{0}', array(
300  'abc' => 'this',
301  0 => 'test'
302  )),
303  array('this_is_a_test', 'this_is_a_test', array(
304  'abc' => 'this'
305  )),
306  array('{abc}_is_{not_found}a_{0}', '{abc}_is_{not_found}a_{0}', array())
307  );
308  }
309 
313  public function testInjectsConfigData($output, $input, $config)
314  {
315  $collection = new Collection($config);
316  $this->assertEquals($output, $collection->inject($input));
317  }
318 
319  public function testCanSearchByKey()
320  {
321  $collection = new Collection(array(
322  'foo' => 'bar',
323  'BaZ' => 'pho'
324  ));
325 
326  $this->assertEquals('foo', $collection->keySearch('FOO'));
327  $this->assertEquals('BaZ', $collection->keySearch('baz'));
328  $this->assertEquals(false, $collection->keySearch('Bar'));
329  }
330 
331  public function testPreparesFromConfig()
332  {
333  $c = Collection::fromConfig(array(
334  'a' => '123',
335  'base_url' => 'http://www.test.com/'
336  ), array(
337  'a' => 'xyz',
338  'b' => 'lol'
339  ), array('a'));
340 
341  $this->assertInstanceOf('Guzzle\Common\Collection', $c);
342  $this->assertEquals(array(
343  'a' => '123',
344  'b' => 'lol',
345  'base_url' => 'http://www.test.com/'
346  ), $c->getAll());
347 
348  try {
349  $c = Collection::fromConfig(array(), array(), array('a'));
350  $this->fail('Exception not throw when missing config');
351  } catch (InvalidArgumentException $e) {
352  }
353  }
354 
355  function falseyDataProvider()
356  {
357  return array(
358  array(false, false),
359  array(null, null),
360  array('', ''),
361  array(array(), array()),
362  array(0, 0),
363  );
364  }
365 
369  public function testReturnsCorrectData($a, $b)
370  {
371  $c = new Collection(array('value' => $a));
372  $this->assertSame($b, $c->get('value'));
373  }
374 
375  public function testRetrievesNestedKeysUsingPath()
376  {
377  $data = array(
378  'foo' => 'bar',
379  'baz' => array(
380  'mesa' => array(
381  'jar' => 'jar'
382  )
383  )
384  );
385  $collection = new Collection($data);
386  $this->assertEquals('bar', $collection->getPath('foo'));
387  $this->assertEquals('jar', $collection->getPath('baz/mesa/jar'));
388  $this->assertNull($collection->getPath('wewewf'));
389  $this->assertNull($collection->getPath('baz/mesa/jar/jar'));
390  }
391 
392  public function testFalseyKeysStillDescend()
393  {
394  $collection = new Collection(array(
395  '0' => array(
396  'a' => 'jar'
397  ),
398  1 => 'other'
399  ));
400  $this->assertEquals('jar', $collection->getPath('0/a'));
401  $this->assertEquals('other', $collection->getPath('1'));
402  }
403 
404  public function getPathProvider()
405  {
406  $data = array(
407  'foo' => 'bar',
408  'baz' => array(
409  'mesa' => array(
410  'jar' => 'jar',
411  'array' => array('a', 'b', 'c')
412  ),
413  'bar' => array(
414  'baz' => 'bam',
415  'array' => array('d', 'e', 'f')
416  )
417  ),
418  'bam' => array(
419  array('foo' => 1),
420  array('foo' => 2),
421  array('array' => array('h', 'i'))
422  )
423  );
424  $c = new Collection($data);
425 
426  return array(
427  // Simple path selectors
428  array($c, 'foo', 'bar'),
429  array($c, 'baz', $data['baz']),
430  array($c, 'bam', $data['bam']),
431  array($c, 'baz/mesa', $data['baz']['mesa']),
432  array($c, 'baz/mesa/jar', 'jar'),
433  // Merge everything two levels under baz
434  array($c, 'baz/*', array(
435  'jar' => 'jar',
436  'array' => array_merge($data['baz']['mesa']['array'], $data['baz']['bar']['array']),
437  'baz' => 'bam'
438  )),
439  // Does not barf on missing keys
440  array($c, 'fefwfw', null),
441  // Does not barf when a wildcard does not resolve correctly
442  array($c, '*/*/*/*/*/wefwfe', array()),
443  // Allows custom separator
444  array($c, '*|mesa', $data['baz']['mesa'], '|'),
445  // Merge all 'array' keys two levels under baz (the trailing * does not hurt the results)
446  array($c, 'baz/*/array/*', array_merge($data['baz']['mesa']['array'], $data['baz']['bar']['array'])),
447  // Merge all 'array' keys two levels under baz
448  array($c, 'baz/*/array', array_merge($data['baz']['mesa']['array'], $data['baz']['bar']['array'])),
449  array($c, 'baz/mesa/array', $data['baz']['mesa']['array']),
450  // Having a trailing * does not hurt the results
451  array($c, 'baz/mesa/array/*', $data['baz']['mesa']['array']),
452  // Merge of anything one level deep
453  array($c, '*', array_merge(array('bar'), $data['baz'], $data['bam'])),
454  // Funky merge of anything two levels deep
455  array($c, '*/*', array(
456  'jar' => 'jar',
457  'array' => array('a', 'b', 'c', 'd', 'e', 'f', 'h', 'i'),
458  'baz' => 'bam',
459  'foo' => array(1, 2)
460  )),
461  // Funky merge of all 'array' keys that are two levels deep
462  array($c, '*/*/array', array('a', 'b', 'c', 'd', 'e', 'f', 'h', 'i'))
463  );
464  }
465 
469  public function testGetPath(Collection $c, $path, $expected, $separator = '/')
470  {
471  $this->assertEquals($expected, $c->getPath($path, $separator));
472  }
473 
474  public function testOverridesSettings()
475  {
476  $c = new Collection(array('foo' => 1, 'baz' => 2, 'bar' => 3));
477  $c->overwriteWith(array('foo' => 10, 'bar' => 300));
478  $this->assertEquals(array('foo' => 10, 'baz' => 2, 'bar' => 300), $c->getAll());
479  }
480 
481  public function testOverwriteWithCollection()
482  {
483  $c = new Collection(array('foo' => 1, 'baz' => 2, 'bar' => 3));
484  $b = new Collection(array('foo' => 10, 'bar' => 300));
485  $c->overwriteWith($b);
486  $this->assertEquals(array('foo' => 10, 'baz' => 2, 'bar' => 300), $c->getAll());
487  }
488 
489  public function testOverwriteWithTraversable()
490  {
491  $c = new Collection(array('foo' => 1, 'baz' => 2, 'bar' => 3));
492  $b = new Collection(array('foo' => 10, 'bar' => 300));
493  $c->overwriteWith($b->getIterator());
494  $this->assertEquals(array('foo' => 10, 'baz' => 2, 'bar' => 300), $c->getAll());
495  }
496 
498  {
499  $c = new Collection(array());
500  $c->setPath('foo/bar/baz/123', 'hi');
501  $this->assertEquals('hi', $c['foo']['bar']['baz']['123']);
502  }
503 
504  public function testCanSetNestedPathValueThatExists()
505  {
506  $c = new Collection(array('foo' => array('bar' => 'test')));
507  $c->setPath('foo/bar', 'hi');
508  $this->assertEquals('hi', $c['foo']['bar']);
509  }
510 
515  {
516  $c = new Collection(array('foo' => 'bar'));
517  $c->setPath('foo/bar', 'hi');
518  $this->assertEquals('hi', $c['foo']['bar']);
519  }
520 
525  {
526  $c = new Collection(array('foo' => 'bar'));
527  $c->setPath('foo/bar/baz', 'test');
528  }
529 }
Guzzle\Tests\Common\CollectionTest\testOverridesSettings
testOverridesSettings()
Definition: CollectionTest.php:477
Guzzle\Tests\Common\CollectionTest\testPreparesFromConfig
testPreparesFromConfig()
Definition: CollectionTest.php:334
Guzzle\Tests\Common\CollectionTest\testAddParamsByMerging
testAddParamsByMerging()
Definition: CollectionTest.php:181
Guzzle\Tests\Common\CollectionTest\testAllowsFunctionalMapping
testAllowsFunctionalMapping()
Definition: CollectionTest.php:227
Guzzle\Tests\GuzzleTestCase
Definition: GuzzleTestCase.php:22
Guzzle\Http\QueryString
Definition: QueryString.php:14
Guzzle\Tests\Common\CollectionTest\testInjectsConfigData
testInjectsConfigData($output, $input, $config)
Definition: CollectionTest.php:316
Guzzle\Tests\Common\CollectionTest\getPathProvider
getPathProvider()
Definition: CollectionTest.php:407
Guzzle\Tests\Common\CollectionTest\testReturnsCorrectData
testReturnsCorrectData($a, $b)
Definition: CollectionTest.php:372
Guzzle\Tests\Common\CollectionTest\testOverwriteWithCollection
testOverwriteWithCollection()
Definition: CollectionTest.php:484
Guzzle\Tests\Common\CollectionTest\testImplementsArrayAccess
testImplementsArrayAccess()
Definition: CollectionTest.php:248
Guzzle\Tests\Common\CollectionTest\testConstructorCanBeCalledWithNoParams
testConstructorCanBeCalledWithNoParams()
Definition: CollectionTest.php:25
Guzzle\Tests\Common\CollectionTest\testGetPath
testGetPath(Collection $c, $path, $expected, $separator='/')
Definition: CollectionTest.php:472
Guzzle\Tests\Common\CollectionTest\testHandlesMergingInDisparateDataSources
testHandlesMergingInDisparateDataSources()
Definition: CollectionTest.php:67
Guzzle\Tests\Common\CollectionTest\testRetrievesNestedKeysUsingPath
testRetrievesNestedKeysUsingPath()
Definition: CollectionTest.php:378
Guzzle\Tests\Common\CollectionTest\testProvidesKeys
testProvidesKeys()
Definition: CollectionTest.php:114
Guzzle\Tests\Common\CollectionTest\testCanSetNestedPathValueThatExists
testCanSetNestedPathValueThatExists()
Definition: CollectionTest.php:507
Guzzle\Tests\Common\CollectionTest\testVerifiesNestedPathIsValidAtExactLevel
testVerifiesNestedPathIsValidAtExactLevel()
Definition: CollectionTest.php:517
Guzzle\Tests\Common\CollectionTest\testOverwriteWithTraversable
testOverwriteWithTraversable()
Definition: CollectionTest.php:492
Guzzle\Tests\Common\CollectionTest\testGetsValuesByKey
testGetsValuesByKey()
Definition: CollectionTest.php:101
Guzzle\Common\Exception\InvalidArgumentException
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Common/Exception/InvalidArgumentException.php:5
Guzzle\Tests\Common\CollectionTest\testFalseyKeysStillDescend
testFalseyKeysStillDescend()
Definition: CollectionTest.php:395
Guzzle\Tests\Common\CollectionTest\testVerifiesThatNestedPathIsValidAtAnyLevel
testVerifiesThatNestedPathIsValidAtAnyLevel()
Definition: CollectionTest.php:527
Guzzle\Tests\Common\CollectionTest\testUsesStaticWhenCreatingNew
testUsesStaticWhenCreatingNew()
Definition: CollectionTest.php:266
Guzzle\Tests\Common\CollectionTest\testCanGetAllValuesByArray
testCanGetAllValuesByArray()
Definition: CollectionTest.php:153
Guzzle\Tests\Common\CollectionTest\dataProvider
dataProvider()
Definition: CollectionTest.php:291
Guzzle\Tests\Common
Guzzle\Tests\Common\CollectionTest\testImplementsCount
testImplementsCount()
Definition: CollectionTest.php:169
Guzzle\Tests\Common\CollectionTest\$coll
$coll
Definition: CollectionTest.php:18
Guzzle\Tests\Common\CollectionTest\testImplementsIteratorAggregate
testImplementsIteratorAggregate()
Definition: CollectionTest.php:43
Guzzle\Tests\Common\CollectionTest\testCanClearAllDataOrSpecificKeys
testCanClearAllDataOrSpecificKeys()
Definition: CollectionTest.php:81
Guzzle\Tests\Common\CollectionTest\testChecksIfHasKey
testChecksIfHasKey()
Definition: CollectionTest.php:130
Guzzle\Tests\Common\CollectionTest
Definition: CollectionTest.php:12
Guzzle\Tests\Common\CollectionTest\testConstructorCanBeCalledWithParams
testConstructorCanBeCalledWithParams()
Definition: CollectionTest.php:32
Guzzle\Tests\Common\CollectionTest\testAllowsFunctionalFilter
testAllowsFunctionalFilter()
Definition: CollectionTest.php:206
Guzzle\Tests\Common\CollectionTest\testChecksIfHasValue
testChecksIfHasValue()
Definition: CollectionTest.php:142
Guzzle\Tests\Common\CollectionTest\falseyDataProvider
falseyDataProvider()
Definition: CollectionTest.php:358
Guzzle\Common\Collection\fromConfig
static fromConfig(array $config=array(), array $defaults=array(), array $required=array())
Definition: paymethod/paypal/lib/vendor/guzzle/guzzle/src/Guzzle/Common/Collection.php:37
Guzzle\Tests\Common\CollectionTest\testCanReplaceAllData
testCanReplaceAllData()
Definition: CollectionTest.php:280
Guzzle\Tests\Common\CollectionTest\testCanSetNestedPathValueThatDoesNotExist
testCanSetNestedPathValueThatDoesNotExist()
Definition: CollectionTest.php:500
Guzzle\Tests\Common\CollectionTest\setUp
setUp()
Definition: CollectionTest.php:20
Guzzle\Common\Collection
Definition: paymethod/paypal/lib/vendor/guzzle/guzzle/src/Guzzle/Common/Collection.php:11
Guzzle\Tests\Common\CollectionTest\testCanSearchByKey
testCanSearchByKey()
Definition: CollectionTest.php:322
Guzzle\Tests\Common\CollectionTest\testCanAddValuesToExistingKeysByUsingArray
testCanAddValuesToExistingKeysByUsingArray()
Definition: CollectionTest.php:57