[TASK] Shorten the extend class in all unit tests
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Mvc / Controller / ArgumentTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller;
3
4 /* *
5 * This script belongs to the Extbase framework. *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License as published by the *
9 * Free Software Foundation, either version 3 of the License, or (at your *
10 * option) any later version. *
11 * *
12 * This script is distributed in the hope that it will be useful, but *
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
14 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
15 * General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with the script. *
19 * If not, see http://www.gnu.org/licenses/lgpl.html *
20 * *
21 * The TYPO3 project - inspiring people to share! *
22 * */
23
24 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25
26 /**
27 * Test case
28 */
29 class ArgumentTest extends UnitTestCase
30 {
31 /**
32 * @var \TYPO3\CMS\Extbase\Mvc\Controller\Argument
33 */
34 protected $simpleValueArgument;
35
36 /**
37 * @var \TYPO3\CMS\Extbase\Mvc\Controller\Argument
38 */
39 protected $objectArgument;
40
41 protected $mockPropertyMapper;
42
43 protected $mockConfigurationBuilder;
44
45 protected $mockConfiguration;
46
47 /**
48 */
49 protected function setUp()
50 {
51 $this->simpleValueArgument = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class, ['dummy'], ['someName', 'string']);
52 $this->objectArgument = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class, ['dummy'], ['someName', 'DateTime']);
53 $this->mockPropertyMapper = $this->createMock(\TYPO3\CMS\Extbase\Property\PropertyMapper::class);
54 $this->simpleValueArgument->_set('propertyMapper', $this->mockPropertyMapper);
55 $this->objectArgument->_set('propertyMapper', $this->mockPropertyMapper);
56 $this->mockConfiguration = new \TYPO3\CMS\Extbase\Mvc\Controller\MvcPropertyMappingConfiguration();
57 $propertyMappingConfiguranion = new \TYPO3\CMS\Extbase\Mvc\Controller\MvcPropertyMappingConfiguration();
58 $this->simpleValueArgument->_set('propertyMappingConfiguration', $propertyMappingConfiguranion);
59 $this->objectArgument->_set('propertyMappingConfiguration', $propertyMappingConfiguranion);
60 }
61
62 /**
63 * @test
64 */
65 public function constructingArgumentWithoutNameThrowsException()
66 {
67 $this->expectException(\InvalidArgumentException::class);
68 $this->expectExceptionCode(1232551853);
69 new \TYPO3\CMS\Extbase\Mvc\Controller\Argument('', 'Text');
70 }
71
72 /**
73 * @test
74 */
75 public function constructingArgumentWithInvalidNameThrowsException()
76 {
77 $this->expectException(\InvalidArgumentException::class);
78 $this->expectExceptionCode(1187951688);
79 new \TYPO3\CMS\Extbase\Mvc\Controller\Argument(new \ArrayObject(), 'Text');
80 }
81
82 /**
83 * @test
84 */
85 public function passingDataTypeToConstructorReallySetsTheDataType()
86 {
87 $this->assertEquals('string', $this->simpleValueArgument->getDataType(), 'The specified data type has not been set correctly.');
88 $this->assertEquals('someName', $this->simpleValueArgument->getName(), 'The specified name has not been set correctly.');
89 }
90
91 /**
92 * @test
93 */
94 public function setShortNameProvidesFluentInterface()
95 {
96 $returnedArgument = $this->simpleValueArgument->setShortName('x');
97 $this->assertSame($this->simpleValueArgument, $returnedArgument, 'The returned argument is not the original argument.');
98 }
99
100 /**
101 * @return array
102 */
103 public function invalidShortNames()
104 {
105 return [
106 [''],
107 ['as'],
108 [5]
109 ];
110 }
111
112 /**
113 * @test
114 * @dataProvider invalidShortNames
115 * @param string $invalidShortName
116 */
117 public function shortNameShouldThrowExceptionIfInvalid($invalidShortName)
118 {
119 $this->expectException(\InvalidArgumentException::class);
120 $this->expectExceptionCode(1195824959);
121 $this->simpleValueArgument->setShortName($invalidShortName);
122 }
123
124 /**
125 * @test
126 */
127 public function shortNameCanBeRetrievedAgain()
128 {
129 $this->simpleValueArgument->setShortName('x');
130 $this->assertEquals('x', $this->simpleValueArgument->getShortName());
131 }
132
133 /**
134 * @test
135 */
136 public function setRequiredShouldProvideFluentInterfaceAndReallySetRequiredState()
137 {
138 $returnedArgument = $this->simpleValueArgument->setRequired(true);
139 $this->assertSame($this->simpleValueArgument, $returnedArgument, 'The returned argument is not the original argument.');
140 $this->assertTrue($this->simpleValueArgument->isRequired());
141 }
142
143 /**
144 * @test
145 */
146 public function setDefaultValueShouldProvideFluentInterfaceAndReallySetDefaultValue()
147 {
148 $returnedArgument = $this->simpleValueArgument->setDefaultValue('default');
149 $this->assertSame($this->simpleValueArgument, $returnedArgument, 'The returned argument is not the original argument.');
150 $this->assertSame('default', $this->simpleValueArgument->getDefaultValue());
151 }
152
153 /**
154 * @test
155 */
156 public function setValidatorShouldProvideFluentInterfaceAndReallySetValidator()
157 {
158 $mockValidator = $this->createMock(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class);
159 $returnedArgument = $this->simpleValueArgument->setValidator($mockValidator);
160 $this->assertSame($this->simpleValueArgument, $returnedArgument, 'The returned argument is not the original argument.');
161 $this->assertSame($mockValidator, $this->simpleValueArgument->getValidator());
162 }
163
164 /**
165 * @test
166 */
167 public function setValueProvidesFluentInterface()
168 {
169 $returnedArgument = $this->simpleValueArgument->setValue(null);
170 $this->assertSame($this->simpleValueArgument, $returnedArgument, 'The returned argument is not the original argument.');
171 }
172
173 /**
174 * @test
175 */
176 public function setValueUsesNullAsIs()
177 {
178 $this->simpleValueArgument = new \TYPO3\CMS\Extbase\Mvc\Controller\Argument('dummy', 'string');
179 $this->simpleValueArgument = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class, ['dummy'], ['dummy', 'string']);
180 $this->simpleValueArgument->setValue(null);
181 $this->assertNull($this->simpleValueArgument->getValue());
182 }
183
184 /**
185 * @test
186 */
187 public function setValueUsesMatchingInstanceAsIs()
188 {
189 $this->mockPropertyMapper->expects($this->never())->method('convert');
190 $this->objectArgument->setValue(new \DateTime());
191 }
192
193 /**
194 * @return \TYPO3\CMS\Extbase\Mvc\Controller\Argument $this
195 */
196 protected function setupPropertyMapperAndSetValue()
197 {
198 $this->mockPropertyMapper->expects($this->once())->method('convert')->with('someRawValue', 'string', $this->mockConfiguration)->will($this->returnValue('convertedValue'));
199 $this->mockPropertyMapper->expects($this->once())->method('getMessages')->will($this->returnValue(new \TYPO3\CMS\Extbase\Error\Result()));
200 return $this->simpleValueArgument->setValue('someRawValue');
201 }
202
203 /**
204 * @test
205 */
206 public function setValueShouldCallPropertyMapperCorrectlyAndStoreResultInValue()
207 {
208 $this->setupPropertyMapperAndSetValue();
209 $this->assertSame('convertedValue', $this->simpleValueArgument->getValue());
210 $this->assertTrue($this->simpleValueArgument->isValid());
211 }
212
213 /**
214 * @test
215 */
216 public function setValueShouldBeFluentInterface()
217 {
218 $this->assertSame($this->simpleValueArgument, $this->setupPropertyMapperAndSetValue());
219 }
220
221 /**
222 * @test
223 */
224 public function setValueShouldSetValidationErrorsIfValidatorIsSetAndValidationFailed()
225 {
226 $error = new \TYPO3\CMS\Extbase\Error\Error('Some Error', 1234);
227 $mockValidator = $this->getMockBuilder(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class)
228 ->setMethods(['validate', 'getOptions'])
229 ->getMock();
230 $validationMessages = new \TYPO3\CMS\Extbase\Error\Result();
231 $validationMessages->addError($error);
232 $mockValidator->expects($this->once())->method('validate')->with('convertedValue')->will($this->returnValue($validationMessages));
233 $this->simpleValueArgument->setValidator($mockValidator);
234 $this->setupPropertyMapperAndSetValue();
235 $this->assertFalse($this->simpleValueArgument->isValid());
236 $this->assertEquals([$error], $this->simpleValueArgument->validate()->getErrors());
237 }
238
239 /**
240 * @test
241 */
242 public function defaultPropertyMappingConfigurationDoesNotAllowCreationOrModificationOfObjects()
243 {
244 $this->assertNull($this->simpleValueArgument->getPropertyMappingConfiguration()->getConfigurationValue(\TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter::class, \TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED));
245 $this->assertNull($this->simpleValueArgument->getPropertyMappingConfiguration()->getConfigurationValue(\TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter::class, \TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter::CONFIGURATION_MODIFICATION_ALLOWED));
246 }
247 }