[!!!] Extbase: Reintegrating branch "dispatcher" to trunk. Resolves: #10605
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / MVC / Controller / Argument_testcase.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_MVC_Controller_Argument_testcase extends Tx_Extbase_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 */
75 public function setValueTriesToConvertAnUidIntoTheRealObjectIfTheDataTypeClassSchemaIsSet() {
76 $object = new StdClass();
77
78 $argument = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_Argument'), array('findObjectByUid'), array(), '', FALSE);
79 $argument->expects($this->once())->method('findObjectByUid')->with('42')->will($this->returnValue($object));
80 $argument->_set('dataTypeClassSchema', 'stdClass');
81 $argument->_set('dataType', 'stdClass');
82 // $argument->_set('queryFactory', $mockQueryFactory);
83 $argument->setValue('42');
84
85 $this->assertSame($object, $argument->_get('value'));
86 $this->assertSame(Tx_Extbase_MVC_Controller_Argument::ORIGIN_PERSISTENCE, $argument->getOrigin());
87 }
88
89
90 /**
91 * @test
92 */
93 public function toStringReturnsTheStringVersionOfTheArgumentsValue() {
94 $argument = new Tx_Extbase_MVC_Controller_Argument('dummy', 'Text');
95 $argument->setValue(123);
96
97 $this->assertSame((string)$argument, '123', 'The returned argument is not a string.');
98 $this->assertNotSame((string)$argument, 123, 'The returned argument is identical to the set value.');
99 }
100
101 /**
102 * @test
103 */
104 public function dataTypeValidatorCanBeAFullClassName() {
105 $this->markTestIncomplete();
106
107 $this->mockObjectManager->expects($this->once())->method('isObjectRegistered')->with('Tx_Extbase_Validation_Validator_TextValidator')->will($this->returnValue(TRUE));
108 $this->mockObjectManager->expects($this->any())->method('get')->with('Tx_Extbase_Validation_Validator_TextValidator')->will($this->returnValue($this->getMock('Tx_Extbase_Validation_Validator_TextValidator')));
109
110 $argument = new Tx_Extbase_MVC_Controller_Argument('SomeArgument', 'Tx_Extbase_Validation_Validator_TextValidator');
111 $argument->injectObjectManager($this->mockObjectManager);
112
113 $this->assertType('Tx_Extbase_Validation_Validator_TextValidator', $argument->getDatatypeValidator(), 'The returned datatype validator is not a text validator as expected.');
114 }
115
116 /**
117 * @test
118 */
119 public function dataTypeValidatorCanBeAShortName() {
120 $this->markTestIncomplete();
121
122 $this->mockObjectManager->expects($this->once())->method('isObjectRegistered')->with('Tx_Extbase_Validation_Validator_TextValidator')->will($this->returnValue(TRUE));
123 $this->mockObjectManager->expects($this->any())->method('get')->with('Tx_Extbase_Validation_Validator_TextValidator')->will($this->returnValue($this->getMock('Tx_Extbase_Validation_Validator_TextValidator')));
124
125 $argument = new Tx_Extbase_MVC_Controller_Argument('SomeArgument', 'Text');
126 $argument->injectObjectManager($this->mockObjectManager);
127
128 $this->assertType('Tx_Extbase_Validation_Validator_TextValidator', $argument->getDatatypeValidator(), 'The returned datatype validator is not a text validator as expected.');
129 }
130
131 /**
132 * @test
133 */
134 public function setNewValidatorConjunctionCreatesANewValidatorConjunctionObject() {
135 $argument = new Tx_Extbase_MVC_Controller_Argument('dummy', 'Text');
136 $argument->setNewValidatorConjunction(array());
137
138 $this->assertType('Tx_Extbase_Validation_Validator_ConjunctionValidator', $argument->getValidator(), 'The returned validator is not a conjunction as expected.');
139 }
140
141 /**
142 * @test
143 */
144 public function setNewValidatorConjunctionAddsThePassedValidatorsToTheCreatedValidatorChain() {
145 eval('class Validator1 implements Tx_Extbase_Validation_Validator_ValidatorInterface {
146 public function isValid($value) {}
147 public function setOptions(array $validationOptions) {}
148 public function getErrors() {}
149 }');
150 eval('class Validator2 implements Tx_Extbase_Validation_Validator_ValidatorInterface {
151 public function isValid($value) {}
152 public function setOptions(array $validationOptions) {}
153 public function getErrors() {}
154 }');
155
156 $validator1 = new Validator1;
157 $validator2 = new Validator2;
158
159 $mockValidatorConjunction = $this->getMock('Tx_Extbase_Validation_Validator_ConjunctionValidator');
160 $mockValidatorConjunction->expects($this->at(0))->method('addValidator')->with($validator1);
161 $mockValidatorConjunction->expects($this->at(1))->method('addValidator')->with($validator2);
162
163 $argument = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_Argument'), array('dummy'), array(), '', FALSE);
164 $argument->_set('validator', $mockValidatorConjunction);
165 $argument->setNewValidatorConjunction(array('Validator1', 'Validator2'));
166 }
167
168 /**
169 * @test
170 */
171 public function settingDefaultValueReallySetsDefaultValue() {
172 $argument = new Tx_Extbase_MVC_Controller_Argument('dummy', 'Text');
173 $argument->setDefaultValue(42);
174
175 $this->assertEquals(42, $argument->getValue(), 'The default value was not stored in the Argument.');
176 }
177
178 }
179 ?>