‪TYPO3CMS  9.5
ArgumentTest.php
Go to the documentation of this file.
1 <?php
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 TYPO3\TestingFramework\Core\Unit\UnitTestCase;
18 
22 class ‪ArgumentTest extends UnitTestCase
23 {
27  protected ‪$simpleValueArgument;
28 
32  protected ‪$objectArgument;
33 
35 
37 
38  protected ‪$mockConfiguration;
39 
42  protected function ‪setUp()
43  {
44  $this->simpleValueArgument = $this->getAccessibleMock(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\Argument::class, ['dummy'], ['someName', 'string']);
45  $this->objectArgument = $this->getAccessibleMock(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\Argument::class, ['dummy'], ['someName', 'DateTime']);
46  $this->mockPropertyMapper = $this->createMock(\‪TYPO3\CMS\‪Extbase\Property\PropertyMapper::class);
47  $this->simpleValueArgument->_set('propertyMapper', $this->mockPropertyMapper);
48  $this->objectArgument->_set('propertyMapper', $this->mockPropertyMapper);
49  $this->mockConfiguration = new \TYPO3\CMS\Extbase\Mvc\Controller\MvcPropertyMappingConfiguration();
50  $propertyMappingConfiguranion = new \TYPO3\CMS\Extbase\Mvc\Controller\MvcPropertyMappingConfiguration();
51  $this->simpleValueArgument->_set('propertyMappingConfiguration', $propertyMappingConfiguranion);
52  $this->objectArgument->_set('propertyMappingConfiguration', $propertyMappingConfiguranion);
53  }
54 
59  {
60  $this->expectException(\InvalidArgumentException::class);
61  $this->expectExceptionCode(1232551853);
62  new \TYPO3\CMS\Extbase\Mvc\Controller\Argument('', 'Text');
63  }
64 
69  {
70  $this->expectException(\InvalidArgumentException::class);
71  $this->expectExceptionCode(1187951688);
72  new \TYPO3\CMS\Extbase\Mvc\Controller\Argument(new \ArrayObject(), 'Text');
73  }
74 
79  {
80  $this->assertEquals('string', $this->simpleValueArgument->getDataType(), 'The specified data type has not been set correctly.');
81  $this->assertEquals('someName', $this->simpleValueArgument->getName(), 'The specified name has not been set correctly.');
82  }
83 
88  {
89  $returnedArgument = $this->simpleValueArgument->setShortName('x');
90  $this->assertSame($this->simpleValueArgument, $returnedArgument, 'The returned argument is not the original argument.');
91  }
92 
96  public function ‪invalidShortNames()
97  {
98  return [
99  [''],
100  ['as'],
101  [5]
102  ];
103  }
104 
110  public function ‪shortNameShouldThrowExceptionIfInvalid($invalidShortName)
111  {
112  $this->expectException(\InvalidArgumentException::class);
113  $this->expectExceptionCode(1195824959);
114  $this->simpleValueArgument->setShortName($invalidShortName);
115  }
116 
120  public function ‪shortNameCanBeRetrievedAgain()
121  {
122  $this->simpleValueArgument->setShortName('x');
123  $this->assertEquals('x', $this->simpleValueArgument->getShortName());
124  }
125 
130  {
131  $returnedArgument = $this->simpleValueArgument->setRequired(true);
132  $this->assertSame($this->simpleValueArgument, $returnedArgument, 'The returned argument is not the original argument.');
133  $this->assertTrue($this->simpleValueArgument->isRequired());
134  }
135 
140  {
141  $returnedArgument = $this->simpleValueArgument->setDefaultValue('default');
142  $this->assertSame($this->simpleValueArgument, $returnedArgument, 'The returned argument is not the original argument.');
143  $this->assertSame('default', $this->simpleValueArgument->getDefaultValue());
144  }
145 
150  {
151  $mockValidator = $this->createMock(\‪TYPO3\CMS\‪Extbase\Validation\Validator\ValidatorInterface::class);
152  $returnedArgument = $this->simpleValueArgument->setValidator($mockValidator);
153  $this->assertSame($this->simpleValueArgument, $returnedArgument, 'The returned argument is not the original argument.');
154  $this->assertSame($mockValidator, $this->simpleValueArgument->getValidator());
155  }
156 
160  public function ‪setValueProvidesFluentInterface()
161  {
162  $returnedArgument = $this->simpleValueArgument->setValue(null);
163  $this->assertSame($this->simpleValueArgument, $returnedArgument, 'The returned argument is not the original argument.');
164  }
165 
169  public function ‪setValueUsesNullAsIs()
170  {
171  $this->simpleValueArgument = new \TYPO3\CMS\Extbase\Mvc\Controller\Argument('dummy', 'string');
172  $this->simpleValueArgument = $this->getAccessibleMock(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\Argument::class, ['dummy'], ['dummy', 'string']);
173  $this->simpleValueArgument->setValue(null);
174  $this->assertNull($this->simpleValueArgument->getValue());
175  }
176 
180  public function ‪setValueUsesMatchingInstanceAsIs()
181  {
182  $this->mockPropertyMapper->expects($this->never())->method('convert');
183  $this->objectArgument->setValue(new \DateTime());
184  }
185 
189  protected function ‪setupPropertyMapperAndSetValue()
190  {
191  $this->mockPropertyMapper->expects($this->once())->method('convert')->with('someRawValue', 'string', $this->mockConfiguration)->will($this->returnValue('convertedValue'));
192  $this->mockPropertyMapper->expects($this->once())->method('getMessages')->will($this->returnValue(new \‪TYPO3\CMS\‪Extbase\Error\Result()));
193  return $this->simpleValueArgument->setValue('someRawValue');
194  }
195 
200  {
202  $this->assertSame('convertedValue', $this->simpleValueArgument->getValue());
203  $this->assertTrue($this->simpleValueArgument->isValid());
204  }
205 
209  public function ‪setValueShouldBeFluentInterface()
210  {
211  $this->assertSame($this->simpleValueArgument, $this->‪setupPropertyMapperAndSetValue());
212  }
213 
218  {
219  $error = new \TYPO3\CMS\Extbase\Error\Error('Some Error', 1234);
220  $mockValidator = $this->getMockBuilder(\‪TYPO3\CMS\‪Extbase\Validation\Validator\ValidatorInterface::class)
221  ->setMethods(['validate', 'getOptions'])
222  ->getMock();
223  $validationMessages = new \TYPO3\CMS\Extbase\Error\Result();
224  $validationMessages->addError($error);
225  $mockValidator->expects($this->once())->method('validate')->with('convertedValue')->will($this->returnValue($validationMessages));
226  $this->simpleValueArgument->setValidator($mockValidator);
228  $this->assertFalse($this->simpleValueArgument->isValid());
229  $this->assertEquals([$error], $this->simpleValueArgument->validate()->getErrors());
230  }
231 
236  {
237  $this->assertNull($this->simpleValueArgument->getPropertyMappingConfiguration()->getConfigurationValue(\‪TYPO3\CMS\‪Extbase\Property\TypeConverter\PersistentObjectConverter::class, \‪TYPO3\CMS\‪Extbase\Property\TypeConverter\PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED));
238  $this->assertNull($this->simpleValueArgument->getPropertyMappingConfiguration()->getConfigurationValue(\‪TYPO3\CMS\‪Extbase\Property\TypeConverter\PersistentObjectConverter::class, \‪TYPO3\CMS\‪Extbase\Property\TypeConverter\PersistentObjectConverter::CONFIGURATION_MODIFICATION_ALLOWED));
239  }
240 }
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\setValidatorShouldProvideFluentInterfaceAndReallySetValidator
‪setValidatorShouldProvideFluentInterfaceAndReallySetValidator()
Definition: ArgumentTest.php:147
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\setupPropertyMapperAndSetValue
‪TYPO3 CMS Extbase Mvc Controller Argument setupPropertyMapperAndSetValue()
Definition: ArgumentTest.php:187
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:4
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller
Definition: AbstractControllerTest.php:2
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\shortNameShouldThrowExceptionIfInvalid
‪shortNameShouldThrowExceptionIfInvalid($invalidShortName)
Definition: ArgumentTest.php:108
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\setValueShouldCallPropertyMapperCorrectlyAndStoreResultInValue
‪setValueShouldCallPropertyMapperCorrectlyAndStoreResultInValue()
Definition: ArgumentTest.php:197
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\constructingArgumentWithInvalidNameThrowsException
‪constructingArgumentWithInvalidNameThrowsException()
Definition: ArgumentTest.php:66
‪TYPO3
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\setUp
‪setUp()
Definition: ArgumentTest.php:40
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\setValueShouldSetValidationErrorsIfValidatorIsSetAndValidationFailed
‪setValueShouldSetValidationErrorsIfValidatorIsSetAndValidationFailed()
Definition: ArgumentTest.php:215
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\setValueShouldBeFluentInterface
‪setValueShouldBeFluentInterface()
Definition: ArgumentTest.php:207
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\$mockConfigurationBuilder
‪$mockConfigurationBuilder
Definition: ArgumentTest.php:34
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\$mockConfiguration
‪$mockConfiguration
Definition: ArgumentTest.php:36
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\shortNameCanBeRetrievedAgain
‪shortNameCanBeRetrievedAgain()
Definition: ArgumentTest.php:118
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\constructingArgumentWithoutNameThrowsException
‪constructingArgumentWithoutNameThrowsException()
Definition: ArgumentTest.php:56
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\defaultPropertyMappingConfigurationDoesNotAllowCreationOrModificationOfObjects
‪defaultPropertyMappingConfigurationDoesNotAllowCreationOrModificationOfObjects()
Definition: ArgumentTest.php:233
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\$objectArgument
‪TYPO3 CMS Extbase Mvc Controller Argument $objectArgument
Definition: ArgumentTest.php:30
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\$simpleValueArgument
‪TYPO3 CMS Extbase Mvc Controller Argument $simpleValueArgument
Definition: ArgumentTest.php:26
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest
Definition: ArgumentTest.php:23
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\setShortNameProvidesFluentInterface
‪setShortNameProvidesFluentInterface()
Definition: ArgumentTest.php:85
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\passingDataTypeToConstructorReallySetsTheDataType
‪passingDataTypeToConstructorReallySetsTheDataType()
Definition: ArgumentTest.php:76
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\$mockPropertyMapper
‪$mockPropertyMapper
Definition: ArgumentTest.php:32
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\setDefaultValueShouldProvideFluentInterfaceAndReallySetDefaultValue
‪setDefaultValueShouldProvideFluentInterfaceAndReallySetDefaultValue()
Definition: ArgumentTest.php:137
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\setRequiredShouldProvideFluentInterfaceAndReallySetRequiredState
‪setRequiredShouldProvideFluentInterfaceAndReallySetRequiredState()
Definition: ArgumentTest.php:127
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\invalidShortNames
‪array invalidShortNames()
Definition: ArgumentTest.php:94
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\setValueUsesNullAsIs
‪setValueUsesNullAsIs()
Definition: ArgumentTest.php:167
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\setValueProvidesFluentInterface
‪setValueProvidesFluentInterface()
Definition: ArgumentTest.php:158
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentTest\setValueUsesMatchingInstanceAsIs
‪setValueUsesMatchingInstanceAsIs()
Definition: ArgumentTest.php:178