WidgetRegistryTest.php 5.7 KB

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