TYPO3 CMS  TYPO3_7-6
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 
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->getMock(\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 
65  {
66  new \TYPO3\CMS\Extbase\Mvc\Controller\Argument('', 'Text');
67  }
68 
74  {
75  new \TYPO3\CMS\Extbase\Mvc\Controller\Argument(new \ArrayObject(), 'Text');
76  }
77 
82  {
83  $this->assertEquals('string', $this->simpleValueArgument->getDataType(), 'The specified data type has not been set correctly.');
84  $this->assertEquals('someName', $this->simpleValueArgument->getName(), 'The specified name has not been set correctly.');
85  }
86 
91  {
92  $returnedArgument = $this->simpleValueArgument->setShortName('x');
93  $this->assertSame($this->simpleValueArgument, $returnedArgument, 'The returned argument is not the original argument.');
94  }
95 
99  public function invalidShortNames()
100  {
101  return [
102  [''],
103  ['as'],
104  [5]
105  ];
106  }
107 
114  public function shortNameShouldThrowExceptionIfInvalid($invalidShortName)
115  {
116  $this->simpleValueArgument->setShortName($invalidShortName);
117  }
118 
123  {
124  $this->simpleValueArgument->setShortName('x');
125  $this->assertEquals('x', $this->simpleValueArgument->getShortName());
126  }
127 
132  {
133  $returnedArgument = $this->simpleValueArgument->setRequired(true);
134  $this->assertSame($this->simpleValueArgument, $returnedArgument, 'The returned argument is not the original argument.');
135  $this->assertTrue($this->simpleValueArgument->isRequired());
136  }
137 
142  {
143  $returnedArgument = $this->simpleValueArgument->setDefaultValue('default');
144  $this->assertSame($this->simpleValueArgument, $returnedArgument, 'The returned argument is not the original argument.');
145  $this->assertSame('default', $this->simpleValueArgument->getDefaultValue());
146  }
147 
152  {
153  $mockValidator = $this->getMock(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class);
154  $returnedArgument = $this->simpleValueArgument->setValidator($mockValidator);
155  $this->assertSame($this->simpleValueArgument, $returnedArgument, 'The returned argument is not the original argument.');
156  $this->assertSame($mockValidator, $this->simpleValueArgument->getValidator());
157  }
158 
163  {
164  $returnedArgument = $this->simpleValueArgument->setValue(null);
165  $this->assertSame($this->simpleValueArgument, $returnedArgument, 'The returned argument is not the original argument.');
166  }
167 
171  public function setValueUsesNullAsIs()
172  {
173  $this->simpleValueArgument = new \TYPO3\CMS\Extbase\Mvc\Controller\Argument('dummy', 'string');
174  $this->simpleValueArgument = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class, ['dummy'], ['dummy', 'string']);
175  $this->simpleValueArgument->setValue(null);
176  $this->assertNull($this->simpleValueArgument->getValue());
177  }
178 
183  {
184  $this->mockPropertyMapper->expects($this->never())->method('convert');
185  $this->objectArgument->setValue(new \DateTime());
186  }
187 
191  protected function setupPropertyMapperAndSetValue()
192  {
193  $this->mockPropertyMapper->expects($this->once())->method('convert')->with('someRawValue', 'string', $this->mockConfiguration)->will($this->returnValue('convertedValue'));
194  $this->mockPropertyMapper->expects($this->once())->method('getMessages')->will($this->returnValue(new \TYPO3\CMS\Extbase\Error\Result()));
195  return $this->simpleValueArgument->setValue('someRawValue');
196  }
197 
202  {
204  $this->assertSame('convertedValue', $this->simpleValueArgument->getValue());
205  $this->assertTrue($this->simpleValueArgument->isValid());
206  }
207 
212  {
213  $this->assertSame($this->simpleValueArgument, $this->setupPropertyMapperAndSetValue());
214  }
215 
220  {
221  $error = new \TYPO3\CMS\Extbase\Error\Error('Some Error', 1234);
222  $mockValidator = $this->getMock(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class, ['validate', 'getOptions']);
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->getValidationResults()->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 }
getAccessibleMock( $originalClassName, $methods=[], array $arguments=[], $mockClassName='', $callOriginalConstructor=true, $callOriginalClone=true, $callAutoload=true)