ComponentTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 TestApp\Controller\ComponentTestController;
  22. use TestApp\Controller\Component\AppleComponent;
  23. /**
  24. * ComponentTest class
  25. *
  26. */
  27. class ComponentTest extends TestCase
  28. {
  29. /**
  30. * setUp method
  31. *
  32. * @return void
  33. */
  34. public function setUp()
  35. {
  36. parent::setUp();
  37. Configure::write('App.namespace', 'TestApp');
  38. $this->_pluginPaths = App::path('Plugin');
  39. }
  40. /**
  41. * test accessing inner components.
  42. *
  43. * @return void
  44. */
  45. public function testInnerComponentConstruction()
  46. {
  47. $Collection = new ComponentRegistry();
  48. $Component = new AppleComponent($Collection);
  49. $this->assertInstanceOf('TestApp\Controller\Component\OrangeComponent', $Component->Orange, 'class is wrong');
  50. }
  51. /**
  52. * test component loading
  53. *
  54. * @return void
  55. */
  56. public function testNestedComponentLoading()
  57. {
  58. $Collection = new ComponentRegistry();
  59. $Apple = new AppleComponent($Collection);
  60. $this->assertInstanceOf('TestApp\Controller\Component\OrangeComponent', $Apple->Orange, 'class is wrong');
  61. $this->assertInstanceOf('TestApp\Controller\Component\BananaComponent', $Apple->Orange->Banana, 'class is wrong');
  62. $this->assertTrue(empty($Apple->Session));
  63. $this->assertTrue(empty($Apple->Orange->Session));
  64. }
  65. /**
  66. * test that component components are not enabled in the collection.
  67. *
  68. * @return void
  69. */
  70. public function testInnerComponentsAreNotEnabled()
  71. {
  72. $mock = $this->getMock('Cake\Event\EventManager');
  73. $controller = new Controller();
  74. $controller->eventManager($mock);
  75. $mock->expects($this->once())
  76. ->method('on')
  77. ->with($this->isInstanceOf('TestApp\Controller\Component\AppleComponent'));
  78. $Collection = new ComponentRegistry($controller);
  79. $Apple = $Collection->load('Apple');
  80. $this->assertInstanceOf('TestApp\Controller\Component\OrangeComponent', $Apple->Orange, 'class is wrong');
  81. }
  82. /**
  83. * test a component being used more than once.
  84. *
  85. * @return void
  86. */
  87. public function testMultipleComponentInitialize()
  88. {
  89. $Collection = new ComponentRegistry();
  90. $Banana = $Collection->load('Banana');
  91. $Orange = $Collection->load('Orange');
  92. $this->assertSame($Banana, $Orange->Banana, 'Should be references');
  93. $Banana->testField = 'OrangeField';
  94. $this->assertSame($Banana->testField, $Orange->Banana->testField, 'References are broken');
  95. }
  96. /**
  97. * Test a duplicate component being loaded more than once with same and differing configurations.
  98. *
  99. * @expectedException RuntimeException
  100. * @expectedExceptionMessage The "Banana" alias has already been loaded with the following config:
  101. * @return void
  102. */
  103. public function testDuplicateComponentInitialize()
  104. {
  105. $Collection = new ComponentRegistry();
  106. $Collection->load('Banana', ['property' => ['closure' => function () {
  107. }]]);
  108. $Collection->load('Banana', ['property' => ['closure' => function () {
  109. }]]);
  110. $this->assertInstanceOf('TestApp\Controller\Component\BananaComponent', $Collection->Banana, 'class is wrong');
  111. $Collection->load('Banana', ['property' => ['differs']]);
  112. }
  113. /**
  114. * Test mutually referencing components.
  115. *
  116. * @return void
  117. */
  118. public function testSomethingReferencingCookieComponent()
  119. {
  120. $Controller = new ComponentTestController();
  121. $Controller->loadComponent('SomethingWithCookie');
  122. $Controller->startupProcess();
  123. $this->assertInstanceOf('TestApp\Controller\Component\SomethingWithCookieComponent', $Controller->SomethingWithCookie);
  124. $this->assertInstanceOf('Cake\Controller\Component\CookieComponent', $Controller->SomethingWithCookie->Cookie);
  125. }
  126. /**
  127. * Tests __debugInfo
  128. *
  129. * @return void
  130. */
  131. public function testDebugInfo()
  132. {
  133. $Collection = new ComponentRegistry();
  134. $Component = new AppleComponent($Collection);
  135. $expected = [
  136. 'components' => [
  137. 'Orange'
  138. ],
  139. 'implementedEvents' => [
  140. 'Controller.startup' => 'startup'
  141. ],
  142. '_config' => []
  143. ];
  144. $result = $Component->__debugInfo();
  145. $this->assertEquals($expected, $result);
  146. }
  147. }