[+BUGFIX] MVC: Make forwarding of objects work
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / MVC / Controller / ArgumentTest.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
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 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 class Tx_Extbase_Tests_Unit_MVC_Controller_ArgumentTest extends Tx_Extbase_Tests_Unit_BaseTestCase {
29
30 /**
31 * @test
32 * @expectedException InvalidArgumentException
33 */
34 public function constructingArgumentWithoutNameThrowsException() {
35 new Tx_Extbase_MVC_Controller_Argument(NULL, 'Text');
36 }
37
38 /**
39 * @test
40 * @expectedException InvalidArgumentException
41 */
42 public function constructingArgumentWithInvalidNameThrowsException() {
43 new Tx_Extbase_MVC_Controller_Argument(new ArrayObject(), 'Text');
44 }
45
46 /**
47 * @test
48 */
49 public function passingDataTypeToConstructorReallySetsTheDataType() {
50 $argument = new Tx_Extbase_MVC_Controller_Argument('dummy', 'Number');
51 $this->assertEquals('Number', $argument->getDataType(), 'The specified data type has not been set correctly.');
52 }
53
54 /**
55 * @test
56 */
57 public function setShortNameProvidesFluentInterface() {
58 $argument = new Tx_Extbase_MVC_Controller_Argument('dummy', 'Text');
59 $returnedArgument = $argument->setShortName('x');
60 $this->assertSame($argument, $returnedArgument, 'The returned argument is not the original argument.');
61 }
62
63 /**
64 * @test
65 */
66 public function setValueProvidesFluentInterface() {
67 $argument = new Tx_Extbase_MVC_Controller_Argument('dummy', 'Text');
68 $returnedArgument = $argument->setValue('x');
69 $this->assertSame($argument, $returnedArgument, 'The returned argument is not the original argument.');
70 }
71
72 /**
73 * @test
74 * @author Karsten Dambekalns <karsten@typo3.org>
75 */
76 public function setValueUsesNullAsIs() {
77 $argument = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array('transformValue'), array('dummy', 'ArrayObject'));
78 $argument->expects($this->never())->method('transformValue');
79 $argument->setValue(NULL);
80 }
81
82 /**
83 * @test
84 * @author Karsten Dambekalns <karsten@typo3.org>
85 */
86 public function setValueUsesMatchingInstanceAsIs() {
87 $argument = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array('transformValue'), array('dummy', 'ArrayObject'));
88 $argument->expects($this->never())->method('transformValue');
89 $argument->setValue(new ArrayObject());
90 }
91
92 /**
93 * @test
94 */
95 public function setValueTriesToConvertAnUidIntoTheRealObjectIfTheDataTypeClassSchemaIsSet() {
96 $object = new StdClass();
97
98 $argument = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_Argument'), array('findObjectByUid'), array(), '', FALSE);
99 $argument->expects($this->once())->method('findObjectByUid')->with('42')->will($this->returnValue($object));
100 $argument->_set('dataTypeClassSchema', 'stdClass');
101 $argument->_set('dataType', 'stdClass');
102 // $argument->_set('queryFactory', $mockQueryFactory);
103 $argument->setValue('42');
104
105 $this->assertSame($object, $argument->_get('value'));
106 $this->assertSame(Tx_Extbase_MVC_Controller_Argument::ORIGIN_PERSISTENCE, $argument->getOrigin());
107 }
108
109
110 /**
111 * @test
112 */
113 public function toStringReturnsTheStringVersionOfTheArgumentsValue() {
114 $argument = new Tx_Extbase_MVC_Controller_Argument('dummy', 'Text');
115 $argument->setValue(123);
116
117 $this->assertSame((string)$argument, '123', 'The returned argument is not a string.');
118 $this->assertNotSame((string)$argument, 123, 'The returned argument is identical to the set value.');
119 }
120
121 /**
122 * @test
123 */
124 public function dataTypeValidatorCanBeAFullClassName() {
125 $this->markTestIncomplete();
126
127 $this->mockObjectManager->expects($this->once())->method('isObjectRegistered')->with('Tx_Extbase_Validation_Validator_TextValidator')->will($this->returnValue(TRUE));
128 $this->mockObjectManager->expects($this->any())->method('get')->with('Tx_Extbase_Validation_Validator_TextValidator')->will($this->returnValue($this->getMock('Tx_Extbase_Validation_Validator_TextValidator')));
129
130 $argument = new Tx_Extbase_MVC_Controller_Argument('SomeArgument', 'Tx_Extbase_Validation_Validator_TextValidator');
131 $argument->injectObjectManager($this->mockObjectManager);
132
133 $this->assertType('Tx_Extbase_Validation_Validator_TextValidator', $argument->getDatatypeValidator(), 'The returned datatype validator is not a text validator as expected.');
134 }
135
136 /**
137 * @test
138 */
139 public function dataTypeValidatorCanBeAShortName() {
140 $this->markTestIncomplete();
141
142 $this->mockObjectManager->expects($this->once())->method('isObjectRegistered')->with('Tx_Extbase_Validation_Validator_TextValidator')->will($this->returnValue(TRUE));
143 $this->mockObjectManager->expects($this->any())->method('get')->with('Tx_Extbase_Validation_Validator_TextValidator')->will($this->returnValue($this->getMock('Tx_Extbase_Validation_Validator_TextValidator')));
144
145 $argument = new Tx_Extbase_MVC_Controller_Argument('SomeArgument', 'Text');
146 $argument->injectObjectManager($this->mockObjectManager);
147
148 $this->assertType('Tx_Extbase_Validation_Validator_TextValidator', $argument->getDatatypeValidator(), 'The returned datatype validator is not a text validator as expected.');
149 }
150
151 /**
152 * @test
153 */
154 public function setNewValidatorConjunctionCreatesANewValidatorConjunctionObject() {
155 $argument = new Tx_Extbase_MVC_Controller_Argument('dummy', 'Text');
156 $mockConjunctionValidator = $this->getMock('Tx_Extbase_Validation_Validator_ConjunctionValidator');
157 $mockObjectManager = $this->getMock('Tx_Extbase_Object_ObjectManagerInterface');
158 $mockObjectManager->expects($this->once())->method('create')->with('Tx_Extbase_Validation_Validator_ConjunctionValidator')->will($this->returnValue($mockConjunctionValidator));
159 $argument->injectObjectManager($mockObjectManager);
160 $argument->setNewValidatorConjunction(array());
161 $this->assertSame($mockConjunctionValidator, $argument->getValidator());
162 }
163
164 /**
165 * @test
166 */
167 public function setNewValidatorConjunctionAddsThePassedValidatorsToTheCreatedValidatorChain() {
168 eval('class Validator1 implements Tx_Extbase_Validation_Validator_ValidatorInterface {
169 public function isValid($value) {}
170 public function setOptions(array $validationOptions) {}
171 public function getErrors() {}
172 }');
173 eval('class Validator2 implements Tx_Extbase_Validation_Validator_ValidatorInterface {
174 public function isValid($value) {}
175 public function setOptions(array $validationOptions) {}
176 public function getErrors() {}
177 }');
178
179 $validator1 = new Validator1;
180 $validator2 = new Validator2;
181
182 $mockValidatorConjunction = $this->getMock('Tx_Extbase_Validation_Validator_ConjunctionValidator');
183 $mockValidatorConjunction->expects($this->at(0))->method('addValidator')->with($validator1);
184 $mockValidatorConjunction->expects($this->at(1))->method('addValidator')->with($validator2);
185
186 $argument = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_Argument'), array('dummy'), array(), '', FALSE);
187 $mockObjectManager = $this->getMock('Tx_Extbase_Object_ObjectManagerInterface');
188 $mockObjectManager->expects($this->never())->method('create');
189 $mockObjectManager->expects($this->at(0))->method('get')->with('Validator1')->will($this->returnValue($validator1));
190 $mockObjectManager->expects($this->at(1))->method('get')->with('Validator2')->will($this->returnValue($validator2));
191 $argument->injectObjectManager($mockObjectManager);
192 $argument->_set('validator', $mockValidatorConjunction);
193 $argument->setNewValidatorConjunction(array('Validator1', 'Validator2'));
194 }
195
196 /**
197 * @test
198 */
199 public function settingDefaultValueReallySetsDefaultValue() {
200 $argument = new Tx_Extbase_MVC_Controller_Argument('dummy', 'Text');
201 $argument->setDefaultValue(42);
202
203 $this->assertEquals(42, $argument->getValue(), 'The default value was not stored in the Argument.');
204 }
205
206 }
207 ?>