PageRenderTime 59ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/share/test/application/model/set-test.php

https://github.com/php-tox/tox
PHP | 826 lines | 635 code | 46 blank | 145 comment | 20 complexity | 9a1207897254106126fc48d45e5e9dde MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * Defines the test case for Tox\Application\Model\Set.
  4. *
  5. * This file is part of Tox.
  6. *
  7. * Tox is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Tox is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Tox. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * @copyright Š 2012-2013 PHP-Tox.org
  21. * @license GNU General Public License, version 3
  22. */
  23. namespace Tox\Application\Model;
  24. use PHPUnit_Framework_TestCase;
  25. require_once __DIR__ . '/../../../../src/core/assembly.php';
  26. require_once __DIR__ . '/../../../../src/application/icommittable.php';
  27. require_once __DIR__ . '/../../../../src/application/imodelset.php';
  28. require_once __DIR__ . '/../../../../src/application/model/set.php';
  29. require_once __DIR__ . '/../../../../src/core/exception.php';
  30. require_once __DIR__ . '/../../../../src/application/model/illegalfilterexception.php';
  31. require_once __DIR__ . '/../../../../src/application/model/attributefilteredexception.php';
  32. require_once __DIR__ . '/../../../../src/application/model/attributesortedexception.php';
  33. require_once __DIR__ . '/../../../../src/application/model/illegalentityforsetexception.php';
  34. require_once __DIR__ . '/../../../../src/application/model/modelincludedinsetexception.php';
  35. require_once __DIR__ . '/../../../../src/application/model/preparedmodeltodropexception.php';
  36. require_once __DIR__ . '/../../../../src/application/model/setwithoutfiltersexception.php';
  37. require_once __DIR__ . '/../../../../src/application/imodel.php';
  38. require_once __DIR__ . '/../../../../src/core/isingleton.php';
  39. require_once __DIR__ . '/../../../../src/application/idao.php';
  40. require_once __DIR__ . '/../../../../src/data/isource.php';
  41. /**
  42. * Tests Tox\Application\Model\Set.
  43. *
  44. * @internal
  45. *
  46. * @package tox.application.model
  47. * @author Snakevil Zen <zsnakevil@gmail.com>
  48. */
  49. class SetTest extends PHPUnit_Framework_TestCase
  50. {
  51. /**
  52. * Stores the mock of data access object for test.
  53. *
  54. * @var Tox\Application\IDao
  55. */
  56. protected $dao;
  57. protected function setUp()
  58. {
  59. $this->dao = $this->getMock('Tox\\Application\\IDao');
  60. }
  61. /**
  62. * @dataProvider provideFiltersAndExcludes
  63. */
  64. public function testMagicFilterAndExclude($func, $func, $attr, $value)
  65. {
  66. $value = array_slice(func_get_args(), 3);
  67. $o_set = $this->getMockForAbstractClass(
  68. 'Tox\\Application\\Model\\Set',
  69. array(),
  70. '',
  71. true,
  72. true,
  73. true,
  74. array($func)
  75. );
  76. $a_args = array($this->equalTo($attr));
  77. foreach ($value as $ii => $jj) {
  78. $a_args[1 + $ii] = $this->equalTo($jj);
  79. }
  80. call_user_func_array(array($o_set->expects($this->once())->method($func), 'with'), $a_args);
  81. $i_pos = ('filter' == substr($func, 0, 6)) ? 6 : 7;
  82. $s_func = substr($func, 0, $i_pos) . $attr . substr($func, $i_pos);
  83. call_user_func_array(array($o_set, $s_func), $value);
  84. }
  85. /**
  86. * @depends testMagicFilterAndExclude
  87. */
  88. public function testFilterBetweenConvertedToFilterEqualsOnPoint()
  89. {
  90. $f_val1 = microtime(true);
  91. $f_val2 = microtime(true);
  92. $s_attr = sha1(microtime());
  93. $o_set = $this->getMockForAbstractClass(
  94. 'Tox\\Application\\Model\\Set',
  95. array(),
  96. '',
  97. true,
  98. true,
  99. true,
  100. array('doFilter')
  101. );
  102. $o_set->expects($this->at(0))->method('doFilter')
  103. ->with(
  104. $this->equalTo('Tox\\Application\\Model\\Set::filterEquals'),
  105. $this->equalTo($s_attr),
  106. $this->equalTo($f_val1)
  107. )->will($this->returnValue($o_set));
  108. $o_set->expects($this->at(1))->method('doFilter')
  109. ->with(
  110. $this->equalTo('Tox\\Application\\Model\\Set::filterEquals'),
  111. $this->equalTo($s_attr),
  112. $this->equalTo($f_val2)
  113. )->will($this->returnValue($o_set));
  114. $o_set->expects($this->at(2))->method('doFilter')
  115. ->with(
  116. $this->equalTo('Tox\\Application\\Model\\Set::filterBetween'),
  117. $this->equalTo($s_attr),
  118. $this->equalTo(array($f_val2, $f_val1))
  119. )->will($this->returnValue($o_set));
  120. $this->assertSame($o_set, call_user_func(array($o_set, "filter{$s_attr}Between"), $f_val1, $f_val1));
  121. $this->assertSame($o_set, call_user_func(array($o_set, "filter{$s_attr}Between"), $f_val2));
  122. $this->assertSame($o_set, call_user_func(array($o_set, "filter{$s_attr}Between"), $f_val2, $f_val1));
  123. }
  124. /**
  125. * @depends testMagicFilterAndExclude
  126. */
  127. public function testExcludeBetweenConvertedToExcludeEqualsOnPoint()
  128. {
  129. $f_val1 = microtime(true);
  130. $f_val2 = microtime(true);
  131. $s_attr = sha1(microtime());
  132. $o_set = $this->getMockForAbstractClass(
  133. 'Tox\\Application\\Model\\Set',
  134. array(),
  135. '',
  136. true,
  137. true,
  138. true,
  139. array('doFilter')
  140. );
  141. $o_set->expects($this->at(0))->method('doFilter')
  142. ->with(
  143. $this->equalTo('Tox\\Application\\Model\\Set::excludeEquals'),
  144. $this->equalTo($s_attr),
  145. $this->equalTo($f_val1)
  146. )->will($this->returnValue($o_set));
  147. $o_set->expects($this->at(1))->method('doFilter')
  148. ->with(
  149. $this->equalTo('Tox\\Application\\Model\\Set::excludeEquals'),
  150. $this->equalTo($s_attr),
  151. $this->equalTo($f_val2)
  152. )->will($this->returnValue($o_set));
  153. $o_set->expects($this->at(2))->method('doFilter')
  154. ->with(
  155. $this->equalTo('Tox\\Application\\Model\\Set::excludeBetween'),
  156. $this->equalTo($s_attr),
  157. $this->equalTo(array($f_val2, $f_val1))
  158. )->will($this->returnValue($o_set));
  159. $this->assertSame($o_set, call_user_func(array($o_set, "exclude{$s_attr}Between"), $f_val1, $f_val1));
  160. $this->assertSame($o_set, call_user_func(array($o_set, "exclude{$s_attr}Between"), $f_val2));
  161. $this->assertSame($o_set, call_user_func(array($o_set, "exclude{$s_attr}Between"), $f_val2, $f_val1));
  162. }
  163. /**
  164. * @depends testMagicFilterAndExclude
  165. * @dataProvider provideFiltersAndExcludes
  166. */
  167. public function testEachFilterCauseAClone($func, $func, $attr, $value)
  168. {
  169. $value = array_slice(func_get_args(), 3);
  170. $o_set1 = $this->getMockForAbstractClass('Tox\\Application\\Model\\Set');
  171. $i_pos = ('filter' == substr($func, 0, 6)) ? 6 : 7;
  172. $s_func = substr($func, 0, $i_pos) . $attr . substr($func, $i_pos);
  173. $o_set2 = call_user_func_array(array($o_set1, $s_func), $value);
  174. $this->assertInstanceOf(get_class($o_set1), $o_set2);
  175. $this->assertNotSame($o_set1, $o_set2);
  176. }
  177. /**
  178. * @depends testEachFilterCauseAClone
  179. * @dataProvider provideFiltersAndExcludes
  180. * @expectedException Tox\Application\Model\AttributeFilteredException
  181. */
  182. public function testEachAttributeCouldFilterOrExcludeOnce($func, $func, $attr, $value)
  183. {
  184. $value = array_slice(func_get_args(), 3);
  185. $o_set = $this->getMockForAbstractClass('Tox\\Application\\Model\\Set');
  186. do {
  187. $a_another = $this->provideFiltersAndExcludes();
  188. $s_another = array_rand($a_another);
  189. $a_another = $a_another[$s_another];
  190. $s_another = $a_another[1];
  191. } while ($s_another == $func);
  192. $i_pos = ('filter' == substr($func, 0, 6)) ? 6 : 7;
  193. $s_func = substr($func, 0, $i_pos) . $attr . substr($func, $i_pos);
  194. $o_set = call_user_func_array(array($o_set, $s_func), $value);
  195. $value = array_slice($a_another, 3);
  196. $i_pos = ('filter' == substr($s_another, 0, 6)) ? 6 : 7;
  197. $s_func = substr($s_another, 0, $i_pos) . $attr . substr($s_another, $i_pos);
  198. call_user_func_array(array($o_set, $s_func), $value);
  199. }
  200. /**
  201. * @depends testEachAttributeCouldFilterOrExcludeOnce
  202. * @dataProvider provideFiltersAndExcludes
  203. */
  204. public function testDuplicateFilterCauseNothing($func, $func, $attr, $value)
  205. {
  206. $value = array_slice(func_get_args(), 3);
  207. $o_set = $this->getMockForAbstractClass('Tox\\Application\\Model\\Set');
  208. $i_pos = ('filter' == substr($func, 0, 6)) ? 6 : 7;
  209. $s_func = substr($func, 0, $i_pos) . $attr . substr($func, $i_pos);
  210. $o_set = call_user_func_array(array($o_set, $s_func), $value);
  211. $this->assertSame($o_set, call_user_func_array(array($o_set, $s_func), $value));
  212. }
  213. public function testMagicSort()
  214. {
  215. $s_attr = sha1(microtime());
  216. $c_val = (rand(0, 999) > 499) ? Set::SORT_ASC : Set::SORT_DESC;
  217. $o_set = $this->getMockForAbstractClass(
  218. 'Tox\\Application\\Model\\Set',
  219. array(),
  220. '',
  221. true,
  222. true,
  223. true,
  224. array('doSort')
  225. );
  226. $o_set->expects($this->once())->method('doSort')
  227. ->with($this->equalTo($s_attr), $this->equalTo($c_val));
  228. call_user_func(array($o_set, 'sort' . $s_attr), $c_val);
  229. }
  230. /**
  231. * @depends testMagicSort
  232. */
  233. public function testEachSortCauseAClone()
  234. {
  235. $o_set1 = $this->getMockForAbstractClass('Tox\\Application\\Model\\Set');
  236. $o_set2 = call_user_func(array($o_set1, 'sort' . sha1(microtime())));
  237. $this->assertInstanceOf(get_class($o_set1), $o_set2);
  238. $this->assertNotSame($o_set1, $o_set2);
  239. }
  240. /**
  241. * @depends testEachSortCauseAClone
  242. * @expectedException Tox\Application\Model\AttributeSortedException
  243. */
  244. public function testEachAttributeCouldSortOnce()
  245. {
  246. $s_attr = sha1(microtime());
  247. $c_val1 = (rand(0, 999) > 499) ? Set::SORT_ASC : Set::SORT_DESC;
  248. $c_val2 = (Set::SORT_ASC == $c_val1) ? Set::SORT_DESC : Set::SORT_ASC;
  249. $o_set = $this->getMockForAbstractClass('Tox\\Application\\Model\\Set');
  250. $o_set = call_user_func(array($o_set, 'sort' . $s_attr), $c_val1);
  251. call_user_func(array($o_set, 'sort' . $s_attr), $c_val2);
  252. }
  253. /**
  254. * @depends testEachAttributeCouldSortOnce
  255. */
  256. public function testDuplicateSortCauseNothing()
  257. {
  258. $s_attr = sha1(microtime());
  259. $c_val = (rand(0, 999) > 499) ? Set::SORT_ASC : Set::SORT_DESC;
  260. $o_set = $this->getMockForAbstractClass('Tox\\Application\\Model\\Set');
  261. $o_set = call_user_func(array($o_set, 'sort' . $s_attr), $c_val);
  262. $this->assertSame($o_set, call_user_func(array($o_set, 'sort' . $s_attr), $c_val));
  263. }
  264. public function testEachCropCauseAClone()
  265. {
  266. $o_set1 = $this->getMockForAbstractClass('Tox\\Application\\Model\\Set');
  267. $o_set2 = $o_set1->crop(rand(), rand());
  268. $this->assertInstanceOf(get_class($o_set1), $o_set2);
  269. $this->assertNotSame($o_set1, $o_set2);
  270. }
  271. /**
  272. * @depends testEachCropCauseAClone
  273. */
  274. public function testInvalidCropCauseNothing()
  275. {
  276. $o_set = $this->getMockForAbstractClass('Tox\\Application\\Model\\Set');
  277. $this->assertSame($o_set, $o_set->crop(0));
  278. $this->assertSame($o_set, $o_set->crop(0 - rand(), 0 - rand()));
  279. }
  280. /**
  281. * @depends testMagicFilterAndExclude
  282. * @depends testMagicSort
  283. * @expectedException Tox\Application\Model\IllegalFilterException
  284. */
  285. public function testUnexpectedMagicMethodsForbidden()
  286. {
  287. $o_set = $this->getMockForAbstractClass('Tox\\Application\\Model\\Set');
  288. call_user_func(array($o_set, 'f' . sha1(microtime())));
  289. }
  290. /**
  291. * @depends testDuplicateFilterCauseNothing
  292. * @depends testInvalidCropCauseNothing
  293. */
  294. public function testLazyReadingDao()
  295. {
  296. $o_set = $this->getMockForAbstractClass('Tox\\Application\\Model\\Set');
  297. $o_set->expects($this->never())->method('getDefaultDao');
  298. $a_filters = array();
  299. foreach ($this->provideFiltersAndExcludes() as $ii) {
  300. if (rand(0, 999) > 499) {
  301. $i_pos = ('filter' == substr($ii[1], 0, 6)) ? 6 : 7;
  302. $s_func = substr($ii[1], 0, $i_pos) . $ii[2] . substr($ii[1], $i_pos);
  303. $a_args = array_slice($ii, 3);
  304. $o_set = call_user_func_array(array($o_set, $s_func), $a_args);
  305. $a_filters[$ii[2]] = array($ii[0], (1 == count($a_args)) ? $a_args[0] : $a_args);
  306. }
  307. }
  308. $o_set = $o_set->crop(rand(), rand());
  309. }
  310. /**
  311. * @depends testLazyReadingDao
  312. */
  313. public function testCountDirectlyWithoutLoading()
  314. {
  315. $o_set = $this->getMockForAbstractClass('Tox\\Application\\Model\\Set');
  316. $a_filters = array();
  317. foreach ($this->provideFiltersAndExcludes() as $ii) {
  318. if (rand(0, 999) > 499) {
  319. $i_pos = ('filter' == substr($ii[1], 0, 6)) ? 6 : 7;
  320. $s_func = substr($ii[1], 0, $i_pos) . $ii[2] . substr($ii[1], $i_pos);
  321. $a_args = array_slice($ii, 3);
  322. $o_set = call_user_func_array(array($o_set, $s_func), $a_args);
  323. $a_filters[$ii[2]] = array($ii[0], (1 == count($a_args)) ? $a_args[0] : $a_args);
  324. }
  325. }
  326. $i_offset = rand(0, 999);
  327. $i_limit = rand(0, 999);
  328. $i_size = rand(0, 999);
  329. $o_set = $o_set->crop($i_offset, $i_limit);
  330. $o_set->expects($this->once())->method('getDefaultDao')
  331. ->will($this->returnValue($this->dao));
  332. $this->dao->expects($this->once())->method('countBy')
  333. ->with($this->equalTo($a_filters), $this->equalTo($i_offset), $this->equalTo($i_limit))
  334. ->will($this->returnValue($i_size));
  335. $this->dao->expects($this->never())->method('listBy');
  336. $this->assertEquals($i_size, count($o_set));
  337. }
  338. /**
  339. * @depends testLazyReadingDao
  340. */
  341. public function testLoadDirectlyWithoutCounting()
  342. {
  343. $o_set = $this->getMockForAbstractClass('Tox\\Application\\Model\\Set');
  344. $a_filters = array();
  345. foreach ($this->provideFiltersAndExcludes() as $ii) {
  346. if (rand(0, 999) > 499) {
  347. $i_pos = ('filter' == substr($ii[1], 0, 6)) ? 6 : 7;
  348. $s_func = substr($ii[1], 0, $i_pos) . $ii[2] . substr($ii[1], $i_pos);
  349. $a_args = array_slice($ii, 3);
  350. $o_set = call_user_func_array(array($o_set, $s_func), $a_args);
  351. $a_filters[$ii[2]] = array($ii[0], (1 == count($a_args)) ? $a_args[0] : $a_args);
  352. }
  353. }
  354. $a_orders = array();
  355. for ($ii = 0, $jj = rand(1, 9); $ii < $jj; $ii++) {
  356. $c_order = (rand(0, 999) > 499) ? Set::SORT_ASC : Set::SORT_DESC;
  357. $s_attr = sha1(microtime());
  358. $o_set = call_user_func(array($o_set, 'sort' . $s_attr), $c_order);
  359. $a_orders[$s_attr] = $c_order;
  360. }
  361. $i_offset = rand(0, 999);
  362. $i_limit = rand(0, 999);
  363. $o_set = $o_set->crop($i_offset, $i_limit);
  364. $o_set->expects($this->once())->method('getDefaultDao')
  365. ->will($this->returnValue($this->dao));
  366. $a_rows = array();
  367. $this->dao->expects($this->once())->method('listBy')
  368. ->with(
  369. $this->equalTo($a_filters),
  370. $this->equalTo($a_orders),
  371. $this->equalTo($i_offset),
  372. $this->equalTo($i_limit)
  373. )->will($this->returnValue($a_rows));
  374. $this->dao->expects($this->never())->method('countBy');
  375. $o_set->rewind();
  376. $this->assertEquals(0, count($o_set));
  377. }
  378. /**
  379. * @depends testLoadDirectlyWithoutCounting
  380. * @expectedException Tox\Application\Model\SetWithoutFiltersException
  381. */
  382. public function testFiltersRequired()
  383. {
  384. $this->getMockForAbstractClass('Tox\\Application\\Model\\Set')->rewind();
  385. }
  386. /**
  387. * @depends testFiltersRequired
  388. */
  389. public function testGenerateAllIncludedModelsEntitiesOnInit()
  390. {
  391. $a_rows = array();
  392. for ($ii = 0, $i_times = rand(1, 9); $ii < $i_times; $ii++) {
  393. $kk = array('id' => sha1($ii . microtime()));
  394. $a_rows[] = $kk;
  395. }
  396. $o_set = $this->getMockForAbstractClass('Tox\\Application\\Model\\Set', array(null, $this->dao))->crop(0, 999);
  397. $o_mod = $this->getMock('Tox\\Application\\IModel');
  398. $o_mod->staticExpects($this->exactly($i_times))->method('import')
  399. ->with($this->equalTo($o_set), $this->equalTo($this->dao))
  400. ->will($this->returnValue($o_mod));
  401. $o_set->expects($this->exactly($i_times))->method('getModelClass')
  402. ->will($this->returnValue(get_class($o_mod)));
  403. $this->dao->expects($this->once())->method('listBy')
  404. ->with($this->equalTo(array()), $this->equalTo(array()), $this->equalTo(0), $this->equalTo(999))
  405. ->will($this->returnValue($a_rows));
  406. $o_set->rewind();
  407. }
  408. /**
  409. * @depends testGenerateAllIncludedModelsEntitiesOnInit
  410. */
  411. public function testIteration()
  412. {
  413. $a_rows = array();
  414. for ($ii = 0, $i_times = rand(1, 9); $ii < $i_times; $ii++) {
  415. $kk = array('id' => sha1($ii . microtime()));
  416. $a_rows[] = $kk;
  417. }
  418. $o_set = $this->getMockForAbstractClass('Tox\\Application\\Model\\Set', array(null, $this->dao))->crop(0, 999);
  419. $o_mod = $this->getMock('Tox\\Application\\IModel');
  420. $o_mod->staticExpects($this->exactly($i_times))->method('import')
  421. ->with($this->equalTo($o_set), $this->equalTo($this->dao))
  422. ->will($this->returnValue($o_mod));
  423. $o_set->expects($this->exactly($i_times))->method('getModelClass')
  424. ->will($this->returnValue(get_class($o_mod)));
  425. $this->dao->expects($this->once())->method('listBy')
  426. ->with($this->equalTo(array()), $this->equalTo(array()), $this->equalTo(0), $this->equalTo(999))
  427. ->will($this->returnValue($a_rows));
  428. $i_counter = 0;
  429. foreach ($o_set as $ii => $jj) {
  430. $this->assertSame($o_mod, $jj);
  431. $this->assertEquals($i_counter++, $ii);
  432. }
  433. $this->assertEquals($i_times, $i_counter);
  434. }
  435. public function testGetParent()
  436. {
  437. $o_set = $this->getMockForAbstractClass('Tox\\Application\\Model\\Set');
  438. $this->assertNull($o_set->getParent());
  439. $this->assertFalse($o_set->hasParent());
  440. $o_mod = $this->getMock('Tox\\Application\\IModel');
  441. $o_set = $this->getMockForAbstractClass('Tox\\Application\\Model\\Set', array($o_mod));
  442. $this->assertSame($o_mod, $o_set->getParent());
  443. $this->assertTrue($o_set->hasParent());
  444. }
  445. /**
  446. * @depends testCountDirectlyWithoutLoading
  447. */
  448. public function testGetLength()
  449. {
  450. $o_set = $this->getMockForAbstractClass('Tox\\Application\\Model\\Set');
  451. $i_size = rand(0, 999);
  452. $o_set->expects($this->once())->method('getDefaultDao')
  453. ->will($this->returnValue($this->dao));
  454. $this->dao->expects($this->once())->method('countBy')
  455. ->with($this->equalTo(array()), $this->equalTo(0), $this->equalTo(0))
  456. ->will($this->returnValue($i_size));
  457. $this->assertEquals($i_size, $o_set->getLength());
  458. }
  459. public function testMagicMethods()
  460. {
  461. $o_set = $this->getMockForAbstractClass(
  462. 'Tox\\Application\\Model\\Set',
  463. array(),
  464. '',
  465. true,
  466. true,
  467. true,
  468. array('getParent', 'getLength')
  469. );
  470. $s_val = microtime();
  471. $o_set->expects($this->once())->method('getParent')
  472. ->will($this->returnValue($s_val));
  473. $o_set->expects($this->once())->method('getLength')
  474. ->will($this->returnValue($s_val));
  475. $this->assertEquals($s_val, $o_set->parent);
  476. $this->assertEquals($s_val, $o_set->length);
  477. }
  478. /**
  479. * @depends testLoadDirectlyWithoutCounting
  480. */
  481. public function testCommitRecursively()
  482. {
  483. $a_rows = array(
  484. array('id' => microtime())
  485. );
  486. $o_mod = $this->getMock('Tox\\Application\\IModel');
  487. $o_mod->staticExpects($this->once())->method('import')
  488. ->will($this->returnValue($o_mod));
  489. $o_set = $this->getMockForAbstractClass('Tox\\Application\\Model\\Set', array(null, $this->dao))->crop(0, 999);
  490. $o_set->expects($this->once())->method('getModelClass')
  491. ->will($this->returnValue(get_class($o_mod)));
  492. $this->dao->expects($this->once())->method('listBy')
  493. ->will($this->returnValue($a_rows));
  494. $o_set->rewind();
  495. $o_mod->expects($this->once())->method('commit')
  496. ->will($this->returnValue($o_mod));
  497. $this->assertSame($o_set, $o_set->commit());
  498. }
  499. /**
  500. * @depends testCountDirectlyWithoutLoading
  501. */
  502. public function testAppendBufferedBeforeCommit()
  503. {
  504. $o_mod = $this->getMock('Tox\\Application\\IModel');
  505. $o_set = $this->getMockForAbstractClass('Tox\\Application\\Model\\Set', array(null, $this->dao))->crop(0, 999);
  506. $o_set->expects($this->once())->method('getModelClass')
  507. ->will($this->returnValue(get_class($o_mod)));
  508. $this->dao->expects($this->once())->method('listBy')
  509. ->will($this->returnValue(array()));
  510. $this->assertFalse($o_set->isChanged());
  511. $this->assertSame($o_set, $o_set->append($o_mod));
  512. $this->assertTrue($o_set->isChanged());
  513. $this->assertFalse($o_set->has($o_mod));
  514. $this->assertEquals(0, count($o_set));
  515. }
  516. /**
  517. * @depends testAppendBufferedBeforeCommit
  518. * @expectedException Tox\Application\Model\IllegalEntityForSetException
  519. */
  520. public function testAppendCorrespondingModelsOnly()
  521. {
  522. $o_mod = $this->getMock('Tox\\Application\\IModel');
  523. $o_set = $this->getMockForAbstractClass('Tox\\Application\\Model\\Set')->crop(0, 999);
  524. $o_set->expects($this->once())->method('getModelClass')
  525. ->will($this->returnValue('Foo'));
  526. $o_set->append($o_mod);
  527. }
  528. /**
  529. * @depends testAppendBufferedBeforeCommit
  530. * @depends testLoadDirectlyWithoutCounting
  531. * @expectedException Tox\Application\Model\ModelIncludedInSetException
  532. */
  533. public function testAppendFailureForIncludedEntity()
  534. {
  535. $s_id = microtime();
  536. $o_mod = $this->getMock('Tox\\Application\\IModel');
  537. $o_mod->staticExpects($this->once())->method('import')
  538. ->will($this->returnValue($o_mod));
  539. $o_mod->expects($this->atLeastOnce())->method('getId')
  540. ->will($this->returnValue($s_id));
  541. $o_mod->expects($this->once())->method('isAlive')
  542. ->will($this->returnValue(true));
  543. $o_set = $this->getMockForAbstractClass('Tox\\Application\\Model\\Set', array(null, $this->dao))->crop(0, 999);
  544. $o_set->expects($this->atLeastOnce())->method('getModelClass')
  545. ->will($this->returnValue(get_class($o_mod)));
  546. $this->dao->expects($this->once())->method('listBy')
  547. ->will($this->returnValue(array(array('id' => $s_id))));
  548. $o_set->append($o_mod);
  549. }
  550. /**
  551. * @depends testAppendBufferedBeforeCommit
  552. * @depends testLoadDirectlyWithoutCounting
  553. */
  554. public function testAppendOnCommit()
  555. {
  556. $o_mod = $this->getMock('Tox\\Application\\IModel');
  557. $o_mod->expects($this->once())->method('commit')
  558. ->will($this->returnValue($o_mod));
  559. $o_mod->expects($this->exactly(2))->method('isAlive')
  560. ->will($this->returnValue(true));
  561. $o_set = $this->getMockForAbstractClass('Tox\\Application\\Model\\Set', array(null, $this->dao))->crop(0, 999);
  562. $o_set->expects($this->once())->method('getModelClass')
  563. ->will($this->returnValue(get_class($o_mod)));
  564. $this->dao->expects($this->once())->method('listBy')
  565. ->will($this->returnValue(array()));
  566. $this->assertFalse($o_set->isChanged());
  567. $o_set->append($o_mod)->commit();
  568. $this->assertFalse($o_set->isChanged());
  569. $this->assertTrue($o_set->has($o_mod));
  570. $this->assertEquals(1, count($o_set));
  571. $this->assertSame($o_mod, $o_set->current());
  572. }
  573. /**
  574. * @depends testCountDirectlyWithoutLoading
  575. */
  576. public function testDropBufferedBeforeCommit()
  577. {
  578. $o_mod = $this->getMock('Tox\\Application\\IModel');
  579. $o_mod->staticExpects($this->once())->method('import')
  580. ->will($this->returnValue($o_mod));
  581. $o_mod->expects($this->exactly(2))->method('isAlive')
  582. ->will($this->returnValue(true));
  583. $o_set = $this->getMockForAbstractClass('Tox\\Application\\Model\\Set', array(null, $this->dao))->crop(0, 999);
  584. $o_set->expects($this->atLeastOnce())->method('getModelClass')
  585. ->will($this->returnValue(get_class($o_mod)));
  586. $this->dao->expects($this->once())->method('listBy')
  587. ->will($this->returnValue(array(array('id' => microtime()))));
  588. $this->assertFalse($o_set->isChanged());
  589. $this->assertSame($o_set, $o_set->drop($o_mod));
  590. $this->assertTrue($o_set->isChanged());
  591. $this->assertTrue($o_set->has($o_mod));
  592. $this->assertEquals(1, count($o_set));
  593. }
  594. /**
  595. * @depends testDropBufferedBeforeCommit
  596. * @expectedException Tox\Application\Model\IllegalEntityForSetException
  597. */
  598. public function testDropCorrespondingModelsOnly()
  599. {
  600. $o_mod = $this->getMock('Tox\\Application\\IModel');
  601. $o_set = $this->getMockForAbstractClass('Tox\\Application\\Model\\Set')->crop(0, 999);
  602. $o_set->expects($this->once())->method('getModelClass')
  603. ->will($this->returnValue('Foo'));
  604. $o_set->drop($o_mod);
  605. }
  606. /**
  607. * @depends testDropBufferedBeforeCommit
  608. * @depends testLoadDirectlyWithoutCounting
  609. * @expectedException Tox\Application\Model\PreparedModelToDropException
  610. */
  611. public function testDropFailureForPreparedEntity()
  612. {
  613. $s_id = microtime();
  614. $o_mod = $this->getMock('Tox\\Application\\IModel');
  615. $o_mod->expects($this->once())->method('isAlive')
  616. ->will($this->returnValue(false));
  617. $o_set = $this->getMockForAbstractClass('Tox\\Application\\Model\\Set', array(null, $this->dao))->crop(0, 999);
  618. $o_set->expects($this->atLeastOnce())->method('getModelClass')
  619. ->will($this->returnValue(get_class($o_mod)));
  620. $this->dao->expects($this->once())->method('listBy')
  621. ->will($this->returnValue(array()));
  622. $o_set->drop($o_mod);
  623. }
  624. /**
  625. * @depends testDropBufferedBeforeCommit
  626. * @depends testLoadDirectlyWithoutCounting
  627. */
  628. public function testDropOnCommit()
  629. {
  630. $s_id = microtime();
  631. $o_mod = $this->getMock('Tox\\Application\\IModel');
  632. $o_mod->staticExpects($this->once())->method('import')
  633. ->will($this->returnValue($o_mod));
  634. $o_mod->expects($this->atLeastOnce())->method('getId')
  635. ->will($this->returnValue($s_id));
  636. $o_mod->expects($this->exactly(2))->method('isAlive')
  637. ->will($this->returnValue(true));
  638. $o_set = $this->getMockForAbstractClass('Tox\\Application\\Model\\Set', array(null, $this->dao))->crop(0, 999);
  639. $o_set->expects($this->atLeastOnce())->method('getModelClass')
  640. ->will($this->returnValue(get_class($o_mod)));
  641. $this->dao->expects($this->once())->method('listBy')
  642. ->will($this->returnValue(array(array('id' => $s_id))));
  643. $this->assertFalse($o_set->isChanged());
  644. $o_set->drop($o_mod)->commit();
  645. $this->assertFalse($o_set->isChanged());
  646. $this->assertFalse($o_set->has($o_mod));
  647. $this->assertEquals(0, count($o_set));
  648. }
  649. public function testClear()
  650. {
  651. $s_id = microtime();
  652. $o_mod = $this->getMock('Tox\\Application\\IModel');
  653. $o_mod->staticExpects($this->atLeastOnce())->method('import')
  654. ->will($this->returnValue($o_mod));
  655. $o_mod->expects($this->atLeastOnce())->method('getId')
  656. ->will($this->returnValue($s_id));
  657. $o_set = $this->getMockForAbstractClass('Tox\\Application\\Model\\Set', array(null, $this->dao))->crop(0, 999);
  658. $o_set->expects($this->atLeastOnce())->method('getModelClass')
  659. ->will($this->returnValue(get_class($o_mod)));
  660. $this->dao->expects($this->once())->method('listBy')
  661. ->will($this->returnValue(array(array('id' => $s_id))));
  662. $this->assertFalse($o_set->isChanged());
  663. $this->assertSame($o_set, $o_set->clear());
  664. $this->assertTrue($o_set->isChanged());
  665. $this->assertEquals(1, count($o_set));
  666. $o_set->commit();
  667. $this->assertFalse($o_set->isChanged());
  668. $this->assertEquals(0, count($o_set));
  669. }
  670. public function testEnableAndDisableAsyncMode()
  671. {
  672. $o_set = $this->getMockForAbstractClass('Tox\\Application\\Model\\Set');
  673. $this->assertTrue($o_set->isAsync());
  674. $this->assertSame($o_set, $o_set->disableAsync());
  675. $this->assertFalse($o_set->isAsync());
  676. $this->assertSame($o_set, $o_set->enableAsync());
  677. $this->assertTrue($o_set->isAsync());
  678. }
  679. /**
  680. * @depends testAppendOnCommit
  681. * @depends testEnableAndDisableAsyncMode
  682. */
  683. public function testAppendImmediatelyInSyncMode()
  684. {
  685. $o_mod = $this->getMock('Tox\\Application\\IModel');
  686. $o_mod->expects($this->once())->method('commit')
  687. ->will($this->returnValue($o_mod));
  688. $o_mod->expects($this->exactly(2))->method('isAlive')
  689. ->will($this->returnValue(true));
  690. $o_set = $this->getMockForAbstractClass('Tox\\Application\\Model\\Set', array(null, $this->dao))
  691. ->disableAsync()
  692. ->crop(0, 999);
  693. $o_set->expects($this->once())->method('getModelClass')
  694. ->will($this->returnValue(get_class($o_mod)));
  695. $this->dao->expects($this->once())->method('listBy')
  696. ->will($this->returnValue(array()));
  697. $this->assertFalse($o_set->isChanged());
  698. $o_set->append($o_mod);
  699. $this->assertFalse($o_set->isChanged());
  700. $this->assertTrue($o_set->has($o_mod));
  701. $this->assertEquals(1, count($o_set));
  702. $this->assertSame($o_mod, $o_set->current());
  703. }
  704. /**
  705. * @depends testDropOnCommit
  706. * @depends testEnableAndDisableAsyncMode
  707. */
  708. public function testDropImmediatelyInSyncMode()
  709. {
  710. $s_id = microtime();
  711. $o_mod = $this->getMock('Tox\\Application\\IModel');
  712. $o_mod->staticExpects($this->once())->method('import')
  713. ->will($this->returnValue($o_mod));
  714. $o_mod->expects($this->atLeastOnce())->method('getId')
  715. ->will($this->returnValue($s_id));
  716. $o_mod->expects($this->exactly(2))->method('isAlive')
  717. ->will($this->returnValue(true));
  718. $o_set = $this->getMockForAbstractClass('Tox\\Application\\Model\\Set', array(null, $this->dao))
  719. ->disableAsync()
  720. ->crop(0, 999);
  721. $o_set->expects($this->atLeastOnce())->method('getModelClass')
  722. ->will($this->returnValue(get_class($o_mod)));
  723. $this->dao->expects($this->once())->method('listBy')
  724. ->will($this->returnValue(array(array('id' => $s_id))));
  725. $this->assertFalse($o_set->isChanged());
  726. $o_set->drop($o_mod);
  727. $this->assertFalse($o_set->isChanged());
  728. $this->assertFalse($o_set->has($o_mod));
  729. $this->assertEquals(0, count($o_set));
  730. }
  731. /**
  732. * @depends testClear
  733. * @depends testEnableAndDisableAsyncMode
  734. */
  735. public function testClearImmediatelyInSyncMode()
  736. {
  737. $s_id = microtime();
  738. $o_mod = $this->getMock('Tox\\Application\\IModel');
  739. $o_mod->staticExpects($this->atLeastOnce())->method('import')
  740. ->will($this->returnValue($o_mod));
  741. $o_mod->expects($this->atLeastOnce())->method('getId')
  742. ->will($this->returnValue($s_id));
  743. $o_set = $this->getMockForAbstractClass('Tox\\Application\\Model\\Set', array(null, $this->dao))
  744. ->disableAsync()
  745. ->crop(0, 999);
  746. $o_set->expects($this->atLeastOnce())->method('getModelClass')
  747. ->will($this->returnValue(get_class($o_mod)));
  748. $this->dao->expects($this->once())->method('listBy')
  749. ->will($this->returnValue(array(array('id' => $s_id))));
  750. $this->assertFalse($o_set->isChanged());
  751. $this->assertSame($o_set, $o_set->clear());
  752. $this->assertFalse($o_set->isChanged());
  753. $this->assertEquals(0, count($o_set));
  754. }
  755. public function provideFiltersAndExcludes()
  756. {
  757. return array(
  758. array('filterEquals', 'filterEquals', sha1(microtime()), sha1(microtime())),
  759. array('filterGreaterThan', 'filterGreaterThan', sha1(microtime()), sha1(microtime())),
  760. array('filterGreaterOrEquals', 'filterGreaterOrEquals', sha1(microtime()), sha1(microtime())),
  761. array('filterLessThan', 'filterLessThan', sha1(microtime()), sha1(microtime())),
  762. array('filterLessOrEquals', 'filterLessOrEquals', sha1(microtime()), sha1(microtime())),
  763. array('filterBetween', 'filterBetween', sha1(microtime()), sha1(microtime()), sha1(microtime(true))),
  764. array('filterIn', 'filterIn', sha1(microtime()), array(sha1(microtime()))),
  765. array('filterLike', 'filterLike', sha1(microtime()), sha1(microtime())),
  766. array('excludeEquals', 'excludeEquals', sha1(microtime()), sha1(microtime())),
  767. array('filterLessOrEquals', 'excludeGreaterThan', sha1(microtime()), sha1(microtime())),
  768. array('filterLessThan', 'excludeGreaterOrEquals', sha1(microtime()), sha1(microtime())),
  769. array('filterGreaterOrEquals', 'excludeLessThan', sha1(microtime()), sha1(microtime())),
  770. array('filterGreaterThan', 'excludeLessOrEquals', sha1(microtime()), sha1(microtime())),
  771. array('excludeBetween', 'excludeBetween', sha1(microtime()), sha1(microtime()), sha1(microtime(true))),
  772. array('excludeIn', 'excludeIn', sha1(microtime()), array(sha1(microtime()))),
  773. array('excludeLike', 'excludeLike', sha1(microtime()), sha1(microtime()))
  774. );
  775. }
  776. }
  777. abstract class SetDummy extends Set
  778. {
  779. }
  780. // vi:ft=php fenc=utf-8 ff=unix ts=4 sts=4 et sw=4 fen fdm=indent fdl=1 tw=120