ComponentTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. /**
  3. * ComponentTest file
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  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. * @package Cake.Test.Case.Controller
  15. * @since CakePHP(tm) v 1.2.0.5436
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('Controller', 'Controller');
  19. App::uses('Component', 'Controller');
  20. /**
  21. * ParamTestComponent
  22. *
  23. * @package Cake.Test.Case.Controller
  24. */
  25. class ParamTestComponent extends Component {
  26. /**
  27. * components property
  28. *
  29. * @var array
  30. */
  31. public $components = array('Banana' => array('config' => 'value'));
  32. }
  33. /**
  34. * ComponentTestController class
  35. *
  36. * @package Cake.Test.Case.Controller
  37. */
  38. class ComponentTestController extends Controller {
  39. /**
  40. * uses property
  41. *
  42. * @var array
  43. */
  44. public $uses = array();
  45. }
  46. /**
  47. * AppleComponent class
  48. *
  49. * @package Cake.Test.Case.Controller
  50. */
  51. class AppleComponent extends Component {
  52. /**
  53. * components property
  54. *
  55. * @var array
  56. */
  57. public $components = array('Orange');
  58. /**
  59. * testName property
  60. *
  61. * @var mixed
  62. */
  63. public $testName = null;
  64. /**
  65. * startup method
  66. *
  67. * @param Controller $controller
  68. * @return void
  69. */
  70. public function startup(Controller $controller) {
  71. $this->testName = $controller->name;
  72. }
  73. }
  74. /**
  75. * OrangeComponent class
  76. *
  77. * @package Cake.Test.Case.Controller
  78. */
  79. class OrangeComponent extends Component {
  80. /**
  81. * components property
  82. *
  83. * @var array
  84. */
  85. public $components = array('Banana');
  86. /**
  87. * initialize method
  88. *
  89. * @param Controller $controller
  90. * @return void
  91. */
  92. public function initialize(Controller $controller) {
  93. $this->Controller = $controller;
  94. $this->Banana->testField = 'OrangeField';
  95. }
  96. /**
  97. * startup method
  98. *
  99. * @param Controller $controller
  100. * @return string
  101. */
  102. public function startup(Controller $controller) {
  103. $controller->foo = 'pass';
  104. }
  105. }
  106. /**
  107. * BananaComponent class
  108. *
  109. * @package Cake.Test.Case.Controller
  110. */
  111. class BananaComponent extends Component {
  112. /**
  113. * testField property
  114. *
  115. * @var string
  116. */
  117. public $testField = 'BananaField';
  118. /**
  119. * startup method
  120. *
  121. * @param Controller $controller
  122. * @return string
  123. */
  124. public function startup(Controller $controller) {
  125. $controller->bar = 'fail';
  126. }
  127. }
  128. /**
  129. * MutuallyReferencingOneComponent class
  130. *
  131. * @package Cake.Test.Case.Controller
  132. */
  133. class MutuallyReferencingOneComponent extends Component {
  134. /**
  135. * components property
  136. *
  137. * @var array
  138. */
  139. public $components = array('MutuallyReferencingTwo');
  140. }
  141. /**
  142. * MutuallyReferencingTwoComponent class
  143. *
  144. * @package Cake.Test.Case.Controller
  145. */
  146. class MutuallyReferencingTwoComponent extends Component {
  147. /**
  148. * components property
  149. *
  150. * @var array
  151. */
  152. public $components = array('MutuallyReferencingOne');
  153. }
  154. /**
  155. * SomethingWithEmailComponent class
  156. *
  157. * @package Cake.Test.Case.Controller
  158. */
  159. class SomethingWithEmailComponent extends Component {
  160. /**
  161. * components property
  162. *
  163. * @var array
  164. */
  165. public $components = array('Email');
  166. }
  167. /**
  168. * ComponentTest class
  169. *
  170. * @package Cake.Test.Case.Controller
  171. */
  172. class ComponentTest extends CakeTestCase {
  173. /**
  174. * setUp method
  175. *
  176. * @return void
  177. */
  178. public function setUp() {
  179. parent::setUp();
  180. $this->_pluginPaths = App::path('plugins');
  181. App::build(array(
  182. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  183. ));
  184. }
  185. /**
  186. * test accessing inner components.
  187. *
  188. * @return void
  189. */
  190. public function testInnerComponentConstruction() {
  191. $Collection = new ComponentCollection();
  192. $Component = new AppleComponent($Collection);
  193. $this->assertInstanceOf('OrangeComponent', $Component->Orange, 'class is wrong');
  194. }
  195. /**
  196. * test component loading
  197. *
  198. * @return void
  199. */
  200. public function testNestedComponentLoading() {
  201. $Collection = new ComponentCollection();
  202. $Apple = new AppleComponent($Collection);
  203. $this->assertInstanceOf('OrangeComponent', $Apple->Orange, 'class is wrong');
  204. $this->assertInstanceOf('BananaComponent', $Apple->Orange->Banana, 'class is wrong');
  205. $this->assertTrue(empty($Apple->Session));
  206. $this->assertTrue(empty($Apple->Orange->Session));
  207. }
  208. /**
  209. * test that component components are not enabled in the collection.
  210. *
  211. * @return void
  212. */
  213. public function testInnerComponentsAreNotEnabled() {
  214. $Collection = new ComponentCollection();
  215. $Apple = $Collection->load('Apple');
  216. $this->assertInstanceOf('OrangeComponent', $Apple->Orange, 'class is wrong');
  217. $result = $Collection->enabled();
  218. $this->assertEquals(array('Apple'), $result, 'Too many components enabled.');
  219. }
  220. /**
  221. * test a component being used more than once.
  222. *
  223. * @return void
  224. */
  225. public function testMultipleComponentInitialize() {
  226. $Collection = new ComponentCollection();
  227. $Banana = $Collection->load('Banana');
  228. $Orange = $Collection->load('Orange');
  229. $this->assertSame($Banana, $Orange->Banana, 'Should be references');
  230. $Banana->testField = 'OrangeField';
  231. $this->assertSame($Banana->testField, $Orange->Banana->testField, 'References are broken');
  232. }
  233. /**
  234. * Test mutually referencing components.
  235. *
  236. * @return void
  237. */
  238. public function testSomethingReferencingEmailComponent() {
  239. $Controller = new ComponentTestController();
  240. $Controller->components = array('SomethingWithEmail');
  241. $Controller->uses = false;
  242. $Controller->constructClasses();
  243. $Controller->Components->trigger('initialize', array(&$Controller));
  244. $Controller->beforeFilter();
  245. $Controller->Components->trigger('startup', array(&$Controller));
  246. $this->assertInstanceOf('SomethingWithEmailComponent', $Controller->SomethingWithEmail);
  247. $this->assertInstanceOf('EmailComponent', $Controller->SomethingWithEmail->Email);
  248. }
  249. }