ComponentCollectionTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * ComponentCollectionTest file
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  14. * @package Cake.Test.Case.Controller
  15. * @since CakePHP(tm) v 2.0
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('CakeResponse', 'Network');
  19. App::uses('CookieComponent', 'Controller/Component');
  20. App::uses('SecurityComponent', 'Controller/Component');
  21. App::uses('ComponentCollection', 'Controller');
  22. /**
  23. * Extended CookieComponent
  24. */
  25. class CookieAliasComponent extends CookieComponent {
  26. }
  27. class ComponentCollectionTest extends CakeTestCase {
  28. /**
  29. * setUp
  30. *
  31. * @return void
  32. */
  33. public function setUp() {
  34. parent::setUp();
  35. $this->Components = new ComponentCollection();
  36. }
  37. /**
  38. * tearDown
  39. *
  40. * @return void
  41. */
  42. public function tearDown() {
  43. parent::tearDown();
  44. unset($this->Components);
  45. }
  46. /**
  47. * test triggering callbacks on loaded helpers
  48. *
  49. * @return void
  50. */
  51. public function testLoad() {
  52. $result = $this->Components->load('Cookie');
  53. $this->assertInstanceOf('CookieComponent', $result);
  54. $this->assertInstanceOf('CookieComponent', $this->Components->Cookie);
  55. $result = $this->Components->loaded();
  56. $this->assertEquals(array('Cookie'), $result, 'loaded() results are wrong.');
  57. $this->assertTrue($this->Components->enabled('Cookie'));
  58. $result = $this->Components->load('Cookie');
  59. $this->assertSame($result, $this->Components->Cookie);
  60. }
  61. /**
  62. * Tests loading as an alias
  63. *
  64. * @return void
  65. */
  66. public function testLoadWithAlias() {
  67. $result = $this->Components->load('Cookie', array('className' => 'CookieAlias', 'somesetting' => true));
  68. $this->assertInstanceOf('CookieAliasComponent', $result);
  69. $this->assertInstanceOf('CookieAliasComponent', $this->Components->Cookie);
  70. $this->assertTrue($this->Components->Cookie->settings['somesetting']);
  71. $result = $this->Components->loaded();
  72. $this->assertEquals(array('Cookie'), $result, 'loaded() results are wrong.');
  73. $this->assertTrue($this->Components->enabled('Cookie'));
  74. $result = $this->Components->load('Cookie');
  75. $this->assertInstanceOf('CookieAliasComponent', $result);
  76. App::build(array('Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)));
  77. CakePlugin::load('TestPlugin');
  78. $result = $this->Components->load('SomeOther', array('className' => 'TestPlugin.Other'));
  79. $this->assertInstanceOf('OtherComponent', $result);
  80. $this->assertInstanceOf('OtherComponent', $this->Components->SomeOther);
  81. $result = $this->Components->loaded();
  82. $this->assertEquals(array('Cookie', 'SomeOther'), $result, 'loaded() results are wrong.');
  83. App::build();
  84. CakePlugin::unload();
  85. }
  86. /**
  87. * test load and enable = false
  88. *
  89. * @return void
  90. */
  91. public function testLoadWithEnableFalse() {
  92. $result = $this->Components->load('Cookie', array('enabled' => false));
  93. $this->assertInstanceOf('CookieComponent', $result);
  94. $this->assertInstanceOf('CookieComponent', $this->Components->Cookie);
  95. $this->assertFalse($this->Components->enabled('Cookie'), 'Cookie should be disabled');
  96. }
  97. /**
  98. * test missingcomponent exception
  99. *
  100. * @expectedException MissingComponentException
  101. * @return void
  102. */
  103. public function testLoadMissingComponent() {
  104. $this->Components->load('ThisComponentShouldAlwaysBeMissing');
  105. }
  106. /**
  107. * test loading a plugin component.
  108. *
  109. * @return void
  110. */
  111. public function testLoadPluginComponent() {
  112. App::build(array(
  113. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
  114. ));
  115. CakePlugin::load('TestPlugin');
  116. $result = $this->Components->load('TestPlugin.Other');
  117. $this->assertInstanceOf('OtherComponent', $result, 'Component class is wrong.');
  118. $this->assertInstanceOf('OtherComponent', $this->Components->Other, 'Class is wrong');
  119. App::build();
  120. CakePlugin::unload();
  121. }
  122. /**
  123. * test unload()
  124. *
  125. * @return void
  126. */
  127. public function testUnload() {
  128. $this->Components->load('Cookie');
  129. $this->Components->load('Security');
  130. $result = $this->Components->loaded();
  131. $this->assertEquals(array('Cookie', 'Security'), $result, 'loaded components is wrong');
  132. $this->Components->unload('Cookie');
  133. $this->assertFalse(isset($this->Components->Cookie));
  134. $this->assertTrue(isset($this->Components->Security));
  135. $result = $this->Components->loaded();
  136. $this->assertEquals(array('Security'), $result, 'loaded components is wrong');
  137. $result = $this->Components->enabled();
  138. $this->assertEquals(array('Security'), $result, 'enabled components is wrong');
  139. }
  140. /**
  141. * test getting the controller out of the collection
  142. *
  143. * @return void
  144. */
  145. public function testGetController() {
  146. $controller = $this->getMock('Controller');
  147. $controller->components = array('Security');
  148. $this->Components->init($controller);
  149. $result = $this->Components->getController();
  150. $this->assertSame($controller, $result);
  151. }
  152. }