[!!!][TASK] Use native trigger_error and ErrorHandler for deprecations
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / UnitDeprecated / Core / ViewHelper / AbstractViewHelperTest.php
1 <?php
2 namespace TYPO3\CMS\Fluid\Tests\UnitDeprecated\Core\ViewHelper;
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 use TYPO3\CMS\Extbase\Reflection\ReflectionService;
17 use TYPO3\CMS\Fluid\Core\Exception;
18 use TYPO3\CMS\Fluid\Tests\Unit\Core\Fixtures\TestViewHelper;
19
20 /**
21 * Test case for deprecated functionality to still behave as before
22 */
23 class AbstractViewHelperTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
24 {
25 /**
26 * @var array
27 */
28 protected $fixtureMethodParameters = [
29 'param1' => [
30 'position' => 0,
31 'optional' => false,
32 'type' => 'integer',
33 'defaultValue' => null
34 ],
35 'param2' => [
36 'position' => 1,
37 'optional' => false,
38 'type' => 'array',
39 'array' => true,
40 'defaultValue' => null
41 ],
42 'param3' => [
43 'position' => 2,
44 'optional' => true,
45 'type' => 'string',
46 'array' => false,
47 'defaultValue' => 'default'
48 ],
49 ];
50
51 /**
52 * @var array
53 */
54 protected $fixtureMethodTags = [
55 'param' => [
56 'integer $param1 P1 Stuff',
57 'array $param2 P2 Stuff',
58 'string $param3 P3 Stuff'
59 ]
60 ];
61
62 protected function setUp()
63 {
64 $this->mockReflectionService = $this->createMock(\TYPO3\CMS\Extbase\Reflection\ReflectionService::class);
65 }
66
67 /**
68 * @test
69 * @dataProvider getCallRenderMethodTestValues
70 * @param array $arguments
71 * @param bool $expectsException
72 */
73 public function registerRenderMethodArgumentsThrowsExceptionOnMissingType(array $arguments, $expectsException = false)
74 {
75 $reflectionService = $this->getMockBuilder(ReflectionService::class)
76 ->setMethods(['getMethodParameters', 'getMethodTagsValues'])
77 ->getMock();
78 $reflectionService->expects($this->once())->method('getMethodParameters')->willReturn(
79 [
80 'param1' => [
81 'position' => 0,
82 'byReference' => false,
83 'optional' => false,
84 'allowsNull' => true
85 ]
86 ]
87 );
88 $reflectionService->expects($this->once())->method('getMethodTagsValues')->willReturn([]);
89 $fixture = $this->getAccessibleMock(TestViewHelper::class, ['render', 'getReflectionService']);
90 $fixture->expects($this->once())->method('getReflectionService')->willReturn($reflectionService);
91 $this->expectException(Exception::class);
92 $this->callInaccessibleMethod($fixture, 'registerRenderMethodArguments');
93 }
94
95 /**
96 * @test
97 * @dataProvider getCallRenderMethodTestValues
98 * @param array $arguments
99 * @param bool $expectsException
100 */
101 public function callRenderMethodBehavesAsExpected(array $arguments, $expectsException = false)
102 {
103 $reflectionService = $this->getMockBuilder(ReflectionService::class)
104 ->setMethods(['getMethodParameters', 'getMethodTagsValues'])
105 ->getMock();
106 $reflectionService->expects($this->once())->method('getMethodParameters')->willReturn(
107 [
108 'param1' => [
109 'position' => 0,
110 'type' => 'integer',
111 'byReference' => false,
112 'array' => false,
113 'optional' => false,
114 'allowsNull' => true
115 ],
116 'param2' => [
117 'position' => 1,
118 'type' => 'array',
119 'byReference' => false,
120 'array' => true,
121 'optional' => false,
122 'allowsNull' => true
123 ],
124 'param3' => [
125 'position' => 2,
126 'type' => 'string',
127 'byReference' => false,
128 'array' => false,
129 'optional' => false,
130 'allowsNull' => true
131 ],
132 ]
133 );
134 $reflectionService->expects($this->once())->method('getMethodTagsValues')->willReturn(
135 []
136 );
137 $fixture = $this->getAccessibleMock(TestViewHelper::class, ['render', 'getReflectionService']);
138 $namedArguments = array_combine(['param1', 'param2', 'param3'], $arguments);
139 $fixture->expects($this->once())->method('getReflectionService')->willReturn($reflectionService);
140 $this->callInaccessibleMethod($fixture, 'registerRenderMethodArguments');
141 $fixture->setArguments($namedArguments);
142 if ($expectsException) {
143 $exception = new \TYPO3Fluid\Fluid\Core\ViewHelper\Exception('test', 1476108352);
144 $this->expectException(get_class($exception));
145 $fixture->expects($this->once())->method('render')->willThrowException($exception);
146 $this->assertEquals('test', $this->callInaccessibleMethod($fixture, 'callRenderMethod'));
147 } else {
148 $fixture->expects($this->once())
149 ->method('render')
150 ->with($arguments[0], $arguments[1], $arguments[2])
151 ->willReturn('okay');
152 $this->assertEquals('okay', $this->callInaccessibleMethod($fixture, 'callRenderMethod'));
153 }
154 }
155
156 /**
157 * @return array
158 */
159 public function getCallRenderMethodTestValues()
160 {
161 return [
162 [[3, ['bar'], 'baz'], false],
163 [[2, ['baz'], 'bar'], false],
164 [[3, ['bar'], 'baz'], true],
165 ];
166 }
167
168 /**
169 * @test
170 */
171 public function prepareArgumentsRegistersAnnotationBasedArgumentsWithDescriptionIfDebugModeIsEnabled()
172 {
173 $dataCacheMock = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class);
174 $dataCacheMock->expects($this->any())->method('has')->will($this->returnValue(true));
175 $dataCacheMock->expects($this->any())->method('get')->will($this->returnValue([]));
176
177 $reflectionServiceMock = $this->getMockBuilder(ReflectionService::class)->getMock();
178 $reflectionServiceMock->expects($this->once())->method('getMethodParameters')->willReturn(['fake' => ['type' => 'int', 'defaultValue' => 'def']]);
179 $reflectionServiceMock->expects($this->once())->method('getMethodTagsValues')->willReturn(['param']);
180
181 $viewHelper = $this->getMockBuilder(\TYPO3\CMS\Fluid\Tests\Unit\Core\Fixtures\TestViewHelper::class)->setMethods(['getReflectionService'])->getMock();
182 $viewHelper->expects($this->once())->method('getReflectionService')->willReturn($reflectionServiceMock);
183
184 $expected = [
185 'fake' => new \TYPO3\CMS\Fluid\Core\ViewHelper\ArgumentDefinition('fake', 'int', '', false, 'def', true)
186 ];
187
188 $this->callInaccessibleMethod($viewHelper, 'registerRenderMethodArguments');
189 $this->assertAttributeEquals($expected, 'argumentDefinitions', $viewHelper, 'Annotation based arguments were not registered.');
190 }
191 }