WidgetRegistryTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. /**
  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 WidgetRegistry($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 registry.
  56. *
  57. * @return void
  58. */
  59. public function testGetViewInstance()
  60. {
  61. $inputs = new WidgetRegistry($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 testAddWidgetsFromConfigInConstuctor()
  71. {
  72. $widgets = [
  73. 'text' => ['Cake\View\Widget\BasicWidget'],
  74. 'test_widgets',
  75. ];
  76. $inputs = new WidgetRegistry($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 testAddPluginWidgetsFromConfigInConstuctor()
  85. {
  86. Plugin::load('TestPlugin');
  87. $widgets = [
  88. 'text' => ['Cake\View\Widget\BasicWidget'],
  89. 'TestPlugin.test_widgets',
  90. ];
  91. $inputs = new WidgetRegistry($this->templates, $this->view, $widgets);
  92. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $inputs->get('text'));
  93. }
  94. /**
  95. * Test adding new widgets.
  96. *
  97. * @return void
  98. */
  99. public function testAdd()
  100. {
  101. $inputs = new WidgetRegistry($this->templates, $this->view);
  102. $result = $inputs->add([
  103. 'text' => ['Cake\View\Widget\BasicWidget'],
  104. ]);
  105. $this->assertNull($result);
  106. $result = $inputs->get('text');
  107. $this->assertInstanceOf('Cake\View\Widget\WidgetInterface', $result);
  108. $inputs = new WidgetRegistry($this->templates, $this->view);
  109. $result = $inputs->add([
  110. 'hidden' => 'Cake\View\Widget\BasicWidget',
  111. ]);
  112. $this->assertNull($result);
  113. $result = $inputs->get('hidden');
  114. $this->assertInstanceOf('Cake\View\Widget\WidgetInterface', $result);
  115. }
  116. /**
  117. * Test adding an instance of an invalid type.
  118. *
  119. * @expectedException \RuntimeException
  120. * @expectedExceptionMessage Widget objects must implement Cake\View\Widget\WidgetInterface
  121. * @return void
  122. */
  123. public function testAddInvalidType()
  124. {
  125. $inputs = new WidgetRegistry($this->templates, $this->view);
  126. $inputs->add([
  127. 'text' => new \StdClass()
  128. ]);
  129. }
  130. /**
  131. * Test getting registered widgets.
  132. *
  133. * @return void
  134. */
  135. public function testGet()
  136. {
  137. $inputs = new WidgetRegistry($this->templates, $this->view);
  138. $inputs->add([
  139. 'text' => ['Cake\View\Widget\BasicWidget'],
  140. ]);
  141. $result = $inputs->get('text');
  142. $this->assertInstanceOf('Cake\View\Widget\BasicWidget', $result);
  143. $this->assertSame($result, $inputs->get('text'));
  144. }
  145. /**
  146. * Test getting fallback widgets.
  147. *
  148. * @return void
  149. */
  150. public function testGetFallback()
  151. {
  152. $inputs = new WidgetRegistry($this->templates, $this->view);
  153. $inputs->add([
  154. '_default' => ['Cake\View\Widget\BasicWidget'],
  155. ]);
  156. $result = $inputs->get('text');
  157. $this->assertInstanceOf('Cake\View\Widget\BasicWidget', $result);
  158. $result2 = $inputs->get('hidden');
  159. $this->assertSame($result, $result2);
  160. }
  161. /**
  162. * Test getting errors
  163. *
  164. * @expectedException \RuntimeException
  165. * @expectedExceptionMessage Unknown widget "foo"
  166. * @return void
  167. */
  168. public function testGetNoFallbackError()
  169. {
  170. $inputs = new WidgetRegistry($this->templates, $this->view);
  171. $inputs->clear();
  172. $inputs->get('foo');
  173. }
  174. /**
  175. * Test getting resolve dependency
  176. *
  177. * @return void
  178. */
  179. public function testGetResolveDependency()
  180. {
  181. $inputs = new WidgetRegistry($this->templates, $this->view);
  182. $inputs->clear();
  183. $inputs->add([
  184. 'label' => ['Cake\View\Widget\LabelWidget'],
  185. 'multicheckbox' => ['Cake\View\Widget\MultiCheckboxWidget', 'label']
  186. ]);
  187. $result = $inputs->get('multicheckbox');
  188. $this->assertInstanceOf('Cake\View\Widget\MultiCheckboxWidget', $result);
  189. }
  190. /**
  191. * Test getting resolve dependency missing class
  192. *
  193. * @expectedException \RuntimeException
  194. * @expectedExceptionMessage Unable to locate widget class "TestApp\View\DerpWidget"
  195. * @return void
  196. */
  197. public function testGetResolveDependencyMissingClass()
  198. {
  199. $inputs = new WidgetRegistry($this->templates, $this->view);
  200. $inputs->add(['test' => ['TestApp\View\DerpWidget']]);
  201. $inputs->get('test');
  202. }
  203. /**
  204. * Test getting resolve dependency missing dependency
  205. *
  206. * @expectedException \RuntimeException
  207. * @expectedExceptionMessage Unknown widget "label"
  208. * @return void
  209. */
  210. public function testGetResolveDependencyMissingDependency()
  211. {
  212. $inputs = new WidgetRegistry($this->templates, $this->view);
  213. $inputs->clear();
  214. $inputs->add(['multicheckbox' => ['Cake\View\Widget\MultiCheckboxWidget', 'label']]);
  215. $inputs->get('multicheckbox');
  216. }
  217. }