[!!!][TASK] Extract testing framework for TYPO3
[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 /**
25 * Test case
26 */
27 class ArgumentTest extends \TYPO3\CMS\Components\TestingFramework\Core\UnitTestCase
28 {
29 /**
30 * @var \TYPO3\CMS\Extbase\Mvc\Controller\Argument
31 */
32 protected $simpleValueArgument;
33
34 /**
35 * @var \TYPO3\CMS\Extbase\Mvc\Controller\Argument
36 */
37 protected $objectArgument;
38
39 protected $mockPropertyMapper;
40
41 protected $mockConfigurationBuilder;
42
43 protected $mockConfiguration;
44
45 /**
46 */
47 protected function setUp()
48 {
49 $this->simpleValueArgument = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class, ['dummy'], ['someName', 'string']);
50 $this->objectArgument = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class, ['dummy'], ['someName', 'DateTime']);
51 $this->mockPropertyMapper = $this->createMock(\TYPO3\CMS\Extbase\Property\PropertyMapper::class);
52 $this->simpleValueArgument->_set('propertyMapper', $this->mockPropertyMapper);
53 $this->objectArgument->_set('propertyMapper', $this->mockPropertyMapper);
54 $this->mockConfiguration = new \TYPO3\CMS\Extbase\Mvc\Controller\MvcPropertyMappingConfiguration();
55 $propertyMappingConfiguranion = new \TYPO3\CMS\Extbase\Mvc\Controller\MvcPropertyMappingConfiguration();
56 $this->simpleValueArgument->_set('propertyMappingConfiguration', $propertyMappingConfiguranion);
57 $this->objectArgument->_set('propertyMappingConfiguration', $propertyMappingConfiguranion);
58 }
59
60 /**
61 * @test
62 */
63 public function constructingArgumentWithoutNameThrowsException()
64 {
65 $this->expectException(\InvalidArgumentException::class);
66 $this->expectExceptionCode(1232551853);
67 new \TYPO3\CMS\Extbase\Mvc\Controller\Argument('', 'Text');
68 }
69
70 /**
71 * @test
72 */
73 public function constructingArgumentWithInvalidNameThrowsException()
74 {
75 $this->expectException(\InvalidArgumentException::class);
76 $this->expectExceptionCode(1187951688);
77 new \TYPO3\CMS\Extbase\Mvc\Controller\Argument(new \ArrayObject(), 'Text');
78 }
79
80 /**
81 * @test
82 */
83 public function passingDataTypeToConstructorReallySetsTheDataType()
84 {
85 $this->assertEquals('string', $this->simpleValueArgument->getDataType(), 'The specified data type has not been set correctly.');
86 $this->assertEquals('someName', $this->simpleValueArgument->getName(), 'The specified name has not been set correctly.');
87 }
88
89 /**
90 * @test
91 */
92 public function setShortNameProvidesFluentInterface()
93 {
94 $returnedArgument = $this->simpleValueArgument->setShortName('x');
95 $this->assertSame($this->simpleValueArgument, $returnedArgument, 'The returned argument is not the original argument.');
96 }
97
98 /**
99 * @return array
100 */
101 public function invalidShortNames()
102 {
103 return [
104 [''],
105 ['as'],
106 [5]
107 ];
108 }
109
110 /**
111 * @test
112 * @dataProvider invalidShortNames
113 * @param string $invalidShortName
114 */
115 public function shortNameShouldThrowExceptionIfInvalid($invalidShortName)
116 {
117 $this->expectException(\InvalidArgumentException::class);
118 $this->expectExceptionCode(1195824959);
119 $this->simpleValueArgument->setShortName($invalidShortName);
120 }
121
122 /**
123 * @test
124 */
125 public function shortNameCanBeRetrievedAgain()
126 {
127 $this->simpleValueArgument->setShortName('x');
128 $this->assertEquals('x', $this->simpleValueArgument->getShortName());
129 }
130
131 /**
132 * @test
133 */
134 public function setRequiredShouldProvideFluentInterfaceAndReallySetRequiredState()
135 {
136 $returnedArgument = $this->simpleValueArgument->setRequired(true);
137 $this->assertSame($this->simpleValueArgument, $returnedArgument, 'The returned argument is not the original argument.');
138 $this->assertTrue($this->simpleValueArgument->isRequired());
139 }
140
141 /**
142 * @test
143 */
144 public function setDefaultValueShouldProvideFluentInterfaceAndReallySetDefaultValue()
145 {
146 $returnedArgument = $this->simpleValueArgument->setDefaultValue('default');
147 $this->assertSame($this->simpleValueArgument, $returnedArgument, 'The returned argument is not the original argument.');
148 $this->assertSame('default', $this->simpleValueArgument->getDefaultValue());
149 }
150
151 /**
152 * @test
153 */
154 public function setValidatorShouldProvideFluentInterfaceAndReallySetValidator()
155 {
156 $mockValidator = $this->createMock(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class);
157 $returnedArgument = $this->simpleValueArgument->setValidator($mockValidator);
158 $this->assertSame($this->simpleValueArgument, $returnedArgument, 'The returned argument is not the original argument.');
159 $this->assertSame($mockValidator, $this->simpleValueArgument->getValidator());
160 }
161
162 /**
163 * @test
164 */
165 public function setValueProvidesFluentInterface()
166 {
167 $returnedArgument = $this->simpleValueArgument->setValue(null);
168 $this->assertSame($this->simpleValueArgument, $returnedArgument, 'The returned argument is not the original argument.');
169 }
170
171 /**
172 * @test
173 */
174 public function setValueUsesNullAsIs()
175 {
176 $this->simpleValueArgument = new \TYPO3\CMS\Extbase\Mvc\Controller\Argument('dummy', 'string');
177 $this->simpleValueArgument = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class, ['dummy'], ['dummy', 'string']);
178 $this->simpleValueArgument->setValue(null);
179 $this->assertNull($this->simpleValueArgument->getValue());
180 }
181
182 /**
183 * @test
184 */
185 public function setValueUsesMatchingInstanceAsIs()
186 {
187 $this->mockPropertyMapper->expects($this->never())->method('convert');
188 $this->objectArgument->setValue(new \DateTime());
189 }
190
191 /**
192 * @return \TYPO3\CMS\Extbase\Mvc\Controller\Argument $this
193 */
194 protected function setupPropertyMapperAndSetValue()
195 {
196 $this->mockPropertyMapper->expects($this->once())->method('convert')->with('someRawValue', 'string', $this->mockConfiguration)->will($this->returnValue('convertedValue'));
197 $this->mockPropertyMapper->expects($this->once())->method('getMessages')->will($this->returnValue(new \TYPO3\CMS\Extbase\Error\Result()));
198 return $this->simpleValueArgument->setValue('someRawValue');
199 }
200
201 /**
202 * @test
203 */
204 public function setValueShouldCallPropertyMapperCorrectlyAndStoreResultInValue()
205 {
206 $this->setupPropertyMapperAndSetValue();
207 $this->assertSame('convertedValue', $this->simpleValueArgument->getValue());
208 $this->assertTrue($this->simpleValueArgument->isValid());
209 }
210
211 /**
212 * @test
213 */
214 public function setValueShouldBeFluentInterface()
215 {
216 $this->assertSame($this->simpleValueArgument, $this->setupPropertyMapperAndSetValue());
217 }
218
219 /**
220 * @test
221 */
222 public function setValueShouldSetValidationErrorsIfValidatorIsSetAndValidationFailed()
223 {
224 $error = new \TYPO3\CMS\Extbase\Error\Error('Some Error', 1234);
225 $mockValidator = $this->getMockBuilder(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class)
226 ->setMethods(['validate', 'getOptions'])
227 ->getMock();
228 $validationMessages = new \TYPO3\CMS\Extbase\Error\Result();
229 $validationMessages->addError($error);
230 $mockValidator->expects($this->once())->method('validate')->with('convertedValue')->will($this->returnValue($validationMessages));
231 $this->simpleValueArgument->setValidator($mockValidator);
232 $this->setupPropertyMapperAndSetValue();
233 $this->assertFalse($this->simpleValueArgument->isValid());
234 $this->assertEquals([$error], $this->simpleValueArgument->getValidationResults()->getErrors());
235 }
236
237 /**
238 * @test
239 */
240 public function defaultPropertyMappingConfigurationDoesNotAllowCreationOrModificationOfObjects()
241 {
242 $this->assertNull($this->simpleValueArgument->getPropertyMappingConfiguration()->getConfigurationValue(\TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter::class, \TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED));
243 $this->assertNull($this->simpleValueArgument->getPropertyMappingConfiguration()->getConfigurationValue(\TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter::class, \TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter::CONFIGURATION_MODIFICATION_ALLOWED));
244 }
245 }