ComponentTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * ComponentTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @since CakePHP(tm) v 1.2.0.5436
  16. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  17. */
  18. namespace Cake\Test\TestCase\Controller;
  19. use Cake\Controller\Component;
  20. use Cake\Controller\ComponentRegistry;
  21. use Cake\Controller\Controller;
  22. use Cake\Core\App;
  23. use Cake\Core\Configure;
  24. use Cake\TestSuite\TestCase;
  25. use Cake\Utility\ClassRegistry;
  26. use TestApp\Controller\ComponentTestController;
  27. use TestApp\Controller\Component\AppleComponent;
  28. use TestApp\Controller\Component\OrangeComponent;
  29. /**
  30. * ComponentTest class
  31. *
  32. */
  33. class ComponentTest extends TestCase {
  34. /**
  35. * setUp method
  36. *
  37. * @return void
  38. */
  39. public function setUp() {
  40. parent::setUp();
  41. Configure::write('App.namespace', 'TestApp');
  42. $this->_pluginPaths = App::path('Plugin');
  43. }
  44. /**
  45. * test accessing inner components.
  46. *
  47. * @return void
  48. */
  49. public function testInnerComponentConstruction() {
  50. $Collection = new ComponentRegistry();
  51. $Component = new AppleComponent($Collection);
  52. $this->assertInstanceOf('TestApp\Controller\Component\OrangeComponent', $Component->Orange, 'class is wrong');
  53. }
  54. /**
  55. * test component loading
  56. *
  57. * @return void
  58. */
  59. public function testNestedComponentLoading() {
  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. $mock = $this->getMock('Cake\Event\EventManager');
  74. $controller = new Controller();
  75. $controller->setEventManager($mock);
  76. $mock->expects($this->once())
  77. ->method('attach')
  78. ->with($this->isInstanceOf('TestApp\Controller\Component\AppleComponent'));
  79. $Collection = new ComponentRegistry($controller);
  80. $Apple = $Collection->load('Apple');
  81. $this->assertInstanceOf('TestApp\Controller\Component\OrangeComponent', $Apple->Orange, 'class is wrong');
  82. }
  83. /**
  84. * test a component being used more than once.
  85. *
  86. * @return void
  87. */
  88. public function testMultipleComponentInitialize() {
  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 mutually referencing components.
  98. *
  99. * @return void
  100. */
  101. public function testSomethingReferencingCookieComponent() {
  102. $Controller = new ComponentTestController();
  103. $Controller->components = array('SomethingWithCookie');
  104. $Controller->uses = false;
  105. $Controller->constructClasses();
  106. $Controller->startupProcess();
  107. $this->assertInstanceOf('TestApp\Controller\Component\SomethingWithCookieComponent', $Controller->SomethingWithCookie);
  108. $this->assertInstanceOf('Cake\Controller\Component\CookieComponent', $Controller->SomethingWithCookie->Cookie);
  109. }
  110. }