WidgetRegistryTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View\Widget;
  16. use Cake\Core\Plugin;
  17. use Cake\TestSuite\TestCase;
  18. use Cake\View\StringTemplate;
  19. use Cake\View\View;
  20. use Cake\View\Widget\WidgetRegistry;
  21. /**
  22. * WidgetRegistry test case
  23. */
  24. class WidgetRegistryTestCase extends TestCase {
  25. /**
  26. * setup method
  27. *
  28. * @return void
  29. */
  30. public function setUp() {
  31. parent::setUp();
  32. $this->templates = new StringTemplate();
  33. $this->view = new View();
  34. }
  35. /**
  36. * Test adding new widgets.
  37. *
  38. * @return void
  39. */
  40. public function testAddInConstructor() {
  41. $widgets = [
  42. 'text' => ['Cake\View\Widget\Basic'],
  43. ];
  44. $inputs = new WidgetRegistry($this->templates, $this->view, $widgets);
  45. $result = $inputs->get('text');
  46. $this->assertInstanceOf('Cake\View\Widget\Basic', $result);
  47. }
  48. /**
  49. * Test loading widgets files in the app.
  50. *
  51. * @return void
  52. */
  53. public function testLoadInConstructor() {
  54. $inputs = new WidgetRegistry($this->templates, $this->view, 'test_widgets');
  55. $this->assertInstanceOf('Cake\View\Widget\Label', $inputs->get('text'));
  56. }
  57. /**
  58. * Test loading templates files from a plugin
  59. *
  60. * @return void
  61. */
  62. public function testLoadPluginInConstuctor() {
  63. Plugin::load('TestPlugin');
  64. $inputs = new WidgetRegistry($this->templates, $this->view, 'TestPlugin.test_widgets');
  65. $this->assertInstanceOf('Cake\View\Widget\Label', $inputs->get('text'));
  66. }
  67. /**
  68. * Test adding new widgets.
  69. *
  70. * @return void
  71. */
  72. public function testAdd() {
  73. $inputs = new WidgetRegistry($this->templates, $this->view);
  74. $result = $inputs->add([
  75. 'text' => ['Cake\View\Widget\Basic'],
  76. ]);
  77. $this->assertNull($result);
  78. $result = $inputs->get('text');
  79. $this->assertInstanceOf('Cake\View\Widget\WidgetInterface', $result);
  80. $inputs = new WidgetRegistry($this->templates, $this->view);
  81. $result = $inputs->add([
  82. 'hidden' => 'Cake\View\Widget\Basic',
  83. ]);
  84. $this->assertNull($result);
  85. $result = $inputs->get('hidden');
  86. $this->assertInstanceOf('Cake\View\Widget\WidgetInterface', $result);
  87. }
  88. /**
  89. * Test adding an instance of an invalid type.
  90. *
  91. * @expectedException \RuntimeException
  92. * @expectedExceptionMessage Input objects must implement Cake\View\Widget\WidgetInterface
  93. * @return void
  94. */
  95. public function testAddInvalidType() {
  96. $inputs = new WidgetRegistry($this->templates, $this->view);
  97. $inputs->add([
  98. 'text' => new \StdClass()
  99. ]);
  100. $inputs->get('text');
  101. }
  102. /**
  103. * Test getting registered widgets.
  104. *
  105. * @return void
  106. */
  107. public function testGet() {
  108. $inputs = new WidgetRegistry($this->templates, $this->view);
  109. $inputs->add([
  110. 'text' => ['Cake\View\Widget\Basic'],
  111. ]);
  112. $result = $inputs->get('text');
  113. $this->assertInstanceOf('Cake\View\Widget\Basic', $result);
  114. $this->assertSame($result, $inputs->get('text'));
  115. }
  116. /**
  117. * Test getting fallback widgets.
  118. *
  119. * @return void
  120. */
  121. public function testGetFallback() {
  122. $inputs = new WidgetRegistry($this->templates, $this->view);
  123. $inputs->add([
  124. '_default' => ['Cake\View\Widget\Basic'],
  125. ]);
  126. $result = $inputs->get('text');
  127. $this->assertInstanceOf('Cake\View\Widget\Basic', $result);
  128. $result2 = $inputs->get('hidden');
  129. $this->assertSame($result, $result2);
  130. }
  131. /**
  132. * Test getting errors
  133. *
  134. * @expectedException RuntimeException
  135. * @expectedExceptionMessage Unknown widget "foo"
  136. * @return void
  137. */
  138. public function testGetNoFallbackError() {
  139. $inputs = new WidgetRegistry($this->templates, $this->view);
  140. $inputs->clear();
  141. $inputs->get('foo');
  142. }
  143. /**
  144. * Test getting resolve dependency
  145. *
  146. * @return void
  147. */
  148. public function testGetResolveDependency() {
  149. $inputs = new WidgetRegistry($this->templates, $this->view);
  150. $inputs->clear();
  151. $inputs->add([
  152. 'label' => ['Cake\View\Widget\Label'],
  153. 'multicheckbox' => ['Cake\View\Widget\MultiCheckbox', 'label']
  154. ]);
  155. $result = $inputs->get('multicheckbox');
  156. $this->assertInstanceOf('Cake\View\Widget\MultiCheckbox', $result);
  157. }
  158. /**
  159. * Test getting resolve dependency missing class
  160. *
  161. * @expectedException RuntimeException
  162. * @expectedExceptionMessage Unable to locate widget class "TestApp\View\Derp"
  163. * @return void
  164. */
  165. public function testGetResolveDependencyMissingClass() {
  166. $inputs = new WidgetRegistry($this->templates, $this->view);
  167. $inputs->add(['test' => ['TestApp\View\Derp']]);
  168. $inputs->get('test');
  169. }
  170. /**
  171. * Test getting resolve dependency missing dependency
  172. *
  173. * @expectedException RuntimeException
  174. * @expectedExceptionMessage Unknown widget "label"
  175. * @return void
  176. */
  177. public function testGetResolveDependencyMissingDependency() {
  178. $inputs = new WidgetRegistry($this->templates, $this->view);
  179. $inputs->clear();
  180. $inputs->add(['multicheckbox' => ['Cake\View\Widget\MultiCheckbox', 'label']]);
  181. $inputs->get('multicheckbox');
  182. }
  183. }