ViewVarsTraitTest.php 6.3 KB

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