PageRenderTime 26ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/MongoTest.php

http://github.com/colinmollenhour/mongodb-php-odm
PHP | 236 lines | 190 code | 40 blank | 6 comment | 4 complexity | 6ee727a1874c03c57b926cfd983c26a6 MD5 | raw file
  1. <?php
  2. require_once __DIR__ . '/../classes/json.php';
  3. require_once __DIR__ . '/../classes/mongo/database.php';
  4. require_once __DIR__ . '/../classes/mongo/collection.php';
  5. require_once __DIR__ . '/../classes/mongo/document.php';
  6. /* Row Data Gateway Pattern */
  7. class Model_Test_Document_Collection extends Mongo_Collection {
  8. protected $name = 'mongotest';
  9. protected $db = 'mongotest';
  10. }
  11. class Model_Test_Document extends Mongo_Document {
  12. protected $_references = array(
  13. 'other' => array('model' => 'test_other'),
  14. 'lots' => array('model' => 'test_other', 'field' => '_lots', 'multiple' => TRUE)
  15. );
  16. }
  17. /* Table Data Gateway Pattern */
  18. class Model_Test_Other extends Mongo_Document {
  19. protected $name = 'mongotest';
  20. protected $db = 'mongotest';
  21. protected $_searches = array(
  22. 'docs' => array('model' => 'test_document', 'field' => '_other'),
  23. );
  24. }
  25. /**
  26. */
  27. class MongoTest extends PHPUnit_Framework_TestCase {
  28. /** @var Mongo_Database */
  29. protected $db;
  30. protected function setUp()
  31. {
  32. $this->db = Mongo_Database::instance('mongotest', array(
  33. 'database' => 'mongotest',
  34. ));
  35. $this->db->createCollection('mongotest');
  36. $this->db->mongotest->remove(array());
  37. }
  38. protected function tearDown()
  39. {
  40. }
  41. public function testCollection()
  42. {
  43. $col = Mongo_Document::factory('test_document')->collection();
  44. $batch = array();
  45. for ($i = 0; $i < 20; $i++)
  46. {
  47. $batch[] = array('name' => base64_encode(rand(0xFFF, 0xFFFF)), 'number' => $i);
  48. }
  49. $col->batchInsert($batch);
  50. $this->assertEquals(20, $col->count(array()), 'batch insert failed');
  51. $col->reset()->find('{number: { $gt: 10 }}')->limit(6)->sort_asc('name');
  52. $this->assertLessThanOrEqual(6, count($col->as_array()), 'limit failed');
  53. $last = '';
  54. foreach ($col as $doc)
  55. {
  56. $this->assertGreaterThan(10, $doc->number, "Failed gt $doc->name: $doc->number ($doc->id)");
  57. $this->assertGreaterThanOrEqual($last, $doc->name, "Failed sort $doc->name: $doc->number ($doc->id)");
  58. $last = $doc->name;
  59. }
  60. }
  61. public function testReference()
  62. {
  63. $doc = new Model_Test_Document();
  64. $doc->id = 'foo';
  65. $doc->other = Mongo_Document::factory('test_other');
  66. $doc->other->bar = 'baz';
  67. $doc->other->save();
  68. $doc->save();
  69. $this->assertNotNull($doc->_other, 'referenced document reference doesnt exist');
  70. $doc = new Model_Test_Document('foo');
  71. $this->assertEquals('baz', $doc->other->bar, 'nested document not saved');
  72. $docs = $doc->other->find_docs();
  73. $this->assertEquals(1, $docs->count(), 'doc not found');
  74. $doc0 = $docs->getNext();
  75. $this->assertEquals('foo', $doc0->id, 'doc id is expected');
  76. for ($i = 0; $i < 3; $i++)
  77. {
  78. $newdoc = Mongo_Document::factory('test_other')->load_values(array('id' => 'more' . $i, 'foo' => 'bar' . $i))->save();
  79. $doc->push('_lots', $newdoc->id);
  80. }
  81. $doc->save();
  82. $lots = $doc->lots;
  83. $this->assertEquals(3, $lots->count(), 'should find 3 referenced docs');
  84. }
  85. public function testDocument()
  86. {
  87. $data = array(
  88. 'name' => 'mongo',
  89. 'counter' => 10,
  90. 'set' => array('foo', 'bar', 'baz'),
  91. 'simplenested' => array(
  92. 'foo' => 'bar',
  93. ),
  94. 'doublenested' => array(
  95. 'foo' => array('bar' => 'baz'),
  96. ),
  97. );
  98. $doc = new Model_Test_Document();
  99. $doc->load_values($data);
  100. $doc->save();
  101. $this->assertTrue($doc->loaded(), 'document loaded after save');
  102. $this->assertNotNull($doc->id, '_id exists');
  103. $id = $doc->id;
  104. $doc = new Model_Test_Document($id);
  105. $doc->load();
  106. $this->assertTrue($doc->loaded());
  107. $this->assertEquals('mongo', $doc->name);
  108. $doc->size = 'huge';
  109. $doc->save()->load();
  110. $this->assertEquals('huge', $doc->size, 'update not saved');
  111. $old = $doc->counter;
  112. $doc->inc('counter')->save()->load();
  113. $this->assertEquals($old + 1, $doc->counter, 'counter not incremented');
  114. $doc = new Model_Test_Document();
  115. $doc->name = 'Bugs Bunny';
  116. $doc->push('friends', 'Daffy Duck');
  117. $doc->upsert();
  118. $this->assertEmpty($doc->id, 'upsert does not get the id');
  119. $doc->load();
  120. $this->assertNotEmpty($doc->id, 'document not inserted on upsert');
  121. $doc = new Model_Test_Document();
  122. $doc->name = 'Bugs Bunny';
  123. $doc->push('friends', 'Elmer Fudd');
  124. $doc->upsert();
  125. $doc->load();
  126. $this->assertEquals(array('Daffy Duck', 'Elmer Fudd'), $doc->friends, 'document not updated on upsert');
  127. $doc->delete();
  128. $doc->load(array('name' => 'Bugs Bunny'));
  129. $this->assertFalse($doc->loaded(), 'document deleted');
  130. $data = array('name' => 'mongo', 'counter' => 10, 'set' => array('foo', 'bar', 'baz'));
  131. $doc = new Model_Test_Document();
  132. $doc->id = 'test_doc';
  133. $doc->load_values($data)->save();
  134. $doc = new Model_Test_Document('test_doc');
  135. $doc->load();
  136. $this->assertTrue($doc->loaded(), 'document not found');
  137. }
  138. public function testNotfound()
  139. {
  140. $doc = new Model_Test_Document('nonexistent');
  141. $this->assertFalse($doc->load(), 'Document loaded, but it should not');
  142. }
  143. /** @dataProvider emulationProvider */
  144. public function testEmulation($data, $expected, $call)
  145. {
  146. foreach (array(false, true) as $modelEmulation)
  147. {
  148. foreach (array(null, false, true) as $functionEmulation)
  149. {
  150. $emulation = $functionEmulation === null ? $modelEmulation : $functionEmulation;
  151. unset($data['_id']);
  152. $doc = new Model_Test_Document();
  153. $doc->set_emulation($modelEmulation);
  154. $doc->load_values($data);
  155. $doc->save();
  156. $expected['_id'] = $doc->id;
  157. $data['_id'] = $doc->id;
  158. $args = $call;
  159. $method = array_shift($args);
  160. if ($functionEmulation !== null) $args[] = $functionEmulation;
  161. call_user_func_array(array($doc, $method), $args);
  162. if ($emulation)
  163. {
  164. $this->assertEquals($expected, $doc->as_array(), "Should be emulated (model:$modelEmulation, func:$functionEmulation, effective:$emulation)");
  165. }
  166. else
  167. {
  168. $this->assertEquals($data, $doc->as_array(), "Should be untouched (model:$modelEmulation, func:$functionEmulation, effective:$emulation)");
  169. }
  170. $doc->save();
  171. $doc->load();
  172. $this->assertEquals($expected, $doc->as_array(), "After save & load (model:$modelEmulation, func:$functionEmulation, effective:$emulation)");
  173. }
  174. }
  175. }
  176. public function emulationProvider()
  177. {
  178. return array(
  179. 'set' => array(array('num' => 1), array('num' => 1, 'foo' => array('bar' => 'baz')), array('set', 'foo.bar', 'baz')),
  180. 'setdot' => array(array('num' => 1), array('num' => 1, 'foo' => array('bar')), array('set', 'foo.0', 'bar')),
  181. 'unset' => array(array('num' => 1), array(), array('_unset', 'num')),
  182. 'inc' => array(array('num' => 1), array('num' => 3), array('inc', 'num', 2)),
  183. 'push' => array(array('foo' => array('bar')), array('foo' => array('bar', 'baz')), array('push', 'foo', 'baz')),
  184. 'push2' => array(array('foo' => array('bar')), array('foo' => array('bar', 'bar')), array('push', 'foo', 'bar')),
  185. 'push3' => array(array('a' => 'b'), array('a' => 'b', 'foo' => array('baz')), array('push', 'foo', 'baz')),
  186. 'pushAll' => array(array('a' => 'b'), array('a' => 'b', 'foo' => array('bar', 'baz')), array('pushAll', 'foo', array('bar', 'baz'))),
  187. 'pull' => array(array('foo' => array('bar', 'baz')), array('foo' => array('bar')), array('pull', 'foo', 'baz')),
  188. 'pullAll' => array(array('foo' => array('bar', 'baz')), array('foo' => array()), array('pullAll', 'foo', array('bar', 'baz'))),
  189. 'pop' => array(array('foo' => array('bar', 'baz')), array('foo' => array('bar')), array('pop', 'foo', true)),
  190. 'shift' => array(array('foo' => array('bar', 'baz')), array('foo' => array('baz')), array('pop', 'foo', false)),
  191. 'shift2' => array(array('foo' => array('bar', 'baz')), array('foo' => array('baz')), array('shift', 'foo')),
  192. 'addToSet' => array(array('foo' => array()), array('foo' => array('bar')), array('addToSet', 'foo', 'bar')),
  193. 'addToSet2' => array(array('foo' => array('bar')), array('foo' => array('bar')), array('addToSet', 'foo', 'bar'))
  194. );
  195. }
  196. }