ComponentTest.php 5.1 KB

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