ViewVarsTraitTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. use Cake\View\Exception\MissingViewException;
  19. /**
  20. * ViewVarsTrait test case
  21. */
  22. class ViewVarsTraitTest extends TestCase
  23. {
  24. /**
  25. * @var \Cake\Controller\Controller
  26. */
  27. protected $subject;
  28. /**
  29. * setup
  30. */
  31. public function setUp(): void
  32. {
  33. parent::setUp();
  34. $this->subject = new Controller();
  35. }
  36. /**
  37. * Test set() with one param.
  38. */
  39. public function testSetOneParam(): void
  40. {
  41. $data = ['test' => 'val', 'foo' => 'bar'];
  42. $this->subject->set($data);
  43. $this->assertEquals($data, $this->subject->viewBuilder()->getVars());
  44. $update = ['test' => 'updated'];
  45. $this->subject->set($update);
  46. $this->assertSame('updated', $this->subject->viewBuilder()->getVar('test'));
  47. }
  48. /**
  49. * test set() with 2 params
  50. */
  51. public function testSetTwoParam(): void
  52. {
  53. $this->subject->set('testing', 'value');
  54. $this->assertEquals(['testing' => 'value'], $this->subject->viewBuilder()->getVars());
  55. }
  56. /**
  57. * test chainable set()
  58. */
  59. public function testSetChained(): void
  60. {
  61. $result = $this->subject->set('testing', 'value')
  62. ->set('foo', 'bar');
  63. $this->assertSame($this->subject, $result);
  64. $this->assertEquals(['testing' => 'value', 'foo' => 'bar'], $this->subject->viewBuilder()->getVars());
  65. }
  66. /**
  67. * test set() with 2 params in combine mode
  68. */
  69. public function testSetTwoParamCombined(): void
  70. {
  71. $keys = ['one', 'key'];
  72. $vals = ['two', 'val'];
  73. $this->subject->set($keys, $vals);
  74. $expected = ['one' => 'two', 'key' => 'val'];
  75. $this->assertEquals($expected, $this->subject->viewBuilder()->getVars());
  76. }
  77. /**
  78. * test that createView() updates viewVars of View instance on each call.
  79. */
  80. public function testUptoDateViewVars(): void
  81. {
  82. $expected = ['one' => 'one'];
  83. $this->subject->set($expected);
  84. $this->assertSame('one', $this->subject->createView()->get('one'));
  85. $expected = ['one' => 'one', 'two' => 'two'];
  86. $this->subject->set($expected);
  87. $this->assertSame('two', $this->subject->createView()->get('two'));
  88. }
  89. /**
  90. * test that parameters beats viewBuilder() and viewClass
  91. */
  92. public function testCreateViewParameter(): void
  93. {
  94. $this->subject->viewBuilder()->setClassName('View');
  95. $view = $this->subject->createView('Xml');
  96. $this->assertInstanceOf('Cake\View\XmlView', $view);
  97. }
  98. /**
  99. * test createView() throws exception if view class cannot be found
  100. */
  101. public function testCreateViewException(): void
  102. {
  103. $this->expectException(MissingViewException::class);
  104. $this->expectExceptionMessage('View class "Foo" is missing.');
  105. $this->subject->createView('Foo');
  106. }
  107. }