DocumentSetTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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\collection;
  9. use MongoId;
  10. use lithium\data\source\MongoDb;
  11. use lithium\data\source\mongo_db\Schema;
  12. use lithium\data\entity\Document;
  13. use lithium\data\collection\DocumentSet;
  14. use lithium\tests\mocks\data\model\MockDocumentPost;
  15. use lithium\tests\mocks\data\source\mongo_db\MockResult;
  16. use lithium\tests\mocks\data\source\MockMongoConnection;
  17. use lithium\util\Collection;
  18. class DocumentSetTest extends \lithium\test\Unit {
  19. protected $_model = 'lithium\tests\mocks\data\model\MockDocumentPost';
  20. public function setUp() {
  21. MockDocumentPost::config(array('connection' => 'mongo'));
  22. MockDocumentPost::$connection = new MongoDb(array('autoConnect' => false));
  23. MockDocumentPost::$connection->connection = new MockMongoConnection();
  24. }
  25. public function tearDown() {
  26. MockDocumentPost::$connection = null;
  27. }
  28. public function testInitialCasting() {
  29. $model = $this->_model;
  30. $schema = new Schema(array('fields' => array(
  31. '_id' => array('type' => 'id'),
  32. 'foo' => array('type' => 'object'),
  33. 'foo.bar' => array('type' => 'int')
  34. )));
  35. $array = new DocumentSet(compact('model', 'schema') + array(
  36. 'pathKey' => 'foo.bar',
  37. 'data' => array('5', '6', '7')
  38. ));
  39. foreach ($array as $value) {
  40. $this->assertTrue(is_int($value));
  41. }
  42. }
  43. public function testInitialCastingOnSubObject() {
  44. $model = $this->_model;
  45. $schema = new Schema(array('fields' => array(
  46. '_id' => array('type' => 'id'),
  47. 'body' => array('type' => 'string'),
  48. 'foo' => array('type' => 'object'),
  49. 'foo.bar' => array('type' => 'int')
  50. )));
  51. $array = new DocumentSet(compact('model', 'schema') + array(
  52. 'data' => array(
  53. array(
  54. '_id' => '4cb4ab6d7addf98506010002',
  55. 'body' => 'body1',
  56. 'foo' => (object) array('bar' => '1')
  57. ),
  58. array(
  59. '_id' => '4cb4ab6d7addf98506010003',
  60. 'body' => 'body2',
  61. 'foo' => (object) array('bar' => '2')
  62. ),
  63. array(
  64. '_id' => '4cb4ab6d7addf98506010004',
  65. 'body' => 'body3',
  66. 'foo' => (object) array('bar' => '3')
  67. )
  68. )));
  69. foreach ($array as $document) {
  70. $this->assertTrue($document->_id instanceof MongoId);
  71. $this->assertTrue(is_string($document->body));
  72. $this->assertTrue(is_object($document->foo));
  73. $this->assertTrue(is_string($document->foo->bar));
  74. }
  75. $array = new DocumentSet(compact('model', 'schema') + array(
  76. 'data' => array(
  77. array(
  78. '_id' => '4cb4ab6d7addf98506010002',
  79. 'body' => 'body1',
  80. 'foo' => array('bar' => '1')
  81. ),
  82. array(
  83. '_id' => '4cb4ab6d7addf98506010003',
  84. 'body' => 'body2',
  85. 'foo' => array('bar' => '2')
  86. ),
  87. array(
  88. '_id' => '4cb4ab6d7addf98506010004',
  89. 'body' => 'body3',
  90. 'foo' => array('bar' => '3')
  91. )
  92. )));
  93. foreach ($array as $document) {
  94. $this->assertTrue($document->_id instanceof MongoId);
  95. $this->assertTrue(is_string($document->body));
  96. $this->assertTrue(is_object($document->foo));
  97. $this->assertTrue(is_int($document->foo->bar));
  98. }
  99. }
  100. public function testAddValueAndExport() {
  101. $array = new DocumentSet(array(
  102. 'model' => $this->_model,
  103. 'pathKey' => 'foo',
  104. 'data' => array('bar')
  105. ));
  106. $array[] = 'baz';
  107. $expected = array('bar', 'baz');
  108. $result = $array->data();
  109. $this->assertEqual($expected, $result);
  110. }
  111. public function testUnsetInForeach() {
  112. $data = array(
  113. 'Hello',
  114. 'Delete me',
  115. 'Delete me',
  116. 'Delete me',
  117. 'Delete me',
  118. 'Delete me',
  119. 'Hello again!',
  120. 'Delete me'
  121. );
  122. $doc = new DocumentSet(compact('data'));
  123. $this->assertIdentical($data, $doc->data());
  124. foreach ($doc as $i => $word) {
  125. if ($word === 'Delete me') {
  126. unset($doc[$i]);
  127. }
  128. }
  129. $expected = array(0 => 'Hello', 6 => 'Hello again!');
  130. $this->assertIdentical($expected, $doc->data());
  131. $doc = new DocumentSet(compact('data'));
  132. foreach ($doc as $i => $word) {
  133. if ($word === 'Delete me') {
  134. unset($doc[$i]);
  135. }
  136. }
  137. $expected = array(0 => 'Hello', 6 => 'Hello again!');
  138. $this->assertIdentical($expected, $doc->data());
  139. }
  140. public function testArrayOfObjects() {
  141. $schema = new Schema();
  142. $first = (object) array('name' => 'First');
  143. $second = (object) array('name' => 'Second');
  144. $third = (object) array('name' => 'Third');
  145. $doc = new DocumentSet(compact('schema') + array(
  146. 'data' => array($first, $second, $third)
  147. ));
  148. $this->assertTrue(is_object($doc[0]));
  149. $this->assertTrue(is_object($doc[1]));
  150. $this->assertTrue(is_object($doc[2]));
  151. $this->assertEqual(3, count($doc));
  152. }
  153. public function testOffsetSet() {
  154. $data = array('change me', 'foo', 'bar');
  155. $doc = new DocumentSet(compact('data'));
  156. $doc[0] = 'new me';
  157. $expected = array(0 => 'new me', 1 => 'foo', 2 => 'bar');
  158. $this->assertIdentical($expected, $doc->data());
  159. }
  160. public function testPopulateResourceClose() {
  161. $resource = new MockResult();
  162. $doc = new DocumentSet(array('model' => $this->_model, 'result' => $resource));
  163. $model = $this->_model;
  164. $result = $doc->rewind();
  165. $this->assertTrue($result instanceof Document);
  166. $this->assertTrue(is_object($result['_id']));
  167. $expected = array('_id' => '4c8f86167675abfabdbf0300', 'title' => 'bar');
  168. $this->assertEqual($expected, $result->data());
  169. $expected = array('_id' => '5c8f86167675abfabdbf0301', 'title' => 'foo');
  170. $this->assertEqual($expected, $doc->next()->data());
  171. $expected = array('_id' => '6c8f86167675abfabdbf0302', 'title' => 'dib');
  172. $result = $doc->next()->data();
  173. $this->assertEqual($expected, $result);
  174. $this->assertFalse($doc->next());
  175. }
  176. public function testOffsetGetBackwards() {
  177. $resource = new MockResult();
  178. $doc = new DocumentSet(array('model' => $this->_model, 'result' => $resource));
  179. $model = $this->_model;
  180. $expected = array('_id' => '6c8f86167675abfabdbf0302', 'title' => 'dib');
  181. $this->assertEqual($expected, $doc['6c8f86167675abfabdbf0302']->data());
  182. $expected = array('_id' => '5c8f86167675abfabdbf0301', 'title' => 'foo');
  183. $this->assertEqual($expected, $doc['5c8f86167675abfabdbf0301']->data());
  184. $expected = array('_id' => '4c8f86167675abfabdbf0300', 'title' => 'bar');
  185. $this->assertEqual($expected, $doc['4c8f86167675abfabdbf0300']->data());
  186. }
  187. public function testMappingToNewDocumentSet() {
  188. $result = new MockResult();
  189. $model = $this->_model;
  190. $doc = new DocumentSet(compact('model', 'result'));
  191. $mapped = $doc->map(function($data) { return $data; });
  192. $this->assertEqual($doc->data(), $mapped->data());
  193. $this->assertEqual($model, $doc->model());
  194. $this->assertEqual($model, $mapped->model());
  195. }
  196. public function testValid() {
  197. $collection = new DocumentSet();
  198. $this->assertFalse($collection->valid());
  199. $collection = new DocumentSet(array('data' => array('value' => 42)));
  200. $this->assertTrue($collection->valid());
  201. $resource = new MockResult(array('data' => array()));
  202. $collection = new DocumentSet(array('model' => $this->_model, 'result' => $resource));
  203. $this->assertFalse($collection->valid());
  204. $resource = new MockResult(array(
  205. 'data' => array(array('id' => 1, 'data' => 'data1'))
  206. ));
  207. $collection = new DocumentSet(array('model' => $this->_model, 'result' => $resource));
  208. $this->assertTrue($collection->valid());
  209. }
  210. public function testInternalKeys() {
  211. $resource = new MockResult();
  212. $doc = new DocumentSet(array('model' => $this->_model, 'result' => $resource));
  213. $this->assertEqual(array(
  214. 0 => '4c8f86167675abfabdbf0300',
  215. 1 => '5c8f86167675abfabdbf0301',
  216. 2 => '6c8f86167675abfabdbf0302'
  217. ),
  218. $doc->keys()
  219. );
  220. }
  221. public function testTo() {
  222. Collection::formats('lithium\net\http\Media');
  223. $resource = new MockResult();
  224. $doc = new DocumentSet(array('model' => $this->_model, 'result' => $resource));
  225. $expected = array(
  226. '4c8f86167675abfabdbf0300' => array(
  227. '_id' => '4c8f86167675abfabdbf0300',
  228. 'title' => 'bar'
  229. ),
  230. '5c8f86167675abfabdbf0301' => array(
  231. '_id' => '5c8f86167675abfabdbf0301',
  232. 'title' => 'foo'
  233. ),
  234. '6c8f86167675abfabdbf0302' => array(
  235. '_id' => '6c8f86167675abfabdbf0302',
  236. 'title' => 'dib'
  237. )
  238. );
  239. $this->assertEqual($expected, $doc->to('array'));
  240. $expected = array(
  241. array(
  242. '_id' => '4c8f86167675abfabdbf0300',
  243. 'title' => 'bar'
  244. ),
  245. array(
  246. '_id' => '5c8f86167675abfabdbf0301',
  247. 'title' => 'foo'
  248. ),
  249. array(
  250. '_id' => '6c8f86167675abfabdbf0302',
  251. 'title' => 'dib'
  252. )
  253. );
  254. $this->assertEqual($expected, $doc->to('array', array('indexed' => false)));
  255. }
  256. public function testParent() {
  257. $model = $this->_model;
  258. $schema = new Schema(array('fields' => array(
  259. '_id' => array('type' => 'id'),
  260. 'bar' => array('array' => true),
  261. 'foo' => array('type' => 'object', 'array' => true),
  262. 'foo.foo' => array('type' => 'integer'),
  263. 'foo.bar' => array('type' => 'integer')
  264. )));
  265. $doc = new Document(compact('model', 'schema'));
  266. $expected = array(
  267. 'foo' => 1,
  268. 'bar' => 2
  269. );
  270. $doc->foo[] = $expected;
  271. $this->assertEqual($doc, $doc->foo->parent());
  272. $this->assertEqual($expected, $doc->foo[0]->data());
  273. $data = array(
  274. '_id' => '4fb6e2df3e91581fe6e75737',
  275. 'foo' => array($expected)
  276. );
  277. $doc = new Document(compact('model', 'schema', 'data'));
  278. $this->assertEqual($doc, $doc->foo->parent());
  279. $this->assertEqual($expected, $doc->foo[0]->data());
  280. }
  281. }
  282. ?>