ViewVarsTraitTest.php 6.3 KB

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