WidgetRegistryTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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\Basic'],
  43. 'label' => ['Label'],
  44. ];
  45. $inputs = new WidgetRegistry($this->templates, $this->view, $widgets);
  46. $result = $inputs->get('text');
  47. $this->assertInstanceOf('Cake\View\Widget\Basic', $result);
  48. $result = $inputs->get('label');
  49. $this->assertInstanceOf('Cake\View\Widget\Label', $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\Label', $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\Label', $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\Basic'],
  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\Basic',
  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. $inputs->get('text');
  114. }
  115. /**
  116. * Test getting registered widgets.
  117. *
  118. * @return void
  119. */
  120. public function testGet() {
  121. $inputs = new WidgetRegistry($this->templates, $this->view);
  122. $inputs->add([
  123. 'text' => ['Cake\View\Widget\Basic'],
  124. ]);
  125. $result = $inputs->get('text');
  126. $this->assertInstanceOf('Cake\View\Widget\Basic', $result);
  127. $this->assertSame($result, $inputs->get('text'));
  128. }
  129. /**
  130. * Test getting fallback widgets.
  131. *
  132. * @return void
  133. */
  134. public function testGetFallback() {
  135. $inputs = new WidgetRegistry($this->templates, $this->view);
  136. $inputs->add([
  137. '_default' => ['Cake\View\Widget\Basic'],
  138. ]);
  139. $result = $inputs->get('text');
  140. $this->assertInstanceOf('Cake\View\Widget\Basic', $result);
  141. $result2 = $inputs->get('hidden');
  142. $this->assertSame($result, $result2);
  143. }
  144. /**
  145. * Test getting errors
  146. *
  147. * @expectedException RuntimeException
  148. * @expectedExceptionMessage Unknown widget "foo"
  149. * @return void
  150. */
  151. public function testGetNoFallbackError() {
  152. $inputs = new WidgetRegistry($this->templates, $this->view);
  153. $inputs->clear();
  154. $inputs->get('foo');
  155. }
  156. /**
  157. * Test getting resolve dependency
  158. *
  159. * @return void
  160. */
  161. public function testGetResolveDependency() {
  162. $inputs = new WidgetRegistry($this->templates, $this->view);
  163. $inputs->clear();
  164. $inputs->add([
  165. 'label' => ['Cake\View\Widget\Label'],
  166. 'multicheckbox' => ['Cake\View\Widget\MultiCheckbox', 'label']
  167. ]);
  168. $result = $inputs->get('multicheckbox');
  169. $this->assertInstanceOf('Cake\View\Widget\MultiCheckbox', $result);
  170. }
  171. /**
  172. * Test getting resolve dependency missing class
  173. *
  174. * @expectedException RuntimeException
  175. * @expectedExceptionMessage Unable to locate widget class "TestApp\View\Derp"
  176. * @return void
  177. */
  178. public function testGetResolveDependencyMissingClass() {
  179. $inputs = new WidgetRegistry($this->templates, $this->view);
  180. $inputs->add(['test' => ['TestApp\View\Derp']]);
  181. $inputs->get('test');
  182. }
  183. /**
  184. * Test getting resolve dependency missing dependency
  185. *
  186. * @expectedException RuntimeException
  187. * @expectedExceptionMessage Unknown widget "label"
  188. * @return void
  189. */
  190. public function testGetResolveDependencyMissingDependency() {
  191. $inputs = new WidgetRegistry($this->templates, $this->view);
  192. $inputs->clear();
  193. $inputs->add(['multicheckbox' => ['Cake\View\Widget\MultiCheckbox', 'label']]);
  194. $inputs->get('multicheckbox');
  195. }
  196. }