CollectionTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, 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\data;
  9. use stdClass;
  10. use lithium\data\entity\Document;
  11. use lithium\data\collection\DocumentSet;
  12. /**
  13. * lithium\data\Connections Test.
  14. */
  15. class CollectionTest extends \lithium\test\Unit {
  16. /**
  17. * Used model.
  18. *
  19. * @var string
  20. */
  21. protected $_model = 'lithium\tests\mocks\data\MockPost';
  22. /**
  23. * Mock database class.
  24. *
  25. * @var string
  26. */
  27. protected $_database = 'lithium\tests\mocks\data\MockSource';
  28. /**
  29. * Tests `Collection::stats()`.
  30. */
  31. public function testGetStats() {
  32. $collection = new DocumentSet(array('stats' => array('foo' => 'bar')));
  33. $this->assertNull($collection->stats('bar'));
  34. $this->assertEqual('bar', $collection->stats('foo'));
  35. $this->assertEqual(array('foo' => 'bar'), $collection->stats());
  36. }
  37. /**
  38. * Tests `Collection` accessors (getters/setters).
  39. */
  40. public function testAccessorMethods() {
  41. $model = $this->_model;
  42. $model::config(array('meta' => array('connection' => false, 'key' => 'id')));
  43. $collection = new DocumentSet(compact('model'));
  44. $this->assertEqual($model, $collection->model());
  45. $this->assertEqual(compact('model'), $collection->meta());
  46. }
  47. /**
  48. * Tests `Collection::offsetExists()`.
  49. */
  50. public function testOffsetExists() {
  51. $collection = new DocumentSet();
  52. $this->assertEqual($collection->offsetExists(0), false);
  53. $collection = new DocumentSet(array('data' => array('bar', 'baz', 'bob' => 'bill')));
  54. $this->assertEqual($collection->offsetExists(0), true);
  55. $this->assertEqual($collection->offsetExists(1), true);
  56. }
  57. /**
  58. * Tests `Collection::rewind` and `Collection::current`.
  59. */
  60. public function testNextRewindCurrent() {
  61. $collection = new DocumentSet(array('data' => array(
  62. 'title' => 'Lorem Ipsum',
  63. 'value' => 42,
  64. 'foo' => 'bar'
  65. )));
  66. $this->assertEqual('Lorem Ipsum', $collection->current());
  67. $this->assertEqual(42, $collection->next());
  68. $this->assertEqual('bar', $collection->next());
  69. $this->assertEqual('Lorem Ipsum', $collection->rewind());
  70. $this->assertEqual(42, $collection->next());
  71. }
  72. /**
  73. * Tests `Collection::each`.
  74. */
  75. public function testEach() {
  76. $collection = new DocumentSet(array('data' => array(
  77. 'Lorem Ipsum',
  78. 'value',
  79. 'bar'
  80. )));
  81. $collection->each(function($value) {
  82. return $value . ' test';
  83. });
  84. $expected = array(
  85. 'Lorem Ipsum test',
  86. 'value test',
  87. 'bar test'
  88. );
  89. $this->assertEqual($expected, $collection->to('array'));
  90. }
  91. /**
  92. * Tests `Collection::map`.
  93. */
  94. public function testMap() {
  95. $collection = new DocumentSet(array('data' => array(
  96. 'Lorem Ipsum',
  97. 'value',
  98. 'bar'
  99. )));
  100. $results = $collection->map(function($value) {
  101. return $value . ' test';
  102. });
  103. $expected = array(
  104. 'Lorem Ipsum test',
  105. 'value test',
  106. 'bar test'
  107. );
  108. $this->assertEqual($results->to('array'), $expected);
  109. $this->assertNotEqual($results->to('array'), $collection->to('array'));
  110. }
  111. /**
  112. * Tests `Collection::reduce`.
  113. */
  114. public function testReduce() {
  115. $collection = new DocumentSet();
  116. $collection->set(array(
  117. 'title' => 'Lorem Ipsum',
  118. 'key' => 'value',
  119. 'foo' => 'bar'
  120. ));
  121. $result = $collection->reduce(function($memo, $value) {
  122. return trim($memo . ' ' . $value);
  123. }, '');
  124. $expected = 'Lorem Ipsum value bar';
  125. $this->assertEqual($expected, $result);
  126. }
  127. /**
  128. * Tests `Collection::data`.
  129. */
  130. public function testData() {
  131. $data = array(
  132. 'Lorem Ipsum',
  133. 'value',
  134. 'bar'
  135. );
  136. $collection = new DocumentSet(array('data' => $data));
  137. $this->assertEqual($data, $collection->data());
  138. }
  139. /**
  140. * Tests the sort method in `lithium\data\Collection`.
  141. */
  142. public function testSort() {
  143. $collection = new DocumentSet(array('data' => array(
  144. array('id' => 1, 'name' => 'Annie'),
  145. array('id' => 2, 'name' => 'Zilean'),
  146. array('id' => 3, 'name' => 'Trynamere'),
  147. array('id' => 4, 'name' => 'Katarina'),
  148. array('id' => 5, 'name' => 'Nunu')
  149. )));
  150. $collection->sort('name');
  151. $idsSorted = $collection->map(function ($v) { return $v['id']; })->to('array');
  152. $this->assertEqual($idsSorted, array(1, 4, 5, 3, 2));
  153. }
  154. /**
  155. * Tests that arrays can be used to filter objects in `find()` and `first()` methods.
  156. */
  157. public function testArrayFiltering() {
  158. $collection = new DocumentSet(array('data' => array(
  159. new Document(array('data' => array('id' => 1, 'name' => 'Annie', 'active' => 1))),
  160. new Document(array('data' => array('id' => 2, 'name' => 'Zilean', 'active' => 1))),
  161. new Document(array('data' => array('id' => 3, 'name' => 'Trynamere', 'active' => 0))),
  162. new Document(array('data' => array('id' => 4, 'name' => 'Katarina', 'active' => 1))),
  163. new Document(array('data' => array('id' => 5, 'name' => 'Nunu', 'active' => 0)))
  164. )));
  165. $result = $collection->find(array('active' => 1))->data();
  166. $expected = array(
  167. 0 => array('id' => 1, 'name' => 'Annie', 'active' => 1),
  168. 1 => array('id' => 2, 'name' => 'Zilean', 'active' => 1),
  169. 3 => array('id' => 4, 'name' => 'Katarina', 'active' => 1)
  170. );
  171. $this->assertEqual($expected, $result);
  172. $result = $collection->first(array('active' => 1))->data();
  173. $expected = array('id' => 1, 'name' => 'Annie', 'active' => 1);
  174. $this->assertEqual($expected, $result);
  175. $result = $collection->first(array('name' => 'Nunu'))->data();
  176. $expected = array('id' => 5, 'name' => 'Nunu', 'active' => 0);
  177. $this->assertEqual($expected, $result);
  178. }
  179. /**
  180. * Tests `Collection::closed` && `Collection::close`.
  181. */
  182. public function testClosed() {
  183. $collection = new DocumentSet();
  184. $this->assertTrue($collection->closed());
  185. $collection = new DocumentSet(array('result' => 'foo'));
  186. $this->assertFalse($collection->closed());
  187. $collection->close();
  188. $this->assertTrue($collection->closed());
  189. }
  190. /**
  191. * Tests `Collection::assignTo`.
  192. */
  193. public function testAssignTo() {
  194. $parent = new stdClass();
  195. $config = array('valid' => false, 'model' => $this->_model);
  196. $collection = new DocumentSet;
  197. $collection->assignTo($parent, $config);
  198. $this->assertEqual($this->_model, $collection->model());
  199. $this->assertEqual($parent, $collection->parent());
  200. }
  201. }
  202. ?>