TYPO3 CMS  TYPO3_6-2
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 
48  public function setUp() {
49  $this->simpleValueArgument = $this->getAccessibleMock('TYPO3\CMS\Extbase\Mvc\Controller\Argument', array('dummy'), array('someName', 'string'));
50  $this->objectArgument = $this->getAccessibleMock('TYPO3\CMS\Extbase\Mvc\Controller\Argument', array('dummy'), array('someName', 'DateTime'));
51  $this->mockPropertyMapper = $this->getMock('TYPO3\\CMS\\Extbase\\Property\\PropertyMapper');
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 
66  new \TYPO3\CMS\Extbase\Mvc\Controller\Argument('', 'Text');
67  }
68 
75  new \TYPO3\CMS\Extbase\Mvc\Controller\Argument(new \ArrayObject(), 'Text');
76  }
77 
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 
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  return array(
101  array(''),
102  array('as'),
103  array(5)
104  );
105  }
106 
114  public function shortNameShouldThrowExceptionIfInvalid($invalidShortName) {
115  $this->simpleValueArgument->setShortName($invalidShortName);
116  }
117 
122  public function shortNameCanBeRetrievedAgain() {
123  $this->simpleValueArgument->setShortName('x');
124  $this->assertEquals('x', $this->simpleValueArgument->getShortName());
125  }
126 
132  $returnedArgument = $this->simpleValueArgument->setRequired(TRUE);
133  $this->assertSame($this->simpleValueArgument, $returnedArgument, 'The returned argument is not the original argument.');
134  $this->assertTrue($this->simpleValueArgument->isRequired());
135  }
136 
142  $returnedArgument = $this->simpleValueArgument->setDefaultValue('default');
143  $this->assertSame($this->simpleValueArgument, $returnedArgument, 'The returned argument is not the original argument.');
144  $this->assertSame('default', $this->simpleValueArgument->getDefaultValue());
145  }
146 
152  $mockValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ValidatorInterface');
153  $returnedArgument = $this->simpleValueArgument->setValidator($mockValidator);
154  $this->assertSame($this->simpleValueArgument, $returnedArgument, 'The returned argument is not the original argument.');
155  $this->assertSame($mockValidator, $this->simpleValueArgument->getValidator());
156  }
157 
163  $this->enableRewrittenPropertyMapperInArgument($this->simpleValueArgument);
164  $returnedArgument = $this->simpleValueArgument->setValue(NULL);
165  $this->assertSame($this->simpleValueArgument, $returnedArgument, 'The returned argument is not the original argument.');
166  }
167 
172  public function setValueUsesNullAsIs() {
173  $this->simpleValueArgument = new \TYPO3\CMS\Extbase\Mvc\Controller\Argument('dummy', 'string');
174  $this->simpleValueArgument = $this->getAccessibleMock('TYPO3\CMS\Extbase\Mvc\Controller\Argument', array('dummy'), array('dummy', 'string'));
175  $this->enableRewrittenPropertyMapperInArgument($this->simpleValueArgument);
176  $this->simpleValueArgument->setValue(NULL);
177  $this->assertNull($this->simpleValueArgument->getValue());
178  }
179 
183  public function validClassNameDataTypes() {
184  return array(
185  array('Tx_Foo_Bar'),
186  array('ExtbaseTeam\\BlogExample\\Foo\\Bar'),
187  );
188  }
189 
194  public function classSchemaIsBuiltForClassNameDataTypesWhenReflectionServiceIsInjected($dataType) {
196  $argument = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('dummy'), array(), '', FALSE);
197  $argument->_set('dataType', $dataType);
198  $reflectionService = $this->getMock('TYPO3\\CMS\\Extbase\\Reflection\\ReflectionService');
199  $reflectionService->expects($this->once())->method('getClassSchema')->with($dataType);
200  $argument->injectReflectionService($reflectionService);
201  }
202 
207  return array(
208  array('integer'),
209  array('array'),
210  array('DateTime'),
211  );
212  }
213 
218  public function classSchemaIsNotBuiltForPrimitiveDataTypesWhenReflectionServiceIsInjected($dataType) {
220  $argument = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('dummy'), array(), '', FALSE);
221  $argument->_set('dataType', $dataType);
222  $reflectionService = $this->getMock('TYPO3\\CMS\\Extbase\\Reflection\\ReflectionService');
223  $reflectionService->expects($this->never())->method('getClassSchema');
224  $argument->injectReflectionService($reflectionService);
225  }
226 
232  $this->enableRewrittenPropertyMapperInArgument($this->objectArgument);
233  $this->mockPropertyMapper->expects($this->never())->method('convert');
234  $this->objectArgument->setValue(new \DateTime());
235  }
236 
240  protected function setupPropertyMapperAndSetValue() {
241  $this->mockPropertyMapper->expects($this->once())->method('convert')->with('someRawValue', 'string', $this->mockConfiguration)->will($this->returnValue('convertedValue'));
242  $this->mockPropertyMapper->expects($this->once())->method('getMessages')->will($this->returnValue(new \TYPO3\CMS\Extbase\Error\Result()));
243  return $this->simpleValueArgument->setValue('someRawValue');
244  }
245 
251  $this->enableRewrittenPropertyMapperInArgument($this->simpleValueArgument);
253  $this->assertSame('convertedValue', $this->simpleValueArgument->getValue());
254  $this->assertTrue($this->simpleValueArgument->isValid());
255  }
256 
262  $this->enableRewrittenPropertyMapperInArgument($this->simpleValueArgument);
263  $this->assertSame($this->simpleValueArgument, $this->setupPropertyMapperAndSetValue());
264  }
265 
271  $this->enableRewrittenPropertyMapperInArgument($this->simpleValueArgument);
272  $error = new \TYPO3\CMS\Extbase\Error\Error('Some Error', 1234);
273  $mockValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ValidatorInterface', array('validate'));
274  $validationMessages = new \TYPO3\CMS\Extbase\Error\Result();
275  $validationMessages->addError($error);
276  $mockValidator->expects($this->once())->method('validate')->with('convertedValue')->will($this->returnValue($validationMessages));
277  $this->simpleValueArgument->setValidator($mockValidator);
279  $this->assertFalse($this->simpleValueArgument->isValid());
280  $this->assertEquals(array($error), $this->simpleValueArgument->getValidationResults()->getErrors());
281  }
282 
288  $this->assertNull($this->simpleValueArgument->getPropertyMappingConfiguration()->getConfigurationValue('TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter', \TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED));
289  $this->assertNull($this->simpleValueArgument->getPropertyMappingConfiguration()->getConfigurationValue('TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter', \TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter::CONFIGURATION_MODIFICATION_ALLOWED));
290  }
291 
297  protected function enableRewrittenPropertyMapperInArgument(\TYPO3\CMS\Extbase\Mvc\Controller\Argument $argument) {
298  $mockConfigurationManager = $this->getMock('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManagerInterface');
299  $mockConfigurationManager->expects($this->any())->method('isFeatureEnabled')->with('rewrittenPropertyMapper')->will($this->returnValue(TRUE));
300  $argument->_set('configurationManager', $mockConfigurationManager);
301  }
302 }
enableRewrittenPropertyMapperInArgument(\TYPO3\CMS\Extbase\Mvc\Controller\Argument $argument)
getAccessibleMock( $originalClassName, array $methods=array(), array $arguments=array(), $mockClassName='', $callOriginalConstructor=TRUE, $callOriginalClone=TRUE, $callAutoload=TRUE)