1e40e3525ebf0867525255cdc7255b2aee543132
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Mvc / Controller / ArgumentBehaviorBeforeExtbase14Test.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * This class is a backport of the corresponding class of TYPO3 Flow.
8 * All credits go to the TYPO3 Flow team.
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 * A copy is found in the textfile GPL.txt and important notices to the license
20 * from the author is found in LICENSE.txt distributed with these scripts.
21 *
22 *
23 * This script is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * This copyright notice MUST APPEAR in all copies of the script!
29 ***************************************************************/
30 /**
31 * This test checks the Argument behavior before Extbase 1.4, i.e. with the old property mapper.
32 *
33 * @deprecated since Extbase 1.4.0, will be removed two versions after Extbase 6.1
34 */
35 class ArgumentBehaviorBeforeExtbase14Test extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
36
37 /**
38 * @test
39 * @expectedException \InvalidArgumentException
40 */
41 public function constructingArgumentWithoutNameThrowsException() {
42 new \TYPO3\CMS\Extbase\Mvc\Controller\Argument(NULL, 'Text');
43 }
44
45 /**
46 * @test
47 * @expectedException \InvalidArgumentException
48 */
49 public function constructingArgumentWithInvalidNameThrowsException() {
50 new \TYPO3\CMS\Extbase\Mvc\Controller\Argument(new \ArrayObject(), 'Text');
51 }
52
53 /**
54 * @test
55 */
56 public function passingDataTypeToConstructorReallySetsTheDataType() {
57 $argument = new \TYPO3\CMS\Extbase\Mvc\Controller\Argument('dummy', 'Number');
58 $this->assertEquals('Number', $argument->getDataType(), 'The specified data type has not been set correctly.');
59 }
60
61 /**
62 * @test
63 */
64 public function setShortNameProvidesFluentInterface() {
65 $argument = new \TYPO3\CMS\Extbase\Mvc\Controller\Argument('dummy', 'Text');
66 $returnedArgument = $argument->setShortName('x');
67 $this->assertSame($argument, $returnedArgument, 'The returned argument is not the original argument.');
68 }
69
70 /**
71 * @test
72 */
73 public function setValueProvidesFluentInterface() {
74 $argument = new \TYPO3\CMS\Extbase\Mvc\Controller\Argument('dummy', 'Text');
75 $this->enableDeprecatedPropertyMapperInArgument($argument);
76 $returnedArgument = $argument->setValue('x');
77 $this->assertSame($argument, $returnedArgument, 'The returned argument is not the original argument.');
78 }
79
80 /**
81 * @test
82 * @author Karsten Dambekalns <karsten@typo3.org>
83 */
84 public function setValueUsesNullAsIs() {
85 $argument = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('transformValue'), array('dummy', 'ArrayObject'));
86 $this->enableDeprecatedPropertyMapperInArgument($argument);
87 $argument->expects($this->never())->method('transformValue');
88 $argument->setValue(NULL);
89 }
90
91 /**
92 * @test
93 * @author Karsten Dambekalns <karsten@typo3.org>
94 */
95 public function setValueUsesMatchingInstanceAsIs() {
96 $argument = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('transformValue'), array('dummy', 'ArrayObject'));
97 $this->enableDeprecatedPropertyMapperInArgument($argument);
98 $argument->expects($this->never())->method('transformValue');
99 $argument->setValue(new \ArrayObject());
100 }
101
102 /**
103 * @test
104 */
105 public function setValueTriesToConvertAnUidIntoTheRealObjectIfTheDataTypeClassSchemaIsSet() {
106 $object = new \StdClass();
107 $argument = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('findObjectByUid'), array(), '', FALSE);
108 $this->enableDeprecatedPropertyMapperInArgument($argument);
109 $argument->expects($this->once())->method('findObjectByUid')->with('42')->will($this->returnValue($object));
110 $argument->_set('dataTypeClassSchema', 'stdClass');
111 $argument->_set('dataType', 'stdClass');
112 // $argument->_set('queryFactory', $mockQueryFactory);
113 $argument->setValue('42');
114 $this->assertSame($object, $argument->_get('value'));
115 $this->assertSame(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::ORIGIN_PERSISTENCE, $argument->getOrigin());
116 }
117
118 /**
119 * @test
120 */
121 public function toStringReturnsTheStringVersionOfTheArgumentsValue() {
122 $argument = new \TYPO3\CMS\Extbase\Mvc\Controller\Argument('dummy', 'Text');
123 $this->enableDeprecatedPropertyMapperInArgument($argument);
124 $argument->setValue(123);
125 $this->assertSame((string) $argument, '123', 'The returned argument is not a string.');
126 $this->assertNotSame((string) $argument, 123, 'The returned argument is identical to the set value.');
127 }
128
129 /**
130 * @test
131 */
132 public function setNewValidatorConjunctionCreatesANewValidatorConjunctionObject() {
133 $argument = new \TYPO3\CMS\Extbase\Mvc\Controller\Argument('dummy', 'Text');
134 $mockConjunctionValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ConjunctionValidator');
135 $mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
136 $mockObjectManager->expects($this->once())->method('get')->with('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ConjunctionValidator')->will($this->returnValue($mockConjunctionValidator));
137 $argument->injectObjectManager($mockObjectManager);
138 $argument->setNewValidatorConjunction(array());
139 $this->assertSame($mockConjunctionValidator, $argument->getValidator());
140 }
141
142 /**
143 * @test
144 */
145 public function setNewValidatorConjunctionAddsThePassedValidatorsToTheCreatedValidatorChain() {
146 eval('class Validator1 implements TYPO3\\CMS\\Extbase\\Validation\\Validator\\ValidatorInterface {
147 public function isValid($value) {}
148 public function setOptions(array $validationOptions) {}
149 public function getErrors() {}
150 }');
151 eval('class Validator2 implements TYPO3\\CMS\\Extbase\\Validation\\Validator\\ValidatorInterface {
152 public function isValid($value) {}
153 public function setOptions(array $validationOptions) {}
154 public function getErrors() {}
155 }');
156 $validator1 = new \Validator1();
157 $validator2 = new \Validator2();
158 $mockValidatorConjunction = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ConjunctionValidator');
159 $mockValidatorConjunction->expects($this->at(0))->method('addValidator')->with($validator1);
160 $mockValidatorConjunction->expects($this->at(1))->method('addValidator')->with($validator2);
161 $argument = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('dummy'), array(), '', FALSE);
162 $mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
163 $mockObjectManager->expects($this->never())->method('create');
164 $mockObjectManager->expects($this->at(0))->method('get')->with('Validator1')->will($this->returnValue($validator1));
165 $mockObjectManager->expects($this->at(1))->method('get')->with('Validator2')->will($this->returnValue($validator2));
166 $argument->injectObjectManager($mockObjectManager);
167 $argument->_set('validator', $mockValidatorConjunction);
168 $argument->setNewValidatorConjunction(array('Validator1', 'Validator2'));
169 }
170
171 /**
172 * @test
173 */
174 public function settingDefaultValueReallySetsDefaultValue() {
175 $argument = new \TYPO3\CMS\Extbase\Mvc\Controller\Argument('dummy', 'Text');
176 $argument->setDefaultValue(42);
177 $this->assertEquals(42, $argument->getValue(), 'The default value was not stored in the Argument.');
178 }
179
180 /**
181 * Helper which enables the deprecated property mapper in the Argument class.
182 *
183 * @param \TYPO3\CMS\Extbase\Mvc\Controller\Argument $argument
184 */
185 protected function enableDeprecatedPropertyMapperInArgument(\TYPO3\CMS\Extbase\Mvc\Controller\Argument $argument) {
186 $mockConfigurationManager = $this->getMock('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManagerInterface');
187 $mockConfigurationManager->expects($this->any())->method('isFeatureEnabled')->with('rewrittenPropertyMapper')->will($this->returnValue(FALSE));
188 $argument->injectConfigurationManager($mockConfigurationManager);
189 }
190 }
191
192 ?>