ViewVarsTraitTest.php 3.5 KB

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