/lib/Cake/Test/Case/Controller/ComponentTest.php

https://bitbucket.org/udeshika/fake_twitter · PHP · 311 lines · 94 code · 41 blank · 176 comment · 0 complexity · 7cfe5e4c0a7d4071fe413451d7bbd32b MD5 · raw file

  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. /**
  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 mixed $controller
  81. * @return void
  82. */
  83. public function startup($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 mixed $controller
  103. * @return void
  104. */
  105. public function initialize($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) {
  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) {
  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. $this->_pluginPaths = App::path('plugins');
  193. App::build(array(
  194. 'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  195. ));
  196. }
  197. /**
  198. * tearDown method
  199. *
  200. * @return void
  201. */
  202. public function tearDown() {
  203. App::build();
  204. ClassRegistry::flush();
  205. }
  206. /**
  207. * test accessing inner components.
  208. *
  209. * @return void
  210. */
  211. public function testInnerComponentConstruction() {
  212. $Collection = new ComponentCollection();
  213. $Component = new AppleComponent($Collection);
  214. $this->assertInstanceOf('OrangeComponent', $Component->Orange, 'class is wrong');
  215. }
  216. /**
  217. * test component loading
  218. *
  219. * @return void
  220. */
  221. public function testNestedComponentLoading() {
  222. $Collection = new ComponentCollection();
  223. $Apple = new AppleComponent($Collection);
  224. $this->assertInstanceOf('OrangeComponent', $Apple->Orange, 'class is wrong');
  225. $this->assertInstanceOf('BananaComponent', $Apple->Orange->Banana, 'class is wrong');
  226. $this->assertTrue(empty($Apple->Session));
  227. $this->assertTrue(empty($Apple->Orange->Session));
  228. }
  229. /**
  230. * test that component components are not enabled in the collection.
  231. *
  232. * @return void
  233. */
  234. public function testInnerComponentsAreNotEnabled() {
  235. $Collection = new ComponentCollection();
  236. $Apple = $Collection->load('Apple');
  237. $this->assertInstanceOf('OrangeComponent', $Apple->Orange, 'class is wrong');
  238. $result = $Collection->enabled();
  239. $this->assertEquals(array('Apple'), $result, 'Too many components enabled.');
  240. }
  241. /**
  242. * test a component being used more than once.
  243. *
  244. * @return void
  245. */
  246. public function testMultipleComponentInitialize() {
  247. $Collection = new ComponentCollection();
  248. $Banana = $Collection->load('Banana');
  249. $Orange = $Collection->load('Orange');
  250. $this->assertSame($Banana, $Orange->Banana, 'Should be references');
  251. $Banana->testField = 'OrangeField';
  252. $this->assertSame($Banana->testField, $Orange->Banana->testField, 'References are broken');
  253. }
  254. /**
  255. * Test mutually referencing components.
  256. *
  257. * @return void
  258. */
  259. public function testSomethingReferencingEmailComponent() {
  260. $Controller = new ComponentTestController();
  261. $Controller->components = array('SomethingWithEmail');
  262. $Controller->uses = false;
  263. $Controller->constructClasses();
  264. $Controller->Components->trigger('initialize', array(&$Controller));
  265. $Controller->beforeFilter();
  266. $Controller->Components->trigger('startup', array(&$Controller));
  267. $this->assertInstanceOf('SomethingWithEmailComponent', $Controller->SomethingWithEmail);
  268. $this->assertInstanceOf('EmailComponent', $Controller->SomethingWithEmail->Email);
  269. }
  270. }