ComponentTest.php 6.0 KB

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