ComponentTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  4. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice
  8. *
  9. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  11. * @since 1.2.0
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Controller;
  15. use Cake\Controller\Component;
  16. use Cake\Controller\ComponentRegistry;
  17. use Cake\Controller\Controller;
  18. use Cake\Core\App;
  19. use Cake\Core\Configure;
  20. use Cake\TestSuite\TestCase;
  21. use Cake\Utility\ClassRegistry;
  22. use TestApp\Controller\ComponentTestController;
  23. use TestApp\Controller\Component\AppleComponent;
  24. use TestApp\Controller\Component\OrangeComponent;
  25. /**
  26. * ComponentTest class
  27. *
  28. */
  29. class ComponentTest extends TestCase
  30. {
  31. /**
  32. * setUp method
  33. *
  34. * @return void
  35. */
  36. public function setUp()
  37. {
  38. parent::setUp();
  39. Configure::write('App.namespace', 'TestApp');
  40. $this->_pluginPaths = App::path('Plugin');
  41. }
  42. /**
  43. * test accessing inner components.
  44. *
  45. * @return void
  46. */
  47. public function testInnerComponentConstruction()
  48. {
  49. $Collection = new ComponentRegistry();
  50. $Component = new AppleComponent($Collection);
  51. $this->assertInstanceOf('TestApp\Controller\Component\OrangeComponent', $Component->Orange, 'class is wrong');
  52. }
  53. /**
  54. * test component loading
  55. *
  56. * @return void
  57. */
  58. public function testNestedComponentLoading()
  59. {
  60. $Collection = new ComponentRegistry();
  61. $Apple = new AppleComponent($Collection);
  62. $this->assertInstanceOf('TestApp\Controller\Component\OrangeComponent', $Apple->Orange, 'class is wrong');
  63. $this->assertInstanceOf('TestApp\Controller\Component\BananaComponent', $Apple->Orange->Banana, 'class is wrong');
  64. $this->assertTrue(empty($Apple->Session));
  65. $this->assertTrue(empty($Apple->Orange->Session));
  66. }
  67. /**
  68. * test that component components are not enabled in the collection.
  69. *
  70. * @return void
  71. */
  72. public function testInnerComponentsAreNotEnabled()
  73. {
  74. $mock = $this->getMock('Cake\Event\EventManager');
  75. $controller = new Controller();
  76. $controller->eventManager($mock);
  77. $mock->expects($this->once())
  78. ->method('on')
  79. ->with($this->isInstanceOf('TestApp\Controller\Component\AppleComponent'));
  80. $Collection = new ComponentRegistry($controller);
  81. $Apple = $Collection->load('Apple');
  82. $this->assertInstanceOf('TestApp\Controller\Component\OrangeComponent', $Apple->Orange, 'class is wrong');
  83. }
  84. /**
  85. * test a component being used more than once.
  86. *
  87. * @return void
  88. */
  89. public function testMultipleComponentInitialize()
  90. {
  91. $Collection = new ComponentRegistry();
  92. $Banana = $Collection->load('Banana');
  93. $Orange = $Collection->load('Orange');
  94. $this->assertSame($Banana, $Orange->Banana, 'Should be references');
  95. $Banana->testField = 'OrangeField';
  96. $this->assertSame($Banana->testField, $Orange->Banana->testField, 'References are broken');
  97. }
  98. /**
  99. * Test a duplicate component being loaded more than once with same and differing configurations.
  100. *
  101. * @expectedException RuntimeException
  102. * @expectedExceptionMessage The "Banana" alias has already been loaded with the following config:
  103. * @return void
  104. */
  105. public function testDuplicateComponentInitialize()
  106. {
  107. $Collection = new ComponentRegistry();
  108. $Collection->load('Banana', ['property' => ['closure' => function () {
  109. }]]);
  110. $Collection->load('Banana', ['property' => ['closure' => function () {
  111. }]]);
  112. $this->assertInstanceOf('TestApp\Controller\Component\BananaComponent', $Collection->Banana, 'class is wrong');
  113. $Collection->load('Banana', ['property' => ['differs']]);
  114. }
  115. /**
  116. * Test mutually referencing components.
  117. *
  118. * @return void
  119. */
  120. public function testSomethingReferencingCookieComponent()
  121. {
  122. $Controller = new ComponentTestController();
  123. $Controller->loadComponent('SomethingWithCookie');
  124. $Controller->startupProcess();
  125. $this->assertInstanceOf('TestApp\Controller\Component\SomethingWithCookieComponent', $Controller->SomethingWithCookie);
  126. $this->assertInstanceOf('Cake\Controller\Component\CookieComponent', $Controller->SomethingWithCookie->Cookie);
  127. }
  128. /**
  129. * Tests __debugInfo
  130. *
  131. * @return void
  132. */
  133. public function testDebugInfo()
  134. {
  135. $Collection = new ComponentRegistry();
  136. $Component = new AppleComponent($Collection);
  137. $expected = [
  138. 'components' => [
  139. 'Orange'
  140. ],
  141. 'implementedEvents' => [
  142. 'Controller.startup' => 'startup'
  143. ],
  144. '_config' => []
  145. ];
  146. $result = $Component->__debugInfo();
  147. $this->assertEquals($expected, $result);
  148. }
  149. }