PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/app/Plugin/Mongodb/Test/Case/Behavior/SqlCompatibleTest.php

https://gitlab.com/digaotinfo/agendaLegislativa
PHP | 438 lines | 251 code | 52 blank | 135 comment | 5 complexity | 9c45e5bb3b402662c302ad3ac50445b1 MD5 | raw file
  1. <?php
  2. /**
  3. * Tests specific to the sql compatible behavior
  4. *
  5. * PHP version 5
  6. *
  7. * Copyright (c) 2010, Andy Dawson
  8. *
  9. * Licensed under The MIT License
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @filesource
  13. * @copyright Copyright (c) 2010, Andy Dawson
  14. * @link www.ad7six.com
  15. * @package mongodb
  16. * @subpackage mongodb.tests.cases.behaviors
  17. * @since v 1.0 (14-Dec-2010)
  18. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  19. */
  20. App::uses('Model', 'Model');
  21. App::uses('AppModel', 'Model');
  22. /**
  23. * SqlCompatiblePost class
  24. *
  25. * @uses Post
  26. * @package mongodb
  27. * @subpackage mongodb.tests.cases.behaviors
  28. */
  29. class SqlCompatiblePost extends AppModel {
  30. /**
  31. * useDbConfig property
  32. *
  33. * @var string 'test_mongo'
  34. * @access public
  35. */
  36. public $useDbConfig = 'test_mongo';
  37. /**
  38. * actsAs property
  39. *
  40. * @var array
  41. * @access public
  42. */
  43. public $actsAs = array(
  44. 'Mongodb.SqlCompatible'
  45. );
  46. public $lastQuery = array();
  47. public function beforeFind($query) {
  48. $this->lastQuery = $query;
  49. return $query;
  50. }
  51. }
  52. /**
  53. * SqlCompatibleTest class
  54. *
  55. * @uses CakeTestCase
  56. * @package mongodb
  57. * @subpackage mongodb.tests.cases.behaviors
  58. */
  59. class SqlCompatibleTest extends CakeTestCase {
  60. /**
  61. * Default db config. overriden by test db connection if present
  62. *
  63. * @var array
  64. * @access protected
  65. */
  66. protected $_config = array(
  67. 'datasource' => 'Mongodb.MongodbSource',
  68. 'host' => 'localhost',
  69. 'login' => '',
  70. 'password' => '',
  71. 'database' => 'test_mongo',
  72. 'port' => 27017,
  73. 'prefix' => '',
  74. 'persistent' => false,
  75. );
  76. public function setUp() {
  77. $connections = ConnectionManager::enumConnectionObjects();
  78. if (!empty($connections['test']['classname']) && $connections['test']['classname'] === 'mongodbSource') {
  79. $config = new DATABASE_CONFIG();
  80. $this->_config = $config->test;
  81. }
  82. if(!isset($connections['test_mongo'])) {
  83. ConnectionManager::create('test_mongo', $this->_config);
  84. $this->Mongo = new MongodbSource($this->_config);
  85. }
  86. $this->Post = ClassRegistry::init(array('class' => 'SqlCompatiblePost', 'alias' => 'Post', 'ds' => 'test_mongo'), true);
  87. }
  88. /**
  89. * Sets up the environment for each test method
  90. *
  91. * @return void
  92. * @access public
  93. */
  94. public function startTest($method) {
  95. $this->_setupData();
  96. }
  97. /**
  98. * Destroys the environment after each test method is run
  99. *
  100. * @return void
  101. * @access public
  102. */
  103. public function endTest($method) {
  104. $this->Post->deleteAll(true);
  105. }
  106. public function tearDown() {
  107. unset($this->Post);
  108. ClassRegistry::flush();
  109. }
  110. /**
  111. * testNOT method
  112. *
  113. * @return void
  114. * @access public
  115. */
  116. public function testNOT() {
  117. $expected = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20);
  118. $result = $this->Post->find('all', array(
  119. 'conditions' => array(
  120. 'title !=' => 10,
  121. ),
  122. 'fields' => array('_id', 'title', 'number'),
  123. 'order' => array('number' => 'ASC')
  124. ));
  125. $result = Hash::extract($result, '{n}.Post.title');
  126. $this->assertEqual($expected, $result);
  127. $conditions = array(
  128. 'title' => array('$ne' => 10)
  129. );
  130. $this->assertEqual($conditions, $this->Post->lastQuery['conditions']);
  131. $result = $this->Post->find('all', array(
  132. 'conditions' => array(
  133. 'NOT' => array(
  134. 'title' => 10
  135. ),
  136. ),
  137. 'fields' => array('_id', 'title', 'number'),
  138. 'order' => array('number' => 'ASC')
  139. ));
  140. $result = Hash::extract($result, '{n}.Post.title');
  141. $this->assertEqual($expected, $result);
  142. $conditions = array(
  143. 'title' => array('$nin' => array(10))
  144. );
  145. $this->assertEqual($conditions, $this->Post->lastQuery['conditions']);
  146. }
  147. /**
  148. * testNOTIN method
  149. *
  150. * @return void
  151. * @access public
  152. */
  153. public function testNOTIN() {
  154. $expected = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20);
  155. $result = $this->Post->find('all', array(
  156. 'conditions' => array(
  157. 'title NOT IN' => array(10),
  158. ),
  159. 'fields' => array('_id', 'title', 'number'),
  160. 'order' => array('number' => 'ASC')
  161. ));
  162. $result = Hash::extract($result, '{n}.Post.title');
  163. $this->assertEqual($expected, $result);
  164. $conditions = array(
  165. 'title' => array('$nin' => array(10))
  166. );
  167. $this->assertEqual($conditions, $this->Post->lastQuery['conditions']);
  168. }
  169. /**
  170. * testGTLT method
  171. *
  172. * @return void
  173. * @access public
  174. */
  175. public function testGTLT() {
  176. $expected = array(8, 9, 10, 11, 12, 13);
  177. $result = $this->Post->find('all', array(
  178. 'conditions' => array(
  179. 'title >' => 7,
  180. 'title <' => 14,
  181. ),
  182. 'fields' => array('_id', 'title', 'number'),
  183. 'order' => array('number' => 'ASC')
  184. ));
  185. $result = Hash::extract($result, '{n}.Post.title');
  186. $this->assertEqual($expected, $result);
  187. $conditions = array(
  188. 'title' => array(
  189. '$gt' => 7,
  190. '$lt' => 14
  191. )
  192. );
  193. $this->assertEqual($conditions, $this->Post->lastQuery['conditions']);
  194. }
  195. /**
  196. * testGTE method
  197. *
  198. * @return void
  199. * @access public
  200. */
  201. public function testGTE() {
  202. $expected = array(19, 20);
  203. $result = $this->Post->find('all', array(
  204. 'conditions' => array(
  205. 'title >=' => 19,
  206. ),
  207. 'fields' => array('_id', 'title', 'number'),
  208. 'order' => array('number' => 'ASC')
  209. ));
  210. $result = Hash::extract($result, '{n}.Post.title');
  211. $this->assertEqual($expected, $result);
  212. $conditions = array(
  213. 'title' => array('$gte' => 19)
  214. );
  215. $this->assertEqual($conditions, $this->Post->lastQuery['conditions']);
  216. }
  217. /**
  218. * testOR method
  219. *
  220. * @return void
  221. * @access public
  222. */
  223. public function testOR() {
  224. $expected = array(1, 2, 19, 20);
  225. $result = $this->Post->find('all', array(
  226. 'conditions' => array(
  227. 'OR' => array(
  228. 'title <=' => 2,
  229. 'title >=' => 19,
  230. )
  231. ),
  232. 'fields' => array('_id', 'title', 'number'),
  233. 'order' => array('number' => 'ASC')
  234. ));
  235. $result = Hash::extract($result, '{n}.Post.title');
  236. $this->assertEqual($expected, $result);
  237. $conditions = array(
  238. '$or' => array(
  239. array('title' => array('$lte' => 2)),
  240. array('title' => array('$gte' => 19))
  241. )
  242. );
  243. $this->assertEqual($conditions, $this->Post->lastQuery['conditions']);
  244. }
  245. /**
  246. * Tests find method with conditions _id=>array()
  247. *
  248. * @return void
  249. * @access public
  250. */
  251. public function testFindConditionIn() {
  252. for ($i = 1; $i <= 5; $i++) {
  253. $data = array(
  254. '_id' => 'A1' . $i,
  255. 'title' => $i,
  256. );
  257. $saveData['Post'] = $data;
  258. $this->Post->create();
  259. $this->Post->save($saveData);
  260. }
  261. $params = array('conditions' => array('_id' => array('A11', 'A12')));
  262. $result = $this->Post->find('all', $params);
  263. $expected = array('A11','A12');
  264. $result = Hash::extract($result, '{n}.Post._id');
  265. $this->assertEqual($expected, $result);
  266. $this->assertEqual(2, count($result));
  267. $conditions = array(
  268. '_id' => array(
  269. '$in' => array('A11', 'A12')
  270. )
  271. );
  272. $this->assertEqual($conditions, $this->Post->lastQuery['conditions']);
  273. $params = array('conditions' => array('_id' => array('$nin' => array('A11', 'A12'))));
  274. $result = $this->Post->find('all', $params);
  275. //$expected = array('A13','A14');
  276. $result = Hash::extract($result, '{n}.Post._id');
  277. $this->assertTrue(in_array('A13', $result));
  278. $this->assertFalse(in_array('A11', $result));
  279. $this->assertFalse(in_array('A12', $result));
  280. $this->assertEqual(23, count($result));
  281. $conditions = array(
  282. '_id' => array(
  283. '$nin' => array('A11', 'A12')
  284. )
  285. );
  286. $this->assertEqual($conditions, $this->Post->lastQuery['conditions']);
  287. }
  288. /**
  289. * Order method
  290. *
  291. * @return void
  292. * @access public
  293. */
  294. public function testOrderDESC() {
  295. $expected = array(20, 19);
  296. $result = $this->Post->find('all', array(
  297. 'conditions' => array('title >' => 18),
  298. 'fields' => array('_id', 'title'),
  299. 'order' => array('title DESC')
  300. ));
  301. $result = Hash::extract($result, '{n}.Post.title');
  302. $this->assertEqual($expected, $result);
  303. $order = array(array('title' => 'DESC'));
  304. $this->assertEqual($order, $this->Post->lastQuery['order']);
  305. }
  306. /**
  307. * Order method
  308. *
  309. * @return void
  310. * @access public
  311. */
  312. public function testOrderASC() {
  313. $expected = array(19, 20);
  314. $result = $this->Post->find('all', array(
  315. 'conditions' => array('title >' => 18),
  316. 'fields' => array('_id', 'title'),
  317. 'order' => array('title ASC')
  318. ));
  319. $result = Hash::extract($result, '{n}.Post.title');
  320. $this->assertEqual($expected, $result);
  321. $order = array(array('title' => 'ASC'));
  322. $this->assertEqual($order, $this->Post->lastQuery['order']);
  323. }
  324. /**
  325. * Order method with model alias
  326. *
  327. * @return void
  328. * @access public
  329. */
  330. public function testOrderWithModelAlias() {
  331. $expected = array(20, 19);
  332. $result = $this->Post->find('all', array(
  333. 'conditions' => array('title >' => 18),
  334. 'fields' => array('_id', 'title'),
  335. 'order' => array('Post.title DESC')
  336. ));
  337. $result = Hash::extract($result, '{n}.Post.title');
  338. $this->assertEqual($expected, $result);
  339. }
  340. /**
  341. * Convert MongoDate objects to strings
  342. *
  343. * @return void
  344. * @access public
  345. */
  346. public function testConvertDates() {
  347. $expected = '2011-Nov-22 00:00:00';
  348. $data = array('title' => 'date', 'created_at' => new MongoDate(strtotime('2011-11-22 00:00:00')));
  349. $this->Post->save($data);
  350. $result = $this->Post->read();
  351. $this->assertEqual($expected, $result['Post']['created_at']);
  352. }
  353. /**
  354. * Convert MongoDate objects to another format strings
  355. *
  356. * @return void
  357. * @access public
  358. */
  359. public function testConvertDatesAnotherFormat() {
  360. $this->Post->Behaviors->detach('SqlCompatible');
  361. $this->Post->Behaviors->attach('Mongodb.SqlCompatible', array('dateFormat' => 'Y-m-d H:i:s'));
  362. $expected = '2011-11-22 00:00:00';
  363. $data = array('title' => 'date', 'created_at' => new MongoDate(strtotime('2011-11-22 00:00:00')));
  364. $this->Post->save($data);
  365. $result = $this->Post->read();
  366. $this->assertEqual($expected, $result['Post']['created_at']);
  367. }
  368. /**
  369. * setupData method
  370. *
  371. * @return void
  372. * @access protected
  373. */
  374. protected function _setupData() {
  375. $this->Post->deleteAll(true, false);
  376. $this->Post->primaryKey = '_id';
  377. for ($i = 1; $i <= 20; $i++) {
  378. $data = array(
  379. 'title' => $i,
  380. );
  381. $saveData['Post'] = $data;
  382. $this->Post->create();
  383. $this->Post->save($saveData);
  384. }
  385. }
  386. }