ComponentTest.php 6.1 KB

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