‪TYPO3CMS  10.4
ArgumentTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
25 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
26 
30 class ‪ArgumentTest extends UnitTestCase
31 {
35  protected ‪$simpleValueArgument;
36 
40  protected ‪$objectArgument;
41 
43 
45 
47 
48  protected function ‪setUp(): void
49  {
50  parent::setUp();
51  $this->simpleValueArgument = new ‪Argument('someName', 'string');
52  $this->objectArgument = new ‪Argument('someName', 'DateTime');
53  $this->mockPropertyMapper = $this->createMock(PropertyMapper::class);
54  $this->simpleValueArgument->injectPropertyMapper($this->mockPropertyMapper);
55  $this->objectArgument->injectPropertyMapper($this->mockPropertyMapper);
56  $this->mockConfiguration = new ‪MvcPropertyMappingConfiguration();
57  $propertyMappingConfiguration = new ‪MvcPropertyMappingConfiguration();
58  $this->simpleValueArgument->injectPropertyMappingConfiguration($propertyMappingConfiguration);
59  $this->objectArgument->injectPropertyMappingConfiguration($propertyMappingConfiguration);
60  }
61 
66  {
67  $this->expectException(\InvalidArgumentException::class);
68  $this->expectExceptionCode(1232551853);
69  new ‪Argument('', 'Text');
70  }
71 
76  {
77  $this->expectException(\InvalidArgumentException::class);
78  $this->expectExceptionCode(1187951688);
79  new Argument(new \ArrayObject(), 'Text');
80  }
81 
86  {
87  self::assertEquals('string', $this->simpleValueArgument->getDataType(), 'The specified data type has not been set correctly.');
88  self::assertEquals('someName', $this->simpleValueArgument->getName(), 'The specified name has not been set correctly.');
89  }
90 
94  public function ‪setShortNameProvidesFluentInterface(): void
95  {
96  $returnedArgument = $this->simpleValueArgument->setShortName('x');
97  self::assertSame($this->simpleValueArgument, $returnedArgument, 'The returned argument is not the original argument.');
98  }
99 
103  public function ‪invalidShortNames(): array
104  {
105  return [
106  [''],
107  ['as'],
108  [5]
109  ];
110  }
111 
117  public function ‪shortNameShouldThrowExceptionIfInvalid($invalidShortName): void
118  {
119  $this->expectException(\InvalidArgumentException::class);
120  $this->expectExceptionCode(1195824959);
121  $this->simpleValueArgument->setShortName($invalidShortName);
122  }
123 
127  public function ‪shortNameCanBeRetrievedAgain(): void
128  {
129  $this->simpleValueArgument->setShortName('x');
130  self::assertEquals('x', $this->simpleValueArgument->getShortName());
131  }
132 
137  {
138  $returnedArgument = $this->simpleValueArgument->setRequired(true);
139  self::assertSame($this->simpleValueArgument, $returnedArgument, 'The returned argument is not the original argument.');
140  self::assertTrue($this->simpleValueArgument->isRequired());
141  }
142 
147  {
148  $returnedArgument = $this->simpleValueArgument->setDefaultValue('default');
149  self::assertSame($this->simpleValueArgument, $returnedArgument, 'The returned argument is not the original argument.');
150  self::assertSame('default', $this->simpleValueArgument->getDefaultValue());
151  }
152 
157  {
158  $mockValidator = $this->createMock(ValidatorInterface::class);
159  $returnedArgument = $this->simpleValueArgument->setValidator($mockValidator);
160  self::assertSame($this->simpleValueArgument, $returnedArgument, 'The returned argument is not the original argument.');
161  self::assertSame($mockValidator, $this->simpleValueArgument->getValidator());
162  }
163 
167  public function ‪setValueProvidesFluentInterface(): void
168  {
169  $returnedArgument = $this->simpleValueArgument->setValue(null);
170  self::assertSame($this->simpleValueArgument, $returnedArgument, 'The returned argument is not the original argument.');
171  }
172 
176  public function ‪setValueUsesNullAsIs(): void
177  {
178  $this->simpleValueArgument = new Argument('dummy', 'string');
179  $this->simpleValueArgument->setValue(null);
180  self::assertNull($this->simpleValueArgument->getValue());
181  }
182 
186  public function ‪setValueUsesMatchingInstanceAsIs(): void
187  {
188  $this->mockPropertyMapper->expects(self::never())->method('convert');
189  $this->objectArgument->setValue(new \DateTime());
190  }
191 
195  protected function ‪setupPropertyMapperAndSetValue(): Argument
196  {
197  $this->mockPropertyMapper->expects(self::once())->method('convert')->with('someRawValue', 'string', $this->mockConfiguration)->willReturn('convertedValue');
198  $this->mockPropertyMapper->expects(self::once())->method('getMessages')->willReturn(new Result());
199  return $this->simpleValueArgument->setValue('someRawValue');
200  }
201 
206  {
208  self::assertSame('convertedValue', $this->simpleValueArgument->getValue());
209  self::assertTrue($this->simpleValueArgument->isValid());
210  }
211 
215  public function ‪setValueShouldBeFluentInterface(): void
216  {
217  self::assertSame($this->simpleValueArgument, $this->‪setupPropertyMapperAndSetValue());
218  }
219 
224  {
225  $error = new Error('Some Error', 1234);
226  $mockValidator = $this->getMockBuilder(ValidatorInterface::class)
227  ->setMethods(['validate', 'getOptions'])
228  ->getMock();
229  $validationMessages = new Result();
230  $validationMessages->addError($error);
231  $mockValidator->expects(self::once())->method('validate')->with('convertedValue')->willReturn($validationMessages);
232  $this->simpleValueArgument->setValidator($mockValidator);
234  self::assertFalse($this->simpleValueArgument->isValid());
235  self::assertEquals([$error], $this->simpleValueArgument->validate()->getErrors());
236  }
237 
242  {
243  self::assertNull($this->simpleValueArgument->getPropertyMappingConfiguration()->getConfigurationValue(PersistentObjectConverter::class, ‪PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED));
244  self::assertNull($this->simpleValueArgument->getPropertyMappingConfiguration()->getConfigurationValue(PersistentObjectConverter::class, ‪PersistentObjectConverter::CONFIGURATION_MODIFICATION_ALLOWED));
245  }
246 }
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument\setValue
‪TYPO3 CMS Extbase Mvc Controller Argument setValue($rawValue)
Definition: Argument.php:246
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\setValidatorShouldProvideFluentInterfaceAndReallySetValidator
‪setValidatorShouldProvideFluentInterfaceAndReallySetValidator()
Definition: ArgumentTest.php:154
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\setupPropertyMapperAndSetValue
‪TYPO3 CMS Extbase Mvc Controller Argument setupPropertyMapperAndSetValue()
Definition: ArgumentTest.php:193
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller
Definition: ActionControllerTest.php:18
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\shortNameShouldThrowExceptionIfInvalid
‪shortNameShouldThrowExceptionIfInvalid($invalidShortName)
Definition: ArgumentTest.php:115
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\setValueShouldCallPropertyMapperCorrectlyAndStoreResultInValue
‪setValueShouldCallPropertyMapperCorrectlyAndStoreResultInValue()
Definition: ArgumentTest.php:203
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\constructingArgumentWithInvalidNameThrowsException
‪constructingArgumentWithInvalidNameThrowsException()
Definition: ArgumentTest.php:73
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\setUp
‪setUp()
Definition: ArgumentTest.php:46
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\setValueShouldSetValidationErrorsIfValidatorIsSetAndValidationFailed
‪setValueShouldSetValidationErrorsIfValidatorIsSetAndValidationFailed()
Definition: ArgumentTest.php:221
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\setValueShouldBeFluentInterface
‪setValueShouldBeFluentInterface()
Definition: ArgumentTest.php:213
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\$mockConfigurationBuilder
‪$mockConfigurationBuilder
Definition: ArgumentTest.php:42
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\$mockConfiguration
‪$mockConfiguration
Definition: ArgumentTest.php:44
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\shortNameCanBeRetrievedAgain
‪shortNameCanBeRetrievedAgain()
Definition: ArgumentTest.php:125
‪TYPO3\CMS\Extbase\Error\Result
Definition: Result.php:24
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\constructingArgumentWithoutNameThrowsException
‪constructingArgumentWithoutNameThrowsException()
Definition: ArgumentTest.php:63
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\defaultPropertyMappingConfigurationDoesNotAllowCreationOrModificationOfObjects
‪defaultPropertyMappingConfigurationDoesNotAllowCreationOrModificationOfObjects()
Definition: ArgumentTest.php:239
‪TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter\CONFIGURATION_CREATION_ALLOWED
‪const CONFIGURATION_CREATION_ALLOWED
Definition: PersistentObjectConverter.php:52
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\$objectArgument
‪TYPO3 CMS Extbase Mvc Controller Argument $objectArgument
Definition: ArgumentTest.php:38
‪TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter
Definition: PersistentObjectConverter.php:43
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\$simpleValueArgument
‪TYPO3 CMS Extbase Mvc Controller Argument $simpleValueArgument
Definition: ArgumentTest.php:34
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest
Definition: ArgumentTest.php:31
‪TYPO3\CMS\Extbase\Error\Error
Definition: Error.php:25
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\setShortNameProvidesFluentInterface
‪setShortNameProvidesFluentInterface()
Definition: ArgumentTest.php:92
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\passingDataTypeToConstructorReallySetsTheDataType
‪passingDataTypeToConstructorReallySetsTheDataType()
Definition: ArgumentTest.php:83
‪TYPO3\CMS\Extbase\Mvc\Controller\MvcPropertyMappingConfiguration
Definition: MvcPropertyMappingConfiguration.php:26
‪TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter\CONFIGURATION_MODIFICATION_ALLOWED
‪const CONFIGURATION_MODIFICATION_ALLOWED
Definition: PersistentObjectConverter.php:47
‪TYPO3\CMS\Extbase\Property\PropertyMapper
Definition: PropertyMapper.php:37
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\$mockPropertyMapper
‪$mockPropertyMapper
Definition: ArgumentTest.php:40
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\setDefaultValueShouldProvideFluentInterfaceAndReallySetDefaultValue
‪setDefaultValueShouldProvideFluentInterfaceAndReallySetDefaultValue()
Definition: ArgumentTest.php:144
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\setRequiredShouldProvideFluentInterfaceAndReallySetRequiredState
‪setRequiredShouldProvideFluentInterfaceAndReallySetRequiredState()
Definition: ArgumentTest.php:134
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\invalidShortNames
‪array invalidShortNames()
Definition: ArgumentTest.php:101
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\setValueUsesNullAsIs
‪setValueUsesNullAsIs()
Definition: ArgumentTest.php:174
‪TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface
Definition: ValidatorInterface.php:22
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\setValueProvidesFluentInterface
‪setValueProvidesFluentInterface()
Definition: ArgumentTest.php:165
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument
Definition: Argument.php:28
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\setValueUsesMatchingInstanceAsIs
‪setValueUsesMatchingInstanceAsIs()
Definition: ArgumentTest.php:184