ComponentCollectionTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * ComponentCollectionTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2010, 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-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package cake.tests.cases.libs
  16. * @since CakePHP(tm) v 2.0
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('CookieComponent', 'Controller/Component');
  20. App::uses('SecurityComponent', 'Controller/Component');
  21. App::uses('ComponentCollection', 'Controller');
  22. /**
  23. * Extended CookieComponent
  24. */
  25. class CookieAliasComponent extends CookieComponent {
  26. }
  27. class ComponentCollectionTest extends CakeTestCase {
  28. /**
  29. * setup
  30. *
  31. * @return void
  32. */
  33. function setup() {
  34. $this->Components = new ComponentCollection();
  35. }
  36. /**
  37. * teardown
  38. *
  39. * @return void
  40. */
  41. function teardown() {
  42. unset($this->Components);
  43. }
  44. /**
  45. * test triggering callbacks on loaded helpers
  46. *
  47. * @return void
  48. */
  49. function testLoad() {
  50. $result = $this->Components->load('Cookie');
  51. $this->assertInstanceOf('CookieComponent', $result);
  52. $this->assertInstanceOf('CookieComponent', $this->Components->Cookie);
  53. $result = $this->Components->attached();
  54. $this->assertEquals(array('Cookie'), $result, 'attached() results are wrong.');
  55. $this->assertTrue($this->Components->enabled('Cookie'));
  56. $result = $this->Components->load('Cookie');
  57. $this->assertSame($result, $this->Components->Cookie);
  58. }
  59. /**
  60. * Tests loading as an alias
  61. *
  62. * @return void
  63. */
  64. function testLoadWithAlias() {
  65. $result = $this->Components->load('Cookie', array('className' => 'CookieAlias', 'somesetting' => true));
  66. $this->assertInstanceOf('CookieAliasComponent', $result);
  67. $this->assertInstanceOf('CookieAliasComponent', $this->Components->Cookie);
  68. $this->assertTrue($this->Components->Cookie->settings['somesetting']);
  69. $result = $this->Components->attached();
  70. $this->assertEquals(array('Cookie'), $result, 'attached() results are wrong.');
  71. $this->assertTrue($this->Components->enabled('Cookie'));
  72. $result = $this->Components->load('Cookie');
  73. $this->assertInstanceOf('CookieAliasComponent', $result);
  74. App::build(array('plugins' => array(LIBS . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)));
  75. $result = $this->Components->load('SomeOther', array('className' => 'TestPlugin.OtherComponent'));
  76. $this->assertInstanceOf('OtherComponentComponent', $result);
  77. $this->assertInstanceOf('OtherComponentComponent', $this->Components->SomeOther);
  78. $result = $this->Components->attached();
  79. $this->assertEquals(array('Cookie', 'SomeOther'), $result, 'attached() results are wrong.');
  80. App::build();
  81. }
  82. /**
  83. * test load and enable = false
  84. *
  85. * @return void
  86. */
  87. function testLoadWithEnableFalse() {
  88. $result = $this->Components->load('Cookie', array('enabled' => false));
  89. $this->assertInstanceOf('CookieComponent', $result);
  90. $this->assertInstanceOf('CookieComponent', $this->Components->Cookie);
  91. $this->assertFalse($this->Components->enabled('Cookie'), 'Cookie should be disabled');
  92. }
  93. /**
  94. * test missingcomponent exception
  95. *
  96. * @expectedException MissingComponentClassException
  97. * @return void
  98. */
  99. function testLoadMissingComponentFile() {
  100. $this->Components->load('ThisComponentShouldAlwaysBeMissing');
  101. }
  102. /**
  103. * test loading a plugin component.
  104. *
  105. * @return void
  106. */
  107. function testLoadPluginComponent() {
  108. App::build(array(
  109. 'plugins' => array(LIBS . 'tests' . DS . 'test_app' . DS . 'plugins' . DS),
  110. ));
  111. $result = $this->Components->load('TestPlugin.OtherComponent');
  112. $this->assertInstanceOf('OtherComponentComponent', $result, 'Component class is wrong.');
  113. $this->assertInstanceOf('OtherComponentComponent', $this->Components->OtherComponent, 'Class is wrong');
  114. App::build();
  115. }
  116. /**
  117. * test unload()
  118. *
  119. * @return void
  120. */
  121. function testUnload() {
  122. $this->Components->load('Cookie');
  123. $this->Components->load('Security');
  124. $result = $this->Components->attached();
  125. $this->assertEquals(array('Cookie', 'Security'), $result, 'loaded components is wrong');
  126. $this->Components->unload('Cookie');
  127. $this->assertFalse(isset($this->Components->Cookie));
  128. $this->assertTrue(isset($this->Components->Security));
  129. $result = $this->Components->attached();
  130. $this->assertEquals(array('Security'), $result, 'loaded components is wrong');
  131. $result = $this->Components->enabled();
  132. $this->assertEquals(array('Security'), $result, 'enabled components is wrong');
  133. }
  134. /**
  135. * test getting the controller out of the collection
  136. *
  137. * @return void
  138. */
  139. function testGetController() {
  140. $controller = $this->getMock('Controller');
  141. $controller->components = array('Security');
  142. $this->Components->init($controller);
  143. $result = $this->Components->getController();
  144. $this->assertSame($controller, $result);
  145. }
  146. }