[TASK] Convert remaining ViewHelpers to static calling
[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\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
20 use TYPO3\CMS\Extbase\Object\ObjectManager;
21 use TYPO3\CMS\Fluid\ViewHelpers\CObjectViewHelper;
22 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
23 use TYPO3\TestingFramework\Fluid\Unit\ViewHelpers\ViewHelperBaseTestcase;
24
25 /**
26 * Class CObjectViewHelperTest
27 */
28 class CObjectViewHelperTest extends ViewHelperBaseTestcase
29 {
30 /**
31 * @var CObjectViewHelper
32 */
33 protected $viewHelper;
34
35 /**
36 * @var ConfigurationManagerInterface
37 */
38 protected $configurationManager;
39
40 /**
41 * @var ContentObjectRenderer
42 */
43 protected $contentObjectRenderer;
44
45 /**
46 * Set up the fixture
47 */
48 protected function setUp()
49 {
50 parent::setUp();
51 $this->viewHelper = new CObjectViewHelper();
52 $this->injectDependenciesIntoViewHelper($this->viewHelper);
53
54 $this->configurationManager = $this->prophesize(ConfigurationManagerInterface::class);
55 $this->contentObjectRenderer = $this->prophesize(ContentObjectRenderer::class);
56 }
57
58 /**
59 * @test
60 */
61 public function viewHelperAcceptsDataParameterAsInput()
62 {
63 $this->stubBaseDependencies();
64
65 $this->setArgumentsUnderTest(
66 $this->viewHelper,
67 [
68 'data' => 'foo',
69 ]
70 );
71 $this->contentObjectRenderer->start(['foo'], '')->willReturn();
72 $actualResult = $this->viewHelper->initializeArgumentsAndRender();
73 $this->assertSame('', $actualResult);
74 }
75
76 /**
77 * @test
78 */
79 public function viewHelperAcceptsChildrenClosureAsInput()
80 {
81 $this->stubBaseDependencies();
82
83 $this->viewHelper->setRenderChildrenClosure(
84 function () {
85 return 'foo';
86 }
87 );
88 $this->setArgumentsUnderTest(
89 $this->viewHelper,
90 []
91 );
92 $this->contentObjectRenderer->start(['foo'], '')->willReturn();
93 $actualResult = $this->viewHelper->initializeArgumentsAndRender();
94 $this->assertSame('', $actualResult);
95 }
96
97 /**
98 * @test
99 */
100 public function renderThrowsExceptionIfTyposcriptObjectPathDoesNotExist()
101 {
102 $this->stubBaseDependencies();
103 $this->contentObjectRenderer->start(Argument::cetera())->willReturn();
104
105 $this->setArgumentsUnderTest(
106 $this->viewHelper,
107 [
108 'data' => 'foo',
109 'typoscriptObjectPath' => 'test.path',
110 ]
111 );
112 $this->expectException(\TYPO3\CMS\Fluid\Core\ViewHelper\Exception::class);
113 $this->expectExceptionCode(1253191023);
114 $this->viewHelper->initializeArgumentsAndRender();
115 }
116
117 /**
118 * @test
119 */
120 public function renderReturnsSimpleTyposcriptValue()
121 {
122 $this->setArgumentsUnderTest(
123 $this->viewHelper,
124 [
125 'typoscriptObjectPath' => 'test',
126 'data' => 'foo',
127 'table' => 'table',
128 ]
129 );
130
131 $subConfigArray = [
132 'value' => 'Hello World',
133 'wrap' => 'ab | cd',
134 ];
135
136 $configArray = [
137 'test' => 'TEXT',
138 'test.' => $subConfigArray,
139 ];
140
141 $this->configurationManager->getConfiguration(Argument::any())->willReturn($configArray);
142
143 $this->contentObjectRenderer->start(['foo'], 'table')->willReturn();
144 $this->contentObjectRenderer->setCurrentVal('foo')->willReturn();
145 $this->contentObjectRenderer->cObjGetSingle('TEXT', $subConfigArray)->willReturn('Hello World');
146
147 $objectManager = $this->prophesize(ObjectManager::class);
148 $objectManager->get(ConfigurationManagerInterface::class)->willReturn($this->configurationManager->reveal());
149 GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager->reveal());
150 $GLOBALS['TSFE'] = (object) ['cObj' => $this->contentObjectRenderer->reveal()];
151
152 $actualResult = $this->viewHelper->initializeArgumentsAndRender();
153 $expectedResult = 'Hello World';
154 $this->assertSame($expectedResult, $actualResult);
155 }
156
157 /**
158 * Stubs base dependencies
159 */
160 protected function stubBaseDependencies()
161 {
162 $this->configurationManager->getConfiguration(Argument::any())->willReturn([]);
163 $this->contentObjectRenderer->setCurrentVal(Argument::cetera())->willReturn();
164 $this->contentObjectRenderer->cObjGetSingle(Argument::cetera())->willReturn('');
165 $objectManager = $this->prophesize(ObjectManager::class);
166 $objectManager->get(ConfigurationManagerInterface::class)->willReturn($this->configurationManager->reveal());
167 GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager->reveal());
168 $GLOBALS['TSFE'] = (object) ['cObj' => $this->contentObjectRenderer->reveal()];
169 }
170 }