ViewBuilderTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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.1.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View;
  16. use Cake\Core\Configure;
  17. use Cake\TestSuite\TestCase;
  18. use Cake\View\ViewBuilder;
  19. /**
  20. * View builder test case.
  21. */
  22. class ViewBuilderTest extends TestCase
  23. {
  24. /**
  25. * data provider for string properties.
  26. *
  27. * @return array
  28. */
  29. public function stringPropertyProvider()
  30. {
  31. return [
  32. ['layoutPath', 'Admin/'],
  33. ['templatePath', 'Admin/'],
  34. ['plugin', 'TestPlugin'],
  35. ['layout', 'admin'],
  36. ['theme', 'TestPlugin'],
  37. ['template', 'edit'],
  38. ['name', 'Articles'],
  39. ['autoLayout', true],
  40. ['className', 'Cake\View\JsonView'],
  41. ];
  42. }
  43. /**
  44. * data provider for array properties.
  45. *
  46. * @return array
  47. */
  48. public function arrayPropertyProvider()
  49. {
  50. return [
  51. ['helpers', ['Html', 'Form']],
  52. ['options', ['key' => 'value']],
  53. ];
  54. }
  55. /**
  56. * Test string property accessor/mutator methods.
  57. *
  58. * @dataProvider stringPropertyProvider
  59. * @return void
  60. */
  61. public function testStringProperties($property, $value)
  62. {
  63. $builder = new ViewBuilder();
  64. $this->assertNull($builder->{$property}(), 'Default value should be null');
  65. $this->assertSame($builder, $builder->{$property}($value), 'Setter returns this');
  66. $this->assertSame($value, $builder->{$property}(), 'Getter gets value.');
  67. }
  68. /**
  69. * Test array property accessor/mutator methods.
  70. *
  71. * @dataProvider arrayPropertyProvider
  72. * @return void
  73. */
  74. public function testArrayProperties($property, $value)
  75. {
  76. $builder = new ViewBuilder();
  77. $this->assertSame([], $builder->{$property}(), 'Default value should be empty list');
  78. $this->assertSame($builder, $builder->{$property}($value), 'Setter returns this');
  79. $this->assertSame($value, $builder->{$property}(), 'Getter gets value.');
  80. }
  81. /**
  82. * Test array property accessor/mutator methods.
  83. *
  84. * @dataProvider arrayPropertyProvider
  85. * @return void
  86. */
  87. public function testArrayPropertyMerge($property, $value)
  88. {
  89. $builder = new ViewBuilder();
  90. $builder->{$property}($value);
  91. $builder->{$property}(['Merged'], true);
  92. $this->assertSame(array_merge($value, ['Merged']), $builder->{$property}(), 'Should merge');
  93. $builder->{$property}($value, false);
  94. $this->assertSame($value, $builder->{$property}(), 'Should replace');
  95. }
  96. /**
  97. * test building with all the options.
  98. *
  99. * @return void
  100. */
  101. public function testBuildComplete()
  102. {
  103. $request = $this->getMock('Cake\Network\Request');
  104. $response = $this->getMock('Cake\Network\Response');
  105. $events = $this->getMock('Cake\Event\EventManager');
  106. $builder = new ViewBuilder();
  107. $builder->name('Articles')
  108. ->className('Ajax')
  109. ->template('edit')
  110. ->layout('default')
  111. ->templatePath('Articles/')
  112. ->helpers(['Form', 'Html'])
  113. ->layoutPath('Admin/')
  114. ->theme('TestTheme')
  115. ->plugin('TestPlugin');
  116. $view = $builder->build(
  117. ['one' => 'value'],
  118. $request,
  119. $response,
  120. $events
  121. );
  122. $this->assertInstanceOf('Cake\View\AjaxView', $view);
  123. $this->assertEquals('edit', $view->view);
  124. $this->assertEquals('default', $view->layout);
  125. $this->assertEquals('Articles/', $view->viewPath);
  126. $this->assertEquals('Admin/', $view->layoutPath);
  127. $this->assertEquals('TestPlugin', $view->plugin);
  128. $this->assertEquals('TestTheme', $view->theme);
  129. $this->assertSame($request, $view->request);
  130. $this->assertSame($response, $view->response);
  131. $this->assertSame($events, $view->eventManager());
  132. $this->assertSame(['one' => 'value'], $view->viewVars);
  133. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $view->Html);
  134. $this->assertInstanceOf('Cake\View\Helper\FormHelper', $view->Form);
  135. }
  136. /**
  137. * Test that the default is AppView.
  138. *
  139. * @return void
  140. */
  141. public function testBuildAppViewMissing()
  142. {
  143. Configure::write('App.namespace', 'Nope');
  144. $builder = new ViewBuilder();
  145. $view = $builder->build();
  146. $this->assertInstanceOf('Cake\View\View', $view);
  147. }
  148. /**
  149. * Test that the default is AppView.
  150. *
  151. * @return void
  152. */
  153. public function testBuildAppViewPresent()
  154. {
  155. Configure::write('App.namespace', 'TestApp');
  156. $builder = new ViewBuilder();
  157. $view = $builder->build();
  158. $this->assertInstanceOf('TestApp\View\AppView', $view);
  159. }
  160. /**
  161. * test missing view class
  162. *
  163. * @expectedException \Cake\View\Exception\MissingViewException
  164. * @expectedExceptionMessage View class "Foo" is missing.
  165. * @return void
  166. */
  167. public function testBuildMissingViewClass()
  168. {
  169. $builder = new ViewBuilder();
  170. $builder->className('Foo');
  171. $builder->build();
  172. }
  173. /**
  174. * testJsonSerialize()
  175. *
  176. * @return void
  177. */
  178. public function testJsonSerialize()
  179. {
  180. $builder = new ViewBuilder();
  181. $builder
  182. ->template('default')
  183. ->layout('test')
  184. ->helpers(['Html'])
  185. ->className('JsonView');
  186. $result = json_decode(json_encode($builder), true);
  187. $expected = [
  188. '_template' => 'default',
  189. '_layout' => 'test',
  190. '_helpers' => ['Html'],
  191. '_className' => 'JsonView',
  192. ];
  193. $this->assertEquals($expected, $result);
  194. $result = json_decode(json_encode(unserialize(serialize($builder))), true);
  195. $this->assertEquals($expected, $result);
  196. }
  197. /**
  198. * testCreateFromArray()
  199. *
  200. * @return void
  201. */
  202. public function testCreateFromArray()
  203. {
  204. $builder = new ViewBuilder();
  205. $builder
  206. ->template('default')
  207. ->layout('test')
  208. ->helpers(['Html'])
  209. ->className('JsonView');
  210. $result = json_encode($builder);
  211. $builder = new ViewBuilder();
  212. $builder->createFromArray(json_decode($result, true));
  213. $this->assertEquals('default', $builder->template());
  214. $this->assertEquals('test', $builder->layout());
  215. $this->assertEquals(['Html'], $builder->helpers());
  216. $this->assertEquals('JsonView', $builder->className());
  217. }
  218. }