PageRenderTime 34ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/controller/mongotest.php

http://github.com/colinmollenhour/mongodb-php-odm
PHP | 215 lines | 179 code | 33 blank | 3 comment | 12 complexity | d6067897039f108b04c266e3af79913a MD5 | raw file
  1. <?php
  2. /* Row Data Gateway Pattern */
  3. class Model_Document_Collection extends Mongo_Collection {
  4. protected $name = 'mongotest';
  5. protected $db = 'mongotest';
  6. }
  7. class Model_Document extends Mongo_Document {
  8. protected $_references = array(
  9. 'other' => array('model' => 'other'),
  10. 'lots' => array('model' => 'other', 'field' => '_lots', 'multiple' => TRUE)
  11. );
  12. }
  13. /* Table Data Gateway Pattern */
  14. class Model_Other extends Mongo_Document {
  15. protected $name = 'mongotest';
  16. protected $db = 'mongotest';
  17. protected $_searches = array(
  18. 'docs' => array('model' => 'document', 'field' => '_other'),
  19. );
  20. }
  21. /* Test Controller */
  22. class Controller_Mongotest extends Controller {
  23. protected $db;
  24. public function before()
  25. {
  26. echo "<h1>STARTING MONGO TESTS</h1><style>span.highlight { background-color:#ffffdf }</style>";
  27. $this->setup();
  28. }
  29. public function after()
  30. {
  31. echo "<h1>TESTS COMPLETE</h1>";
  32. echo '<div id="kohana-profiler">'.View::factory('profiler/stats').'</div>';
  33. $this->teardown();
  34. }
  35. public function assert($desc, $condition)
  36. {
  37. if($condition)
  38. echo $desc.' <span class="pass">OK</span><br/>';
  39. else
  40. {
  41. echo $desc.' <span class="fail">FAIL</span><br/>';
  42. $this->teardown();
  43. $bt = debug_backtrace();
  44. $bt = array_shift($bt);
  45. echo "<hr/><b>{$bt['file']}: ({$bt['line']})</b><br/>";
  46. echo Kohana::debug_source($bt['file'], $bt['line']);
  47. $this->after(TRUE);
  48. exit;
  49. }
  50. }
  51. public function test($str)
  52. {
  53. echo "<hr/><b>$str</b><br/>";
  54. }
  55. public function out()
  56. {
  57. $args = func_get_args(); $str = array_shift($args);
  58. echo "<pre>$str</pre>";
  59. foreach($args as $arg) echo Kohana::debug($arg);
  60. }
  61. public function setup()
  62. {
  63. $this->db = Mongo_Database::instance('mongotest', array(
  64. 'database' => 'mongotest',
  65. 'profiling' => TRUE
  66. ));
  67. $this->db->createCollection('mongotest');
  68. $this->db->mongotest->remove(array());
  69. }
  70. public function teardown()
  71. {
  72. }
  73. public function action_document()
  74. {
  75. $this->out('Driver Version: '.Mongo::VERSION);
  76. $this->test('INSERT Document WITHOUT _id');
  77. $data = array(
  78. 'name' => 'mongo',
  79. 'counter' => 10,
  80. 'set' => array('foo','bar','baz'),
  81. 'simplenested' => array(
  82. 'foo' => 'bar',
  83. ),
  84. 'doublenested' => array(
  85. 'foo' => array('bar' => 'baz'),
  86. ),
  87. );
  88. $this->out('BEFORE',$data);
  89. $doc = new Model_Document();
  90. $doc->load_values($data);
  91. $doc->save();
  92. $this->assert('document loaded after save', $doc->loaded() === TRUE);
  93. $this->out('AFTER',$doc->as_array());
  94. $this->assert('_id exists', $doc->id);
  95. $this->test('RETRIEVE DOCUMENT BY _id');
  96. $id = $doc->id;
  97. $doc = new Model_Document($id);
  98. $doc->load();
  99. $this->assert('document found', $doc->loaded() && $doc->name == 'mongo');
  100. $this->test('UPDATE Document');
  101. $doc->size = 'huge';
  102. $doc->save()->load();
  103. $this->assert('update saved', $doc->size == 'huge');
  104. $this->test('INCREMENT COUNTER');
  105. $old = $doc->counter;
  106. $doc->inc('counter')->save()->load();
  107. $this->assert('counter incremented', $old + 1 === $doc->counter);
  108. $this->test('UPSERT NON-EXISTING DOCUMENT');
  109. $doc = new Model_Document();
  110. $doc->name = 'Bugs Bunny';
  111. $doc->push('friends','Daffy Duck');
  112. $doc->upsert();
  113. $doc->load();
  114. $this->assert('document inserted on upsert', !empty($doc->id));
  115. $this->test('UPSERT EXISTING DOCUMENT');
  116. $doc = new Model_Document();
  117. $doc->name = 'Bugs Bunny';
  118. $doc->push('friends','Elmer Fudd');
  119. $doc->upsert();
  120. $doc->load();
  121. $this->assert('document updated on upsert', $doc->friends === array('Daffy Duck','Elmer Fudd'));
  122. $this->test('DELETE Document');
  123. $doc->delete();
  124. $doc->load(array('name' => 'Bugs Bunny'));
  125. $this->assert('document deleted', empty($doc->id));
  126. $this->test('INSERT Document WITH _id');
  127. $data = array('name' => 'mongo', 'counter' => 10, 'set' => array('foo','bar','baz'));
  128. $doc = new Model_Document();
  129. $doc->id = 'test_doc';
  130. $doc->load_values($data)->save();
  131. $doc = new Model_Document('test_doc');
  132. $doc->load();
  133. $this->assert('document found', $doc->loaded());
  134. }
  135. public function action_collection()
  136. {
  137. $col = Mongo_Document::factory('document')->collection();
  138. $this->test('INSERT MULTIPLE');
  139. $batch = array();
  140. for($i = 0; $i < 20; $i++){
  141. $batch[] = array('name' => base64_encode(rand(0xFFF,0xFFFF)), 'number' => $i);
  142. }
  143. $col->batchInsert($batch);
  144. $this->assert('all records inserted', $col->count(array()) == 20);
  145. $this->test('ITERATE WITH FILTER LIMIT AND SORT');
  146. $col->reset()->find('{number: { $gt: 10 }}')->limit(6)->sort_asc('name');
  147. $this->assert('collection limit', count($col->as_array()) <= 6);
  148. $last = '';
  149. foreach($col as $doc){
  150. $this->assert("$doc->name: $doc->number ($doc->id)", $doc->number > 10 && $last < $doc->name);
  151. $last = $doc->name;
  152. }
  153. $col->count();
  154. $col->count(array('number' => array('$gt' => 10)));
  155. }
  156. public function action_reference()
  157. {
  158. $this->test('CREATE DOCUMENT WITH REFERENCED DOCUMENT');
  159. $doc = new Model_Document();
  160. $doc->id = 'foo';
  161. $doc->other = Mongo_Document::factory('other');
  162. $doc->other->bar = 'baz';
  163. $doc->other->save();
  164. $doc->save();
  165. $this->assert('referenced document reference exists', $doc->_other);
  166. $doc = new Model_Document('foo');
  167. $this->assert('nested document saved',$doc->other->bar == 'baz');
  168. $this->test('SEARCH DOCUMENTS BY PREDEFINED SEARCH');
  169. $docs = $doc->other->find_docs();
  170. $this->assert('1 docs found', $docs->count() == 1);
  171. $doc0 = $docs->getNext();
  172. $this->assert('doc id is expected', $doc0->id == 'foo');
  173. $this->test('LOAD MULTIPLE REFERENCED DOCUMENTS FROM ARRAY OF _ids');
  174. for($i = 0; $i < 3; $i++){
  175. $newdoc = Mongo_Document::factory('other')->load_values(array('id' => 'more'.$i, 'foo' => 'bar'.$i))->save();
  176. $doc->push('_lots',$newdoc->id);
  177. }
  178. $doc->save();
  179. $lots = $doc->lots;
  180. $this->assert('found 3 referenced docs', $lots->count() == 3);
  181. }
  182. }