ComponentTest.php 6.2 KB

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