PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Test/Case/Utility/ObjectCollectionTest.php

http://github.com/cakephp/cakephp
PHP | 395 lines | 226 code | 58 blank | 111 comment | 4 complexity | fe401ebd2e8ba29aab21bcbaecc3f575 MD5 | raw file
Possible License(s): JSON
  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. parent::setUp();
  75. $this->Objects = new GenericObjectCollection();
  76. }
  77. /**
  78. * tearDown
  79. *
  80. * @return void
  81. */
  82. public function tearDown() {
  83. unset($this->Objects);
  84. parent::tearDown();
  85. }
  86. /**
  87. * test triggering callbacks on loaded helpers
  88. *
  89. * @return void
  90. */
  91. public function testLoad() {
  92. $result = $this->Objects->load('First');
  93. $this->assertInstanceOf('FirstGenericObject', $result);
  94. $this->assertInstanceOf('FirstGenericObject', $this->Objects->First);
  95. $result = $this->Objects->attached();
  96. $this->assertEquals(array('First'), $result, 'attached() results are wrong.');
  97. $this->assertTrue($this->Objects->enabled('First'));
  98. $result = $this->Objects->load('First');
  99. $this->assertSame($result, $this->Objects->First);
  100. }
  101. /**
  102. * test unload()
  103. *
  104. * @return void
  105. */
  106. public function testUnload() {
  107. $this->Objects->load('First');
  108. $this->Objects->load('Second');
  109. $result = $this->Objects->attached();
  110. $this->assertEquals(array('First', 'Second'), $result, 'loaded objects are wrong');
  111. $this->Objects->unload('First');
  112. $this->assertFalse(isset($this->Objects->First));
  113. $this->assertTrue(isset($this->Objects->Second));
  114. $result = $this->Objects->attached();
  115. $this->assertEquals(array('Second'), $result, 'loaded objects are wrong');
  116. $result = $this->Objects->enabled();
  117. $this->assertEquals(array('Second'), $result, 'enabled objects are wrong');
  118. }
  119. /**
  120. * Tests set()
  121. *
  122. * @return void
  123. */
  124. public function testSet() {
  125. $this->Objects->load('First');
  126. $result = $this->Objects->attached();
  127. $this->assertEquals(array('First'), $result, 'loaded objects are wrong');
  128. $result = $this->Objects->set('First', new SecondGenericObject());
  129. $this->assertInstanceOf('SecondGenericObject', $result['First'], 'set failed');
  130. $result = $this->Objects->set('Second', new SecondGenericObject());
  131. $this->assertInstanceOf('SecondGenericObject', $result['Second'], 'set failed');
  132. $this->assertEquals(count($result), 2);
  133. }
  134. /**
  135. * creates mock classes for testing
  136. *
  137. * @return void
  138. */
  139. protected function _makeMockClasses() {
  140. if (!class_exists('TriggerMockFirstGenericObject')) {
  141. $this->getMock('FirstGenericObject', array(), array(), 'TriggerMockFirstGenericObject', false);
  142. }
  143. if (!class_exists('TriggerMockSecondGenericObject')) {
  144. $this->getMock('SecondGenericObject', array(), array(), 'TriggerMockSecondGenericObject', false);
  145. }
  146. }
  147. /**
  148. * test triggering callbacks.
  149. *
  150. * @return void
  151. */
  152. public function testTrigger() {
  153. $this->_makeMockClasses();
  154. $this->Objects->load('TriggerMockFirst');
  155. $this->Objects->load('TriggerMockSecond');
  156. $this->mockObjects[] = $this->Objects->TriggerMockFirst;
  157. $this->mockObjects[] = $this->Objects->TriggerMockSecond;
  158. $this->Objects->TriggerMockFirst->expects($this->once())
  159. ->method('callback')
  160. ->will($this->returnValue(true));
  161. $this->Objects->TriggerMockSecond->expects($this->once())
  162. ->method('callback')
  163. ->will($this->returnValue(true));
  164. $this->assertTrue($this->Objects->trigger('callback'));
  165. }
  166. /**
  167. * test trigger and disabled objects
  168. *
  169. * @return void
  170. */
  171. public function testTriggerWithDisabledObjects() {
  172. $this->_makeMockClasses();
  173. $this->Objects->load('TriggerMockFirst');
  174. $this->Objects->load('TriggerMockSecond');
  175. $this->mockObjects[] = $this->Objects->TriggerMockFirst;
  176. $this->mockObjects[] = $this->Objects->TriggerMockSecond;
  177. $this->Objects->TriggerMockFirst->expects($this->once())
  178. ->method('callback')
  179. ->will($this->returnValue(true));
  180. $this->Objects->TriggerMockSecond->expects($this->never())
  181. ->method('callback')
  182. ->will($this->returnValue(true));
  183. $this->Objects->disable('TriggerMockSecond');
  184. $this->assertTrue($this->Objects->trigger('callback', array()));
  185. }
  186. /**
  187. * test that the collectReturn option works.
  188. *
  189. * @return void
  190. */
  191. public function testTriggerWithCollectReturn() {
  192. $this->_makeMockClasses();
  193. $this->Objects->load('TriggerMockFirst');
  194. $this->Objects->load('TriggerMockSecond');
  195. $this->mockObjects[] = $this->Objects->TriggerMockFirst;
  196. $this->mockObjects[] = $this->Objects->TriggerMockSecond;
  197. $this->Objects->TriggerMockFirst->expects($this->once())
  198. ->method('callback')
  199. ->will($this->returnValue(array('one', 'two')));
  200. $this->Objects->TriggerMockSecond->expects($this->once())
  201. ->method('callback')
  202. ->will($this->returnValue(array('three', 'four')));
  203. $result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
  204. $expected = array(
  205. array('one', 'two'),
  206. array('three', 'four')
  207. );
  208. $this->assertEquals($expected, $result);
  209. }
  210. /**
  211. * test that trigger with break & breakOn works.
  212. *
  213. * @return void
  214. */
  215. public function testTriggerWithBreak() {
  216. $this->_makeMockClasses();
  217. $this->Objects->load('TriggerMockFirst');
  218. $this->Objects->load('TriggerMockSecond');
  219. $this->mockObjects[] = $this->Objects->TriggerMockFirst;
  220. $this->mockObjects[] = $this->Objects->TriggerMockSecond;
  221. $this->Objects->TriggerMockFirst->expects($this->once())
  222. ->method('callback')
  223. ->will($this->returnValue(false));
  224. $this->Objects->TriggerMockSecond->expects($this->never())
  225. ->method('callback');
  226. $result = $this->Objects->trigger(
  227. 'callback',
  228. array(),
  229. array('break' => true, 'breakOn' => false)
  230. );
  231. $this->assertFalse($result);
  232. }
  233. /**
  234. * test that trigger with modParams works.
  235. *
  236. * @return void
  237. */
  238. public function testTriggerWithModParams() {
  239. $this->_makeMockClasses();
  240. $this->Objects->load('TriggerMockFirst');
  241. $this->Objects->load('TriggerMockSecond');
  242. $this->mockObjects[] = $this->Objects->TriggerMockFirst;
  243. $this->mockObjects[] = $this->Objects->TriggerMockSecond;
  244. $this->Objects->TriggerMockFirst->expects($this->once())
  245. ->method('callback')
  246. ->with(array('value'))
  247. ->will($this->returnValue(array('new value')));
  248. $this->Objects->TriggerMockSecond->expects($this->once())
  249. ->method('callback')
  250. ->with(array('new value'))
  251. ->will($this->returnValue(array('newer value')));
  252. $result = $this->Objects->trigger(
  253. 'callback',
  254. array(array('value')),
  255. array('modParams' => 0)
  256. );
  257. $this->assertEquals(array('newer value'), $result);
  258. }
  259. /**
  260. * test that setting modParams to an index that doesn't exist doesn't cause errors.
  261. *
  262. * @expectedException CakeException
  263. * @return void
  264. */
  265. public function testTriggerModParamsInvalidIndex() {
  266. $this->_makeMockClasses();
  267. $this->Objects->load('TriggerMockFirst');
  268. $this->Objects->load('TriggerMockSecond');
  269. $this->mockObjects[] = $this->Objects->TriggerMockFirst;
  270. $this->mockObjects[] = $this->Objects->TriggerMockSecond;
  271. $this->Objects->TriggerMockFirst->expects($this->never())
  272. ->method('callback');
  273. $this->Objects->TriggerMockSecond->expects($this->never())
  274. ->method('callback');
  275. $result = $this->Objects->trigger(
  276. 'callback',
  277. array(array('value')),
  278. array('modParams' => 2)
  279. );
  280. }
  281. /**
  282. * test that returrning null doesn't modify parameters.
  283. *
  284. * @return void
  285. */
  286. public function testTriggerModParamsNullIgnored() {
  287. $this->_makeMockClasses();
  288. $this->Objects->load('TriggerMockFirst');
  289. $this->Objects->load('TriggerMockSecond');
  290. $this->mockObjects[] = $this->Objects->TriggerMockFirst;
  291. $this->mockObjects[] = $this->Objects->TriggerMockSecond;
  292. $this->Objects->TriggerMockFirst->expects($this->once())
  293. ->method('callback')
  294. ->with(array('value'))
  295. ->will($this->returnValue(null));
  296. $this->Objects->TriggerMockSecond->expects($this->once())
  297. ->method('callback')
  298. ->with(array('value'))
  299. ->will($this->returnValue(array('new value')));
  300. $result = $this->Objects->trigger(
  301. 'callback',
  302. array(array('value')),
  303. array('modParams' => 0)
  304. );
  305. $this->assertEquals(array('new value'), $result);
  306. }
  307. /**
  308. * test normalizeObjectArray
  309. *
  310. * @return void
  311. */
  312. public function testnormalizeObjectArray() {
  313. $components = array(
  314. 'Html',
  315. 'Foo.Bar' => array('one', 'two'),
  316. 'Something',
  317. 'Banana.Apple' => array('foo' => 'bar')
  318. );
  319. $result = ObjectCollection::normalizeObjectArray($components);
  320. $expected = array(
  321. 'Html' => array('class' => 'Html', 'settings' => array()),
  322. 'Bar' => array('class' => 'Foo.Bar', 'settings' => array('one', 'two')),
  323. 'Something' => array('class' => 'Something', 'settings' => array()),
  324. 'Apple' => array('class' => 'Banana.Apple', 'settings' => array('foo' => 'bar')),
  325. );
  326. $this->assertEquals($expected, $result);
  327. // This is the result after Controller::_mergeVars
  328. $components = array(
  329. 'Html' => null,
  330. 'Foo.Bar' => array('one', 'two'),
  331. 'Something' => null,
  332. 'Banana.Apple' => array('foo' => 'bar')
  333. );
  334. $result = ObjectCollection::normalizeObjectArray($components);
  335. $this->assertEquals($expected, $result);
  336. }
  337. }