TYPO3 CMS  TYPO3_7-6
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 
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->getMock(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class, ['getName'], [], '', false);
50  $mockFirstArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234'));
51  $arguments->addArgument($mockFirstArgument);
52  $mockSecondArgument = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class, ['getName'], [], '', false);
53  $mockSecondArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234'));
54  $arguments->addArgument($mockSecondArgument);
55  $this->assertSame($mockSecondArgument, $arguments->getArgument('argumentName1234'), 'The added and retrieved argument is not the same.');
56  }
57 
62  {
63  $mockArgument = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class, [], [], '', false);
64  $mockObjectManager = $this->getMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
65  $mockObjectManager->expects($this->once())->method('get')->with(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class)->will($this->returnValue($mockArgument));
66  $arguments = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Controller\Arguments::class, ['dummy']);
67  $arguments->_set('objectManager', $mockObjectManager);
68  $newArgument = $arguments->addNewArgument('someArgument');
69  $this->assertSame($newArgument, $mockArgument);
70  }
71 
76  {
77  $mockArgument = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class, ['getName'], [], '', false);
78  $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234'));
79  $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
80  $arguments[] = $mockArgument;
81  $this->assertTrue($arguments->hasArgument('argumentName1234'), 'Added argument does not exist.');
82  $this->assertSame($mockArgument, $arguments->getArgument('argumentName1234'), 'Added and retrieved arguments are not the same.');
83  }
84 
89  {
90  $mockArgument = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class, ['getName'], [], '', false);
91  $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234'));
92  $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
93  $arguments[] = $mockArgument;
94  $this->assertSame($mockArgument, $arguments['argumentName1234'], 'Argument retrieved by array access is not the one we added.');
95  }
96 
101  {
102  $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
103  try {
104  $arguments->getArgument('someArgument');
105  $this->fail('getArgument() did not throw an exception although the specified argument does not exist.');
106  } catch (\TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException $exception) {
107  }
108  }
109 
113  public function issetReturnsCorrectResult()
114  {
115  $mockArgument = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class, ['getName'], [], '', false);
116  $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234'));
117  $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
118  $this->assertFalse(isset($arguments['argumentName1234']), 'isset() did not return FALSE.');
119  $arguments[] = $mockArgument;
120  $this->assertTrue(isset($arguments['argumentName1234']), 'isset() did not return TRUE.');
121  }
122 
127  {
128  $mockArgument1 = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class, ['getName'], [], '', false);
129  $mockArgument1->expects($this->any())->method('getName')->will($this->returnValue('argumentName1'));
130  $mockArgument2 = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class, ['getName'], [], '', false);
131  $mockArgument2->expects($this->any())->method('getName')->will($this->returnValue('argumentName2'));
132  $mockArgument3 = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class, ['getName'], [], '', false);
133  $mockArgument3->expects($this->any())->method('getName')->will($this->returnValue('argumentName3'));
134  $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
135  $arguments[] = $mockArgument1;
136  $arguments[] = $mockArgument2;
137  $arguments[] = $mockArgument3;
138  $expectedArgumentNames = ['argumentName1', 'argumentName2', 'argumentName3'];
139  $this->assertEquals($expectedArgumentNames, $arguments->getArgumentNames(), 'Returned argument names were not as expected.');
140  }
141 
146  {
147  $mockArgument1 = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class, ['getName', 'getShortName'], [], '', false);
148  $mockArgument1->expects($this->any())->method('getName')->will($this->returnValue('argumentName1'));
149  $mockArgument1->expects($this->any())->method('getShortName')->will($this->returnValue('a'));
150  $mockArgument2 = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class, ['getName', 'getShortName'], [], '', false);
151  $mockArgument2->expects($this->any())->method('getName')->will($this->returnValue('argumentName2'));
152  $mockArgument2->expects($this->any())->method('getShortName')->will($this->returnValue('b'));
153  $mockArgument3 = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class, ['getName', 'getShortName'], [], '', false);
154  $mockArgument3->expects($this->any())->method('getName')->will($this->returnValue('argumentName3'));
155  $mockArgument3->expects($this->any())->method('getShortName')->will($this->returnValue('c'));
156  $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
157  $arguments[] = $mockArgument1;
158  $arguments[] = $mockArgument2;
159  $arguments[] = $mockArgument3;
160  $expectedShortNames = ['a', 'b', 'c'];
161  $this->assertEquals($expectedShortNames, $arguments->getArgumentShortNames(), 'Returned argument short names were not as expected.');
162  }
163 
168  {
169  $mockArgument = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class, ['getName'], [], '', false);
170  $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('dummyName'));
171  $mockObjectManager = $this->getMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
172  $mockObjectManager->expects($this->once())->method('get')->with(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class)->will($this->returnValue($mockArgument));
173  $arguments = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Controller\Arguments::class, ['dummy']);
174  $arguments->_set('objectManager', $mockObjectManager);
175  $addedArgument = $arguments->addNewArgument('dummyName');
176  $this->assertInstanceOf(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class, $addedArgument, 'addNewArgument() either did not add a new argument or did not return it.');
177  $retrievedArgument = $arguments['dummyName'];
178  $this->assertSame($addedArgument, $retrievedArgument, 'The added and the retrieved argument are not the same.');
179  }
180 
185  {
186  $mockArgument = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class, ['getName'], [], '', false);
187  $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('dummyName'));
188  $mockObjectManager = $this->getMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
189  $mockObjectManager->expects($this->once())->method('get')->with(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class, 'dummyName', 'Text')->will($this->returnValue($mockArgument));
190  $arguments = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Controller\Arguments::class, ['dummy']);
191  $arguments->_set('objectManager', $mockObjectManager);
192  $arguments->addNewArgument('dummyName');
193  }
194 
199  {
200  $mockArgument = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class, ['getName', 'setRequired'], [], '', false);
201  $mockArgument->expects($this->once())->method('getName')->will($this->returnValue('dummyName'));
202  $mockArgument->expects($this->once())->method('setRequired')->with(true);
203  $mockObjectManager = $this->getMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
204  $mockObjectManager->expects($this->once())->method('get')->with(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class, 'dummyName', 'Text')->will($this->returnValue($mockArgument));
205  $arguments = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Controller\Arguments::class, ['dummy']);
206  $arguments->_set('objectManager', $mockObjectManager);
207  $arguments->addNewArgument('dummyName', 'Text', true);
208  }
209 
214  {
215  $mockArgument = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class, ['getName', 'setRequired', 'setDefaultValue'], [], '', false);
216  $mockArgument->expects($this->once())->method('getName')->will($this->returnValue('dummyName'));
217  $mockArgument->expects($this->once())->method('setRequired')->with(false);
218  $mockArgument->expects($this->once())->method('setDefaultValue')->with('someDefaultValue');
219  $mockObjectManager = $this->getMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
220  $mockObjectManager->expects($this->once())->method('get')->with(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class, 'dummyName', 'Text')->will($this->returnValue($mockArgument));
221  $arguments = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Controller\Arguments::class, ['dummy']);
222  $arguments->_set('objectManager', $mockObjectManager);
223  $arguments->addNewArgument('dummyName', 'Text', false, 'someDefaultValue');
224  }
225 
231  {
232  $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
233  $arguments->nonExistingMethod();
234  }
235 
239  public function removeAllClearsAllArguments()
240  {
241  $mockArgument1 = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class, ['getName', 'getShortName'], [], '', false);
242  $mockArgument1->expects($this->any())->method('getName')->will($this->returnValue('argumentName1'));
243  $mockArgument2 = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class, ['getName', 'getShortName'], [], '', false);
244  $mockArgument2->expects($this->any())->method('getName')->will($this->returnValue('argumentName2'));
245  $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
246  $arguments[] = $mockArgument1;
247  $arguments[] = $mockArgument2;
248  $this->assertTrue($arguments->hasArgument('argumentName2'));
249  $arguments->removeAll();
250  $this->assertFalse($arguments->hasArgument('argumentName2'));
251  }
252 }
getAccessibleMock( $originalClassName, $methods=[], array $arguments=[], $mockClassName='', $callOriginalConstructor=true, $callOriginalClone=true, $callAutoload=true)