ViewVarsTraitTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View;
  16. use Cake\Controller\Controller;
  17. use Cake\TestSuite\TestCase;
  18. /**
  19. * ViewVarsTrait test case
  20. */
  21. class ViewVarsTraitTest extends TestCase
  22. {
  23. /**
  24. * @var \Cake\Controller\Controller;
  25. */
  26. protected $subject;
  27. /**
  28. * setup
  29. *
  30. * @return void
  31. */
  32. public function setUp(): void
  33. {
  34. parent::setUp();
  35. $this->subject = new Controller();
  36. }
  37. /**
  38. * Test set() with one param.
  39. *
  40. * @return void
  41. */
  42. public function testSetOneParam()
  43. {
  44. $data = ['test' => 'val', 'foo' => 'bar'];
  45. $this->subject->set($data);
  46. $this->assertEquals($data, $this->subject->viewBuilder()->getVars());
  47. $update = ['test' => 'updated'];
  48. $this->subject->set($update);
  49. $this->assertSame('updated', $this->subject->viewBuilder()->getVar('test'));
  50. }
  51. /**
  52. * test set() with 2 params
  53. *
  54. * @return void
  55. */
  56. public function testSetTwoParam()
  57. {
  58. $this->subject->set('testing', 'value');
  59. $this->assertEquals(['testing' => 'value'], $this->subject->viewBuilder()->getVars());
  60. }
  61. /**
  62. * test chainable set()
  63. *
  64. * @return void
  65. */
  66. public function testSetChained()
  67. {
  68. $result = $this->subject->set('testing', 'value')
  69. ->set('foo', 'bar');
  70. $this->assertSame($this->subject, $result);
  71. $this->assertEquals(['testing' => 'value', 'foo' => 'bar'], $this->subject->viewBuilder()->getVars());
  72. }
  73. /**
  74. * test set() with 2 params in combine mode
  75. *
  76. * @return void
  77. */
  78. public function testSetTwoParamCombined()
  79. {
  80. $keys = ['one', 'key'];
  81. $vals = ['two', 'val'];
  82. $this->subject->set($keys, $vals);
  83. $expected = ['one' => 'two', 'key' => 'val'];
  84. $this->assertEquals($expected, $this->subject->viewBuilder()->getVars());
  85. }
  86. /**
  87. * test that createView() updates viewVars of View instance on each call.
  88. *
  89. * @return void
  90. */
  91. public function testUptoDateViewVars()
  92. {
  93. $expected = ['one' => 'one'];
  94. $this->subject->set($expected);
  95. $this->assertSame('one', $this->subject->createView()->get('one'));
  96. $expected = ['one' => 'one', 'two' => 'two'];
  97. $this->subject->set($expected);
  98. $this->assertSame('two', $this->subject->createView()->get('two'));
  99. }
  100. /**
  101. * test that parameters beats viewBuilder() and viewClass
  102. *
  103. * @return void
  104. */
  105. public function testCreateViewParameter()
  106. {
  107. $this->subject->viewBuilder()->setClassName('View');
  108. $view = $this->subject->createView('Xml');
  109. $this->assertInstanceOf('Cake\View\XmlView', $view);
  110. }
  111. /**
  112. * test createView() throws exception if view class cannot be found
  113. *
  114. * @return void
  115. */
  116. public function testCreateViewException()
  117. {
  118. $this->expectException(\Cake\View\Exception\MissingViewException::class);
  119. $this->expectExceptionMessage('View class "Foo" is missing.');
  120. $this->subject->createView('Foo');
  121. }
  122. }