WidgetLocatorTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://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\WidgetLocator;
  21. /**
  22. * WidgetLocator test case
  23. */
  24. class WidgetLocatorTestCase extends TestCase
  25. {
  26. /**
  27. * setup method
  28. *
  29. * @return void
  30. */
  31. public function setUp()
  32. {
  33. parent::setUp();
  34. $this->templates = new StringTemplate();
  35. $this->view = new View();
  36. }
  37. /**
  38. * Test adding new widgets.
  39. *
  40. * @return void
  41. */
  42. public function testAddInConstructor()
  43. {
  44. $widgets = [
  45. 'text' => ['Cake\View\Widget\BasicWidget'],
  46. 'label' => ['Label'],
  47. ];
  48. $inputs = new WidgetLocator($this->templates, $this->view, $widgets);
  49. $result = $inputs->get('text');
  50. $this->assertInstanceOf('Cake\View\Widget\BasicWidget', $result);
  51. $result = $inputs->get('label');
  52. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $result);
  53. }
  54. /**
  55. * Test getting view instance from locator.
  56. *
  57. * @return void
  58. */
  59. public function testGetViewInstance()
  60. {
  61. $inputs = new WidgetLocator($this->templates, $this->view, []);
  62. $result = $inputs->get('_view');
  63. $this->assertInstanceOf('Cake\View\View', $result);
  64. }
  65. /**
  66. * Test loading widgets files in the app.
  67. *
  68. * @return void
  69. */
  70. public function testAddWidgetsFromConfigInConstructor()
  71. {
  72. $widgets = [
  73. 'text' => ['Cake\View\Widget\BasicWidget'],
  74. 'test_widgets',
  75. ];
  76. $inputs = new WidgetLocator($this->templates, $this->view, $widgets);
  77. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $inputs->get('text'));
  78. }
  79. /**
  80. * Test loading templates files from a plugin
  81. *
  82. * @return void
  83. */
  84. public function testAddPluginWidgetsFromConfigInConstructor()
  85. {
  86. $this->loadPlugins(['TestPlugin']);
  87. $widgets = [
  88. 'text' => ['Cake\View\Widget\BasicWidget'],
  89. 'TestPlugin.test_widgets',
  90. ];
  91. $inputs = new WidgetLocator($this->templates, $this->view, $widgets);
  92. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $inputs->get('text'));
  93. $this->clearPlugins();
  94. }
  95. /**
  96. * Test adding new widgets.
  97. *
  98. * @return void
  99. */
  100. public function testAdd()
  101. {
  102. $inputs = new WidgetLocator($this->templates, $this->view);
  103. $result = $inputs->add([
  104. 'text' => ['Cake\View\Widget\BasicWidget'],
  105. ]);
  106. $this->assertNull($result);
  107. $result = $inputs->get('text');
  108. $this->assertInstanceOf('Cake\View\Widget\WidgetInterface', $result);
  109. $inputs = new WidgetLocator($this->templates, $this->view);
  110. $result = $inputs->add([
  111. 'hidden' => 'Cake\View\Widget\BasicWidget',
  112. ]);
  113. $this->assertNull($result);
  114. $result = $inputs->get('hidden');
  115. $this->assertInstanceOf('Cake\View\Widget\WidgetInterface', $result);
  116. }
  117. /**
  118. * Test adding an instance of an invalid type.
  119. *
  120. * @return void
  121. */
  122. public function testAddInvalidType()
  123. {
  124. $this->expectException(\RuntimeException::class);
  125. $this->expectExceptionMessage('Widget objects must implement Cake\View\Widget\WidgetInterface');
  126. $inputs = new WidgetLocator($this->templates, $this->view);
  127. $inputs->add([
  128. 'text' => new \StdClass(),
  129. ]);
  130. }
  131. /**
  132. * Test getting registered widgets.
  133. *
  134. * @return void
  135. */
  136. public function testGet()
  137. {
  138. $inputs = new WidgetLocator($this->templates, $this->view);
  139. $inputs->add([
  140. 'text' => ['Cake\View\Widget\BasicWidget'],
  141. ]);
  142. $result = $inputs->get('text');
  143. $this->assertInstanceOf('Cake\View\Widget\BasicWidget', $result);
  144. $this->assertSame($result, $inputs->get('text'));
  145. }
  146. /**
  147. * Test getting fallback widgets.
  148. *
  149. * @return void
  150. */
  151. public function testGetFallback()
  152. {
  153. $inputs = new WidgetLocator($this->templates, $this->view);
  154. $inputs->add([
  155. '_default' => ['Cake\View\Widget\BasicWidget'],
  156. ]);
  157. $result = $inputs->get('text');
  158. $this->assertInstanceOf('Cake\View\Widget\BasicWidget', $result);
  159. $result2 = $inputs->get('hidden');
  160. $this->assertSame($result, $result2);
  161. }
  162. /**
  163. * Test getting errors
  164. *
  165. * @return void
  166. */
  167. public function testGetNoFallbackError()
  168. {
  169. $this->expectException(\RuntimeException::class);
  170. $this->expectExceptionMessage('Unknown widget "foo"');
  171. $inputs = new WidgetLocator($this->templates, $this->view);
  172. $inputs->clear();
  173. $inputs->get('foo');
  174. }
  175. /**
  176. * Test getting resolve dependency
  177. *
  178. * @return void
  179. */
  180. public function testGetResolveDependency()
  181. {
  182. $inputs = new WidgetLocator($this->templates, $this->view);
  183. $inputs->clear();
  184. $inputs->add([
  185. 'label' => ['Cake\View\Widget\LabelWidget'],
  186. 'multicheckbox' => ['Cake\View\Widget\MultiCheckboxWidget', 'label'],
  187. ]);
  188. $result = $inputs->get('multicheckbox');
  189. $this->assertInstanceOf('Cake\View\Widget\MultiCheckboxWidget', $result);
  190. }
  191. /**
  192. * Test getting resolve dependency missing class
  193. *
  194. * @return void
  195. */
  196. public function testGetResolveDependencyMissingClass()
  197. {
  198. $this->expectException(\RuntimeException::class);
  199. $this->expectExceptionMessage('Unable to locate widget class "TestApp\View\DerpWidget"');
  200. $inputs = new WidgetLocator($this->templates, $this->view);
  201. $inputs->add(['test' => ['TestApp\View\DerpWidget']]);
  202. $inputs->get('test');
  203. }
  204. /**
  205. * Test getting resolve dependency missing dependency
  206. *
  207. * @return void
  208. */
  209. public function testGetResolveDependencyMissingDependency()
  210. {
  211. $this->expectException(\RuntimeException::class);
  212. $this->expectExceptionMessage('Unknown widget "label"');
  213. $inputs = new WidgetLocator($this->templates, $this->view);
  214. $inputs->clear();
  215. $inputs->add(['multicheckbox' => ['Cake\View\Widget\MultiCheckboxWidget', 'label']]);
  216. $inputs->get('multicheckbox');
  217. }
  218. }