WidgetRegistryTest.php 4.4 KB

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