ViewVarsTraitTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. * setup
  24. *
  25. * @return void
  26. */
  27. public function setUp() {
  28. parent::setUp();
  29. $this->subject = $this->getObjectForTrait('Cake\View\ViewVarsTrait');
  30. }
  31. /**
  32. * Test set() with one param.
  33. *
  34. * @return void
  35. */
  36. public function testSetOneParam() {
  37. $data = ['test' => 'val', 'foo' => 'bar'];
  38. $this->subject->set($data);
  39. $this->assertEquals($data, $this->subject->viewVars);
  40. $update = ['test' => 'updated'];
  41. $this->subject->set($update);
  42. $this->assertEquals('updated', $this->subject->viewVars['test']);
  43. }
  44. /**
  45. * test set() with 2 params
  46. *
  47. * @return void
  48. */
  49. public function testSetTwoParam() {
  50. $this->subject->set('testing', 'value');
  51. $this->assertEquals(['testing' => 'value'], $this->subject->viewVars);
  52. }
  53. /**
  54. * test chainable set()
  55. *
  56. * @return void
  57. */
  58. public function testSetChained() {
  59. $this->subject->set('testing', 'value')
  60. ->set('foo', 'bar');
  61. $this->assertEquals(['testing' => 'value', 'foo' => 'bar'], $this->subject->viewVars);
  62. }
  63. /**
  64. * test set() with 2 params in combine mode
  65. *
  66. * @return void
  67. */
  68. public function testSetTwoParamCombind() {
  69. $keys = ['one', 'key'];
  70. $vals = ['two', 'val'];
  71. $this->subject->set($keys, $vals);
  72. $expected = ['one' => 'two', 'key' => 'val'];
  73. $this->assertEquals($expected, $this->subject->viewVars);
  74. }
  75. /**
  76. * test viewOptions() with 1 string param, merge true
  77. *
  78. * @return void
  79. */
  80. public function testAddOneViewOption() {
  81. $option = 'newOption';
  82. $this->subject->viewOptions($option);
  83. $this->assertContains($option, $this->subject->_validViewOptions);
  84. }
  85. /**
  86. * test viewOptions() with 2 strings in array, merge true.
  87. *
  88. * @return void
  89. */
  90. public function testAddTwoViewOption() {
  91. $this->subject->_validViewOptions = ['oldOption'];
  92. $option = ['newOption', 'anotherOption'];
  93. $result = $this->subject->viewOptions($option);
  94. $expects = ['oldOption', 'newOption', 'anotherOption'];
  95. $this->assertContainsOnly('string', $result);
  96. $this->assertEquals($expects, $result);
  97. }
  98. /**
  99. * test empty params reads _validViewOptions.
  100. *
  101. * @return void
  102. */
  103. public function testReadingViewOptions() {
  104. $expected = $this->subject->_validViewOptions = ['one', 'two', 'three'];
  105. $result = $this->subject->viewOptions();
  106. $this->assertEquals($expected, $result);
  107. }
  108. /**
  109. * test setting $merge `false` overrides currect options.
  110. *
  111. * @return void
  112. */
  113. public function testMergeFalseViewOptions() {
  114. $this->subject->_validViewOptions = ['one', 'two', 'three'];
  115. $expected = ['four', 'five', 'six'];
  116. $result = $this->subject->viewOptions($expected, false);
  117. $this->assertEquals($expected, $result);
  118. }
  119. /**
  120. * test _validViewOptions is undefined and $opts is null, an empty array is returned.
  121. *
  122. * @return void
  123. */
  124. public function testUndefinedValidViewOptions() {
  125. $result = $this->subject->viewOptions();
  126. $this->assertTrue(is_array($result));
  127. $this->assertTrue(empty($result));
  128. }
  129. /**
  130. * test getView() throws exception if view class cannot be found
  131. *
  132. * @expectedException \Cake\View\Exception\MissingViewException
  133. * @expectedExceptionMessage View class "Foo" is missing.
  134. * @return void
  135. */
  136. public function testGetViewException() {
  137. $this->subject->getView('Foo');
  138. }
  139. /**
  140. * test createView() throws exception if view class cannot be found
  141. *
  142. * @expectedException \Cake\View\Exception\MissingViewException
  143. * @expectedExceptionMessage View class "Foo" is missing.
  144. * @return void
  145. */
  146. public function testCreateViewException() {
  147. $this->subject->getView('Foo');
  148. }
  149. }