WidgetRegistryTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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\TestSuite\TestCase;
  17. use Cake\View\StringTemplate;
  18. use Cake\View\View;
  19. use Cake\View\Widget\WidgetRegistry;
  20. /**
  21. * WidgetRegistry test case
  22. */
  23. class WidgetRegistryTestCase extends TestCase {
  24. /**
  25. * setup method
  26. *
  27. * @return void
  28. */
  29. public function setUp() {
  30. parent::setUp();
  31. $this->templates = new StringTemplate();
  32. $this->view = new View();
  33. }
  34. /**
  35. * Test adding new widgets.
  36. *
  37. * @return void
  38. */
  39. public function testAddInConstructor() {
  40. $widgets = [
  41. 'text' => ['Cake\View\Widget\Basic'],
  42. ];
  43. $inputs = new WidgetRegistry($this->templates, $this->view, $widgets);
  44. $result = $inputs->get('text');
  45. $this->assertInstanceOf('Cake\View\Widget\Basic', $result);
  46. }
  47. /**
  48. * Test adding new widgets.
  49. *
  50. * @return void
  51. */
  52. public function testAdd() {
  53. $inputs = new WidgetRegistry($this->templates, $this->view);
  54. $result = $inputs->add([
  55. 'text' => ['Cake\View\Widget\Basic'],
  56. ]);
  57. $this->assertNull($result);
  58. $result = $inputs->get('text');
  59. $this->assertInstanceOf('Cake\View\Widget\WidgetInterface', $result);
  60. $inputs = new WidgetRegistry($this->templates, $this->view);
  61. $result = $inputs->add([
  62. 'hidden' => 'Cake\View\Widget\Basic',
  63. ]);
  64. $this->assertNull($result);
  65. $result = $inputs->get('hidden');
  66. $this->assertInstanceOf('Cake\View\Widget\WidgetInterface', $result);
  67. }
  68. /**
  69. * Test adding an instance of an invalid type.
  70. *
  71. * @expectedException \RuntimeException
  72. * @expectedExceptionMessage Input objects must implement Cake\View\Widget\WidgetInterface
  73. * @return void
  74. */
  75. public function testAddInvalidType() {
  76. $inputs = new WidgetRegistry($this->templates, $this->view);
  77. $inputs->add([
  78. 'text' => new \StdClass()
  79. ]);
  80. $inputs->get('text');
  81. }
  82. /**
  83. * Test getting registered widgets.
  84. *
  85. * @return void
  86. */
  87. public function testGet() {
  88. $inputs = new WidgetRegistry($this->templates, $this->view);
  89. $inputs->add([
  90. 'text' => ['Cake\View\Widget\Basic'],
  91. ]);
  92. $result = $inputs->get('text');
  93. $this->assertInstanceOf('Cake\View\Widget\Basic', $result);
  94. $this->assertSame($result, $inputs->get('text'));
  95. }
  96. /**
  97. * Test getting fallback widgets.
  98. *
  99. * @return void
  100. */
  101. public function testGetFallback() {
  102. $inputs = new WidgetRegistry($this->templates, $this->view);
  103. $inputs->add([
  104. '_default' => ['Cake\View\Widget\Basic'],
  105. ]);
  106. $result = $inputs->get('text');
  107. $this->assertInstanceOf('Cake\View\Widget\Basic', $result);
  108. $result2 = $inputs->get('hidden');
  109. $this->assertSame($result, $result2);
  110. }
  111. /**
  112. * Test getting errors
  113. *
  114. * @expectedException RuntimeException
  115. * @expectedExceptionMessage Unknown widget "foo"
  116. * @return void
  117. */
  118. public function testGetNoFallbackError() {
  119. $inputs = new WidgetRegistry($this->templates, $this->view);
  120. $inputs->clear();
  121. $inputs->get('foo');
  122. }
  123. /**
  124. * Test getting resolve dependency
  125. *
  126. * @return void
  127. */
  128. public function testGetResolveDependency() {
  129. $inputs = new WidgetRegistry($this->templates, $this->view);
  130. $inputs->clear();
  131. $inputs->add([
  132. 'label' => ['Cake\View\Widget\Label'],
  133. 'multicheckbox' => ['Cake\View\Widget\MultiCheckbox', 'label']
  134. ]);
  135. $result = $inputs->get('multicheckbox');
  136. $this->assertInstanceOf('Cake\View\Widget\MultiCheckbox', $result);
  137. }
  138. /**
  139. * Test getting resolve dependency missing class
  140. *
  141. * @expectedException RuntimeException
  142. * @expectedExceptionMessage Unable to locate widget class "TestApp\View\Derp"
  143. * @return void
  144. */
  145. public function testGetResolveDependencyMissingClass() {
  146. $inputs = new WidgetRegistry($this->templates, $this->view);
  147. $inputs->add(['test' => ['TestApp\View\Derp']]);
  148. $inputs->get('test');
  149. }
  150. /**
  151. * Test getting resolve dependency missing dependency
  152. *
  153. * @expectedException RuntimeException
  154. * @expectedExceptionMessage Unknown widget "label"
  155. * @return void
  156. */
  157. public function testGetResolveDependencyMissingDependency() {
  158. $inputs = new WidgetRegistry($this->templates, $this->view);
  159. $inputs->clear();
  160. $inputs->add(['multicheckbox' => ['Cake\View\Widget\MultiCheckbox', 'label']]);
  161. $inputs->get('multicheckbox');
  162. }
  163. }