WidgetRegistryTest.php 5.8 KB

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