ObjectCollectionTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. <?php
  2. /**
  3. * ObjectCollectionTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package Cake.Test.Case.Utility
  16. * @since CakePHP(tm) v 2.0
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('ObjectCollection', 'Utility');
  20. /**
  21. * A generic object class
  22. */
  23. class GenericObject {
  24. }
  25. /**
  26. * First Extension of Generic Object
  27. */
  28. class FirstGenericObject extends GenericObject {
  29. /**
  30. * A generic callback
  31. */
  32. public function callback() {
  33. }
  34. }
  35. /**
  36. * Second Extension of Generic Object
  37. */
  38. class SecondGenericObject extends GenericObject {
  39. public function callback() {
  40. }
  41. }
  42. /**
  43. * A collection of Generic objects
  44. */
  45. class GenericObjectCollection extends ObjectCollection {
  46. /**
  47. * Loads a generic object
  48. *
  49. * @param string $object Object name
  50. * @param array $settings Settings array
  51. * @return array List of loaded objects
  52. */
  53. public function load($object, $settings = array()) {
  54. list($plugin, $name) = pluginSplit($object);
  55. if (isset($this->_loaded[$name])) {
  56. return $this->_loaded[$name];
  57. }
  58. $objectClass = $name . 'GenericObject';
  59. $this->_loaded[$name] = new $objectClass($this, $settings);
  60. $enable = isset($settings['enabled']) ? $settings['enabled'] : true;
  61. if ($enable === true) {
  62. $this->_enabled[] = $name;
  63. }
  64. return $this->_loaded[$name];
  65. }
  66. }
  67. class ObjectCollectionTest extends CakeTestCase {
  68. /**
  69. * setup
  70. *
  71. * @return void
  72. */
  73. public function setup() {
  74. $this->Objects = new GenericObjectCollection();
  75. }
  76. /**
  77. * teardown
  78. *
  79. * @return void
  80. */
  81. public function teardown() {
  82. unset($this->Objects);
  83. }
  84. /**
  85. * test triggering callbacks on loaded helpers
  86. *
  87. * @return void
  88. */
  89. public function testLoad() {
  90. $result = $this->Objects->load('First');
  91. $this->assertInstanceOf('FirstGenericObject', $result);
  92. $this->assertInstanceOf('FirstGenericObject', $this->Objects->First);
  93. $result = $this->Objects->attached();
  94. $this->assertEquals(array('First'), $result, 'attached() results are wrong.');
  95. $this->assertTrue($this->Objects->enabled('First'));
  96. $result = $this->Objects->load('First');
  97. $this->assertSame($result, $this->Objects->First);
  98. }
  99. /**
  100. * test unload()
  101. *
  102. * @return void
  103. */
  104. public function testUnload() {
  105. $this->Objects->load('First');
  106. $this->Objects->load('Second');
  107. $result = $this->Objects->attached();
  108. $this->assertEquals(array('First', 'Second'), $result, 'loaded objects are wrong');
  109. $this->Objects->unload('First');
  110. $this->assertFalse(isset($this->Objects->First));
  111. $this->assertTrue(isset($this->Objects->Second));
  112. $result = $this->Objects->attached();
  113. $this->assertEquals(array('Second'), $result, 'loaded objects are wrong');
  114. $result = $this->Objects->enabled();
  115. $this->assertEquals(array('Second'), $result, 'enabled objects are wrong');
  116. }
  117. /**
  118. * Tests set()
  119. *
  120. * @return void
  121. */
  122. public function testSet() {
  123. $this->Objects->load('First');
  124. $result = $this->Objects->attached();
  125. $this->assertEquals(array('First'), $result, 'loaded objects are wrong');
  126. $result = $this->Objects->set('First', new SecondGenericObject());
  127. $this->assertInstanceOf('SecondGenericObject', $result['First'], 'set failed');
  128. $result = $this->Objects->set('Second', new SecondGenericObject());
  129. $this->assertInstanceOf('SecondGenericObject', $result['Second'], 'set failed');
  130. $this->assertEquals(count($result), 2);
  131. }
  132. /**
  133. * creates mock classes for testing
  134. *
  135. * @return void
  136. */
  137. protected function _makeMockClasses() {
  138. if (!class_exists('TriggerMockFirstGenericObject')) {
  139. $this->getMock('FirstGenericObject', array(), array(), 'TriggerMockFirstGenericObject', false);
  140. }
  141. if (!class_exists('TriggerMockSecondGenericObject')) {
  142. $this->getMock('SecondGenericObject', array(), array(), 'TriggerMockSecondGenericObject', false);
  143. }
  144. }
  145. /**
  146. * test triggering callbacks.
  147. *
  148. * @return void
  149. */
  150. public function testTrigger() {
  151. $this->_makeMockClasses();
  152. $this->Objects->load('TriggerMockFirst');
  153. $this->Objects->load('TriggerMockSecond');
  154. $this->mockObjects[] = $this->Objects->TriggerMockFirst;
  155. $this->mockObjects[] = $this->Objects->TriggerMockSecond;
  156. $this->Objects->TriggerMockFirst->expects($this->once())
  157. ->method('callback')
  158. ->will($this->returnValue(true));
  159. $this->Objects->TriggerMockSecond->expects($this->once())
  160. ->method('callback')
  161. ->will($this->returnValue(true));
  162. $this->assertTrue($this->Objects->trigger('callback'));
  163. }
  164. /**
  165. * test trigger and disabled objects
  166. *
  167. * @return void
  168. */
  169. public function testTriggerWithDisabledObjects() {
  170. $this->_makeMockClasses();
  171. $this->Objects->load('TriggerMockFirst');
  172. $this->Objects->load('TriggerMockSecond');
  173. $this->mockObjects[] = $this->Objects->TriggerMockFirst;
  174. $this->mockObjects[] = $this->Objects->TriggerMockSecond;
  175. $this->Objects->TriggerMockFirst->expects($this->once())
  176. ->method('callback')
  177. ->will($this->returnValue(true));
  178. $this->Objects->TriggerMockSecond->expects($this->never())
  179. ->method('callback')
  180. ->will($this->returnValue(true));
  181. $this->Objects->disable('TriggerMockSecond');
  182. $this->assertTrue($this->Objects->trigger('callback', array()));
  183. }
  184. /**
  185. * test that the collectReturn option works.
  186. *
  187. * @return void
  188. */
  189. public function testTriggerWithCollectReturn() {
  190. $this->_makeMockClasses();
  191. $this->Objects->load('TriggerMockFirst');
  192. $this->Objects->load('TriggerMockSecond');
  193. $this->mockObjects[] = $this->Objects->TriggerMockFirst;
  194. $this->mockObjects[] = $this->Objects->TriggerMockSecond;
  195. $this->Objects->TriggerMockFirst->expects($this->once())
  196. ->method('callback')
  197. ->will($this->returnValue(array('one', 'two')));
  198. $this->Objects->TriggerMockSecond->expects($this->once())
  199. ->method('callback')
  200. ->will($this->returnValue(array('three', 'four')));
  201. $result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
  202. $expected = array(
  203. array('one', 'two'),
  204. array('three', 'four')
  205. );
  206. $this->assertEquals($expected, $result);
  207. }
  208. /**
  209. * test that trigger with break & breakOn works.
  210. *
  211. * @return void
  212. */
  213. public function testTriggerWithBreak() {
  214. $this->_makeMockClasses();
  215. $this->Objects->load('TriggerMockFirst');
  216. $this->Objects->load('TriggerMockSecond');
  217. $this->mockObjects[] = $this->Objects->TriggerMockFirst;
  218. $this->mockObjects[] = $this->Objects->TriggerMockSecond;
  219. $this->Objects->TriggerMockFirst->expects($this->once())
  220. ->method('callback')
  221. ->will($this->returnValue(false));
  222. $this->Objects->TriggerMockSecond->expects($this->never())
  223. ->method('callback');
  224. $result = $this->Objects->trigger(
  225. 'callback',
  226. array(),
  227. array('break' => true, 'breakOn' => false)
  228. );
  229. $this->assertFalse($result);
  230. }
  231. /**
  232. * test that trigger with modParams works.
  233. *
  234. * @return void
  235. */
  236. public function testTriggerWithModParams() {
  237. $this->_makeMockClasses();
  238. $this->Objects->load('TriggerMockFirst');
  239. $this->Objects->load('TriggerMockSecond');
  240. $this->mockObjects[] = $this->Objects->TriggerMockFirst;
  241. $this->mockObjects[] = $this->Objects->TriggerMockSecond;
  242. $this->Objects->TriggerMockFirst->expects($this->once())
  243. ->method('callback')
  244. ->with(array('value'))
  245. ->will($this->returnValue(array('new value')));
  246. $this->Objects->TriggerMockSecond->expects($this->once())
  247. ->method('callback')
  248. ->with(array('new value'))
  249. ->will($this->returnValue(array('newer value')));
  250. $result = $this->Objects->trigger(
  251. 'callback',
  252. array(array('value')),
  253. array('modParams' => 0)
  254. );
  255. $this->assertEquals(array('newer value'), $result);
  256. }
  257. /**
  258. * test that setting modParams to an index that doesn't exist doesn't cause errors.
  259. *
  260. * @expectedException CakeException
  261. * @return void
  262. */
  263. public function testTriggerModParamsInvalidIndex() {
  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->never())
  270. ->method('callback');
  271. $this->Objects->TriggerMockSecond->expects($this->never())
  272. ->method('callback');
  273. $result = $this->Objects->trigger(
  274. 'callback',
  275. array(array('value')),
  276. array('modParams' => 2)
  277. );
  278. }
  279. /**
  280. * test that returrning null doesn't modify parameters.
  281. *
  282. * @return void
  283. */
  284. public function testTriggerModParamsNullIgnored() {
  285. $this->_makeMockClasses();
  286. $this->Objects->load('TriggerMockFirst');
  287. $this->Objects->load('TriggerMockSecond');
  288. $this->mockObjects[] = $this->Objects->TriggerMockFirst;
  289. $this->mockObjects[] = $this->Objects->TriggerMockSecond;
  290. $this->Objects->TriggerMockFirst->expects($this->once())
  291. ->method('callback')
  292. ->with(array('value'))
  293. ->will($this->returnValue(null));
  294. $this->Objects->TriggerMockSecond->expects($this->once())
  295. ->method('callback')
  296. ->with(array('value'))
  297. ->will($this->returnValue(array('new value')));
  298. $result = $this->Objects->trigger(
  299. 'callback',
  300. array(array('value')),
  301. array('modParams' => 0)
  302. );
  303. $this->assertEquals(array('new value'), $result);
  304. }
  305. /**
  306. * test normalizeObjectArray
  307. *
  308. * @return void
  309. */
  310. public function testnormalizeObjectArray() {
  311. $components = array(
  312. 'Html',
  313. 'Foo.Bar' => array('one', 'two'),
  314. 'Something',
  315. 'Banana.Apple' => array('foo' => 'bar')
  316. );
  317. $result = ObjectCollection::normalizeObjectArray($components);
  318. $expected = array(
  319. 'Html' => array('class' => 'Html', 'settings' => array()),
  320. 'Bar' => array('class' => 'Foo.Bar', 'settings' => array('one', 'two')),
  321. 'Something' => array('class' => 'Something', 'settings' => array()),
  322. 'Apple' => array('class' => 'Banana.Apple', 'settings' => array('foo' => 'bar')),
  323. );
  324. $this->assertEquals($expected, $result);
  325. // This is the result after Controller::_mergeVars
  326. $components = array(
  327. 'Html' => null,
  328. 'Foo.Bar' => array('one', 'two'),
  329. 'Something' => null,
  330. 'Banana.Apple' => array('foo' => 'bar')
  331. );
  332. $result = ObjectCollection::normalizeObjectArray($components);
  333. $this->assertEquals($expected, $result);
  334. }
  335. }