‪TYPO3CMS  9.5
ArgumentsTest.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
17 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
18 
22 class ‪ArgumentsTest extends UnitTestCase
23 {
28  {
29  $arguments1 = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
30  $arguments2 = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
31  $this->assertNotSame($arguments1, $arguments2, 'The arguments object is not of scope prototype!');
32  }
33 
38  {
39  $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
40  $newArgument = new \TYPO3\CMS\Extbase\Mvc\Controller\Argument('argumentName1234', 'dummyValue');
41  $arguments->addArgument($newArgument);
42  $this->assertSame($newArgument, $arguments->getArgument('argumentName1234'), 'The added and retrieved argument is not the same.');
43  }
44 
49  {
50  $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
51  $mockFirstArgument = $this->getMockBuilder(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\Argument::class)
52  ->setMethods(['getName'])
53  ->disableOriginalConstructor()
54  ->getMock();
55  $mockFirstArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234'));
56  $arguments->addArgument($mockFirstArgument);
57  $mockSecondArgument = $this->getMockBuilder(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\Argument::class)
58  ->setMethods(['getName'])
59  ->disableOriginalConstructor()
60  ->getMock();
61  $mockSecondArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234'));
62  $arguments->addArgument($mockSecondArgument);
63  $this->assertSame($mockSecondArgument, $arguments->getArgument('argumentName1234'), 'The added and retrieved argument is not the same.');
64  }
65 
70  {
71  $mockArgument = $this->createMock(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\Argument::class);
72  $mockObjectManager = $this->createMock(\‪TYPO3\CMS\‪Extbase\Object\ObjectManagerInterface::class);
73  $mockObjectManager->expects($this->once())->method('get')->with(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\Argument::class)->will($this->returnValue($mockArgument));
74  $arguments = $this->getAccessibleMock(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\Arguments::class, ['dummy']);
75  $arguments->_set('objectManager', $mockObjectManager);
76  $newArgument = $arguments->addNewArgument('someArgument');
77  $this->assertSame($newArgument, $mockArgument);
78  }
79 
84  {
85  $mockArgument = $this->getMockBuilder(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\Argument::class)
86  ->setMethods(['getName'])
87  ->disableOriginalConstructor()
88  ->getMock();
89  $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234'));
90  $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
91  $arguments[] = $mockArgument;
92  $this->assertTrue($arguments->hasArgument('argumentName1234'), 'Added argument does not exist.');
93  $this->assertSame($mockArgument, $arguments->getArgument('argumentName1234'), 'Added and retrieved arguments are not the same.');
94  }
95 
100  {
101  $mockArgument = $this->getMockBuilder(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\Argument::class)
102  ->setMethods(['getName'])
103  ->disableOriginalConstructor()
104  ->getMock();
105  $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234'));
106  $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
107  $arguments[] = $mockArgument;
108  $this->assertSame($mockArgument, $arguments['argumentName1234'], 'Argument retrieved by array access is not the one we added.');
109  }
110 
115  {
116  $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
117  try {
118  $arguments->getArgument('someArgument');
119  $this->fail('getArgument() did not throw an exception although the specified argument does not exist.');
120  } catch (\‪TYPO3\CMS\‪Extbase\Mvc\‪Exception\NoSuchArgumentException $exception) {
121  }
122  }
123 
128  {
129  $mockArgument = $this->getMockBuilder(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\Argument::class)
130  ->setMethods(['getName'])
131  ->disableOriginalConstructor()
132  ->getMock();
133  $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234'));
134  $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
135  $this->assertFalse(isset($arguments['argumentName1234']), 'isset() did not return FALSE.');
136  $arguments[] = $mockArgument;
137  $this->assertTrue(isset($arguments['argumentName1234']), 'isset() did not return TRUE.');
138  }
139 
144  {
145  $mockArgument1 = $this->getMockBuilder(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\Argument::class)
146  ->setMethods(['getName'])
147  ->disableOriginalConstructor()
148  ->getMock();
149  $mockArgument1->expects($this->any())->method('getName')->will($this->returnValue('argumentName1'));
150  $mockArgument2 = $this->getMockBuilder(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\Argument::class)
151  ->setMethods(['getName'])
152  ->disableOriginalConstructor()
153  ->getMock();
154  $mockArgument2->expects($this->any())->method('getName')->will($this->returnValue('argumentName2'));
155  $mockArgument3 = $this->getMockBuilder(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\Argument::class)
156  ->setMethods(['getName'])
157  ->disableOriginalConstructor()
158  ->getMock();
159  $mockArgument3->expects($this->any())->method('getName')->will($this->returnValue('argumentName3'));
160  $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
161  $arguments[] = $mockArgument1;
162  $arguments[] = $mockArgument2;
163  $arguments[] = $mockArgument3;
164  $expectedArgumentNames = ['argumentName1', 'argumentName2', 'argumentName3'];
165  $this->assertEquals($expectedArgumentNames, $arguments->getArgumentNames(), 'Returned argument names were not as expected.');
166  }
167 
172  {
173  $mockArgument1 = $this->getMockBuilder(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\Argument::class)
174  ->setMethods(['getName', 'getShortName'])
175  ->disableOriginalConstructor()
176  ->getMock();
177  $mockArgument1->expects($this->any())->method('getName')->will($this->returnValue('argumentName1'));
178  $mockArgument1->expects($this->any())->method('getShortName')->will($this->returnValue('a'));
179  $mockArgument2 = $this->getMockBuilder(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\Argument::class)
180  ->setMethods(['getName', 'getShortName'])
181  ->disableOriginalConstructor()
182  ->getMock();
183  $mockArgument2->expects($this->any())->method('getName')->will($this->returnValue('argumentName2'));
184  $mockArgument2->expects($this->any())->method('getShortName')->will($this->returnValue('b'));
185  $mockArgument3 = $this->getMockBuilder(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\Argument::class)
186  ->setMethods(['getName', 'getShortName'])
187  ->disableOriginalConstructor()
188  ->getMock();
189  $mockArgument3->expects($this->any())->method('getName')->will($this->returnValue('argumentName3'));
190  $mockArgument3->expects($this->any())->method('getShortName')->will($this->returnValue('c'));
191  $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
192  $arguments[] = $mockArgument1;
193  $arguments[] = $mockArgument2;
194  $arguments[] = $mockArgument3;
195  $expectedShortNames = ['a', 'b', 'c'];
196  $this->assertEquals($expectedShortNames, $arguments->getArgumentShortNames(), 'Returned argument short names were not as expected.');
197  }
198 
203  {
204  $mockArgument = $this->getMockBuilder(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\Argument::class)
205  ->setMethods(['getName'])
206  ->disableOriginalConstructor()
207  ->getMock();
208  $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('dummyName'));
209  $mockObjectManager = $this->createMock(\‪TYPO3\CMS\‪Extbase\Object\ObjectManagerInterface::class);
210  $mockObjectManager->expects($this->once())->method('get')->with(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\Argument::class)->will($this->returnValue($mockArgument));
211  $arguments = $this->getAccessibleMock(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\Arguments::class, ['dummy']);
212  $arguments->_set('objectManager', $mockObjectManager);
213  $addedArgument = $arguments->addNewArgument('dummyName');
214  $this->assertInstanceOf(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\Argument::class, $addedArgument, 'addNewArgument() either did not add a new argument or did not return it.');
215  $retrievedArgument = $arguments['dummyName'];
216  $this->assertSame($addedArgument, $retrievedArgument, 'The added and the retrieved argument are not the same.');
217  }
218 
223  {
224  $mockArgument = $this->getMockBuilder(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\Argument::class)
225  ->setMethods(['getName'])
226  ->disableOriginalConstructor()
227  ->getMock();
228  $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('dummyName'));
229  $mockObjectManager = $this->createMock(\‪TYPO3\CMS\‪Extbase\Object\ObjectManagerInterface::class);
230  $mockObjectManager->expects($this->once())->method('get')->with(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\Argument::class, 'dummyName', 'Text')->will($this->returnValue($mockArgument));
231  $arguments = $this->getAccessibleMock(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\Arguments::class, ['dummy']);
232  $arguments->_set('objectManager', $mockObjectManager);
233  $arguments->addNewArgument('dummyName');
234  }
235 
240  {
241  $mockArgument = $this->getMockBuilder(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\Argument::class)
242  ->setMethods(['getName', 'setRequired'])
243  ->disableOriginalConstructor()
244  ->getMock();
245  $mockArgument->expects($this->once())->method('getName')->will($this->returnValue('dummyName'));
246  $mockArgument->expects($this->once())->method('setRequired')->with(true);
247  $mockObjectManager = $this->createMock(\‪TYPO3\CMS\‪Extbase\Object\ObjectManagerInterface::class);
248  $mockObjectManager->expects($this->once())->method('get')->with(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\Argument::class, 'dummyName', 'Text')->will($this->returnValue($mockArgument));
249  $arguments = $this->getAccessibleMock(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\Arguments::class, ['dummy']);
250  $arguments->_set('objectManager', $mockObjectManager);
251  $arguments->addNewArgument('dummyName', 'Text', true);
252  }
253 
258  {
259  $mockArgument = $this->getMockBuilder(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\Argument::class)
260  ->setMethods(['getName', 'setRequired', 'setDefaultValue'])
261  ->disableOriginalConstructor()
262  ->getMock();
263  $mockArgument->expects($this->once())->method('getName')->will($this->returnValue('dummyName'));
264  $mockArgument->expects($this->once())->method('setRequired')->with(false);
265  $mockArgument->expects($this->once())->method('setDefaultValue')->with('someDefaultValue');
266  $mockObjectManager = $this->createMock(\‪TYPO3\CMS\‪Extbase\Object\ObjectManagerInterface::class);
267  $mockObjectManager->expects($this->once())->method('get')->with(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\Argument::class, 'dummyName', 'Text')->will($this->returnValue($mockArgument));
268  $arguments = $this->getAccessibleMock(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\Arguments::class, ['dummy']);
269  $arguments->_set('objectManager', $mockObjectManager);
270  $arguments->addNewArgument('dummyName', 'Text', false, 'someDefaultValue');
271  }
272 
277  {
278  $this->expectException(\LogicException::class);
279  $this->expectExceptionCode(1210858451);
280  $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
281  $arguments->nonExistingMethod();
282  }
283 
288  {
289  $mockArgument1 = $this->getMockBuilder(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\Argument::class)
290  ->setMethods(['getName', 'getShortName'])
291  ->disableOriginalConstructor()
292  ->getMock();
293  $mockArgument1->expects($this->any())->method('getName')->will($this->returnValue('argumentName1'));
294  $mockArgument2 = $this->getMockBuilder(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\Argument::class)
295  ->setMethods(['getName', 'getShortName'])
296  ->disableOriginalConstructor()
297  ->getMock();
298  $mockArgument2->expects($this->any())->method('getName')->will($this->returnValue('argumentName2'));
299  $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
300  $arguments[] = $mockArgument1;
301  $arguments[] = $mockArgument2;
302  $this->assertTrue($arguments->hasArgument('argumentName2'));
303  $arguments->removeAll();
304  $this->assertFalse($arguments->hasArgument('argumentName2'));
305  }
306 }
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\addNewArgumentCreatesAndAddsNewArgument
‪addNewArgumentCreatesAndAddsNewArgument()
Definition: ArgumentsTest.php:202
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\addingAnArgumentReplacesArgumentWithSameName
‪addingAnArgumentReplacesArgumentWithSameName()
Definition: ArgumentsTest.php:48
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:4
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\callingInvalidMethodThrowsException
‪callingInvalidMethodThrowsException()
Definition: ArgumentsTest.php:276
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\getArgumentNamesReturnsNamesOfAddedArguments
‪getArgumentNamesReturnsNamesOfAddedArguments()
Definition: ArgumentsTest.php:143
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller
Definition: AbstractControllerTest.php:2
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\removeAllClearsAllArguments
‪removeAllClearsAllArguments()
Definition: ArgumentsTest.php:287
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\getArgumentShortNamesReturnsShortNamesOfAddedArguments
‪getArgumentShortNamesReturnsShortNamesOfAddedArguments()
Definition: ArgumentsTest.php:171
‪TYPO3
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\addingAnArgumentManuallyWorks
‪addingAnArgumentManuallyWorks()
Definition: ArgumentsTest.php:37
‪TYPO3\CMS\Extbase\Exception
Definition: Exception.php:23
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\addNewArgumentCanAddArgumentsMarkedAsRequired
‪addNewArgumentCanAddArgumentsMarkedAsRequired()
Definition: ArgumentsTest.php:239
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\argumentsObjectIsOfScopePrototype
‪argumentsObjectIsOfScopePrototype()
Definition: ArgumentsTest.php:27
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\issetReturnsCorrectResult
‪issetReturnsCorrectResult()
Definition: ArgumentsTest.php:127
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\addNewArgumentCanAddArgumentsMarkedAsOptionalWithDefaultValues
‪addNewArgumentCanAddArgumentsMarkedAsOptionalWithDefaultValues()
Definition: ArgumentsTest.php:257
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\addingArgumentThroughArrayAccessWorks
‪addingArgumentThroughArrayAccessWorks()
Definition: ArgumentsTest.php:83
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\getArgumentWithNonExistingArgumentNameThrowsException
‪getArgumentWithNonExistingArgumentNameThrowsException()
Definition: ArgumentsTest.php:114
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\addNewArgumentProvidesFluentInterface
‪addNewArgumentProvidesFluentInterface()
Definition: ArgumentsTest.php:69
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest
Definition: ArgumentsTest.php:23
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\retrievingArgumentThroughArrayAccessWorks
‪retrievingArgumentThroughArrayAccessWorks()
Definition: ArgumentsTest.php:99
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\addNewArgumentAssumesTextDataTypeByDefault
‪addNewArgumentAssumesTextDataTypeByDefault()
Definition: ArgumentsTest.php:222