ViewVarsTraitTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  10. * @link https://cakephp.org CakePHP(tm) Project
  11. * @since 3.0.0
  12. * @license https://opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\View;
  15. use Cake\Controller\Controller;
  16. use Cake\TestSuite\TestCase;
  17. /**
  18. * ViewVarsTrait test case
  19. */
  20. class ViewVarsTraitTest extends TestCase
  21. {
  22. /**
  23. * @var \Cake\Controller\Controller;
  24. */
  25. public $subject;
  26. /**
  27. * setup
  28. *
  29. * @return void
  30. */
  31. public function setUp()
  32. {
  33. parent::setUp();
  34. $this->subject = new Controller;
  35. }
  36. /**
  37. * Test set() with one param.
  38. *
  39. * @return void
  40. */
  41. public function testSetOneParam()
  42. {
  43. $data = ['test' => 'val', 'foo' => 'bar'];
  44. $this->subject->set($data);
  45. $this->assertEquals($data, $this->subject->viewVars);
  46. $update = ['test' => 'updated'];
  47. $this->subject->set($update);
  48. $this->assertEquals('updated', $this->subject->viewVars['test']);
  49. }
  50. /**
  51. * test set() with 2 params
  52. *
  53. * @return void
  54. */
  55. public function testSetTwoParam()
  56. {
  57. $this->subject->set('testing', 'value');
  58. $this->assertEquals(['testing' => 'value'], $this->subject->viewVars);
  59. }
  60. /**
  61. * test chainable set()
  62. *
  63. * @return void
  64. */
  65. public function testSetChained()
  66. {
  67. $result = $this->subject->set('testing', 'value')
  68. ->set('foo', 'bar');
  69. $this->assertSame($this->subject, $result);
  70. $this->assertEquals(['testing' => 'value', 'foo' => 'bar'], $this->subject->viewVars);
  71. }
  72. /**
  73. * test set() with 2 params in combine mode
  74. *
  75. * @return void
  76. */
  77. public function testSetTwoParamCombined()
  78. {
  79. $keys = ['one', 'key'];
  80. $vals = ['two', 'val'];
  81. $this->subject->set($keys, $vals);
  82. $expected = ['one' => 'two', 'key' => 'val'];
  83. $this->assertEquals($expected, $this->subject->viewVars);
  84. }
  85. /**
  86. * test viewOptions() with 1 string param, merge true
  87. *
  88. * @return void
  89. */
  90. public function testAddOneViewOption()
  91. {
  92. $option = 'newOption';
  93. $this->subject->viewOptions($option);
  94. $this->assertContains($option, $this->subject->viewOptions());
  95. }
  96. /**
  97. * test viewOptions() with 2 strings in array, merge true.
  98. *
  99. * @return void
  100. */
  101. public function testAddTwoViewOption()
  102. {
  103. $this->subject->viewOptions(['oldOption'], false);
  104. $option = ['newOption', 'anotherOption'];
  105. $result = $this->subject->viewOptions($option);
  106. $expects = ['oldOption', 'newOption', 'anotherOption'];
  107. $this->assertContainsOnly('string', $result);
  108. $this->assertEquals($expects, $result);
  109. }
  110. /**
  111. * test empty params reads _viewOptions.
  112. *
  113. * @return void
  114. */
  115. public function testReadingViewOptions()
  116. {
  117. $expected = $this->subject->viewOptions(['one', 'two', 'three'], false);
  118. $result = $this->subject->viewOptions();
  119. $this->assertEquals($expected, $result);
  120. }
  121. /**
  122. * test setting $merge `false` overrides correct options.
  123. *
  124. * @return void
  125. */
  126. public function testMergeFalseViewOptions()
  127. {
  128. $this->subject->viewOptions(['one', 'two', 'three'], false);
  129. $expected = ['four', 'five', 'six'];
  130. $result = $this->subject->viewOptions($expected, false);
  131. $this->assertEquals($expected, $result);
  132. }
  133. /**
  134. * test _viewOptions is undefined and $opts is null, an empty array is returned.
  135. *
  136. * @return void
  137. */
  138. public function testUndefinedValidViewOptions()
  139. {
  140. $result = $this->subject->viewOptions([], false);
  141. $this->assertInternalType('array', $result);
  142. $this->assertEmpty($result);
  143. }
  144. /**
  145. * test that createView() updates viewVars of View instance on each call.
  146. *
  147. * @return void
  148. */
  149. public function testUptoDateViewVars()
  150. {
  151. $expected = ['one' => 'one'];
  152. $this->subject->set($expected);
  153. $this->assertEquals($expected, $this->subject->createView()->viewVars);
  154. $expected = ['one' => 'one', 'two' => 'two'];
  155. $this->subject->set($expected);
  156. $this->assertEquals($expected, $this->subject->createView()->viewVars);
  157. }
  158. /**
  159. * test that options are passed to the view builder when using createView().
  160. *
  161. * @return void
  162. */
  163. public function testViewOptionsGetsToBuilder()
  164. {
  165. $this->subject->passedArgs = 'test';
  166. $this->subject->createView();
  167. $result = $this->subject->viewBuilder()->getOptions();
  168. $this->assertEquals(['passedArgs' => 'test'], $result);
  169. }
  170. /**
  171. * test that viewClass is used to create the view
  172. *
  173. * @deprecated
  174. * @return void
  175. */
  176. public function testCreateViewViewClass()
  177. {
  178. $this->deprecated(function () {
  179. $this->subject->viewClass = 'Json';
  180. $view = $this->subject->createView();
  181. $this->assertInstanceOf('Cake\View\JsonView', $view);
  182. });
  183. }
  184. /**
  185. * test that viewBuilder settings override viewClass
  186. *
  187. * @return void
  188. */
  189. public function testCreateViewViewBuilder()
  190. {
  191. $this->subject->viewBuilder()->setClassName('Xml');
  192. $this->subject->viewClass = 'Json';
  193. $view = $this->subject->createView();
  194. $this->assertInstanceOf('Cake\View\XmlView', $view);
  195. }
  196. /**
  197. * test that parameters beats viewBuilder() and viewClass
  198. *
  199. * @return void
  200. */
  201. public function testCreateViewParameter()
  202. {
  203. $this->subject->viewBuilder()->setClassName('View');
  204. $this->subject->viewClass = 'Json';
  205. $view = $this->subject->createView('Xml');
  206. $this->assertInstanceOf('Cake\View\XmlView', $view);
  207. }
  208. /**
  209. * test createView() throws exception if view class cannot be found
  210. *
  211. * @return void
  212. */
  213. public function testCreateViewException()
  214. {
  215. $this->expectException(\Cake\View\Exception\MissingViewException::class);
  216. $this->expectExceptionMessage('View class "Foo" is missing.');
  217. $this->subject->createView('Foo');
  218. }
  219. }