ViewVarsTraitTest.php 5.3 KB

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