PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/cases/data/source/MongoDbTest.php

https://github.com/ifunk/lithium
PHP | 654 lines | 519 code | 117 blank | 18 comment | 2 complexity | fc9b5ea410c7167d0bc18d12870ad0c1 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2011, 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\source;
  9. use lithium\data\source\MongoDb;
  10. use Exception;
  11. use MongoId;
  12. use MongoCode;
  13. use MongoDate;
  14. use MongoRegex;
  15. use lithium\data\Connections;
  16. use lithium\data\model\Query;
  17. use lithium\data\entity\Document;
  18. use lithium\data\collection\DocumentSet;
  19. use lithium\tests\mocks\data\source\MockMongoSource;
  20. use lithium\tests\mocks\data\source\MockMongoConnection;
  21. class MongoDbTest extends \lithium\test\Unit {
  22. protected $_model = 'lithium\tests\mocks\data\source\MockMongoPost';
  23. protected $_testConfig = array(
  24. 'type' => 'MongoDb',
  25. 'adapter' => false,
  26. 'database' => 'lithium_test',
  27. 'host' => 'localhost',
  28. 'port' => '27017',
  29. 'persistent' => null,
  30. 'autoConnect' => false
  31. );
  32. protected $_schema = array(
  33. '_id' => array('type' => 'id'),
  34. 'guid' => array('type' => 'id'),
  35. 'title' => array('type' => 'string'),
  36. 'tags' => array('type' => 'string', 'array' => true),
  37. 'comments' => array('type' => 'MongoId'),
  38. 'authors' => array('type' => 'MongoId', 'array' => true),
  39. 'created' => array('type' => 'MongoDate'),
  40. 'modified' => array('type' => 'datetime'),
  41. 'voters' => array('type' => 'id', 'array' => true),
  42. 'rank_count' => array('type' => 'integer', 'default' => 0),
  43. 'rank' => array('type' => 'float', 'default' => 0.0),
  44. 'notifications.foo' => array('type' => 'boolean'),
  45. 'notifications.bar' => array('type' => 'boolean'),
  46. 'notifications.baz' => array('type' => 'boolean')
  47. );
  48. protected $_configs = array();
  49. public function skip() {
  50. $this->skipIf(!MongoDb::enabled(), 'MongoDb is not enabled');
  51. $db = new MongoDb($this->_testConfig);
  52. $message = "`{$this->_testConfig['database']}` database or connection unavailable";
  53. $this->skipIf(!$db->isConnected(array('autoConnect' => true)), $message);
  54. }
  55. /**
  56. * This hack is a necessary optimization until these tests are properly mocked out.
  57. *
  58. * @param array $options Options for the parent class' method.
  59. */
  60. public function run(array $options = array()) {
  61. $this->_results = array();
  62. try {
  63. $this->skip();
  64. } catch (Exception $e) {
  65. $this->_handleException($e);
  66. return $this->_results;
  67. }
  68. $this->_configs = Connections::config();
  69. $result = parent::run($options);
  70. Connections::get('lithium_mongo_test')->dropDB('lithium_test');
  71. Connections::reset();
  72. Connections::config($this->_configs);
  73. return $result;
  74. }
  75. public function setUp() {
  76. Connections::config(array('lithium_mongo_test' => $this->_testConfig));
  77. $this->db = Connections::get('lithium_mongo_test');
  78. $model = $this->_model;
  79. $model::config(array('key' => '_id'));
  80. $model::resetConnection(false);
  81. $this->query = new Query(compact('model') + array(
  82. 'entity' => new Document(compact('model'))
  83. ));
  84. }
  85. public function tearDown() {
  86. try {
  87. $this->db->delete($this->query);
  88. } catch (Exception $e) {}
  89. unset($this->query);
  90. }
  91. public function testBadConnection() {
  92. $db = new MongoDb(array('host' => null, 'autoConnect' => false));
  93. $this->expectException('Could not connect to the database.');
  94. $this->assertFalse($db->connect());
  95. $this->assertTrue($db->disconnect());
  96. }
  97. public function testGoodConnectionBadDatabase() {
  98. $this->expectException('Could not connect to the database.');
  99. $db = new MongoDb(array('database' => null, 'autoConnnect' => false));
  100. }
  101. public function testGoodConnectionGoodDatabase() {
  102. $db = new MongoDb(array('autoConnect' => false) + $this->_testConfig);
  103. $this->assertFalse($db->isConnected());
  104. $this->assertTrue($db->connect());
  105. $this->assertTrue($db->isConnected());
  106. }
  107. public function testSources() {
  108. $result = $this->db->sources();
  109. $expected = array();
  110. $this->assertEqual($expected, $result);
  111. }
  112. public function testDescribe() {
  113. $result = $this->db->describe('test');
  114. $expected = array();
  115. $this->assertEqual($expected, $result);
  116. }
  117. public function testName() {
  118. $result = $this->db->name('{(\'Li\':"∆")}');
  119. $expected = '{(\'Li\':"∆")}';
  120. $this->assertEqual($expected, $result);
  121. }
  122. public function testSchema() {
  123. $result = $this->db->schema($this->query);
  124. $expected = array();
  125. $this->assertEqual($expected, $result);
  126. }
  127. public function testCreateFail() {
  128. $this->expectException('no elements in doc');
  129. $result = $this->db->create($this->query);
  130. }
  131. public function testCreateSuccess() {
  132. $this->query->data(array('title' => 'Test Post'));
  133. $result = $this->db->create($this->query);
  134. $this->assertTrue($result);
  135. }
  136. public function testConditions() {
  137. $result = $this->db->conditions(null, null);
  138. $this->assertEqual(array(), $result);
  139. $function = 'function() { return this.x < y;}';
  140. $conditions = new MongoCode($function);
  141. $result = $this->db->conditions($conditions, null);
  142. $this->assertTrue(is_array($result));
  143. $this->assertTrue(isset($result['$where']));
  144. $this->assertEqual($conditions, $result['$where']);
  145. $conditions = $function;
  146. $result = $this->db->conditions($conditions, null);
  147. $this->assertTrue(is_array($result));
  148. $this->assertTrue(isset($result['$where']));
  149. $this->assertEqual($conditions, $result['$where']);
  150. $conditions = array('key' => 'value', 'anotherkey' => 'some other value');
  151. $result = $this->db->conditions($conditions, null);
  152. $this->assertTrue(is_array($result));
  153. $this->assertEqual($conditions, $result);
  154. $conditions = array('key' => array('one', 'two', 'three'));
  155. $result = $this->db->conditions($conditions, null);
  156. $this->assertTrue(is_array($result));
  157. $this->assertTrue(isset($result['key']));
  158. $this->assertTrue(isset($result['key']['$in']));
  159. $this->assertEqual($conditions['key'], $result['key']['$in']);
  160. }
  161. public function testMongoConditionalOperators() {
  162. $conditions = array('key' => array('<' => 10));
  163. $expected = array('key' => array('$lt' => 10));
  164. $result = $this->db->conditions($conditions, null);
  165. $this->assertEqual($expected, $result);
  166. $conditions = array('key' => array('<=' => 10));
  167. $expected = array('key' => array('$lte' => 10));
  168. $result = $this->db->conditions($conditions, null);
  169. $this->assertEqual($expected, $result);
  170. $conditions = array('key' => array('>' => 10));
  171. $expected = array('key' => array('$gt' => 10));
  172. $result = $this->db->conditions($conditions, null);
  173. $this->assertEqual($expected, $result);
  174. $conditions = array('key' => array('>=' => 10));
  175. $expected = array('key' => array('$gte' => 10));
  176. $result = $this->db->conditions($conditions, null);
  177. $this->assertEqual($expected, $result);
  178. $conditions = array('key' => array('!=' => 10));
  179. $expected = array('key' => array('$ne' => 10));
  180. $result = $this->db->conditions($conditions, null);
  181. $this->assertEqual($expected, $result);
  182. $conditions = array('key' => array('<>' => 10));
  183. $expected = array('key' => array('$ne' => 10));
  184. $result = $this->db->conditions($conditions, null);
  185. $this->assertEqual($expected, $result);
  186. $conditions = array('key' => array('!=' => array(10, 20, 30)));
  187. $expected = array('key' => array('$nin' => array(10, 20, 30)));
  188. $result = $this->db->conditions($conditions, null);
  189. $this->assertEqual($expected, $result);
  190. $conditions = array('key' => array('<>' => array(10, 20, 30)));
  191. $expected = array('key' => array('$nin' => array(10, 20, 30)));
  192. $result = $this->db->conditions($conditions, null);
  193. $this->assertEqual($expected, $result);
  194. $conditions = array('key' => array('like' => '/regex/i'));
  195. $result = $this->db->conditions($conditions, null);
  196. $expected = array('key' => new MongoRegex('/regex/i'));
  197. $this->assertEqual($expected, $result);
  198. }
  199. public function testReadNoConditions() {
  200. $this->db->connect();
  201. $connection = $this->db->connection;
  202. $this->db->connection = new MockMongoSource();
  203. $this->db->connection->resultSets = array(array('ok' => true));
  204. $data = array('title' => 'Test Post');
  205. $options = array('safe' => false, 'fsync' => false);
  206. $this->query->data($data);
  207. $this->assertIdentical(true, $this->db->create($this->query));
  208. $this->assertEqual(compact('data', 'options'), end($this->db->connection->queries));
  209. $this->db->connection->resultSets = array(array(array('_id' => new MongoId()) + $data));
  210. $result = $this->db->read($this->query);
  211. $this->assertTrue($result instanceof DocumentSet);
  212. $this->assertEqual(1, $result->count());
  213. $this->assertEqual('Test Post', $result->first()->title);
  214. $this->db->connection = $connection;
  215. }
  216. public function testReadWithConditions() {
  217. $this->db->connect();
  218. $connection = $this->db->connection;
  219. $this->db->connection = new MockMongoSource();
  220. $this->db->connection->resultSets = array(array('ok' => true));
  221. $data = array('title' => 'Test Post');
  222. $options = array('safe' => false, 'fsync' => false);
  223. $this->query->data($data);
  224. $this->assertTrue($this->db->create($this->query));
  225. $this->query->data(null);
  226. $this->db->connection->resultSets = array(array());
  227. $this->query->conditions(array('title' => 'Nonexistent Post'));
  228. $result = $this->db->read($this->query);
  229. $this->assertTrue($result == true);
  230. $this->assertEqual(0, $result->count());
  231. $this->db->connection->resultSets = array(array($data));
  232. $this->query->conditions($data);
  233. $result = $this->db->read($this->query);
  234. $this->assertTrue($result == true);
  235. $this->assertEqual(1, $result->count());
  236. $this->db->connection = $connection;
  237. }
  238. public function testUpdate() {
  239. $model = $this->_model;
  240. $this->query->model($model);
  241. $this->query->data(array('title' => 'Test Post'));
  242. $this->db->create($this->query);
  243. $result = $this->db->read(new Query(compact('model')));
  244. $original = $result->first()->to('array');
  245. $this->assertEqual(array('_id', 'title'), array_keys($original));
  246. $this->assertEqual('Test Post', $original['title']);
  247. $this->assertPattern('/^[0-9a-f]{24}$/', $original['_id']);
  248. $this->query = new Query(compact('model') + array(
  249. 'data' => array('title' => 'New Post Title'),
  250. 'conditions' => array('_id' => $original['_id'])
  251. ));
  252. $this->assertTrue($this->db->update($this->query));
  253. $result = $this->db->read(new Query(compact('model') + array(
  254. 'conditions' => array('_id' => $original['_id'])
  255. )));
  256. $this->assertEqual(1, $result->count());
  257. $updated = $result->first();
  258. $updated = $updated ? $updated->to('array') : array();
  259. $this->assertEqual($original['_id'], $updated['_id']);
  260. $this->assertEqual('New Post Title', $updated['title']);
  261. }
  262. public function testDelete() {
  263. $data = array('title' => 'Delete Me');
  264. $this->query->data($data);
  265. $this->db->create($this->query);
  266. $result = $this->db->read($this->query);
  267. $expected = 1;
  268. $this->assertEqual($expected, $result->count());
  269. $record = $result->first()->to('array');
  270. $model = $this->_model;
  271. $this->query = new Query(compact('model') + array(
  272. 'entity' => new Document(compact('model'))
  273. ));
  274. $this->query->conditions(array('_id' => $record['_id']));
  275. $result = $this->db->delete($this->query);
  276. $this->assertTrue($result);
  277. $result = $this->db->read($this->query);
  278. $this->assertTrue($result);
  279. $expected = 0;
  280. $this->assertEqual($expected, $result->count());
  281. }
  282. public function testItem() {
  283. $model = $this->_model;
  284. $data = array('title' => 'New Item');
  285. $result = $this->db->item($model, $data);
  286. $this->assertTrue($result instanceof \lithium\data\entity\Document);
  287. $expected = $data;
  288. $result = $result->to('array');
  289. $this->assertEqual($expected, $result);
  290. }
  291. public function testCalculation() {
  292. $result = $this->db->calculation('count', $this->query);
  293. $expected = 0;
  294. $this->assertEqual($expected, $result);
  295. }
  296. public function testEnabled() {
  297. $this->assertTrue(MongoDb::enabled());
  298. $this->assertTrue(MongoDb::enabled('arrays'));
  299. $this->assertTrue(MongoDb::enabled('booleans'));
  300. $this->assertTrue(MongoDb::enabled('relationships'));
  301. }
  302. public function testArbitraryMethodCalls() {
  303. $config = $this->_testConfig;
  304. $result = $this->db->__toString();
  305. $this->assertTrue(strpos($result, $config['host']) !== false);
  306. $this->assertTrue(strpos($result, $config['port']) !== false);
  307. $this->assertTrue(is_array($this->db->listDBs()));
  308. }
  309. public function testDocumentSorting() {
  310. $model = $this->_model;
  311. $model::config(array('connection' => 'lithium_mongo_test', 'source' => 'ordered_docs'));
  312. $model::create(array('title' => 'Third document', 'position' => 3))->save();
  313. $model::create(array('title' => 'First document', 'position' => 1))->save();
  314. $model::create(array('title' => 'Second document', 'position' => 2))->save();
  315. $documents = $model::all(array('order' => 'position'));
  316. $this->assertEqual('First document', $documents[0]->title);
  317. $this->assertEqual('Second document', $documents[1]->title);
  318. $this->assertEqual('Third document', $documents[2]->title);
  319. $documents = $model::all(array('order' => array('position' => 'asc')));
  320. $this->assertEqual('First document', $documents[0]->title);
  321. $this->assertEqual('Second document', $documents[1]->title);
  322. $this->assertEqual('Third document', $documents[2]->title);
  323. $copy = $model::all(array('order' => array('position')));
  324. $this->assertIdentical($documents->data(), $copy->data());
  325. $documents = $model::all(array('order' => array('position' => 'desc')));
  326. $this->assertEqual('Third document', $documents[0]->title);
  327. $this->assertEqual('Second document', $documents[1]->title);
  328. $this->assertEqual('First document', $documents[2]->title);
  329. $list = $model::find('list');
  330. $this->assertEqual(3, count($list));
  331. foreach ($list as $id => $title) {
  332. $this->assertTrue(is_string($id));
  333. $this->assertPattern('/^[a-f0-9]{24}$/', $id);
  334. $this->assertNull($title);
  335. }
  336. $model::config(array('title' => 'title'));
  337. $list = $model::find('list');
  338. $this->assertEqual(3, count($list));
  339. foreach ($list as $id => $title) {
  340. $this->assertTrue(is_string($id));
  341. $this->assertPattern('/^[a-f0-9]{24}$/', $id);
  342. $this->assertPattern('/^(First|Second|Third) document$/', $title);
  343. }
  344. foreach ($documents as $i => $doc) {
  345. $this->assertTrue($doc->delete());
  346. }
  347. }
  348. public function testMongoIdPreservation() {
  349. $model = $this->_model;
  350. $model::config(array('connection' => 'lithium_mongo_test', 'source' => 'ordered_docs'));
  351. $post = $model::create(array('title' => 'A post'));
  352. $post->save();
  353. $id = $post->_id;
  354. $data = Connections::get('lithium_mongo_test')->connection->ordered_docs->findOne(array(
  355. '_id' => $id
  356. ));
  357. $this->assertEqual('A post', $data['title']);
  358. $this->assertEqual($id, (string) $data['_id']);
  359. $this->assertTrue($data['_id'] instanceof MongoId);
  360. $post->title = 'An updated post';
  361. $post->save();
  362. $data = Connections::get('lithium_mongo_test')->connection->ordered_docs->findOne(array(
  363. '_id' => new MongoId($id)
  364. ));
  365. $this->assertEqual('An updated post', $data['title']);
  366. $this->assertEqual($id, (string) $data['_id']);
  367. }
  368. public function testRelationshipGeneration() {
  369. Connections::add('mock-source', $this->_testConfig);
  370. $from = 'lithium\tests\mocks\data\MockComment';
  371. $to = 'lithium\tests\mocks\data\MockPost';
  372. $from::config(array('connection' => 'mock-source'));
  373. $to::config(array('connection' => 'mock-source', 'key' => '_id'));
  374. $result = $this->db->relationship($from, 'belongsTo', 'MockPost');
  375. $expected = array(
  376. 'name' => 'MockPost',
  377. 'type' => 'belongsTo',
  378. 'key' => array('mockComment' => '_id'),
  379. 'from' => $from,
  380. 'link' => 'contained',
  381. 'to' => $to,
  382. 'fields' => true,
  383. 'fieldName' => 'mockPost',
  384. 'constraint' => null,
  385. 'init' => true
  386. );
  387. $this->assertEqual($expected, $result->data());
  388. Connections::config(array('mock-source' => false));
  389. }
  390. public function testCreateNoConnectionException() {
  391. $db = new MockMongoConnection($this->_testConfig + array('autoConnect' => false));
  392. $this->expectException('Could not connect to the database.');
  393. $result = $db->create(null);
  394. }
  395. public function testReadNoConnectionException() {
  396. $db = new MockMongoConnection($this->_testConfig + array('autoConnect' => false));
  397. $this->expectException('Could not connect to the database.');
  398. $result = $db->read(null);
  399. }
  400. public function testUpdateNoConnectionException() {
  401. $db = new MockMongoConnection($this->_testConfig + array('autoConnect' => false));
  402. $this->expectException('Could not connect to the database.');
  403. $result = $db->update(null);
  404. }
  405. public function testDeleteNoConnectionException() {
  406. $db = new MockMongoConnection($this->_testConfig + array('autoConnect' => false));
  407. $this->expectException('Could not connect to the database.');
  408. $result = $db->delete(null);
  409. }
  410. public function testSourcesNoConnectionException() {
  411. $db = new MockMongoConnection($this->_testConfig + array('autoConnect' => false));
  412. $this->expectException('Could not connect to the database.');
  413. $result = $db->sources(null);
  414. }
  415. public function testAtomicUpdate() {
  416. $model = $this->_model;
  417. $model::config(array('connection' => 'lithium_mongo_test', 'source' => 'posts'));
  418. $document = $model::create(array('initial' => 'one', 'values' => 'two'));
  419. $document->save();
  420. $duplicate = $model::create(array('_id' => $document->_id), array('exists' => true));
  421. $duplicate->values = 'new';
  422. $this->assertTrue($duplicate->save());
  423. $document = $model::find((string) $duplicate->_id);
  424. $expected = array(
  425. '_id' => (string) $duplicate->_id, 'initial' => 'one', 'values' => 'new'
  426. );
  427. $this->assertEqual($expected, $document->data());
  428. }
  429. /**
  430. * Tests that the MongoDB adapter will not attempt to overwrite the _id field on document
  431. * update.
  432. */
  433. public function testPreserveId() {
  434. $model = $this->_model;
  435. $model::config(array('connection' => 'lithium_mongo_test', 'source' => 'posts'));
  436. $document = $model::create(array('_id' => 'custom'));
  437. $document->save();
  438. $document->_id = 'custom2';
  439. $document->foo = 'bar';
  440. $this->assertTrue($document->save());
  441. $this->assertNull($model::first('custom2'));
  442. $this->assertEqual(array('_id' => 'custom'), $model::first('custom')->data());
  443. }
  444. public function testCastingConditionsValues() {
  445. $query = new Query(array('schema' => $this->_schema));
  446. $conditions = array('_id' => new MongoId("4c8f86167675abfabdbe0300"));
  447. $result = $this->db->conditions($conditions, $query);
  448. $this->assertEqual($conditions, $result);
  449. $conditions = array('_id' => "4c8f86167675abfabdbe0300");
  450. $result = $this->db->conditions($conditions, $query);
  451. $this->assertEqual(array_keys($conditions), array_keys($result));
  452. $this->assertTrue($result['_id'] instanceof MongoId);
  453. $this->assertEqual($conditions['_id'], (string) $result['_id']);
  454. $conditions = array('_id' => array(
  455. "4c8f86167675abfabdbe0300", "4c8f86167675abfabdbf0300", "4c8f86167675abfabdc00300"
  456. ));
  457. $result = $this->db->conditions($conditions, $query);
  458. $this->assertEqual(3, count($result['_id']['$in']));
  459. $this->assertTrue($result['_id']['$in'][0] instanceof MongoId);
  460. $this->assertTrue($result['_id']['$in'][1] instanceof MongoId);
  461. $this->assertTrue($result['_id']['$in'][2] instanceof MongoId);
  462. $conditions = array('voters' => array('$all' => array(
  463. "4c8f86167675abfabdbf0300", "4c8f86167675abfabdc00300"
  464. )));
  465. $result = $this->db->conditions($conditions, $query);
  466. $this->assertEqual(2, count($result['voters']['$all']));
  467. $this->assertTrue($result['voters']['$all'][0] instanceof MongoId);
  468. $this->assertTrue($result['voters']['$all'][1] instanceof MongoId);
  469. $conditions = array('$or' => array(
  470. array('_id' => "4c8f86167675abfabdbf0300"),
  471. array('guid' => "4c8f86167675abfabdbf0300")
  472. ));
  473. $result = $this->db->conditions($conditions, $query);
  474. $this->assertEqual(array('$or'), array_keys($result));
  475. $this->assertEqual(2, count($result['$or']));
  476. $this->assertTrue($result['$or'][0]['_id'] instanceof MongoId);
  477. $this->assertTrue($result['$or'][1]['guid'] instanceof MongoId);
  478. }
  479. public function testMultiOperationConditions() {
  480. $conditions = array('loc' => array('$near' => array(50, 50), '$maxDistance' => 5));
  481. $result = $this->db->conditions($conditions, $this->query);
  482. $this->assertEqual($conditions, $result);
  483. }
  484. public function testCreateWithEmbeddedObjects() {
  485. $data = array(
  486. '_id' => new MongoId(),
  487. 'created' => new MongoDate(strtotime('-1 hour')),
  488. 'list' => array('foo', 'bar', 'baz')
  489. );
  490. $entity = new Document(compact('data') + array('exists' => false));
  491. $query = new Query(array('type' => 'create') + compact('entity'));
  492. $result = $query->export($this->db);
  493. $this->assertIdentical($data, $result['data']['data']);
  494. }
  495. public function testUpdateWithEmbeddedObjects() {
  496. $data = array(
  497. '_id' => new MongoId(),
  498. 'created' => new MongoDate(strtotime('-1 hour')),
  499. 'list' => array('foo', 'bar', 'baz')
  500. );
  501. $model = $this->_model;
  502. $schema = array('updated' => array('type' => 'MongoDate'));
  503. $entity = new Document(compact('data', 'schema', 'model') + array('exists' => true));
  504. $entity->updated = time();
  505. $entity->list[] = 'dib';
  506. $query = new Query(array('type' => 'update') + compact('entity'));
  507. $result = $query->export($this->db);
  508. $expected = array('updated', '_id', 'created', 'list');
  509. $this->assertEqual($expected, array_keys($result['data']['update']));
  510. $this->assertTrue($result['data']['update']['updated'] instanceof MongoDate);
  511. }
  512. /**
  513. * Assert that Mongo and the Mongo Exporter don't mangle manual geospatial queries.
  514. */
  515. public function testGeoQueries() {
  516. $coords = array(84.13, 11.38);
  517. $coords2 = array_map(function($point) { return $point + 5; }, $coords);
  518. $conditions = array('location' => array('$near' => $coords));
  519. $query = new Query(compact('conditions') + array('model' => $this->_model));
  520. $result = $query->export($this->db);
  521. $this->assertEqual($result['conditions'], $conditions);
  522. $conditions = array('location' => array(
  523. '$within' => array('$box' => array($coords2, $coords))
  524. ));
  525. $query = new Query(compact('conditions') + array('model' => $this->_model));
  526. $result = $query->export($this->db);
  527. $this->assertEqual($conditions, $result['conditions']);
  528. }
  529. public function testSchemaCallback() {
  530. $schema = array('_id' => array('type' => 'id'), 'created' => array('type' => 'date'));
  531. $db = new MongoDb(array('autoConnect' => false, 'schema' => function() use ($schema) {
  532. return $schema;
  533. }));
  534. $this->assertEqual($schema, $db->describe(null));
  535. }
  536. }
  537. ?>