ViewVarsTraitTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. $this->deprecated(function () {
  93. $option = 'newOption';
  94. $this->subject->viewOptions($option);
  95. $this->assertContains($option, $this->subject->viewOptions());
  96. });
  97. }
  98. /**
  99. * test viewOptions() with 2 strings in array, merge true.
  100. *
  101. * @return void
  102. */
  103. public function testAddTwoViewOption()
  104. {
  105. $this->deprecated(function () {
  106. $this->subject->viewOptions(['oldOption'], false);
  107. $option = ['newOption', 'anotherOption'];
  108. $result = $this->subject->viewOptions($option);
  109. $expects = ['oldOption', 'newOption', 'anotherOption'];
  110. $this->assertContainsOnly('string', $result);
  111. $this->assertEquals($expects, $result);
  112. });
  113. }
  114. /**
  115. * test empty params reads _viewOptions.
  116. *
  117. * @return void
  118. */
  119. public function testReadingViewOptions()
  120. {
  121. $this->deprecated(function () {
  122. $expected = $this->subject->viewOptions(['one', 'two', 'three'], false);
  123. $result = $this->subject->viewOptions();
  124. $this->assertEquals($expected, $result);
  125. });
  126. }
  127. /**
  128. * test setting $merge `false` overrides correct options.
  129. *
  130. * @return void
  131. */
  132. public function testMergeFalseViewOptions()
  133. {
  134. $this->deprecated(function () {
  135. $this->subject->viewOptions(['one', 'two', 'three'], false);
  136. $expected = ['four', 'five', 'six'];
  137. $result = $this->subject->viewOptions($expected, false);
  138. $this->assertEquals($expected, $result);
  139. });
  140. }
  141. /**
  142. * test _viewOptions is undefined and $opts is null, an empty array is returned.
  143. *
  144. * @return void
  145. */
  146. public function testUndefinedValidViewOptions()
  147. {
  148. $this->deprecated(function () {
  149. $result = $this->subject->viewOptions([], false);
  150. $this->assertInternalType('array', $result);
  151. $this->assertEmpty($result);
  152. });
  153. }
  154. /**
  155. * test that createView() updates viewVars of View instance on each call.
  156. *
  157. * @return void
  158. */
  159. public function testUptoDateViewVars()
  160. {
  161. $expected = ['one' => 'one'];
  162. $this->subject->set($expected);
  163. $this->assertEquals($expected, $this->subject->createView()->viewVars);
  164. $expected = ['one' => 'one', 'two' => 'two'];
  165. $this->subject->set($expected);
  166. $this->assertEquals($expected, $this->subject->createView()->viewVars);
  167. }
  168. /**
  169. * test that options are passed to the view builder when using createView().
  170. *
  171. * @return void
  172. */
  173. public function testViewOptionsGetsToBuilder()
  174. {
  175. $this->subject->passedArgs = 'test';
  176. $this->subject->createView();
  177. $result = $this->subject->viewBuilder()->getOptions();
  178. $this->assertEquals(['passedArgs' => 'test'], $result);
  179. }
  180. /**
  181. * test that viewClass is used to create the view
  182. *
  183. * @deprecated
  184. * @return void
  185. */
  186. public function testCreateViewViewClass()
  187. {
  188. $this->deprecated(function () {
  189. $this->subject->viewClass = 'Json';
  190. $view = $this->subject->createView();
  191. $this->assertInstanceOf('Cake\View\JsonView', $view);
  192. });
  193. }
  194. /**
  195. * test that viewBuilder settings override viewClass
  196. *
  197. * @return void
  198. */
  199. public function testCreateViewViewBuilder()
  200. {
  201. $this->subject->viewBuilder()->setClassName('Xml');
  202. $this->subject->viewClass = 'Json';
  203. $view = $this->subject->createView();
  204. $this->assertInstanceOf('Cake\View\XmlView', $view);
  205. }
  206. /**
  207. * test that parameters beats viewBuilder() and viewClass
  208. *
  209. * @return void
  210. */
  211. public function testCreateViewParameter()
  212. {
  213. $this->subject->viewBuilder()->setClassName('View');
  214. $this->subject->viewClass = 'Json';
  215. $view = $this->subject->createView('Xml');
  216. $this->assertInstanceOf('Cake\View\XmlView', $view);
  217. }
  218. /**
  219. * test createView() throws exception if view class cannot be found
  220. *
  221. * @return void
  222. */
  223. public function testCreateViewException()
  224. {
  225. $this->expectException(\Cake\View\Exception\MissingViewException::class);
  226. $this->expectExceptionMessage('View class "Foo" is missing.');
  227. $this->subject->createView('Foo');
  228. }
  229. }