/frameworks/lithium/0.9.9/libraries/lithium/tests/cases/util/CollectionTest.php

https://github.com/ggunlugu/ornekler · PHP · 270 lines · 179 code · 43 blank · 48 comment · 2 complexity · 04351e8d99e895ceb09c422e4786fb5a MD5 · raw file

  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2010, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\cases\util;
  9. use \stdClass;
  10. use \lithium\util\Collection;
  11. use \lithium\tests\mocks\util\MockCollectionMarker;
  12. use \lithium\tests\mocks\util\MockCollectionObject;
  13. use \lithium\tests\mocks\util\MockCollectionStringCast;
  14. class CollectionTest extends \lithium\test\Unit {
  15. public function setUp() {
  16. Collection::formats('\lithium\net\http\Media');
  17. }
  18. public function testArrayLike() {
  19. $collection = new Collection();
  20. $collection[] = 'foo';
  21. $this->assertEqual($collection[0], 'foo');
  22. $this->assertEqual(count($collection), 1);
  23. $collection = new Collection(array('data' => array('foo')));
  24. $this->assertEqual($collection[0], 'foo');
  25. $this->assertEqual(count($collection), 1);
  26. }
  27. public function testObjectMethodDispatch() {
  28. $collection = new Collection();
  29. for ($i = 0; $i < 10; $i++) {
  30. $collection[] = new MockCollectionMarker();
  31. }
  32. $result = $collection->mark();
  33. $this->assertEqual($result, array_fill(0, 10, true));
  34. $result = $collection->mapArray();
  35. $this->assertEqual($result, array_fill(0, 10, array('foo')));
  36. $result = $collection->invoke('mapArray', array(), array('merge' => true));
  37. $this->assertEqual($result, array_fill(0, 10, 'foo'));
  38. $collection = new Collection(array(
  39. 'data' => array_fill(0, 10, new MockCollectionObject())
  40. ));
  41. $result = $collection->testFoo();
  42. $this->assertEqual($result, array_fill(0, 10, 'testFoo'));
  43. $result = $collection->invoke('testFoo', array(), array('collect' => true));
  44. $this->assertTrue($result instanceof Collection);
  45. $this->assertEqual($result->to('array'), array_fill(0, 10, 'testFoo'));
  46. }
  47. public function testObjectCasting() {
  48. $collection = new Collection(array(
  49. 'data' => array_fill(0, 10, new MockCollectionObject())
  50. ));
  51. $result = $collection->to('array');
  52. $expected = array_fill(0, 10, array(1 => 2, 2 => 3));
  53. $this->assertEqual($expected, $result);
  54. $collection = new Collection(array(
  55. 'data' => array_fill(0, 10, new MockCollectionMarker())
  56. ));
  57. $result = $collection->to('array');
  58. $expected = array_fill(0, 10, array('marker' => false, 'data' => 'foo'));
  59. $this->assertEqual($expected, $result);
  60. $collection = new Collection(array(
  61. 'data' => array_fill(0, 10, new MockCollectionStringCast())
  62. ));
  63. $result = $collection->to('array');
  64. $expected = array_fill(0, 10, json_encode(array(1 => 2, 2 => 3)));
  65. $this->assertEqual($expected, $result);
  66. }
  67. /**
  68. * Tests that the `find()` method properly filters items out of the resulting collection.
  69. *
  70. * @return void
  71. */
  72. public function testCollectionFindFilter() {
  73. $collection = new Collection(array('data' => array_merge(
  74. array_fill(0, 10, 1),
  75. array_fill(0, 10, 2)
  76. )));
  77. $this->assertEqual(20, count($collection->to('array')));
  78. $filter = function($item) { return $item == 1; };
  79. $result = $collection->find($filter);
  80. $this->assertTrue($result instanceof Collection);
  81. $this->assertEqual(array_fill(0, 10, 1), $result->to('array'));
  82. $result = $collection->find($filter, array('collect' => false));
  83. $this->assertEqual(array_fill(0, 10, 1), $result);
  84. }
  85. /**
  86. * Tests that the `first()` method properly returns the first non-empty value.
  87. *
  88. * @return void
  89. */
  90. public function testCollectionFirstFilter() {
  91. $collection = new Collection(array('data' => array(0, 1, 2)));
  92. $result = $collection->first(function($value) { return $value; });
  93. $this->assertEqual(1, $result);
  94. $collection = new Collection(array('data' => array('Hello', '', 'Goodbye')));
  95. $result = $collection->first(function($value) { return $value; });
  96. $this->assertEqual('Hello', $result);
  97. $collection = new Collection(array('data' => array('', 'Hello', 'Goodbye')));
  98. $result = $collection->first(function($value) { return $value; });
  99. $this->assertEqual('Hello', $result);
  100. $collection = new Collection(array('data' => array('', 'Hello', 'Goodbye')));
  101. $result = $collection->first();
  102. $this->assertEqual('', $result);
  103. }
  104. /**
  105. * Tests that the `each()` filter applies the callback to each item in the current collection,
  106. * returning an instance of itself.
  107. *
  108. * @return void
  109. */
  110. public function testCollectionEachFilter() {
  111. $collection = new Collection(array('data' => array(1, 2, 3, 4, 5)));
  112. $filter = function($item) { return ++$item; };
  113. $result = $collection->each($filter);
  114. $this->assertIdentical($collection, $result);
  115. $this->assertEqual(array(2, 3, 4, 5, 6), $collection->to('array'));
  116. }
  117. public function testCollectionMapFilter() {
  118. $collection = new Collection(array('data' => array(1, 2, 3, 4, 5)));
  119. $filter = function($item) { return ++$item; };
  120. $result = $collection->map($filter);
  121. $this->assertNotEqual($collection, $result);
  122. $this->assertEqual(array(1, 2, 3, 4, 5), $collection->to('array'));
  123. $this->assertEqual(array(2, 3, 4, 5, 6), $result->to('array'));
  124. $result = $collection->map($filter, array('collect' => false));
  125. $this->assertEqual(array(2, 3, 4, 5, 6), $result);
  126. }
  127. /**
  128. * Tests the `ArrayAccess` interface implementation for manipulating values by direct offsets.
  129. *
  130. * @return void
  131. */
  132. public function testArrayAccessOffsetMethods() {
  133. $collection = new Collection(array('data' => array('foo', 'bar', 'baz' => 'dib')));
  134. $this->assertTrue($collection->offsetExists(0));
  135. $this->assertTrue($collection->offsetExists(1));
  136. $this->assertTrue($collection->offsetExists('0'));
  137. $this->assertTrue($collection->offsetExists('baz'));
  138. $this->assertFalse($collection->offsetExists('2'));
  139. $this->assertFalse($collection->offsetExists('bar'));
  140. $this->assertFalse($collection->offsetExists(2));
  141. $this->assertEqual('foo', $collection->offsetSet('bar', 'foo'));
  142. $this->assertTrue($collection->offsetExists('bar'));
  143. $this->assertNull($collection->offsetUnset('bar'));
  144. $this->assertFalse($collection->offsetExists('bar'));
  145. }
  146. /**
  147. * Tests the `ArrayAccess` interface implementation for traversing values.
  148. *
  149. * @return void
  150. */
  151. public function testArrayAccessTraversalMethods() {
  152. $collection = new Collection(array('data' => array('foo', 'bar', 'baz' => 'dib')));
  153. $this->assertEqual('foo', $collection->current());
  154. $this->assertEqual('bar', $collection->next());
  155. $this->assertEqual('foo', $collection->prev());
  156. $this->assertEqual('bar', $collection->next());
  157. $this->assertEqual('dib', $collection->next());
  158. $this->assertEqual('baz', $collection->key());
  159. $this->assertTrue($collection->valid());
  160. $this->assertFalse($collection->next());
  161. $this->assertFalse($collection->valid());
  162. $this->assertEqual('foo', $collection->rewind());
  163. $this->assertTrue($collection->valid());
  164. $this->assertEqual('dib', $collection->prev());
  165. $this->assertTrue($collection->valid());
  166. $this->assertEqual('bar', $collection->prev());
  167. $this->assertTrue($collection->valid());
  168. $this->assertEqual('dib', $collection->end());
  169. $this->assertTrue($collection->valid());
  170. }
  171. /**
  172. * Tests objects and scalar values being appended to the collection.
  173. *
  174. * @return void
  175. */
  176. public function testValueAppend() {
  177. $collection = new Collection();
  178. $this->assertFalse($collection->valid());
  179. $this->assertEqual(0, count($collection));
  180. $collection->append(1);
  181. $this->assertEqual(1, count($collection));
  182. $collection->append(new stdClass());
  183. $this->assertEqual(2, count($collection));
  184. $this->assertEqual(1, $collection->current());
  185. $this->assertEqual(new stdClass(), $collection->next());
  186. }
  187. /**
  188. * Tests getting the index of the internal array.
  189. *
  190. * @return void
  191. */
  192. public function testInternalKeys() {
  193. $collection = new Collection(array('data' => array('foo', 'bar', 'baz' => 'dib')));
  194. $this->assertEqual(array(0, 1, 'baz'), $collection->keys());
  195. }
  196. /**
  197. * Tests that various types of handlers can be registered with `Collection::formats()`, and
  198. * that collection instances are converted correctly.
  199. *
  200. * @return void
  201. */
  202. public function testCollectionFormatConversion() {
  203. Collection::formats('lithium\net\http\Media');
  204. $data = array('hello', 'goodbye', 'foo' => array('bar', 'baz' => 'dib'));
  205. $collection = new Collection(compact('data'));
  206. $expected = json_encode($data);
  207. $result = $collection->to('json');
  208. $this->assertEqual($expected, $result);
  209. $this->assertNull($collection->to('badness'));
  210. Collection::formats(false);
  211. $this->assertNull($collection->to('json'));
  212. Collection::formats('json', function($collection, $options) {
  213. return json_encode($collection->to('array'));
  214. });
  215. $result = $collection->to('json');
  216. $this->assertEqual($expected, $result);
  217. $result = $collection->to(function($collection) {
  218. $value = array_map(
  219. function($i) { return is_array($i) ? join(',', $i) : $i; }, $collection->to('array')
  220. );
  221. return join(',', $value);
  222. });
  223. $expected = 'hello,goodbye,bar,dib';
  224. $this->assertEqual($expected, $result);
  225. }
  226. }
  227. ?>