ComponentTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. * setUp method
  32. *
  33. * @return void
  34. */
  35. public function setUp() {
  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. $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. $Collection = new ComponentRegistry();
  57. $Apple = new AppleComponent($Collection);
  58. $this->assertInstanceOf('TestApp\Controller\Component\OrangeComponent', $Apple->Orange, 'class is wrong');
  59. $this->assertInstanceOf('TestApp\Controller\Component\BananaComponent', $Apple->Orange->Banana, 'class is wrong');
  60. $this->assertTrue(empty($Apple->Session));
  61. $this->assertTrue(empty($Apple->Orange->Session));
  62. }
  63. /**
  64. * test that component components are not enabled in the collection.
  65. *
  66. * @return void
  67. */
  68. public function testInnerComponentsAreNotEnabled() {
  69. $mock = $this->getMock('Cake\Event\EventManager');
  70. $controller = new Controller();
  71. $controller->eventManager($mock);
  72. $mock->expects($this->once())
  73. ->method('attach')
  74. ->with($this->isInstanceOf('TestApp\Controller\Component\AppleComponent'));
  75. $Collection = new ComponentRegistry($controller);
  76. $Apple = $Collection->load('Apple');
  77. $this->assertInstanceOf('TestApp\Controller\Component\OrangeComponent', $Apple->Orange, 'class is wrong');
  78. }
  79. /**
  80. * test a component being used more than once.
  81. *
  82. * @return void
  83. */
  84. public function testMultipleComponentInitialize() {
  85. $Collection = new ComponentRegistry();
  86. $Banana = $Collection->load('Banana');
  87. $Orange = $Collection->load('Orange');
  88. $this->assertSame($Banana, $Orange->Banana, 'Should be references');
  89. $Banana->testField = 'OrangeField';
  90. $this->assertSame($Banana->testField, $Orange->Banana->testField, 'References are broken');
  91. }
  92. /**
  93. * Test mutually referencing components.
  94. *
  95. * @return void
  96. */
  97. public function testSomethingReferencingCookieComponent() {
  98. $Controller = new ComponentTestController();
  99. $Controller->components = array('SomethingWithCookie');
  100. $Controller->uses = false;
  101. $Controller->constructClasses();
  102. $Controller->startupProcess();
  103. $this->assertInstanceOf('TestApp\Controller\Component\SomethingWithCookieComponent', $Controller->SomethingWithCookie);
  104. $this->assertInstanceOf('Cake\Controller\Component\CookieComponent', $Controller->SomethingWithCookie->Cookie);
  105. }
  106. }