ViewVarsTraitTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. $result = $this->subject->set('testing', 'value')
  60. ->set('foo', 'bar');
  61. $this->assertSame($this->subject, $result);
  62. $this->assertEquals(['testing' => 'value', 'foo' => 'bar'], $this->subject->viewVars);
  63. }
  64. /**
  65. * test set() with 2 params in combine mode
  66. *
  67. * @return void
  68. */
  69. public function testSetTwoParamCombind() {
  70. $keys = ['one', 'key'];
  71. $vals = ['two', 'val'];
  72. $this->subject->set($keys, $vals);
  73. $expected = ['one' => 'two', 'key' => 'val'];
  74. $this->assertEquals($expected, $this->subject->viewVars);
  75. }
  76. /**
  77. * test viewOptions() with 1 string param, merge true
  78. *
  79. * @return void
  80. */
  81. public function testAddOneViewOption() {
  82. $option = 'newOption';
  83. $this->subject->viewOptions($option);
  84. $this->assertContains($option, $this->subject->_validViewOptions);
  85. }
  86. /**
  87. * test viewOptions() with 2 strings in array, merge true.
  88. *
  89. * @return void
  90. */
  91. public function testAddTwoViewOption() {
  92. $this->subject->_validViewOptions = ['oldOption'];
  93. $option = ['newOption', 'anotherOption'];
  94. $result = $this->subject->viewOptions($option);
  95. $expects = ['oldOption', 'newOption', 'anotherOption'];
  96. $this->assertContainsOnly('string', $result);
  97. $this->assertEquals($expects, $result);
  98. }
  99. /**
  100. * test empty params reads _validViewOptions.
  101. *
  102. * @return void
  103. */
  104. public function testReadingViewOptions() {
  105. $expected = $this->subject->_validViewOptions = ['one', 'two', 'three'];
  106. $result = $this->subject->viewOptions();
  107. $this->assertEquals($expected, $result);
  108. }
  109. /**
  110. * test setting $merge `false` overrides currect options.
  111. *
  112. * @return void
  113. */
  114. public function testMergeFalseViewOptions() {
  115. $this->subject->_validViewOptions = ['one', 'two', 'three'];
  116. $expected = ['four', 'five', 'six'];
  117. $result = $this->subject->viewOptions($expected, false);
  118. $this->assertEquals($expected, $result);
  119. }
  120. /**
  121. * test _validViewOptions is undefined and $opts is null, an empty array is returned.
  122. *
  123. * @return void
  124. */
  125. public function testUndefinedValidViewOptions() {
  126. $result = $this->subject->viewOptions();
  127. $this->assertTrue(is_array($result));
  128. $this->assertTrue(empty($result));
  129. }
  130. /**
  131. * test getView() throws exception if view class cannot be found
  132. *
  133. * @expectedException \Cake\View\Exception\MissingViewException
  134. * @expectedExceptionMessage View class "Foo" is missing.
  135. * @return void
  136. */
  137. public function testGetViewException() {
  138. $this->subject->getView('Foo');
  139. }
  140. /**
  141. * test createView() throws exception if view class cannot be found
  142. *
  143. * @expectedException \Cake\View\Exception\MissingViewException
  144. * @expectedExceptionMessage View class "Foo" is missing.
  145. * @return void
  146. */
  147. public function testCreateViewException() {
  148. $this->subject->getView('Foo');
  149. }
  150. }