ViewVarsTraitTest.php 3.9 KB

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