TYPO3 CMS  TYPO3_8-7
ArgumentTest.php
Go to the documentation of this file.
1 <?php
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 
27 class ArgumentTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
28 {
33 
37  protected $objectArgument;
38 
40 
42 
43  protected $mockConfiguration;
44 
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 
64  {
65  $this->expectException(\InvalidArgumentException::class);
66  $this->expectExceptionCode(1232551853);
67  new \TYPO3\CMS\Extbase\Mvc\Controller\Argument('', 'Text');
68  }
69 
74  {
75  $this->expectException(\InvalidArgumentException::class);
76  $this->expectExceptionCode(1187951688);
77  new \TYPO3\CMS\Extbase\Mvc\Controller\Argument(new \ArrayObject(), 'Text');
78  }
79 
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 
93  {
94  $returnedArgument = $this->simpleValueArgument->setShortName('x');
95  $this->assertSame($this->simpleValueArgument, $returnedArgument, 'The returned argument is not the original argument.');
96  }
97 
101  public function invalidShortNames()
102  {
103  return [
104  [''],
105  ['as'],
106  [5]
107  ];
108  }
109 
115  public function shortNameShouldThrowExceptionIfInvalid($invalidShortName)
116  {
117  $this->expectException(\InvalidArgumentException::class);
118  $this->expectExceptionCode(1195824959);
119  $this->simpleValueArgument->setShortName($invalidShortName);
120  }
121 
126  {
127  $this->simpleValueArgument->setShortName('x');
128  $this->assertEquals('x', $this->simpleValueArgument->getShortName());
129  }
130 
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 
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 
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 
166  {
167  $returnedArgument = $this->simpleValueArgument->setValue(null);
168  $this->assertSame($this->simpleValueArgument, $returnedArgument, 'The returned argument is not the original argument.');
169  }
170 
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 
186  {
187  $this->mockPropertyMapper->expects($this->never())->method('convert');
188  $this->objectArgument->setValue(new \DateTime());
189  }
190 
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 
205  {
207  $this->assertSame('convertedValue', $this->simpleValueArgument->getValue());
208  $this->assertTrue($this->simpleValueArgument->isValid());
209  }
210 
215  {
216  $this->assertSame($this->simpleValueArgument, $this->setupPropertyMapperAndSetValue());
217  }
218 
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);
233  $this->assertFalse($this->simpleValueArgument->isValid());
234  $this->assertEquals([$error], $this->simpleValueArgument->getValidationResults()->getErrors());
235  }
236 
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 }