TYPO3 CMS  TYPO3_8-7
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 
20 class ArgumentsTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
21 {
26  {
27  $arguments1 = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
28  $arguments2 = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
29  $this->assertNotSame($arguments1, $arguments2, 'The arguments object is not of scope prototype!');
30  }
31 
36  {
37  $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
38  $newArgument = new \TYPO3\CMS\Extbase\Mvc\Controller\Argument('argumentName1234', 'dummyValue');
39  $arguments->addArgument($newArgument);
40  $this->assertSame($newArgument, $arguments->getArgument('argumentName1234'), 'The added and retrieved argument is not the same.');
41  }
42 
47  {
48  $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
49  $mockFirstArgument = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class)
50  ->setMethods(['getName'])
51  ->disableOriginalConstructor()
52  ->getMock();
53  $mockFirstArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234'));
54  $arguments->addArgument($mockFirstArgument);
55  $mockSecondArgument = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class)
56  ->setMethods(['getName'])
57  ->disableOriginalConstructor()
58  ->getMock();
59  $mockSecondArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234'));
60  $arguments->addArgument($mockSecondArgument);
61  $this->assertSame($mockSecondArgument, $arguments->getArgument('argumentName1234'), 'The added and retrieved argument is not the same.');
62  }
63 
68  {
69  $mockArgument = $this->createMock(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class);
70  $mockObjectManager = $this->createMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
71  $mockObjectManager->expects($this->once())->method('get')->with(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class)->will($this->returnValue($mockArgument));
72  $arguments = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Controller\Arguments::class, ['dummy']);
73  $arguments->_set('objectManager', $mockObjectManager);
74  $newArgument = $arguments->addNewArgument('someArgument');
75  $this->assertSame($newArgument, $mockArgument);
76  }
77 
82  {
83  $mockArgument = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class)
84  ->setMethods(['getName'])
85  ->disableOriginalConstructor()
86  ->getMock();
87  $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234'));
88  $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
89  $arguments[] = $mockArgument;
90  $this->assertTrue($arguments->hasArgument('argumentName1234'), 'Added argument does not exist.');
91  $this->assertSame($mockArgument, $arguments->getArgument('argumentName1234'), 'Added and retrieved arguments are not the same.');
92  }
93 
98  {
99  $mockArgument = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class)
100  ->setMethods(['getName'])
101  ->disableOriginalConstructor()
102  ->getMock();
103  $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234'));
104  $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
105  $arguments[] = $mockArgument;
106  $this->assertSame($mockArgument, $arguments['argumentName1234'], 'Argument retrieved by array access is not the one we added.');
107  }
108 
113  {
114  $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
115  try {
116  $arguments->getArgument('someArgument');
117  $this->fail('getArgument() did not throw an exception although the specified argument does not exist.');
118  } catch (\TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException $exception) {
119  }
120  }
121 
125  public function issetReturnsCorrectResult()
126  {
127  $mockArgument = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class)
128  ->setMethods(['getName'])
129  ->disableOriginalConstructor()
130  ->getMock();
131  $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234'));
132  $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
133  $this->assertFalse(isset($arguments['argumentName1234']), 'isset() did not return FALSE.');
134  $arguments[] = $mockArgument;
135  $this->assertTrue(isset($arguments['argumentName1234']), 'isset() did not return TRUE.');
136  }
137 
142  {
143  $mockArgument1 = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class)
144  ->setMethods(['getName'])
145  ->disableOriginalConstructor()
146  ->getMock();
147  $mockArgument1->expects($this->any())->method('getName')->will($this->returnValue('argumentName1'));
148  $mockArgument2 = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class)
149  ->setMethods(['getName'])
150  ->disableOriginalConstructor()
151  ->getMock();
152  $mockArgument2->expects($this->any())->method('getName')->will($this->returnValue('argumentName2'));
153  $mockArgument3 = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class)
154  ->setMethods(['getName'])
155  ->disableOriginalConstructor()
156  ->getMock();
157  $mockArgument3->expects($this->any())->method('getName')->will($this->returnValue('argumentName3'));
158  $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
159  $arguments[] = $mockArgument1;
160  $arguments[] = $mockArgument2;
161  $arguments[] = $mockArgument3;
162  $expectedArgumentNames = ['argumentName1', 'argumentName2', 'argumentName3'];
163  $this->assertEquals($expectedArgumentNames, $arguments->getArgumentNames(), 'Returned argument names were not as expected.');
164  }
165 
170  {
171  $mockArgument1 = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class)
172  ->setMethods(['getName', 'getShortName'])
173  ->disableOriginalConstructor()
174  ->getMock();
175  $mockArgument1->expects($this->any())->method('getName')->will($this->returnValue('argumentName1'));
176  $mockArgument1->expects($this->any())->method('getShortName')->will($this->returnValue('a'));
177  $mockArgument2 = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class)
178  ->setMethods(['getName', 'getShortName'])
179  ->disableOriginalConstructor()
180  ->getMock();
181  $mockArgument2->expects($this->any())->method('getName')->will($this->returnValue('argumentName2'));
182  $mockArgument2->expects($this->any())->method('getShortName')->will($this->returnValue('b'));
183  $mockArgument3 = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class)
184  ->setMethods(['getName', 'getShortName'])
185  ->disableOriginalConstructor()
186  ->getMock();
187  $mockArgument3->expects($this->any())->method('getName')->will($this->returnValue('argumentName3'));
188  $mockArgument3->expects($this->any())->method('getShortName')->will($this->returnValue('c'));
189  $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
190  $arguments[] = $mockArgument1;
191  $arguments[] = $mockArgument2;
192  $arguments[] = $mockArgument3;
193  $expectedShortNames = ['a', 'b', 'c'];
194  $this->assertEquals($expectedShortNames, $arguments->getArgumentShortNames(), 'Returned argument short names were not as expected.');
195  }
196 
201  {
202  $mockArgument = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class)
203  ->setMethods(['getName'])
204  ->disableOriginalConstructor()
205  ->getMock();
206  $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('dummyName'));
207  $mockObjectManager = $this->createMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
208  $mockObjectManager->expects($this->once())->method('get')->with(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class)->will($this->returnValue($mockArgument));
209  $arguments = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Controller\Arguments::class, ['dummy']);
210  $arguments->_set('objectManager', $mockObjectManager);
211  $addedArgument = $arguments->addNewArgument('dummyName');
212  $this->assertInstanceOf(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class, $addedArgument, 'addNewArgument() either did not add a new argument or did not return it.');
213  $retrievedArgument = $arguments['dummyName'];
214  $this->assertSame($addedArgument, $retrievedArgument, 'The added and the retrieved argument are not the same.');
215  }
216 
221  {
222  $mockArgument = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class)
223  ->setMethods(['getName'])
224  ->disableOriginalConstructor()
225  ->getMock();
226  $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('dummyName'));
227  $mockObjectManager = $this->createMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
228  $mockObjectManager->expects($this->once())->method('get')->with(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class, 'dummyName', 'Text')->will($this->returnValue($mockArgument));
229  $arguments = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Controller\Arguments::class, ['dummy']);
230  $arguments->_set('objectManager', $mockObjectManager);
231  $arguments->addNewArgument('dummyName');
232  }
233 
238  {
239  $mockArgument = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class)
240  ->setMethods(['getName', 'setRequired'])
241  ->disableOriginalConstructor()
242  ->getMock();
243  $mockArgument->expects($this->once())->method('getName')->will($this->returnValue('dummyName'));
244  $mockArgument->expects($this->once())->method('setRequired')->with(true);
245  $mockObjectManager = $this->createMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
246  $mockObjectManager->expects($this->once())->method('get')->with(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class, 'dummyName', 'Text')->will($this->returnValue($mockArgument));
247  $arguments = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Controller\Arguments::class, ['dummy']);
248  $arguments->_set('objectManager', $mockObjectManager);
249  $arguments->addNewArgument('dummyName', 'Text', true);
250  }
251 
256  {
257  $mockArgument = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class)
258  ->setMethods(['getName', 'setRequired', 'setDefaultValue'])
259  ->disableOriginalConstructor()
260  ->getMock();
261  $mockArgument->expects($this->once())->method('getName')->will($this->returnValue('dummyName'));
262  $mockArgument->expects($this->once())->method('setRequired')->with(false);
263  $mockArgument->expects($this->once())->method('setDefaultValue')->with('someDefaultValue');
264  $mockObjectManager = $this->createMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
265  $mockObjectManager->expects($this->once())->method('get')->with(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class, 'dummyName', 'Text')->will($this->returnValue($mockArgument));
266  $arguments = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Controller\Arguments::class, ['dummy']);
267  $arguments->_set('objectManager', $mockObjectManager);
268  $arguments->addNewArgument('dummyName', 'Text', false, 'someDefaultValue');
269  }
270 
275  {
276  $this->expectException(\LogicException::class);
277  $this->expectExceptionCode(1210858451);
278  $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
279  $arguments->nonExistingMethod();
280  }
281 
285  public function removeAllClearsAllArguments()
286  {
287  $mockArgument1 = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class)
288  ->setMethods(['getName', 'getShortName'])
289  ->disableOriginalConstructor()
290  ->getMock();
291  $mockArgument1->expects($this->any())->method('getName')->will($this->returnValue('argumentName1'));
292  $mockArgument2 = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class)
293  ->setMethods(['getName', 'getShortName'])
294  ->disableOriginalConstructor()
295  ->getMock();
296  $mockArgument2->expects($this->any())->method('getName')->will($this->returnValue('argumentName2'));
297  $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
298  $arguments[] = $mockArgument1;
299  $arguments[] = $mockArgument2;
300  $this->assertTrue($arguments->hasArgument('argumentName2'));
301  $arguments->removeAll();
302  $this->assertFalse($arguments->hasArgument('argumentName2'));
303  }
304 }