ObjectCollectionTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. <?php
  2. /**
  3. * ObjectCollectionTest file
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  14. * @since CakePHP(tm) v 2.0
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\Utility;
  18. use Cake\Core\Object;
  19. use Cake\Event\Event;
  20. use Cake\TestSuite\TestCase;
  21. use Cake\Utility\ObjectCollection;
  22. /**
  23. * A generic object class
  24. */
  25. class GenericObject {
  26. /**
  27. * Constructor
  28. *
  29. * @param GenericObjectCollection $collection
  30. * @param array $settings
  31. */
  32. public function __construct(GenericObjectCollection $collection, $settings = array()) {
  33. $this->_Collection = $collection;
  34. $this->settings = $settings;
  35. }
  36. }
  37. /**
  38. * First Extension of Generic Object
  39. */
  40. class FirstGenericObject extends GenericObject {
  41. /**
  42. * A generic callback
  43. */
  44. public function callback() {
  45. }
  46. }
  47. /**
  48. * Second Extension of Generic Object
  49. */
  50. class SecondGenericObject extends GenericObject {
  51. public function callback() {
  52. }
  53. }
  54. /**
  55. * Third Extension of Generic Object
  56. */
  57. class ThirdGenericObject extends GenericObject {
  58. public function callback() {
  59. }
  60. }
  61. /**
  62. * A collection of Generic objects
  63. */
  64. class GenericObjectCollection extends ObjectCollection {
  65. /**
  66. * Loads a generic object
  67. *
  68. * @param string $object Object name
  69. * @param array $settings Settings array
  70. * @return array List of loaded objects
  71. */
  72. public function load($object, $settings = array()) {
  73. list(, $name) = pluginSplit($object);
  74. if (isset($this->_loaded[$name])) {
  75. return $this->_loaded[$name];
  76. }
  77. $objectClass = $name . 'GenericObject';
  78. if (strpos($objectClass, 'Mock') === false) {
  79. $objectClass = __NAMESPACE__ . '\\' . $objectClass;
  80. }
  81. $this->_loaded[$name] = new $objectClass($this, $settings);
  82. $enable = isset($settings['enabled']) ? $settings['enabled'] : true;
  83. if ($enable === true) {
  84. $this->enable($name);
  85. }
  86. return $this->_loaded[$name];
  87. }
  88. }
  89. class ObjectCollectionTest extends TestCase {
  90. /**
  91. * setUp
  92. *
  93. * @return void
  94. */
  95. public function setUp() {
  96. parent::setUp();
  97. $this->Objects = new GenericObjectCollection();
  98. }
  99. /**
  100. * tearDown
  101. *
  102. * @return void
  103. */
  104. public function tearDown() {
  105. parent::tearDown();
  106. unset($this->Objects);
  107. }
  108. /**
  109. * test triggering callbacks on loaded helpers
  110. *
  111. * @return void
  112. */
  113. public function testLoad() {
  114. $result = $this->Objects->load('First');
  115. $this->assertInstanceOf(__NAMESPACE__ . '\FirstGenericObject', $result);
  116. $this->assertInstanceOf(__NAMESPACE__ . '\FirstGenericObject', $this->Objects->First);
  117. $result = $this->Objects->loaded();
  118. $this->assertEquals(array('First'), $result, 'loaded() results are wrong.');
  119. $this->assertTrue($this->Objects->enabled('First'));
  120. $result = $this->Objects->load('First');
  121. $this->assertSame($result, $this->Objects->First);
  122. }
  123. /**
  124. * test unload()
  125. *
  126. * @return void
  127. */
  128. public function testUnload() {
  129. $this->Objects->load('First');
  130. $this->Objects->load('Second');
  131. $result = $this->Objects->loaded();
  132. $this->assertEquals(array('First', 'Second'), $result, 'loaded objects are wrong');
  133. $this->Objects->unload('First');
  134. $this->assertFalse(isset($this->Objects->First));
  135. $this->assertTrue(isset($this->Objects->Second));
  136. $result = $this->Objects->loaded();
  137. $this->assertEquals(array('Second'), $result, 'loaded objects are wrong');
  138. $result = $this->Objects->loaded();
  139. $this->assertEquals(array('Second'), $result, 'enabled objects are wrong');
  140. }
  141. /**
  142. * Tests set()
  143. *
  144. * @return void
  145. */
  146. public function testSet() {
  147. $this->Objects->load('First');
  148. $result = $this->Objects->loaded();
  149. $this->assertEquals(array('First'), $result, 'loaded objects are wrong');
  150. $result = $this->Objects->set('First', new SecondGenericObject($this->Objects));
  151. $this->assertInstanceOf(__NAMESPACE__ . '\SecondGenericObject', $result['First'], 'set failed');
  152. $result = $this->Objects->set('Second', new SecondGenericObject($this->Objects));
  153. $this->assertInstanceOf(__NAMESPACE__ . '\SecondGenericObject', $result['Second'], 'set failed');
  154. $this->assertEquals(2, count($result));
  155. }
  156. /**
  157. * creates mock classes for testing
  158. *
  159. * @return void
  160. */
  161. protected function _makeMockClasses() {
  162. if (!class_exists('TriggerMockFirstGenericObject')) {
  163. $this->getMock(__NAMESPACE__ . '\FirstGenericObject', array(), array(), 'TriggerMockFirstGenericObject', false);
  164. }
  165. if (!class_exists('TriggerMockSecondGenericObject')) {
  166. $this->getMock(__NAMESPACE__ . '\SecondGenericObject', array(), array(), 'TriggerMockSecondGenericObject', false);
  167. }
  168. if (!class_exists('TriggerMockThirdGenericObject')) {
  169. $this->getMock(__NAMESPACE__ . '\ThirdGenericObject', array(), array(), 'TriggerMockThirdGenericObject', false);
  170. }
  171. }
  172. /**
  173. * test triggering callbacks.
  174. *
  175. * @return void
  176. */
  177. public function testTrigger() {
  178. $this->_makeMockClasses();
  179. $this->Objects->load('TriggerMockFirst');
  180. $this->Objects->load('TriggerMockSecond');
  181. $this->mockObjects[] = $this->Objects->TriggerMockFirst;
  182. $this->mockObjects[] = $this->Objects->TriggerMockSecond;
  183. $this->Objects->TriggerMockFirst->expects($this->once())
  184. ->method('callback')
  185. ->will($this->returnValue(true));
  186. $this->Objects->TriggerMockSecond->expects($this->once())
  187. ->method('callback')
  188. ->will($this->returnValue(true));
  189. $this->assertTrue($this->Objects->trigger('callback'));
  190. }
  191. /**
  192. * test trigger and disabled objects
  193. *
  194. * @return void
  195. */
  196. public function testTriggerWithDisabledObjects() {
  197. $this->_makeMockClasses();
  198. $this->Objects->load('TriggerMockFirst');
  199. $this->Objects->load('TriggerMockSecond');
  200. $this->mockObjects[] = $this->Objects->TriggerMockFirst;
  201. $this->mockObjects[] = $this->Objects->TriggerMockSecond;
  202. $this->Objects->TriggerMockFirst->expects($this->once())
  203. ->method('callback')
  204. ->will($this->returnValue(true));
  205. $this->Objects->TriggerMockSecond->expects($this->never())
  206. ->method('callback')
  207. ->will($this->returnValue(true));
  208. $this->Objects->disable('TriggerMockSecond');
  209. $this->assertTrue($this->Objects->trigger('callback', array()));
  210. }
  211. /**
  212. * test that the collectReturn option works.
  213. *
  214. * @return void
  215. */
  216. public function testTriggerWithCollectReturn() {
  217. $this->_makeMockClasses();
  218. $this->Objects->load('TriggerMockFirst');
  219. $this->Objects->load('TriggerMockSecond');
  220. $this->mockObjects[] = $this->Objects->TriggerMockFirst;
  221. $this->mockObjects[] = $this->Objects->TriggerMockSecond;
  222. $this->Objects->TriggerMockFirst->expects($this->once())
  223. ->method('callback')
  224. ->will($this->returnValue(array('one', 'two')));
  225. $this->Objects->TriggerMockSecond->expects($this->once())
  226. ->method('callback')
  227. ->will($this->returnValue(array('three', 'four')));
  228. $result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
  229. $expected = array(
  230. array('one', 'two'),
  231. array('three', 'four')
  232. );
  233. $this->assertEquals($expected, $result);
  234. }
  235. /**
  236. * test that trigger with break & breakOn works.
  237. *
  238. * @return void
  239. */
  240. public function testTriggerWithBreak() {
  241. $this->_makeMockClasses();
  242. $this->Objects->load('TriggerMockFirst');
  243. $this->Objects->load('TriggerMockSecond');
  244. $this->mockObjects[] = $this->Objects->TriggerMockFirst;
  245. $this->mockObjects[] = $this->Objects->TriggerMockSecond;
  246. $this->Objects->TriggerMockFirst->expects($this->once())
  247. ->method('callback')
  248. ->will($this->returnValue(false));
  249. $this->Objects->TriggerMockSecond->expects($this->never())
  250. ->method('callback');
  251. $result = $this->Objects->trigger(
  252. 'callback',
  253. array(),
  254. array('break' => true, 'breakOn' => false)
  255. );
  256. $this->assertFalse($result);
  257. }
  258. /**
  259. * test that trigger with modParams works.
  260. *
  261. * @return void
  262. */
  263. public function testTriggerWithModParams() {
  264. $this->_makeMockClasses();
  265. $this->Objects->load('TriggerMockFirst');
  266. $this->Objects->load('TriggerMockSecond');
  267. $this->mockObjects[] = $this->Objects->TriggerMockFirst;
  268. $this->mockObjects[] = $this->Objects->TriggerMockSecond;
  269. $this->Objects->TriggerMockFirst->expects($this->once())
  270. ->method('callback')
  271. ->with(array('value'))
  272. ->will($this->returnValue(array('new value')));
  273. $this->Objects->TriggerMockSecond->expects($this->once())
  274. ->method('callback')
  275. ->with(array('new value'))
  276. ->will($this->returnValue(array('newer value')));
  277. $result = $this->Objects->trigger(
  278. 'callback',
  279. array(array('value')),
  280. array('modParams' => 0)
  281. );
  282. $this->assertEquals(array('newer value'), $result);
  283. }
  284. /**
  285. * test that setting modParams to an index that doesn't exist doesn't cause errors.
  286. *
  287. * @expectedException Cake\Error\Exception
  288. * @return void
  289. */
  290. public function testTriggerModParamsInvalidIndex() {
  291. $this->_makeMockClasses();
  292. $this->Objects->load('TriggerMockFirst');
  293. $this->Objects->load('TriggerMockSecond');
  294. $this->mockObjects[] = $this->Objects->TriggerMockFirst;
  295. $this->mockObjects[] = $this->Objects->TriggerMockSecond;
  296. $this->Objects->TriggerMockFirst->expects($this->never())
  297. ->method('callback');
  298. $this->Objects->TriggerMockSecond->expects($this->never())
  299. ->method('callback');
  300. $this->Objects->trigger(
  301. 'callback',
  302. array(array('value')),
  303. array('modParams' => 2)
  304. );
  305. }
  306. /**
  307. * test that returning null doesn't modify parameters.
  308. *
  309. * @return void
  310. */
  311. public function testTriggerModParamsNullIgnored() {
  312. $this->_makeMockClasses();
  313. $this->Objects->load('TriggerMockFirst');
  314. $this->Objects->load('TriggerMockSecond');
  315. $this->mockObjects[] = $this->Objects->TriggerMockFirst;
  316. $this->mockObjects[] = $this->Objects->TriggerMockSecond;
  317. $this->Objects->TriggerMockFirst->expects($this->once())
  318. ->method('callback')
  319. ->with(array('value'))
  320. ->will($this->returnValue(null));
  321. $this->Objects->TriggerMockSecond->expects($this->once())
  322. ->method('callback')
  323. ->with(array('value'))
  324. ->will($this->returnValue(array('new value')));
  325. $result = $this->Objects->trigger(
  326. 'callback',
  327. array(array('value')),
  328. array('modParams' => 0)
  329. );
  330. $this->assertEquals(array('new value'), $result);
  331. }
  332. /**
  333. * test order of callbacks triggering based on priority.
  334. *
  335. * @return void
  336. */
  337. public function testTriggerPriority() {
  338. $this->_makeMockClasses();
  339. $this->Objects->load('TriggerMockFirst');
  340. $this->Objects->load('TriggerMockSecond', array('priority' => 5));
  341. $this->mockObjects[] = $this->Objects->TriggerMockFirst;
  342. $this->mockObjects[] = $this->Objects->TriggerMockSecond;
  343. $this->Objects->TriggerMockFirst->expects($this->any())
  344. ->method('callback')
  345. ->will($this->returnValue('1st'));
  346. $this->Objects->TriggerMockSecond->expects($this->any())
  347. ->method('callback')
  348. ->will($this->returnValue('2nd'));
  349. $result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
  350. $expected = array(
  351. '2nd',
  352. '1st'
  353. );
  354. $this->assertEquals($expected, $result);
  355. $this->Objects->load('TriggerMockThird', array('priority' => 7));
  356. $this->mockObjects[] = $this->Objects->TriggerMockThird;
  357. $this->Objects->TriggerMockThird->expects($this->any())
  358. ->method('callback')
  359. ->will($this->returnValue('3rd'));
  360. $result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
  361. $expected = array(
  362. '2nd',
  363. '3rd',
  364. '1st'
  365. );
  366. $this->assertEquals($expected, $result);
  367. $this->Objects->disable('TriggerMockFirst');
  368. $result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
  369. $expected = array(
  370. '2nd',
  371. '3rd'
  372. );
  373. $this->assertEquals($expected, $result);
  374. $this->Objects->enable('TriggerMockFirst');
  375. $result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
  376. $expected = array(
  377. '2nd',
  378. '3rd',
  379. '1st'
  380. );
  381. $this->assertEquals($expected, $result);
  382. $this->Objects->disable('TriggerMockThird');
  383. $result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
  384. $expected = array(
  385. '2nd',
  386. '1st'
  387. );
  388. $this->assertEquals($expected, $result);
  389. $this->Objects->enable('TriggerMockThird', false);
  390. $result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
  391. $expected = array(
  392. '2nd',
  393. '1st',
  394. '3rd'
  395. );
  396. $this->assertEquals($expected, $result);
  397. $this->Objects->setPriority('TriggerMockThird', 1);
  398. $result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
  399. $expected = array(
  400. '3rd',
  401. '2nd',
  402. '1st'
  403. );
  404. $this->assertEquals($expected, $result);
  405. $this->Objects->disable('TriggerMockThird');
  406. $this->Objects->setPriority('TriggerMockThird', 11);
  407. $result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
  408. $expected = array(
  409. '2nd',
  410. '1st'
  411. );
  412. $this->assertEquals($expected, $result);
  413. $this->Objects->enable('TriggerMockThird');
  414. $result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
  415. $expected = array(
  416. '2nd',
  417. '1st',
  418. '3rd'
  419. );
  420. $this->assertEquals($expected, $result);
  421. $this->Objects->setPriority('TriggerMockThird');
  422. $result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
  423. $expected = array(
  424. '2nd',
  425. '1st',
  426. '3rd'
  427. );
  428. $this->assertEquals($expected, $result);
  429. }
  430. /**
  431. * test normalizeObjectArray
  432. *
  433. * @return void
  434. */
  435. public function testnormalizeObjectArray() {
  436. $components = array(
  437. 'Html',
  438. 'Foo.Bar' => array('one', 'two'),
  439. 'Something',
  440. 'Banana.Apple' => array('foo' => 'bar')
  441. );
  442. $result = ObjectCollection::normalizeObjectArray($components);
  443. $expected = array(
  444. 'Html' => array('class' => 'Html', 'settings' => array()),
  445. 'Bar' => array('class' => 'Foo.Bar', 'settings' => array('one', 'two')),
  446. 'Something' => array('class' => 'Something', 'settings' => array()),
  447. 'Apple' => array('class' => 'Banana.Apple', 'settings' => array('foo' => 'bar')),
  448. );
  449. $this->assertEquals($expected, $result);
  450. // This is the result after Controller::_mergeVars
  451. $components = array(
  452. 'Html' => null,
  453. 'Foo.Bar' => array('one', 'two'),
  454. 'Something' => null,
  455. 'Banana.Apple' => array('foo' => 'bar')
  456. );
  457. $result = ObjectCollection::normalizeObjectArray($components);
  458. $this->assertEquals($expected, $result);
  459. }
  460. /**
  461. * tests that passing an instance of Cake\Event\Event to trigger will prepend the subject to the list of arguments
  462. *
  463. * @return void
  464. */
  465. public function testDispatchEventWithSubject() {
  466. $this->_makeMockClasses();
  467. $this->Objects->load('TriggerMockFirst');
  468. $this->Objects->load('TriggerMockSecond');
  469. $this->mockObjects[] = $this->Objects->TriggerMockFirst;
  470. $this->mockObjects[] = $this->Objects->TriggerMockSecond;
  471. $subjectClass = new Object();
  472. $this->Objects->TriggerMockFirst->expects($this->once())
  473. ->method('callback')
  474. ->with($subjectClass, 'first argument')
  475. ->will($this->returnValue(true));
  476. $this->Objects->TriggerMockSecond->expects($this->once())
  477. ->method('callback')
  478. ->with($subjectClass, 'first argument')
  479. ->will($this->returnValue(true));
  480. $event = new Event('callback', $subjectClass, array('first argument'));
  481. $this->assertTrue($this->Objects->trigger($event));
  482. }
  483. /**
  484. * tests that passing an instance of Cake\Event\Event to trigger with omitSubject property
  485. * will NOT prepend the subject to the list of arguments
  486. *
  487. * @return void
  488. */
  489. public function testDispatchEventNoSubject() {
  490. $this->_makeMockClasses();
  491. $this->Objects->load('TriggerMockFirst');
  492. $this->Objects->load('TriggerMockSecond');
  493. $this->mockObjects[] = $this->Objects->TriggerMockFirst;
  494. $this->mockObjects[] = $this->Objects->TriggerMockSecond;
  495. $subjectClass = new Object();
  496. $this->Objects->TriggerMockFirst->expects($this->once())
  497. ->method('callback')
  498. ->with('first argument')
  499. ->will($this->returnValue(true));
  500. $this->Objects->TriggerMockSecond->expects($this->once())
  501. ->method('callback')
  502. ->with('first argument')
  503. ->will($this->returnValue(true));
  504. $event = new Event('callback', $subjectClass, array('first argument'));
  505. $event->omitSubject = true;
  506. $this->assertTrue($this->Objects->trigger($event));
  507. }
  508. }