2392928eda44400c7a19b69492c596e2c81f52cc
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Mvc / Controller / ArgumentTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller;
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
24 /**
25 * Test case
26 */
27 class ArgumentTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
28
29 /**
30 * @var \TYPO3\CMS\Extbase\Mvc\Controller\Argument
31 */
32 protected $simpleValueArgument;
33
34 /**
35 * @var \TYPO3\CMS\Extbase\Mvc\Controller\Argument
36 */
37 protected $objectArgument;
38
39 protected $mockPropertyMapper;
40
41 protected $mockConfigurationBuilder;
42
43 protected $mockConfiguration;
44
45 /**
46 * @author Sebastian Kurfürst <sebastian@typo3.org>
47 */
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
60 /**
61 * @test
62 * @author Robert Lemke <robert@typo3.org>
63 * @expectedException \InvalidArgumentException
64 */
65 public function constructingArgumentWithoutNameThrowsException() {
66 new \TYPO3\CMS\Extbase\Mvc\Controller\Argument('', 'Text');
67 }
68
69 /**
70 * @test
71 * @expectedException \InvalidArgumentException
72 * @author Robert Lemke <robert@typo3.org>
73 */
74 public function constructingArgumentWithInvalidNameThrowsException() {
75 new \TYPO3\CMS\Extbase\Mvc\Controller\Argument(new \ArrayObject(), 'Text');
76 }
77
78 /**
79 * @test
80 * @author Robert Lemke <robert@typo3.org>
81 */
82 public function passingDataTypeToConstructorReallySetsTheDataType() {
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
87 /**
88 * @test
89 * @author Robert Lemke <robert@typo3.org>
90 */
91 public function setShortNameProvidesFluentInterface() {
92 $returnedArgument = $this->simpleValueArgument->setShortName('x');
93 $this->assertSame($this->simpleValueArgument, $returnedArgument, 'The returned argument is not the original argument.');
94 }
95
96 /**
97 * @return array
98 */
99 public function invalidShortNames() {
100 return array(
101 array(''),
102 array('as'),
103 array(5)
104 );
105 }
106
107 /**
108 * @test
109 * @dataProvider invalidShortNames
110 * @expectedException \InvalidArgumentException
111 * @author Sebastian Kurfürst <sebastian@typo3.org>
112 * @param string $invalidShortName
113 */
114 public function shortNameShouldThrowExceptionIfInvalid($invalidShortName) {
115 $this->simpleValueArgument->setShortName($invalidShortName);
116 }
117
118 /**
119 * @test
120 * @author Sebastian Kurfürst <sebastian@typo3.org>
121 */
122 public function shortNameCanBeRetrievedAgain() {
123 $this->simpleValueArgument->setShortName('x');
124 $this->assertEquals('x', $this->simpleValueArgument->getShortName());
125 }
126
127 /**
128 * @test
129 * @author Sebastian Kurfürst <sebastian@typo3.org>
130 */
131 public function setRequiredShouldProvideFluentInterfaceAndReallySetRequiredState() {
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
137 /**
138 * @test
139 * @author Sebastian Kurfürst <sebastian@typo3.org>
140 */
141 public function setDefaultValueShouldProvideFluentInterfaceAndReallySetDefaultValue() {
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
147 /**
148 * @test
149 * @author Sebastian Kurfürst <sebastian@typo3.org>
150 */
151 public function setValidatorShouldProvideFluentInterfaceAndReallySetValidator() {
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
158 /**
159 * @test
160 * @author Robert Lemke <robert@typo3.org>
161 */
162 public function setValueProvidesFluentInterface() {
163 $returnedArgument = $this->simpleValueArgument->setValue(NULL);
164 $this->assertSame($this->simpleValueArgument, $returnedArgument, 'The returned argument is not the original argument.');
165 }
166
167 /**
168 * @test
169 * @author Karsten Dambekalns <karsten@typo3.org>
170 */
171 public function setValueUsesNullAsIs() {
172 $this->simpleValueArgument = new \TYPO3\CMS\Extbase\Mvc\Controller\Argument('dummy', 'string');
173 $this->simpleValueArgument = $this->getAccessibleMock('TYPO3\CMS\Extbase\Mvc\Controller\Argument', array('dummy'), array('dummy', 'string'));
174 $this->simpleValueArgument->setValue(NULL);
175 $this->assertNull($this->simpleValueArgument->getValue());
176 }
177
178 /**
179 * @test
180 * @author Karsten Dambekalns <karsten@typo3.org>
181 */
182 public function setValueUsesMatchingInstanceAsIs() {
183 $this->mockPropertyMapper->expects($this->never())->method('convert');
184 $this->objectArgument->setValue(new \DateTime());
185 }
186
187 /**
188 * @return \TYPO3\CMS\Extbase\Mvc\Controller\Argument $this
189 */
190 protected function setupPropertyMapperAndSetValue() {
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
196 /**
197 * @test
198 * @author Sebastian Kurfürst <sebastian@typo3.org>
199 */
200 public function setValueShouldCallPropertyMapperCorrectlyAndStoreResultInValue() {
201 $this->setupPropertyMapperAndSetValue();
202 $this->assertSame('convertedValue', $this->simpleValueArgument->getValue());
203 $this->assertTrue($this->simpleValueArgument->isValid());
204 }
205
206 /**
207 * @test
208 * @author Sebastian Kurfürst <sebastian@typo3.org>
209 */
210 public function setValueShouldBeFluentInterface() {
211 $this->assertSame($this->simpleValueArgument, $this->setupPropertyMapperAndSetValue());
212 }
213
214 /**
215 * @test
216 * @author Sebastian Kurfürst <sebastian@typo3.org>
217 */
218 public function setValueShouldSetValidationErrorsIfValidatorIsSetAndValidationFailed() {
219 $error = new \TYPO3\CMS\Extbase\Error\Error('Some Error', 1234);
220 $mockValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ValidatorInterface', array('validate', 'getOptions'));
221 $validationMessages = new \TYPO3\CMS\Extbase\Error\Result();
222 $validationMessages->addError($error);
223 $mockValidator->expects($this->once())->method('validate')->with('convertedValue')->will($this->returnValue($validationMessages));
224 $this->simpleValueArgument->setValidator($mockValidator);
225 $this->setupPropertyMapperAndSetValue();
226 $this->assertFalse($this->simpleValueArgument->isValid());
227 $this->assertEquals(array($error), $this->simpleValueArgument->getValidationResults()->getErrors());
228 }
229
230 /**
231 * @test
232 * @author Sebastian Kurfürst <sebastian@typo3.org>
233 */
234 public function defaultPropertyMappingConfigurationDoesNotAllowCreationOrModificationOfObjects() {
235 $this->assertNull($this->simpleValueArgument->getPropertyMappingConfiguration()->getConfigurationValue('TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter', \TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED));
236 $this->assertNull($this->simpleValueArgument->getPropertyMappingConfiguration()->getConfigurationValue('TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter', \TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter::CONFIGURATION_MODIFICATION_ALLOWED));
237 }
238 }