ComponentTest.php 6.4 KB

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