ViewVarsTraitTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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\TestSuite\TestCase;
  16. use Cake\View\ViewVarsTrait;
  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 = $this->getObjectForTrait('Cake\View\ViewVarsTrait');
  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->_validViewOptions);
  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->_validViewOptions = ['oldOption'];
  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 _validViewOptions.
  109. *
  110. * @return void
  111. */
  112. public function testReadingViewOptions()
  113. {
  114. $expected = $this->subject->_validViewOptions = ['one', 'two', 'three'];
  115. $result = $this->subject->viewOptions();
  116. $this->assertEquals($expected, $result);
  117. }
  118. /**
  119. * test setting $merge `false` overrides currect options.
  120. *
  121. * @return void
  122. */
  123. public function testMergeFalseViewOptions()
  124. {
  125. $this->subject->_validViewOptions = ['one', 'two', 'three'];
  126. $expected = ['four', 'five', 'six'];
  127. $result = $this->subject->viewOptions($expected, false);
  128. $this->assertEquals($expected, $result);
  129. }
  130. /**
  131. * test _validViewOptions 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();
  138. $this->assertTrue(is_array($result));
  139. $this->assertTrue(empty($result));
  140. }
  141. /**
  142. * test getView() throws exception if view class cannot be found
  143. *
  144. * @expectedException \Cake\View\Exception\MissingViewException
  145. * @expectedExceptionMessage View class "Foo" is missing.
  146. * @return void
  147. */
  148. public function testGetViewException()
  149. {
  150. $this->subject->getView('Foo');
  151. }
  152. /**
  153. * test createView() throws exception if view class cannot be found
  154. *
  155. * @expectedException \Cake\View\Exception\MissingViewException
  156. * @expectedExceptionMessage View class "Foo" is missing.
  157. * @return void
  158. */
  159. public function testCreateViewException()
  160. {
  161. $this->subject->getView('Foo');
  162. }
  163. }