WidgetLocatorTest.php 6.5 KB

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