a1563178ee95fd8c273a777306038da8957a2d3b
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / Unit / ViewHelpers / CObjectViewHelperTest.php
1 <?php
2 namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use Prophecy\Argument;
18 use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
19 use TYPO3\CMS\Fluid\ViewHelpers\CObjectViewHelper;
20 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
21 use TYPO3\TestingFramework\Fluid\Unit\ViewHelpers\ViewHelperBaseTestcase;
22
23 /**
24 * Class CObjectViewHelperTest
25 */
26 class CObjectViewHelperTest extends ViewHelperBaseTestcase
27 {
28 /**
29 * @var CObjectViewHelper
30 */
31 protected $viewHelper;
32
33 /**
34 * @var ConfigurationManagerInterface
35 */
36 protected $configurationManager;
37
38 /**
39 * @var ContentObjectRenderer
40 */
41 protected $contentObjectRenderer;
42
43 /**
44 * Set up the fixture
45 */
46 protected function setUp()
47 {
48 parent::setUp();
49 $this->viewHelper = new CObjectViewHelper();
50 $this->injectDependenciesIntoViewHelper($this->viewHelper);
51
52 $this->configurationManager = $this->prophesize(ConfigurationManagerInterface::class);
53 $this->contentObjectRenderer = $this->prophesize(ContentObjectRenderer::class);
54 }
55
56 /**
57 * @test
58 */
59 public function viewHelperAcceptsDataParameterAsInput()
60 {
61 $this->stubBaseDependencies();
62
63 $this->setArgumentsUnderTest(
64 $this->viewHelper,
65 [
66 'data' => 'foo',
67 ]
68 );
69 $this->contentObjectRenderer->start(['foo'], '')->willReturn();
70 $actualResult = $this->viewHelper->initializeArgumentsAndRender();
71 $this->assertSame('', $actualResult);
72 }
73
74 /**
75 * @test
76 */
77 public function viewHelperAcceptsChildrenClosureAsInput()
78 {
79 $this->stubBaseDependencies();
80
81 $this->viewHelper->setRenderChildrenClosure(
82 function () {
83 return 'foo';
84 }
85 );
86 $this->setArgumentsUnderTest(
87 $this->viewHelper,
88 []
89 );
90 $this->contentObjectRenderer->start(['foo'], '')->willReturn();
91 $actualResult = $this->viewHelper->initializeArgumentsAndRender();
92 $this->assertSame('', $actualResult);
93 }
94
95 /**
96 * @test
97 */
98 public function renderThrowsExceptionIfTyposcriptObjectPathDoesNotExist()
99 {
100 $this->stubBaseDependencies();
101 $this->contentObjectRenderer->start(Argument::cetera())->willReturn();
102
103 $this->setArgumentsUnderTest(
104 $this->viewHelper,
105 [
106 'data' => 'foo',
107 'typoscriptObjectPath' => 'test.path',
108 ]
109 );
110 $this->expectException(\TYPO3\CMS\Fluid\Core\ViewHelper\Exception::class);
111 $this->expectExceptionCode(1253191023);
112 $this->viewHelper->initializeArgumentsAndRender();
113 }
114
115 /**
116 * @test
117 */
118 public function renderReturnsSimpleTyposcriptValue()
119 {
120 $this->setArgumentsUnderTest(
121 $this->viewHelper,
122 [
123 'typoscriptObjectPath' => 'test',
124 'data' => 'foo',
125 'table' => 'table',
126 ]
127 );
128
129 $subConfigArray = [
130 'value' => 'Hello World',
131 'wrap' => 'ab | cd',
132 ];
133
134 $configArray = [
135 'test' => 'TEXT',
136 'test.' => $subConfigArray,
137 ];
138
139 $this->configurationManager->getConfiguration(Argument::any())->willReturn($configArray);
140 $this->viewHelper->injectConfigurationManager($this->configurationManager->reveal());
141
142 $this->contentObjectRenderer->start(['foo'], 'table')->willReturn();
143 $this->contentObjectRenderer->setCurrentVal('foo')->willReturn();
144 $this->contentObjectRenderer->cObjGetSingle('TEXT', $subConfigArray)->willReturn('Hello World');
145 $this->viewHelper->injectContentObjectRenderer($this->contentObjectRenderer->reveal());
146
147 $actualResult = $this->viewHelper->initializeArgumentsAndRender();
148 $expectedResult = 'Hello World';
149 $this->assertSame($expectedResult, $actualResult);
150 }
151
152 /**
153 * Stubs base dependencies
154 */
155 protected function stubBaseDependencies()
156 {
157 $this->configurationManager->getConfiguration(Argument::any())->willReturn([]);
158 $this->viewHelper->injectConfigurationManager($this->configurationManager->reveal());
159
160 $this->contentObjectRenderer->setCurrentVal(Argument::cetera())->willReturn();
161 $this->contentObjectRenderer->cObjGetSingle(Argument::cetera())->willReturn('');
162 $this->viewHelper->injectContentObjectRenderer($this->contentObjectRenderer->reveal());
163 }
164 }