PageRenderTime 31ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Test/Case/Datasource/MongodbSourceTest.php

http://github.com/ichikaway/cakephp-mongodb
PHP | 1956 lines | 1364 code | 277 blank | 315 comment | 39 complexity | 89b2b3d8997aea065fdb20d6cae66172 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * Test cases for the Cakephp mongoDB datasource.
  4. *
  5. * This datasource uses Pecl Mongo (http://php.net/mongo)
  6. * and is thus dependent on PHP 5.0 and greater.
  7. *
  8. * Copyright 2010, Yasushi Ichikawa http://github.com/ichikaway/
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright (c) 2010, Yasushi Ichikawa http://github.com/ichikaway/
  14. * @package mongodb
  15. * @subpackage mongodb.tests.cases.datasources
  16. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  17. */
  18. /**
  19. * Import relevant classes for testing
  20. */
  21. App::uses('Model', 'Model');
  22. App::uses('AppModel', 'Model');
  23. App::uses('MongodbSource', 'Mongodb.Model/Datasource');
  24. /**
  25. * Post Model for the test
  26. *
  27. * @package app
  28. * @subpackage app.model.post
  29. */
  30. class Post extends AppModel {
  31. public $useDbConfig = 'test_mongo';
  32. /**
  33. * mongoSchema property
  34. *
  35. * @public array
  36. * @access public
  37. */
  38. public $primaryKey='_id';
  39. public $validate = array(
  40. 'uniquefield1' => array(
  41. 'rule' => 'isUnique',
  42. 'required' => false
  43. ),
  44. 'uniquefield2' => array(
  45. 'rule' => 'manualUniqueValidation',
  46. 'required' => false
  47. ),
  48. );
  49. public $mongoSchema = array(
  50. 'title' => array('type' => 'string'),
  51. 'body' => array('type' => 'string'),
  52. 'text' => array('type' => 'text'),
  53. 'uniquefield1' => array('type' => 'text'),
  54. 'uniquefield2' => array('type' => 'text'),
  55. 'count' => array('type' => 'integer'),
  56. 'created' => array('type' => 'datetime'),
  57. 'modified' => array('type' => 'datetime'),
  58. );
  59. function manualUniqueValidation($check) {
  60. $c = $this->find('count', array(
  61. 'conditions' => array(
  62. 'uniquefield2' => $check['uniquefield2']
  63. )
  64. ));
  65. if ($c === 0) {
  66. return true;
  67. }
  68. return false;
  69. }
  70. }
  71. /**
  72. * Comment Model for the test
  73. *
  74. * @package app
  75. * @subpackage app.model.post
  76. */
  77. class Comment extends AppModel {
  78. public $useDbConfig = 'test_mongo';
  79. public $primaryKey = '_id';
  80. public $mongoSchema = array(
  81. 'post_id' => array('type' => 'integer'),
  82. 'comment' => array('type' => 'string'),
  83. 'comment_at' => array('type' => 'datetime'),
  84. 'created' => array('type' => 'datetime'),
  85. 'modified' => array('type' => 'datetime'),
  86. );
  87. }
  88. /**
  89. * MongoArticle class
  90. *
  91. * @uses AppModel
  92. * @package mongodb
  93. * @subpackage mongodb.tests.cases.datasources
  94. */
  95. class MongoArticle extends AppModel {
  96. public $useDbConfig = 'test_mongo';
  97. }
  98. /**
  99. * MongoDB Source test class
  100. *
  101. * @package app
  102. * @subpackage app.model.datasources
  103. */
  104. class MongodbSourceTest extends CakeTestCase {
  105. /**
  106. * Database Instance
  107. *
  108. * @var resource
  109. * @access public
  110. */
  111. public $mongodb;
  112. /**
  113. * Base Config
  114. *
  115. * @var array
  116. * @access public
  117. *
  118. */
  119. protected $_config = array(
  120. 'datasource' => 'Mongodb.MongodbSource',
  121. 'host' => 'localhost',
  122. 'login' => '',
  123. 'password' => '',
  124. 'database' => 'test_mongo',
  125. 'port' => 27017,
  126. 'prefix' => '',
  127. 'persistent' => true,
  128. );
  129. /**
  130. * Sets up the environment for each test method
  131. *
  132. * @return void
  133. * @access public
  134. */
  135. public function setUp() {
  136. $connections = ConnectionManager::enumConnectionObjects();
  137. if (!empty($connections['test']['classname']) && $connections['test']['classname'] === 'mongodbSource') {
  138. $config = new DATABASE_CONFIG();
  139. $this->_config = $config->test;
  140. } elseif (isset($connections['test_mongo'])) {
  141. $this->_config = $connections['test_mongo'];
  142. }
  143. if(!isset($connections['test_mongo'])) {
  144. ConnectionManager::create('test_mongo', $this->_config);
  145. }
  146. $this->Mongo = new MongodbSource($this->_config);
  147. $this->Post = ClassRegistry::init(array('class' => 'Post', 'alias' => 'Post', 'ds' => 'test_mongo'), true);
  148. $this->MongoArticle = ClassRegistry::init(array('class' => 'MongoArticle', 'alias' => 'MongoArticle', 'ds' => 'test_mongo'), true);
  149. $this->mongodb = ConnectionManager::getDataSource($this->Post->useDbConfig);
  150. $this->mongodb->connect();
  151. }
  152. /**
  153. * Destroys the environment after each test method is run
  154. *
  155. * @return void
  156. * @access public
  157. */
  158. public function tearDown() {
  159. $this->dropData();
  160. unset($this->Post);
  161. unset($this->MongoArticle);
  162. unset($this->Mongo);
  163. unset($this->mongodb);
  164. ClassRegistry::flush();
  165. }
  166. /**
  167. * get Mongod server version
  168. *
  169. * @return numeric
  170. * @access public
  171. */
  172. public function getMongodVersion() {
  173. $mongo = $this->Post->getDataSource();
  174. return $mongo->execute('db.version()');
  175. }
  176. /**
  177. * Insert data method for mongodb.
  178. *
  179. * @param array insert data
  180. * @return void
  181. * @access public
  182. */
  183. public function insertData($data) {
  184. $version = Mongo::VERSION;
  185. try {
  186. if ($version >= '1.3.0') {
  187. $this->mongodb
  188. ->connection
  189. ->selectDB($this->_config['database'])
  190. ->selectCollection($this->Post->table)
  191. ->insert($data, array('safe' => true));
  192. } else {
  193. $this->mongodb
  194. ->connection
  195. ->selectDB($this->_config['database'])
  196. ->selectCollection($this->Post->table)
  197. ->insert($data, true);
  198. }
  199. } catch (MongoException $e) {
  200. trigger_error($e->getMessage());
  201. }
  202. }
  203. /**
  204. * Drop database
  205. *
  206. * @return void
  207. * @access public
  208. */
  209. public function dropData() {
  210. try {
  211. $db = $this->mongodb
  212. ->connection
  213. ->selectDB($this->_config['database']);
  214. foreach($db->listCollections() as $collection) {
  215. $response = $collection->drop();
  216. }
  217. } catch (MongoException $e) {
  218. trigger_error($e->getMessage());
  219. }
  220. }
  221. /**
  222. * testCreateConnectionName
  223. *
  224. * @return void
  225. * @access public
  226. */
  227. public function testCreateConnectionName() {
  228. $config = array(
  229. 'datasource' => 'mongodb',
  230. 'host' => 'localhost',
  231. 'login' => '',
  232. 'password' => '',
  233. 'database' => 'test_mongo',
  234. 'port' => 27017,
  235. 'prefix' => '',
  236. 'persistent' => false,
  237. );
  238. $version = '1.2.2';
  239. $expect = 'mongodb://localhost:27017';
  240. $host = $this->mongodb->createConnectionName($config, $version);
  241. $this->assertIdentical($expect, $host);
  242. $config = array(
  243. 'datasource' => 'mongodb',
  244. 'host' => 'localhost',
  245. 'login' => 'user',
  246. 'password' => 'pass',
  247. 'database' => 'test_mongo',
  248. 'port' => 27017,
  249. 'prefix' => '',
  250. 'persistent' => false,
  251. );
  252. $version = '1.2.2';
  253. $expect = 'mongodb://user:pass@localhost:27017/test_mongo';
  254. $host = $this->mongodb->createConnectionName($config, $version);
  255. $this->assertIdentical($expect, $host);
  256. $config = array(
  257. 'datasource' => 'mongodb',
  258. 'host' => 'localhost',
  259. 'login' => 'user',
  260. 'password' => 'pass',
  261. 'database' => 'test_mongo',
  262. 'port' => 27017,
  263. 'prefix' => '',
  264. 'persistent' => false,
  265. );
  266. $version = '1.0.0';
  267. $expect = 'user:pass@localhost:27017/test_mongo';
  268. $host = $this->mongodb->createConnectionName($config, $version);
  269. $this->assertIdentical($expect, $host);
  270. }
  271. /**
  272. * Tests connection
  273. *
  274. * @return void
  275. * @access public
  276. */
  277. public function testConnect() {
  278. $result = $this->Mongo->connect();
  279. $this->assertTrue($result);
  280. $this->assertTrue($this->Mongo->connected);
  281. $this->assertTrue($this->Mongo->isConnected());
  282. }
  283. /**
  284. * Tests the disconnect method of the Mongodb DataSource
  285. *
  286. * @return void
  287. * @access public
  288. */
  289. public function testDisconnect() {
  290. $result = $this->Mongo->disconnect();
  291. $this->assertTrue($result);
  292. $this->assertNull($this->Mongo->connected);
  293. }
  294. /**
  295. * Tests the listSources method of the Mongodb DataSource
  296. *
  297. * @return void
  298. * @access public
  299. */
  300. public function testListSources() {
  301. $this->assertTrue($this->mongodb->listSources());
  302. }
  303. /**
  304. * Tests the getMongoDb method of the Mongodb DataSource
  305. *
  306. * @return void
  307. * @access public
  308. */
  309. public function testGetMongoDb() {
  310. $obj = $this->mongodb->getMongoDb();
  311. $this->assertTrue(is_object($obj));
  312. $objName = get_class($obj);
  313. $this->assertEqual('MongoDB', $objName);
  314. }
  315. /**
  316. * Tests the Model::getMongoDb() call MongodbSource::getMongoDb
  317. *
  318. * @return void
  319. * @access public
  320. */
  321. public function testGetMongoDbFromModel() {
  322. $obj = $this->Post->getMongoDb();
  323. $this->assertTrue(is_object($obj));
  324. $objName = get_class($obj);
  325. $this->assertEqual('MongoDB', $objName);
  326. }
  327. /**
  328. * Tests the getMongoCollection method of the Mongodb DataSource
  329. *
  330. * @return void
  331. * @access public
  332. */
  333. public function testGetMongoCollection() {
  334. $obj = $this->mongodb->getMongoCollection($this->Post);
  335. $this->assertTrue(is_object($obj));
  336. $objName = get_class($obj);
  337. $this->assertEqual('MongoCollection', $objName);
  338. }
  339. /**
  340. * Tests the describe method of the Mongodb DataSource
  341. *
  342. * @return void
  343. * @access public
  344. */
  345. public function testDescribe() {
  346. $mockObj = $this->getMock('AppModel');
  347. $result = $this->mongodb->describe($mockObj);
  348. $expected = array(
  349. '_id' => array('type' => 'string', 'length' => 24, 'key' => 'primary'),
  350. 'created' => array('type' => 'datetime', 'default' => null),
  351. 'modified' => array('type' => 'datetime', 'default' => null),
  352. );
  353. $this->assertEqual($expected, $result);
  354. $result = $this->mongodb->describe($this->Post);
  355. $expect = array(
  356. '_id' => array('type' => 'string', 'length' => 24, 'key' => 'primary'),
  357. 'title' => array('type' => 'string'),
  358. 'body' => array('type' => 'string'),
  359. 'text' => array('type' => 'text'),
  360. 'uniquefield1' => array('type' => 'text'),
  361. 'uniquefield2' => array('type' => 'text'),
  362. 'count' => array('type' => 'integer'),
  363. 'created' => array('type' => 'datetime'),
  364. 'modified' => array('type' => 'datetime'),
  365. );
  366. ksort($result);
  367. ksort($expect);
  368. $this->assertEqual($expect, $result);
  369. }
  370. /**
  371. * Test truncate method
  372. */
  373. public function testTruncate() {
  374. $this->insertData(array(
  375. 'title' => 'test',
  376. 'body' => 'aaaa',
  377. 'text' => 'bbbb',
  378. ));
  379. $this->assertSame(1, $this->Post->find('count'));
  380. $this->mongodb->truncate($this->Post);
  381. $this->assertSame(0, $this->Post->find('count'));
  382. }
  383. /**
  384. * Test truncate method using mock
  385. */
  386. public function testTruncateStatement() {
  387. $connection = $this->mongodb->connection;
  388. $dbname = $this->mongodb->config['database'];
  389. $tableName = $this->mongodb->fullTableName($this->Post);
  390. $this->mongodb = $this->getMock(
  391. 'MongodbSource',
  392. array('getMongoDb'),
  393. array($this->_config)
  394. );
  395. $mongo = $this->getMock(
  396. 'MongoDB',
  397. array('selectCollection'),
  398. array($connection, $dbname)
  399. );
  400. $mongoCollection = $this->getMock(
  401. 'MongoCollection',
  402. array('remove'),
  403. array($mongo, $tableName)
  404. );
  405. // truncate method call MongoCollection::remove()
  406. $mongoCollection->expects($this->once())->method('remove')
  407. ->with(array())->will($this->returnValue(true));
  408. $mongo->expects($this->once())->method('selectCollection')
  409. ->with($tableName)->will($this->returnValue($mongoCollection));
  410. $this->mongodb->expects($this->once())->method('getMongoDb')
  411. ->will($this->returnValue($mongo));
  412. $this->mongodb->truncate($this->Post);
  413. }
  414. /**
  415. * Tests find method.
  416. *
  417. * @return void
  418. * @access public
  419. */
  420. public function testFind() {
  421. $data = array(
  422. 'title' => 'test',
  423. 'body' => 'aaaa',
  424. 'text' => 'bbbb'
  425. );
  426. $this->insertData($data);
  427. $result = $this->Post->find('all');
  428. $this->assertEqual(1, count($result));
  429. $resultData = $result[0]['Post'];
  430. $this->assertEqual(4, count($resultData));
  431. $this->assertTrue(!empty($resultData['_id']));
  432. $this->assertEqual($data['title'], $resultData['title']);
  433. $this->assertEqual($data['body'], $resultData['body']);
  434. $this->assertEqual($data['text'], $resultData['text']);
  435. }
  436. /**
  437. * Tests findBy* method
  438. *
  439. * @return void
  440. * @access public
  441. */
  442. public function testFindBy() {
  443. $data = array(
  444. array(
  445. 'title' => 'test',
  446. 'body' => 'aaaa',
  447. 'text' => 'bbbb'
  448. ),
  449. array(
  450. 'title' => 'test2',
  451. 'body' => 'abab',
  452. 'text' => 'bcbc'
  453. ),
  454. );
  455. foreach($data as $set) {
  456. $this->insertData($set);
  457. }
  458. $result = $this->Post->findByTitle('test');
  459. $this->assertEqual(1, count($result));
  460. $resultData = $result['Post'];
  461. $this->assertEqual(4, count($resultData));
  462. $this->assertTrue(!empty($resultData['_id']));
  463. $this->assertEqual($resultData['title'], $data[0]['title']);
  464. $this->assertEqual($resultData['body'], $data[0]['body']);
  465. $this->assertEqual($resultData['text'], $data[0]['text']);
  466. $result = $this->Post->findByBody('abab');
  467. $this->assertEqual(1, count($result));
  468. $resultData = $result['Post'];
  469. $this->assertEqual(4, count($resultData));
  470. $this->assertTrue(!empty($resultData['_id']));
  471. $this->assertEqual($data[1]['title'], $resultData['title']);
  472. $this->assertEqual($data[1]['body'], $resultData['body']);
  473. $this->assertEqual($data[1]['text'], $resultData['text']);
  474. }
  475. /**
  476. * Tests findAllBy* method
  477. *
  478. * @return void
  479. * @access public
  480. */
  481. public function testFindAllBy() {
  482. $data = array(
  483. array(
  484. 'title' => 'test',
  485. 'body' => 'abab',
  486. 'text' => 'bbbb'
  487. ),
  488. array(
  489. 'title' => 'test2',
  490. 'body' => 'abab',
  491. 'text' => 'bcbc'
  492. ),
  493. );
  494. foreach($data as $set) {
  495. $this->insertData($set);
  496. }
  497. $result = $this->Post->findAllByBody('abab');
  498. $this->assertEqual(2, count($result));
  499. $result = $this->Post->findAllByTitle('test2');
  500. $this->assertEqual(1, count($result));
  501. }
  502. /**
  503. * Tests save method.
  504. *
  505. * @return void
  506. * @access public
  507. */
  508. public function testSave() {
  509. $data = array(
  510. 'title' => 'test',
  511. 'body' => 'aaaa',
  512. 'text' => 'bbbb'
  513. );
  514. $saveData['Post'] = $data;
  515. $this->Post->create();
  516. $saveResult = $this->Post->save($saveData);
  517. $this->assertTrue(!empty($saveResult) && is_array($saveResult));
  518. $result = $this->Post->find('all');
  519. $this->assertEqual(1, count($result));
  520. $resultData = $result[0]['Post'];
  521. $this->assertEqual(6, count($resultData));
  522. $this->assertTrue(!empty($resultData['_id']));
  523. $this->assertEqual($this->Post->id, $resultData['_id']);
  524. $this->assertEqual($data['title'], $resultData['title']);
  525. $this->assertEqual($data['body'], $resultData['body']);
  526. $this->assertEqual($data['text'], $resultData['text']);
  527. $this->assertTrue(is_a($resultData['created'], 'MongoDate'));
  528. $this->assertTrue(is_a($resultData['modified'], 'MongoDate'));
  529. }
  530. /**
  531. * Tests insertId after saving
  532. *
  533. * @return void
  534. * @access public
  535. */
  536. public function testCheckInsertIdAfterSaving() {
  537. $saveData['Post'] = array(
  538. 'title' => 'test',
  539. 'body' => 'aaaa',
  540. 'text' => 'bbbb'
  541. );
  542. $this->Post->create();
  543. $saveResult = $this->Post->save($saveData);
  544. $this->assertTrue(!empty($saveResult) && is_array($saveResult));
  545. $this->assertEqual($this->Post->id, $this->Post->getInsertId());
  546. $this->assertTrue(is_string($this->Post->id));
  547. $this->assertTrue(is_string($this->Post->getInsertId()));
  548. //set Numeric _id
  549. $saveData['Post'] = array(
  550. '_id' => 123456789,
  551. 'title' => 'test',
  552. 'body' => 'aaaa',
  553. 'text' => 'bbbb'
  554. );
  555. $this->Post->create();
  556. $saveResult = $this->Post->save($saveData);
  557. $this->assertTrue(!empty($saveResult) && is_array($saveResult));
  558. $this->assertEqual($saveData['Post']['_id'] ,$this->Post->id);
  559. $this->assertEqual($this->Post->id, $this->Post->getInsertId());
  560. $this->assertTrue(is_numeric($this->Post->id));
  561. $this->assertTrue(is_numeric($this->Post->getInsertId()));
  562. $readArray1 = $this->Post->read();
  563. $readArray2 = $this->Post->read(null, $saveData['Post']['_id']);
  564. $this->assertEqual($readArray1, $readArray2);
  565. $this->assertEqual($saveData['Post']['_id'], $readArray2['Post']['_id']);
  566. }
  567. /**
  568. * Tests saveAll method.
  569. *
  570. * @return void
  571. * @access public
  572. */
  573. public function testSaveAll() {
  574. $saveData[0]['Post'] = array(
  575. 'title' => 'test1',
  576. 'body' => 'aaaa1',
  577. 'text' => 'bbbb1'
  578. );
  579. $saveData[1]['Post'] = array(
  580. 'title' => 'test2',
  581. 'body' => 'aaaa2',
  582. 'text' => 'bbbb2'
  583. );
  584. $this->Post->create();
  585. $saveResult = $this->Post->saveAll($saveData);
  586. $result = $this->Post->find('all');
  587. $this->assertEqual(2, count($result));
  588. $resultData = $result[0]['Post'];
  589. $this->assertEqual(6, count($resultData));
  590. $this->assertTrue(!empty($resultData['_id']));
  591. $data = $saveData[0]['Post'];
  592. $this->assertEqual($data['title'], $resultData['title']);
  593. $this->assertEqual($data['body'], $resultData['body']);
  594. $this->assertEqual($data['text'], $resultData['text']);
  595. $this->assertTrue(is_a($resultData['created'], 'MongoDate'));
  596. $this->assertTrue(is_a($resultData['modified'], 'MongoDate'));
  597. $resultData = $result[1]['Post'];
  598. $this->assertEqual(6, count($resultData));
  599. $this->assertTrue(!empty($resultData['_id']));
  600. $data = $saveData[1]['Post'];
  601. $this->assertEqual($data['title'], $resultData['title']);
  602. $this->assertEqual($data['body'], $resultData['body']);
  603. $this->assertEqual($data['text'], $resultData['text']);
  604. $this->assertTrue(is_a($resultData['created'], 'MongoDate'));
  605. $this->assertTrue(is_a($resultData['modified'], 'MongoDate'));
  606. }
  607. /**
  608. * Tests update method.
  609. *
  610. * @return void
  611. * @access public
  612. */
  613. public function testUpdate() {
  614. $count0 = $this->Post->find('count');
  615. $data = array(
  616. 'title' => 'test',
  617. 'body' => 'aaaa',
  618. 'text' => 'bbbb',
  619. 'count' => 0
  620. );
  621. $saveData['Post'] = $data;
  622. $this->Post->create();
  623. $saveResult = $this->Post->save($saveData);
  624. $postId = $this->Post->id;
  625. $count1 = $this->Post->find('count');
  626. $this->assertIdentical($count1 - $count0, 1, 'Save failed to create one row');
  627. $this->assertTrue(!empty($saveResult) && is_array($saveResult));
  628. $this->assertTrue(!empty($postId) && is_string($postId));
  629. $findresult = $this->Post->find('all');
  630. $this->assertEqual(0, $findresult[0]['Post']['count']);
  631. $updatedata = array(
  632. 'title' => 'test2',
  633. 'body' => 'aaaa2',
  634. 'text' => 'bbbb2'
  635. );
  636. $saveData['Post'] = $updatedata;
  637. $saveResult = $this->Post->save($saveData);
  638. $count2 = $this->Post->find('count');
  639. $this->assertIdentical($count2 - $count1, 0, 'Save test 2 created another row, it did not update the existing row');
  640. $this->assertTrue(!empty($saveResult) && is_array($saveResult));
  641. $this->assertIdentical($this->Post->id, $postId);
  642. $this->Post->create();
  643. $updatedata = array(
  644. '_id' => $postId,
  645. 'title' => 'test3',
  646. 'body' => 'aaaa3',
  647. 'text' => 'bbbb3'
  648. );
  649. $saveData['Post'] = $updatedata;
  650. $saveResult = $this->Post->save($saveData);
  651. $count3 = $this->Post->find('count');
  652. $this->assertIdentical($count3 - $count2, 0, 'Saving with the id in the data created another row');
  653. $this->assertTrue(!empty($saveResult) && is_array($saveResult));
  654. $this->assertIdentical($this->Post->id, $postId);
  655. $this->Post->create();
  656. $updatedata = array(
  657. 'title' => 'test4',
  658. 'body' => 'aaaa4',
  659. 'text' => 'bbbb4'
  660. );
  661. $saveData['Post'] = $updatedata;
  662. $this->Post->id = $postId;
  663. $saveResult = $this->Post->save($saveData);
  664. $count4 = $this->Post->find('count');
  665. $this->assertIdentical($count4 - $count3, 0, 'Saving with $Model->id set and no id in the data created another row');
  666. $this->assertTrue(!empty($saveResult) && is_array($saveResult));
  667. $this->assertIdentical($this->Post->id, $postId);
  668. $result = $this->Post->find('all');
  669. $this->assertEqual(1, count($result));
  670. $resultData = $result[0]['Post'];
  671. $this->assertEqual(7, count($resultData));
  672. $this->assertTrue(!empty($resultData['_id']));
  673. $this->assertEqual($this->Post->id, $resultData['_id']);
  674. $this->assertEqual($updatedata['title'], $resultData['title']);
  675. $this->assertEqual($updatedata['body'], $resultData['body']);
  676. $this->assertEqual($updatedata['text'], $resultData['text']);
  677. $this->assertEqual(0, $resultData['count']);
  678. // using $inc operator
  679. $this->Post->mongoNoSetOperator = '$inc';
  680. $this->Post->create();
  681. $updatedataIncrement = array(
  682. '_id' => $postId,
  683. 'count' => 1,
  684. );
  685. $saveData['Post'] = $updatedataIncrement;
  686. $saveResult = $this->Post->save($saveData);
  687. $this->assertTrue(!empty($saveResult) && is_array($saveResult));
  688. $this->assertIdentical($this->Post->id, $postId);
  689. $result = $this->Post->find('all');
  690. $this->assertEqual(1, count($result));
  691. $resultData = $result[0]['Post'];
  692. $this->assertEqual(7, count($resultData));
  693. $this->assertTrue(!empty($resultData['_id']));
  694. $this->assertEqual($this->Post->id, $resultData['_id']);
  695. $this->assertEqual($updatedata['title'], $resultData['title']); //not update
  696. $this->assertEqual($updatedata['body'], $resultData['body']); //not update
  697. $this->assertEqual($updatedata['text'], $resultData['text']); //not update
  698. $this->assertEqual(1, $resultData['count']); //increment
  699. unset($this->Post->mongoNoSetOperator);
  700. }
  701. /**
  702. * Tests updateAll method.
  703. *
  704. * @return void
  705. * @access public
  706. */
  707. public function testUpdateAll() {
  708. $saveData[0]['Post'] = array(
  709. 'title' => 'test',
  710. 'name' => 'ichi',
  711. 'body' => 'aaaa1',
  712. 'text' => 'bbbb1'
  713. );
  714. $saveData[1]['Post'] = array(
  715. 'title' => 'test',
  716. 'name' => 'ichi',
  717. 'body' => 'aaaa2',
  718. 'text' => 'bbbb2'
  719. );
  720. $this->Post->create();
  721. $this->Post->saveAll($saveData);
  722. $updateData = array('name' => 'ichikawa');
  723. $conditions = array('title' => 'test');
  724. $resultUpdateAll = $this->Post->updateAll($updateData, $conditions);
  725. $this->assertTrue($resultUpdateAll);
  726. $result = $this->Post->find('all');
  727. $this->assertEqual(2, count($result));
  728. $resultData = $result[0]['Post'];
  729. $this->assertEqual(7, count($resultData));
  730. $this->assertTrue(!empty($resultData['_id']));
  731. $data = $saveData[0]['Post'];
  732. $this->assertEqual($data['title'], $resultData['title']);
  733. $this->assertEqual('ichikawa', $resultData['name']);
  734. $this->assertEqual($data['body'], $resultData['body']);
  735. $this->assertEqual($data['text'], $resultData['text']);
  736. $this->assertTrue(is_a($resultData['created'], 'MongoDate'));
  737. $this->assertTrue(is_a($resultData['modified'], 'MongoDate'));
  738. $resultData = $result[1]['Post'];
  739. $this->assertEqual(7, count($resultData));
  740. $this->assertTrue(!empty($resultData['_id']));
  741. $data = $saveData[1]['Post'];
  742. $this->assertEqual($data['title'], $resultData['title']);
  743. $this->assertEqual('ichikawa', $resultData['name']);
  744. $this->assertEqual($data['body'], $resultData['body']);
  745. $this->assertEqual($data['text'], $resultData['text']);
  746. $this->assertTrue(is_a($resultData['created'], 'MongoDate'));
  747. $this->assertTrue(is_a($resultData['modified'], 'MongoDate'));
  748. }
  749. /**
  750. * Tests updateAll method.
  751. *
  752. * @return void
  753. * @access public
  754. */
  755. public function testSetMongoUpdateOperator() {
  756. $ds = $this->Post->getDataSource();
  757. //normal
  758. $data = array('title' => 'test1', 'name' => 'ichikawa');
  759. $expect = array('$set' => array('title' => 'test1', 'name' => 'ichikawa'));
  760. $result = $ds->setMongoUpdateOperator($this->Post, $data);
  761. $this->assertEqual($expect, $result);
  762. //using $inc
  763. $data = array('title' => 'test1', 'name' => 'ichikawa', '$inc' => array('count' => 1));
  764. $expect = array('title' => 'test1', 'name' => 'ichikawa', '$inc' => array('count' => 1));
  765. $result = $ds->setMongoUpdateOperator($this->Post, $data);
  766. $this->assertEqual($expect, $result);
  767. //using $inc and modified
  768. $data = array('modified' => '2011/8/1', '$inc' => array('count' => 1));
  769. $expect = array('$set' => array('modified' => '2011/8/1'), '$inc' => array('count' => 1));
  770. $result = $ds->setMongoUpdateOperator($this->Post, $data);
  771. $this->assertEqual($expect, $result);
  772. //using $inc and updated
  773. $data = array('updated' => '2011/8/1', '$inc' => array('count' => 1));
  774. $expect = array('$set' => array('updated' => '2011/8/1'), '$inc' => array('count' => 1));
  775. $result = $ds->setMongoUpdateOperator($this->Post, $data);
  776. $this->assertEqual($expect, $result);
  777. //using $inc, $push and modified
  778. $data = array('$push' => array('tag' => 'tag1'), 'modified' => '2011/8/1', '$inc' => array('count' => 1));
  779. $expect = array('$push' => array('tag' => 'tag1'),'$set' => array('modified' => '2011/8/1'), '$inc' => array('count' => 1));
  780. $result = $ds->setMongoUpdateOperator($this->Post, $data);
  781. $this->assertEqual($expect, $result);
  782. //mongoNoSetOperator is true,
  783. // using $inc, $push and modified
  784. $this->Post->mongoNoSetOperator = true;
  785. $data = array('$push' => array('tag' => 'tag1'), 'modified' => '2011/8/1', '$inc' => array('count' => 1));
  786. $expect = array('$push' => array('tag' => 'tag1'),'modified' => '2011/8/1', '$inc' => array('count' => 1));
  787. $result = $ds->setMongoUpdateOperator($this->Post, $data);
  788. $this->assertEqual($expect, $result);
  789. //mongoNoSetOperator is $inc,
  790. $this->Post->mongoNoSetOperator = '$inc';
  791. $data = array('count' => 1);
  792. $expect = array('$inc' => array('count' => 1));
  793. $result = $ds->setMongoUpdateOperator($this->Post, $data);
  794. $this->assertEqual($expect, $result);
  795. //mongoNoSetOperator is $inc,
  796. // with modified field
  797. $this->Post->mongoNoSetOperator = '$inc';
  798. $data = array('count' => 1, 'modified' => '2011/8/1');
  799. $expect = array('$inc' => array('count' => 1),'$set' => array('modified' => '2011/8/1'));
  800. $result = $ds->setMongoUpdateOperator($this->Post, $data);
  801. $this->assertEqual($expect, $result);
  802. //mongoNoSetOperator is $inc,
  803. // with updated field
  804. $this->Post->mongoNoSetOperator = '$inc';
  805. $data = array('count' => 1, 'updated' => '2011/8/1');
  806. $expect = array('$inc' => array('count' => 1),'$set' => array('updated' => '2011/8/1'));
  807. $result = $ds->setMongoUpdateOperator($this->Post, $data);
  808. $this->assertEqual($expect, $result);
  809. }
  810. /**
  811. * Tests update method without $set operator.
  812. *
  813. * @return void
  814. * @access public
  815. */
  816. public function testUpdateWithoutMongoSchemaProperty() {
  817. $data = array(
  818. 'title' => 'test',
  819. 'body' => 'aaaa',
  820. 'text' => 'bbbb',
  821. 'count' => 0,
  822. 'created' => new mongoDate(),
  823. 'modified' => new mongoDate(),
  824. );
  825. $saveData['MongoArticle'] = $data;
  826. $this->MongoArticle->create();
  827. $saveResult = $this->MongoArticle->save($saveData);
  828. $postId = $this->MongoArticle->id;
  829. //using $set operator
  830. $this->MongoArticle->create();
  831. $updatedata = array(
  832. 'id' => $postId,
  833. 'title' => 'test3',
  834. 'body' => 'aaaa3',
  835. );
  836. $saveData['MongoArticle'] = $updatedata;
  837. $saveResult = $this->MongoArticle->save($saveData); // using $set operator
  838. $this->assertTrue(!empty($saveResult) && is_array($saveResult));
  839. $this->assertIdentical($this->MongoArticle->id, $postId);
  840. $result = null;
  841. $result = $this->MongoArticle->find('all');
  842. $this->assertEqual(1, count($result));
  843. $resultData = $result[0]['MongoArticle'];
  844. $this->assertEqual($this->MongoArticle->id, $resultData['id']);
  845. $this->assertEqual($updatedata['title'], $resultData['title']); //update
  846. $this->assertEqual($updatedata['body'], $resultData['body']); //update
  847. $this->assertEqual($data['text'], $resultData['text']); //not update
  848. $this->assertEqual($data['count'], $resultData['count']); //not update
  849. //using $inc operator insted of $set operator
  850. $this->MongoArticle->create();
  851. $updatedataInc = array(
  852. 'id' => $postId,
  853. '$inc' => array('count' => 1),
  854. );
  855. $saveData['MongoArticle'] = $updatedataInc;
  856. $saveResult = $this->MongoArticle->save($saveData); // using $set operator
  857. $this->assertTrue(!empty($saveResult) && is_array($saveResult));
  858. $this->assertIdentical($this->MongoArticle->id, $postId);
  859. $result = null;
  860. $result = $this->MongoArticle->find('all');
  861. $this->assertEqual(1, count($result));
  862. $resultData = $result[0]['MongoArticle'];
  863. $this->assertEqual($this->MongoArticle->id, $resultData['id']);
  864. $this->assertEqual($updatedata['title'], $resultData['title']); //not update
  865. $this->assertEqual($updatedata['body'], $resultData['body']); //not update
  866. $this->assertEqual($data['text'], $resultData['text']); //not update
  867. $this->assertEqual(1, $resultData['count']); //increment
  868. //using $inc and $push
  869. $this->MongoArticle->create();
  870. $updatedataInc = array(
  871. 'id' => $postId,
  872. '$push' => array(
  873. 'comments' => array(
  874. '_id' => new MongoId(),
  875. 'created' => new MongoDate(),
  876. 'vote_count' => 0,
  877. 'user' => 'user1',
  878. 'body' => 'comment',
  879. )
  880. ),
  881. '$inc' => array('count' => 1),
  882. );
  883. $saveData['MongoArticle'] = $updatedataInc;
  884. $saveResult = $this->MongoArticle->save($saveData); // using $set operator
  885. $this->assertTrue(!empty($saveResult) && is_array($saveResult));
  886. $this->assertIdentical($this->MongoArticle->id, $postId);
  887. $result = null;
  888. $result = $this->MongoArticle->find('all');
  889. $this->assertEqual(1, count($result));
  890. $resultData = $result[0]['MongoArticle'];
  891. $this->assertEqual($this->MongoArticle->id, $resultData['id']);
  892. $this->assertEqual($updatedata['title'], $resultData['title']); //not update
  893. $this->assertEqual($updatedata['body'], $resultData['body']); //not update
  894. $this->assertEqual($data['text'], $resultData['text']); //not update
  895. $this->assertEqual(2, $resultData['count']); //increment
  896. $this->assertEqual('user1', $resultData['comments'][0]['user']); //push
  897. $this->assertEqual('comment', $resultData['comments'][0]['body']); //push
  898. $this->assertEqual(1, count($resultData['comments'])); //push
  899. $this->assertTrue(!empty($resultData['created']));
  900. $this->assertTrue(!empty($resultData['modified']));
  901. //no $set operator
  902. $this->MongoArticle->mongoNoSetOperator = true;
  903. $this->MongoArticle->create();
  904. $updatedata = array(
  905. 'id' => $postId,
  906. 'title' => 'test4',
  907. 'body' => 'aaaa4',
  908. 'count' => '1',
  909. );
  910. $saveData['MongoArticle'] = $updatedata;
  911. $saveResult = $this->MongoArticle->save($saveData);
  912. $this->assertTrue(!empty($saveResult) && is_array($saveResult));
  913. $this->assertIdentical($this->MongoArticle->id, $postId);
  914. $result = null;
  915. $result = $this->MongoArticle->find('all');
  916. $this->assertEqual(1, count($result));
  917. $resultData = $result[0]['MongoArticle'];
  918. $this->assertEqual($this->MongoArticle->id, $resultData['id']);
  919. $this->assertEqual($updatedata['title'], $resultData['title']); //update
  920. $this->assertEqual($updatedata['body'], $resultData['body']); //update
  921. $this->assertTrue(empty($resultData['text']));
  922. $this->assertEqual(1, $resultData['count']);
  923. $this->MongoArticle->mongoNoSetOperator = null;
  924. //use $push
  925. $this->MongoArticle->create();
  926. $updatedata = array(
  927. 'id' => $postId,
  928. 'push_column' => array('push1'),
  929. );
  930. $saveData['MongoArticle'] = $updatedata;
  931. $saveResult = $this->MongoArticle->save($saveData); //use $set
  932. $result = $this->MongoArticle->find('all');
  933. $resultData = $result[0]['MongoArticle'];
  934. $this->assertEqual('test4', $resultData['title']); // no update
  935. $this->assertEqual(array('push1'), $resultData['push_column']);
  936. $this->MongoArticle->mongoNoSetOperator = '$push';
  937. $this->MongoArticle->create();
  938. $updatedata = array(
  939. 'id' => $postId,
  940. 'push_column' => 'push2',
  941. );
  942. $saveData['MongoArticle'] = $updatedata;
  943. $saveResult = $this->MongoArticle->save($saveData); //use $push
  944. $this->assertTrue(!empty($saveResult) && is_array($saveResult));
  945. $this->assertIdentical($this->MongoArticle->id, $postId);
  946. $result = null;
  947. $result = $this->MongoArticle->find('all');
  948. $this->assertEqual(1, count($result));
  949. $resultData = $result[0]['MongoArticle'];
  950. $this->assertEqual($this->MongoArticle->id, $resultData['id']);
  951. $this->assertEqual('test4', $resultData['title']); // no update
  952. $this->assertEqual(array('push1','push2'), $resultData['push_column']); //update
  953. $this->MongoArticle->mongoNoSetOperator = null;
  954. unset($this->MongoArticle);
  955. }
  956. /**
  957. * Tests groupBy
  958. *
  959. * @return void
  960. * @access public
  961. */
  962. public function testGroupBy() {
  963. for($i = 0 ; $i < 30 ; $i++) {
  964. $saveData[$i]['Post'] = array(
  965. 'title' => 'test'.$i,
  966. 'body' => 'aaaa'.$i,
  967. 'text' => 'bbbb'.$i,
  968. 'count' => $i,
  969. );
  970. }
  971. $saveData[30]['Post'] = array(
  972. 'title' => 'test1',
  973. 'body' => 'aaaa1',
  974. 'text' => 'bbbb1',
  975. 'count' => 1,
  976. );
  977. $saveData[31]['Post'] = array(
  978. 'title' => 'test2',
  979. 'body' => 'aaaa2',
  980. 'text' => 'bbbb2',
  981. 'count' => 2,
  982. );
  983. $this->Post->create();
  984. $saveResult = $this->Post->saveAll($saveData);
  985. $cond_count = 5;
  986. $query = array(
  987. 'key' => array('title' => true ),
  988. 'initial' => array('csum' => 0),
  989. 'reduce' => 'function(obj, prev){prev.csum += 1;}',
  990. 'options' => array(
  991. 'condition' => array('count' => array('$lt' => $cond_count)),
  992. ),
  993. );
  994. $mongo = $this->Post->getDataSource();
  995. $result = $mongo->group($query, $this->Post);
  996. $this->assertTrue($result['ok'] == 1 && count($result['retval']) > 0);
  997. $this->assertEqual($cond_count, count($result['retval']));
  998. $this->assertEqual('test0', $result['retval'][0]['title']);
  999. $this->assertEqual('test1', $result['retval'][1]['title']);
  1000. $this->assertEqual('test2', $result['retval'][2]['title']);
  1001. $this->assertEqual(1, $result['retval'][0]['csum']);
  1002. $this->assertEqual(2, $result['retval'][1]['csum']);
  1003. $this->assertEqual(2, $result['retval'][2]['csum']);
  1004. }
  1005. /**
  1006. * Tests query
  1007. * Distinct, Group
  1008. *
  1009. * @return void
  1010. * @access public
  1011. */
  1012. public function testQuery() {
  1013. for($i = 0 ; $i < 30 ; $i++) {
  1014. $saveData[$i]['Post'] = array(
  1015. 'title' => 'test'.$i,
  1016. 'body' => 'aaaa'.$i,
  1017. 'text' => 'bbbb'.$i,
  1018. 'count' => $i,
  1019. );
  1020. }
  1021. $saveData[30]['Post'] = array(
  1022. 'title' => 'test1',
  1023. 'body' => 'aaaa1',
  1024. 'text' => 'bbbb1',
  1025. 'count' => 1,
  1026. );
  1027. $saveData[31]['Post'] = array(
  1028. 'title' => 'test2',
  1029. 'body' => 'aaaa2',
  1030. 'text' => 'bbbb2',
  1031. 'count' => 2,
  1032. );
  1033. $saveData[32]['Post'] = array(
  1034. 'title' => 'test2',
  1035. 'body' => 'aaaa2',
  1036. 'text' => 'bbbb2',
  1037. 'count' => 32,
  1038. );
  1039. $this->Post->create();
  1040. $saveResult = $this->Post->saveAll($saveData);
  1041. //using query() Distinct
  1042. $params = array(
  1043. 'distinct' => 'posts',
  1044. 'key' => 'count',
  1045. );
  1046. $result = $this->Post->query( $params );
  1047. $this->assertEqual(1, $result['values'][1]);
  1048. $this->assertEqual(2, $result['values'][2]);
  1049. $this->assertEqual(32, $result['values'][30]);
  1050. //using query() group
  1051. $cond_count = 5;
  1052. $reduce = "function(obj,prev){prev.csum++;}";
  1053. $params = array(
  1054. 'group'=>array(
  1055. 'ns'=>'posts',
  1056. 'cond'=>array('count' => array('$lt' => $cond_count)),
  1057. 'key'=>array('title'=>true),
  1058. 'initial'=>array('csum'=>0),
  1059. '$reduce'=>$reduce
  1060. )
  1061. );
  1062. $result = $this->Post->query( $params );
  1063. $this->assertTrue($result['ok'] == 1 && count($result['retval']) > 0);
  1064. $this->assertEqual($cond_count, count($result['retval']));
  1065. $this->assertEqual('test0', $result['retval'][0]['title']);
  1066. $this->assertEqual('test1', $result['retval'][1]['title']);
  1067. $this->assertEqual('test2', $result['retval'][2]['title']);
  1068. $this->assertEqual(1, $result['retval'][0]['csum']);
  1069. $this->assertEqual(2, $result['retval'][1]['csum']);
  1070. $this->assertEqual(2, $result['retval'][2]['csum']);
  1071. }
  1072. /**
  1073. * Tests MapReduce
  1074. *
  1075. * @return void
  1076. * @access public
  1077. */
  1078. public function testMapReduce() {
  1079. for($i = 0 ; $i < 30 ; $i++) {
  1080. $saveData[$i]['Post'] = array(
  1081. 'title' => 'test'.$i,
  1082. 'body' => 'aaaa'.$i,
  1083. 'text' => 'bbbb'.$i,
  1084. 'count' => $i,
  1085. );
  1086. }
  1087. $saveData[30]['Post'] = array(
  1088. 'title' => 'test1',
  1089. 'body' => 'aaaa1',
  1090. 'text' => 'bbbb1',
  1091. 'count' => 1,
  1092. );
  1093. $saveData[31]['Post'] = array(
  1094. 'title' => 'test2',
  1095. 'body' => 'aaaa2',
  1096. 'text' => 'bbbb2',
  1097. 'count' => 2,
  1098. );
  1099. $saveData[32]['Post'] = array(
  1100. 'title' => 'test2',
  1101. 'body' => 'aaaa2',
  1102. 'text' => 'bbbb2',
  1103. 'count' => 32,
  1104. );
  1105. $this->Post->create();
  1106. $saveResult = $this->Post->saveAll($saveData);
  1107. $map = new MongoCode("function() { emit(this.title,1); }");
  1108. $reduce = new MongoCode("function(k, vals) { ".
  1109. "var sum = 0;".
  1110. "for (var i in vals) {".
  1111. "sum += vals[i];".
  1112. "}".
  1113. "return sum; }"
  1114. );
  1115. $params = array(
  1116. "mapreduce" => "posts",
  1117. "map" => $map,
  1118. "reduce" => $reduce,
  1119. "query" => array(
  1120. "count" => array('$gt' => -2),
  1121. ),
  1122. 'out' => 'test_mapreduce_posts',
  1123. );
  1124. $mongo = $this->Post->getDataSource();
  1125. $results = $mongo->mapReduce($params);
  1126. $posts = array();
  1127. foreach ($results as $post) {
  1128. $posts[$post['_id']] = $post['value'];
  1129. }
  1130. $this->assertEqual(30, count($posts));
  1131. $this->assertEqual(1, $posts['test0']);
  1132. $this->assertEqual(2, $posts['test1']);
  1133. $this->assertEqual(3, $posts['test2']);
  1134. $this->assertEqual(1, $posts['test3']);
  1135. //set timeout
  1136. $results = $mongo->mapReduce($params, 100); //set timeout 100msec
  1137. $posts = array();
  1138. foreach ($results as $post) {
  1139. $posts[$post['_id']] = $post['value'];
  1140. }
  1141. $this->assertEqual(30, count($posts));
  1142. $this->assertEqual(1, $posts['test0']);
  1143. $this->assertEqual(2, $posts['test1']);
  1144. $this->assertEqual(3, $posts['test2']);
  1145. $this->assertEqual(1, $posts['test3']);
  1146. //get results as inline data
  1147. $version = $this->getMongodVersion();
  1148. if( $version >= '1.7.4') {
  1149. $params = array(
  1150. "mapreduce" => "posts",
  1151. "map" => $map,
  1152. "reduce" => $reduce,
  1153. "query" => array(
  1154. "count" => array('$gt' => -2),
  1155. ),
  1156. 'out' => array('inline' => 1),
  1157. );
  1158. $results = $mongo->mapReduce($params);
  1159. $posts = array();
  1160. foreach ($results as $post) {
  1161. $posts[$post['_id']] = $post['value'];
  1162. }
  1163. $this->assertEqual(30, count($posts));
  1164. $this->assertEqual(1, $posts['test0']);
  1165. $this->assertEqual(2, $posts['test1']);
  1166. $this->assertEqual(3, $posts['test2']);
  1167. $this->assertEqual(1, $posts['test3']);
  1168. }
  1169. }
  1170. /**
  1171. * testSort method
  1172. *
  1173. * @return void
  1174. * @access public
  1175. */
  1176. public function testSort() {
  1177. $data = array(
  1178. 'title' => 'AAA',
  1179. 'body' => 'aaaa',
  1180. 'text' => 'aaaa'
  1181. );
  1182. $saveData['Post'] = $data;
  1183. $this->Post->create();
  1184. $this->Post->save($saveData);
  1185. $data = array(
  1186. 'title' => 'CCC',
  1187. 'body' => 'cccc',
  1188. 'text' => 'cccc'
  1189. );
  1190. $saveData['Post'] = $data;
  1191. $this->Post->create();
  1192. $this->Post->save($saveData);
  1193. $this->Post->create();
  1194. $data = array(
  1195. 'title' => 'BBB',
  1196. 'body' => 'bbbb',
  1197. 'text' => 'bbbb'
  1198. );
  1199. $saveData['Post'] = $data;
  1200. $this->Post->create();
  1201. $this->Post->save($saveData);
  1202. $expected = array('AAA', 'BBB', 'CCC');
  1203. $result = $this->Post->find('all', array(
  1204. 'fields' => array('_id', 'title'),
  1205. 'order' => array('title' => 1)
  1206. ));
  1207. $result = Hash::extract($result, '{n}.Post.title');
  1208. $this->assertEqual($expected, $result);
  1209. $result = $this->Post->find('all', array(
  1210. 'fields' => array('_id', 'title'),
  1211. 'order' => array('title' => 'ASC')
  1212. ));
  1213. $result = Hash::extract($result, '{n}.Post.title');
  1214. $expected = array_reverse($expected);
  1215. $result = $this->Post->find('all', array(
  1216. 'fields' => array('_id', 'title'),
  1217. 'order' => array('title' => '-1')
  1218. ));
  1219. $result = Hash::extract($result, '{n}.Post.title');
  1220. $this->assertEqual($expected, $result);
  1221. $result = $this->Post->find('all', array(
  1222. 'fields' => array('_id', 'title'),
  1223. 'order' => array('title' => 'DESC')
  1224. ));
  1225. $result = Hash::extract($result, '{n}.Post.title');
  1226. $this->assertEqual($expected, $result);
  1227. }
  1228. /**
  1229. * testSchemaless method
  1230. *
  1231. * Test you can save to a model without specifying mongodb.
  1232. *
  1233. * @return void
  1234. * @access public
  1235. */
  1236. public function testSchemaless() {
  1237. $toSave = array(
  1238. 'title' => 'A test article',
  1239. 'body' => str_repeat('Lorum ipsum ', 100),
  1240. 'tags' => array(
  1241. 'one',
  1242. 'two',
  1243. 'three'
  1244. ),
  1245. 'modified' => null,
  1246. 'created' => null
  1247. );
  1248. $this->MongoArticle = ClassRegistry::init('MongoArticle');
  1249. $this->MongoArticle->create();
  1250. $saveResult = $this->MongoArticle->save($toSave);
  1251. $this->assertTrue(!empty($saveResult) && is_array($saveResult));
  1252. $expected = array_intersect_key($toSave, array_flip(array('title', 'body', 'tags')));
  1253. $result = $this->MongoArticle->read(array('title', 'body', 'tags'));
  1254. unset ($result['MongoArticle']['id']); // prevent auto added field from screwing things up
  1255. $this->assertEqual($expected, $result['MongoArticle']);
  1256. $toSave = array(
  1257. 'title' => 'Another test article',
  1258. 'body' => str_repeat('Lorum pipsum ', 100),
  1259. 'tags' => array(
  1260. 'four',
  1261. 'five',
  1262. 'six'
  1263. ),
  1264. 'starts' => date('Y-M-d H:i:s'),
  1265. 'modified' => null,
  1266. 'created' => null
  1267. );
  1268. $this->MongoArticle->create();
  1269. $saveResult = $this->MongoArticle->save($toSave);
  1270. $this->assertTrue(!empty($saveResult) && is_array($saveResult));
  1271. $starts = $this->MongoArticle->field('starts');
  1272. $this->assertEqual($toSave['starts'], $starts);
  1273. }
  1274. /**
  1275. * testSpecificId method
  1276. *
  1277. * Test you can save specifying your own _id values - and update by _id
  1278. *
  1279. * @return void
  1280. * @access public
  1281. */
  1282. public function testSpecificId() {
  1283. $data = array(
  1284. '_id' => 123,
  1285. 'title' => 'test',
  1286. 'body' => 'aaaa',
  1287. 'text' => 'bbbb'
  1288. );
  1289. $saveData['Post'] = $data;
  1290. $this->Post->create();
  1291. $saveResult = $this->Post->save($saveData);
  1292. $this->assertTrue(!empty($saveResult) && is_array($saveResult));
  1293. $found = $this->Post->find('first', array(
  1294. 'fields' => array('_id', 'title', 'body', 'text'),
  1295. 'conditions' => array('_id' => 123)
  1296. ));
  1297. $this->assertEqual($found, $saveData);
  1298. $data = array(
  1299. '_id' => 123,
  1300. 'title' => 'test2',
  1301. 'body' => 'aaaa2',
  1302. 'text' => 'bbbb2'
  1303. );
  1304. $saveData['Post'] = $data;
  1305. $this->Post->create();
  1306. $saveResult = $this->Post->save($saveData);
  1307. $this->assertTrue(!empty($saveResult) && is_array($saveResult));
  1308. $found = $this->Post->find('first', array(
  1309. 'fields' => array('_id', 'title', 'body', 'text'),
  1310. 'conditions' => array('_id' => 123)
  1311. ));
  1312. $this->assertEqual($found, $saveData);
  1313. }
  1314. /**
  1315. * testOr method
  1316. *
  1317. * @return void
  1318. * @access public
  1319. */
  1320. public function testOr() {
  1321. $mongoVersion = $this->mongodb->execute('db.version()');
  1322. $shouldSkip = version_compare($mongoVersion, '1.5.3', '<');
  1323. if ($this->skipIf($shouldSkip, '$or tests require at least version mongo version 1.5.3, currently using ' . $mongoVersion . ' %s')) {
  1324. return;
  1325. }
  1326. $this->MongoArticle = ClassRegistry::init('MongoArticle');
  1327. $this->MongoArticle->create();
  1328. for ($i = 1; $i <= 20; $i++) {
  1329. $data = array(
  1330. 'title' => "Article $i",
  1331. 'subtitle' => "Sub Article $i",
  1332. );
  1333. $saveData['MongoArticle'] = $data;
  1334. $this->MongoArticle->create();
  1335. $this->MongoArticle->save($saveData);
  1336. }
  1337. $expected = $this->MongoArticle->find('all', array(
  1338. 'conditions' => array(
  1339. 'title' => array('$in' => array('Article 1', 'Article 10'))
  1340. ),
  1341. 'order' => array('number' => 'ASC')
  1342. ));
  1343. $this->assertEqual(count($expected), 2);
  1344. $result = $this->MongoArticle->find('all', array(
  1345. 'conditions' => array(
  1346. '$or' => array(
  1347. array('title' => 'Article 1'),
  1348. array('title' => 'Article 10'),
  1349. )
  1350. ),
  1351. 'order' => array('number' => 'ASC')
  1352. ));
  1353. $this->assertEqual($result, $expected);
  1354. }
  1355. /**
  1356. * testDeleteAll method
  1357. *
  1358. * @return void
  1359. * @access public
  1360. */
  1361. function testDeleteAll($cascade = true) {
  1362. $this->MongoArticle = ClassRegistry::init('MongoArticle');
  1363. $this->MongoArticle->create(array('title' => 'Article 1', 'cat' => 1));
  1364. $this->MongoArticle->save();
  1365. $this->MongoArticle->create(array('title' => 'Article 2', 'cat' => 1));
  1366. $this->MongoArticle->save();
  1367. $this->MongoArticle->create(array('title' => 'Article 3', 'cat' => 2));
  1368. $this->MongoArticle->save();
  1369. $this->MongoArticle->create(array('title' => 'Article 4', 'cat' => 2));
  1370. $this->MongoArticle->save();
  1371. $count = $this->MongoArticle->find('count');
  1372. $this->assertEqual($count, 4);
  1373. $this->MongoArticle->deleteAll(array('cat' => 2), $cascade);
  1374. $count = $this->MongoArticle->find('count');
  1375. $this->assertEqual($count, 2);
  1376. $this->MongoArticle->deleteAll(true, $cascade);
  1377. $count = $this->MongoArticle->find('count');
  1378. $this->assertEqual($count, 0);
  1379. }
  1380. /**
  1381. * testDeleteAllNoCascade method
  1382. *
  1383. * @return void
  1384. * @access public
  1385. */
  1386. function testDeleteAllNoCascade() {
  1387. $this->testDeleteAll(false);
  1388. }
  1389. /**
  1390. * testRegexSearch method
  1391. *
  1392. * @return void
  1393. * @access public
  1394. */
  1395. public function testRegexSearch() {
  1396. $this->MongoArticle = ClassRegistry::init('MongoArticle');
  1397. $this->MongoArticle->create(array('title' => 'Article 1', 'cat' => 1));
  1398. $this->MongoArticle->save();
  1399. $this->MongoArticle->create(array('title' => 'Article 2', 'cat' => 1));
  1400. $this->MongoArticle->save();
  1401. $this->MongoArticle->create(array('title' => 'Article 3', 'cat' => 2));
  1402. $this->MongoArticle->save();
  1403. $count=$this->MongoArticle->find('count',array(
  1404. 'conditions'=>array(
  1405. 'title'=>'Article 2'
  1406. )
  1407. ));
  1408. $this->assertEqual($count, 1);
  1409. $count = $this->MongoArticle->find('count',array(
  1410. 'conditions'=>array(
  1411. 'title'=> new MongoRegex('/^Article/')
  1412. )
  1413. ));
  1414. $this->assertEqual($count, 3);
  1415. }
  1416. /**
  1417. * testEmptyReturn method
  1418. * inserts article into table. searches for a different non existing article. should return an empty array in the same that that it does from other datasources
  1419. * @return void
  1420. * @access public
  1421. */
  1422. public function testEmptyReturn() {
  1423. $this->MongoArticle = ClassRegistry::init('MongoArticle');
  1424. $this->MongoArticle->create(array('title' => 'Article 1', 'cat' => 1));
  1425. $this->MongoArticle->save();
  1426. $articles=$this->MongoArticle->find('all',array(
  1427. 'conditions'=>array(
  1428. 'title'=>'Article 2'
  1429. )
  1430. ));
  1431. $this->assertTrue(is_array($articles) && empty($articles));
  1432. $articles=$this->MongoArticle->find('first',array(
  1433. 'conditions'=>array(
  1434. 'title'=>'Article 2'
  1435. )
  1436. ));
  1437. $this->assertTrue(is_array($articles) && empty($articles));
  1438. }
  1439. /**
  1440. * Tests isUnique validation.
  1441. *
  1442. * @return void
  1443. * @access public
  1444. */
  1445. public function testSaveUniques() {
  1446. $data = array(
  1447. 'title' => 'test',
  1448. 'body' => 'aaaa',
  1449. 'text' => 'bbbb',
  1450. 'uniquefield1'=>'uniquenameforthistest'
  1451. );
  1452. $saveData['Post'] = $data;
  1453. $this->Post->Behaviors->attach('Mongodb.SqlCompatible');
  1454. $this->Post->create();
  1455. $saveResult = $this->Post->save($saveData);
  1456. $this->assertTrue(!empty($saveResult) && is_array($saveResult));
  1457. $data = array(
  1458. 'title' => 'test',
  1459. 'body' => 'asdf',
  1460. 'text' => 'bbbb',
  1461. 'uniquefield1'=>'uniquenameforthistest'
  1462. );
  1463. $saveData['Post'] = $data;
  1464. $this->Post->create();
  1465. $saveResult = $this->Post->save($saveData);
  1466. $this->assertFalse($saveResult);
  1467. }
  1468. /**
  1469. * Tests isUnique validation with custom validation.
  1470. *
  1471. * @return void
  1472. * @access public
  1473. */
  1474. public function testSaveUniquesCustom() {
  1475. $data = array(
  1476. 'title' => 'test',
  1477. 'body' => 'aaaa',
  1478. 'text' => 'bbbb',
  1479. 'uniquefield2' => 'someunqiuename'
  1480. );
  1481. $saveData['Post'] = $data;
  1482. $this->Post->create();
  1483. $saveResult = $this->Post->save($saveData);
  1484. $this->assertTrue(!empty($saveResult) && is_array($saveResult));
  1485. $data = array(
  1486. 'title' => 'test',
  1487. 'body' => 'asdf',
  1488. 'text' => 'bbbb',
  1489. 'uniquefield2' => 'someunqiuename'
  1490. );
  1491. $saveData['Post'] = $data;
  1492. $this->Post->create();
  1493. $saveResult = $this->Post->save($saveData);
  1494. $this->assertFalse($saveResult);
  1495. }
  1496. public function testReturn() {
  1497. $this->MongoArticle = ClassRegistry::init('MongoArticle');
  1498. $this->MongoArticle->create(array('title' => 'Article 1', 'cat' => 1));
  1499. $this->MongoArticle->save();
  1500. $this->MongoArticle->create(array('title' => 'Article 2', 'cat' => 1));
  1501. $this->MongoArticle->save();
  1502. $return = $this->MongoArticle->find('all', array(
  1503. 'conditions' => array(
  1504. 'title' => 'Article 2'
  1505. )
  1506. ));
  1507. $this->assertTrue(is_array($return));
  1508. $return = $this->MongoArticle->find('first', array(
  1509. 'conditions' => array(
  1510. 'title' => 'Article 2'
  1511. )
  1512. ));
  1513. $this->assertTrue(is_array($return));
  1514. $return = $this->MongoArticle->find('first', array(
  1515. 'conditions' => array(
  1516. 'title' => 'Article 2'
  1517. )
  1518. ));
  1519. $this->assertTrue(is_array($return));
  1520. $return = $this->MongoArticle->find('count', array(
  1521. 'conditions' => array(
  1522. 'title' => 'Article 2'
  1523. )
  1524. ));
  1525. $this->assertTrue(is_int($return));
  1526. $return = $this->MongoArticle->find('neighbors', array(
  1527. 'conditions' => array(
  1528. 'title' => 'Article 2'
  1529. )
  1530. ));
  1531. $this->assertTrue(is_array($return));
  1532. $return = $this->MongoArticle->find('list', array(
  1533. 'conditions' => array(
  1534. 'title' => 'Article 2'
  1535. )
  1536. ));
  1537. $this->assertTrue(is_array($return));
  1538. $return = $this->MongoArticle->find('all', array(
  1539. 'conditions' => array(
  1540. 'title' => 'Doesn\'t exist'
  1541. )
  1542. ));
  1543. $this->assertTrue(is_array($return));
  1544. $return = $this->MongoArticle->find('first', array(
  1545. 'conditions' => array(
  1546. 'title' => 'Doesn\'t exist'
  1547. )
  1548. ));
  1549. $this->assertTrue(is_array($return) && empty($return));
  1550. $return = $this->MongoArticle->find('count', array(
  1551. 'conditions' => array(
  1552. 'title' => 'Doesn\'t exist'
  1553. )
  1554. ));
  1555. $this->assertTrue(is_int($return));
  1556. $return = $this->MongoArticle->find('neighbors', array(
  1557. 'conditions' => array(
  1558. 'title' => 'Doesn\'t exist'
  1559. )
  1560. ));
  1561. $this->assertTrue(is_array($return));
  1562. $return = $this->MongoArticle->find('list', array(
  1563. 'conditions' => array(
  1564. 'title' => 'Doesn\'t exist'
  1565. )
  1566. ));
  1567. $this->assertTrue(is_array($return));
  1568. }
  1569. public function testDatetimeFieldUsingMongoDate() {
  1570. $this->Comment = ClassRegistry::init(array('class' => 'Comment', 'alias' => 'Comment', 'ds' => 'test_mongo'), true);
  1571. $ds = $this->Comment->getDataSource();
  1572. $fields = array(
  1573. 'post_id',
  1574. 'comment',
  1575. 'comment_at',
  1576. );
  1577. $values = array(
  1578. array( 1, 'comment 1', '2014-02-21 01:02:03Z'),
  1579. array( 1, 'comment 2', '2014-02-22 01:02:03Z'),
  1580. array( 1, 'comment 3', '2014-02-23 01:02:03Z'),
  1581. array( 1, 'comment 4', '2014-02-24 01:02:03Z'),
  1582. array( 1, 'comment 5', '2014-02-25 01:02:03Z')
  1583. );
  1584. $ds->insertMulti('comments', $fields, $values);
  1585. $result = $this->Comment->find('all');
  1586. $this->assertEqual(count($result), 5);
  1587. $this->assertTrue(is_a($result[0]['Comment']['comment_at'], 'MongoDate'));
  1588. $this->assertTrue(is_a($result[1]['Comment']['comment_at'], 'MongoDate'));
  1589. $this->assertTrue(is_a($result[2]['Comment']['comment_at'], 'MongoDate'));
  1590. $this->assertTrue(is_a($result[3]['Comment']['comment_at'], 'MongoDate'));
  1591. $this->assertTrue(is_a($result[4]['Comment']['comment_at'], 'MongoDate'));
  1592. $values = array(
  1593. array( 2, 'comment 2 1', new MongoDate(strtotime('2014-02-21 02:02:01Z'))),
  1594. );
  1595. $ds->insertM

Large files files are truncated, but you can click here to view the full file