‪TYPO3CMS  10.4
ArgumentsTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
22 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
23 
27 class ‪ArgumentsTest extends UnitTestCase
28 {
32  public function ‪argumentsObjectIsOfScopePrototype(): void
33  {
34  $arguments1 = new ‪Arguments();
35  $arguments2 = new ‪Arguments();
36  self::assertNotSame($arguments1, $arguments2, 'The arguments object is not of scope prototype!');
37  }
38 
42  public function ‪addingAnArgumentManuallyWorks(): void
43  {
44  $arguments = new ‪Arguments();
45  $newArgument = new ‪Argument('argumentName1234', 'dummyValue');
46  $arguments->addArgument($newArgument);
47  self::assertSame($newArgument, $arguments->getArgument('argumentName1234'), 'The added and retrieved argument is not the same.');
48  }
49 
54  {
55  $arguments = new ‪Arguments();
56  $mockFirstArgument = $this->getMockBuilder(Argument::class)
57  ->onlyMethods(['getName'])
58  ->disableOriginalConstructor()
59  ->getMock();
60  $mockFirstArgument->method('getName')->willReturn('argumentName1234');
61  $arguments->addArgument($mockFirstArgument);
62  $mockSecondArgument = $this->getMockBuilder(Argument::class)
63  ->onlyMethods(['getName'])
64  ->disableOriginalConstructor()
65  ->getMock();
66  $mockSecondArgument->method('getName')->willReturn('argumentName1234');
67  $arguments->addArgument($mockSecondArgument);
68  self::assertSame($mockSecondArgument, $arguments->getArgument('argumentName1234'), 'The added and retrieved argument is not the same.');
69  }
70 
75  {
76  $mockArgument = $this->createMock(Argument::class);
77  $mockObjectManager = $this->createMock(ObjectManagerInterface::class);
78  $mockObjectManager->expects(self::once())->method('get')->with(Argument::class)->willReturn($mockArgument);
79  $arguments = new ‪Arguments();
80  $arguments->injectObjectManager($mockObjectManager);
81  $newArgument = $arguments->addNewArgument('someArgument');
82  self::assertSame($newArgument, $mockArgument);
83  }
84 
89  {
90  $mockArgument = $this->getMockBuilder(Argument::class)
91  ->onlyMethods(['getName'])
92  ->disableOriginalConstructor()
93  ->getMock();
94  $mockArgument->method('getName')->willReturn('argumentName1234');
95  $arguments = new ‪Arguments();
96  $arguments[] = $mockArgument;
97  self::assertTrue($arguments->hasArgument('argumentName1234'), 'Added argument does not exist.');
98  self::assertSame($mockArgument, $arguments->getArgument('argumentName1234'), 'Added and retrieved arguments are not the same.');
99  }
100 
105  {
106  $mockArgument = $this->getMockBuilder(Argument::class)
107  ->onlyMethods(['getName'])
108  ->disableOriginalConstructor()
109  ->getMock();
110  $mockArgument->method('getName')->willReturn('argumentName1234');
111  $arguments = new ‪Arguments();
112  $arguments[] = $mockArgument;
113  self::assertSame($mockArgument, $arguments['argumentName1234'], 'Argument retrieved by array access is not the one we added.');
114  }
115 
120  {
121  $arguments = new ‪Arguments();
122  try {
123  $arguments->getArgument('someArgument');
124  self::fail('getArgument() did not throw an exception although the specified argument does not exist.');
125  } catch (‪NoSuchArgumentException $exception) {
126  }
127  }
128 
132  public function ‪issetReturnsCorrectResult(): void
133  {
134  $mockArgument = $this->getMockBuilder(Argument::class)
135  ->onlyMethods(['getName'])
136  ->disableOriginalConstructor()
137  ->getMock();
138  $mockArgument->method('getName')->willReturn('argumentName1234');
139  $arguments = new ‪Arguments();
140  self::assertFalse(isset($arguments['argumentName1234']), 'isset() did not return FALSE.');
141  $arguments[] = $mockArgument;
142  self::assertTrue(isset($arguments['argumentName1234']), 'isset() did not return TRUE.');
143  }
144 
149  {
150  $mockArgument1 = $this->getMockBuilder(Argument::class)
151  ->onlyMethods(['getName'])
152  ->disableOriginalConstructor()
153  ->getMock();
154  $mockArgument1->method('getName')->willReturn('argumentName1');
155  $mockArgument2 = $this->getMockBuilder(Argument::class)
156  ->onlyMethods(['getName'])
157  ->disableOriginalConstructor()
158  ->getMock();
159  $mockArgument2->method('getName')->willReturn('argumentName2');
160  $mockArgument3 = $this->getMockBuilder(Argument::class)
161  ->onlyMethods(['getName'])
162  ->disableOriginalConstructor()
163  ->getMock();
164  $mockArgument3->method('getName')->willReturn('argumentName3');
165  $arguments = new ‪Arguments();
166  $arguments[] = $mockArgument1;
167  $arguments[] = $mockArgument2;
168  $arguments[] = $mockArgument3;
169  $expectedArgumentNames = ['argumentName1', 'argumentName2', 'argumentName3'];
170  self::assertEquals($expectedArgumentNames, $arguments->getArgumentNames(), 'Returned argument names were not as expected.');
171  }
172 
177  {
178  $mockArgument1 = $this->getMockBuilder(Argument::class)
179  ->onlyMethods(['getName', 'getShortName'])
180  ->disableOriginalConstructor()
181  ->getMock();
182  $mockArgument1->method('getName')->willReturn('argumentName1');
183  $mockArgument1->method('getShortName')->willReturn('a');
184  $mockArgument2 = $this->getMockBuilder(Argument::class)
185  ->onlyMethods(['getName', 'getShortName'])
186  ->disableOriginalConstructor()
187  ->getMock();
188  $mockArgument2->method('getName')->willReturn('argumentName2');
189  $mockArgument2->method('getShortName')->willReturn('b');
190  $mockArgument3 = $this->getMockBuilder(Argument::class)
191  ->onlyMethods(['getName', 'getShortName'])
192  ->disableOriginalConstructor()
193  ->getMock();
194  $mockArgument3->method('getName')->willReturn('argumentName3');
195  $mockArgument3->method('getShortName')->willReturn('c');
196  $arguments = new ‪Arguments();
197  $arguments[] = $mockArgument1;
198  $arguments[] = $mockArgument2;
199  $arguments[] = $mockArgument3;
200  $expectedShortNames = ['a', 'b', 'c'];
201  self::assertEquals($expectedShortNames, $arguments->getArgumentShortNames(), 'Returned argument short names were not as expected.');
202  }
203 
208  {
209  $mockArgument = $this->getMockBuilder(Argument::class)
210  ->onlyMethods(['getName'])
211  ->disableOriginalConstructor()
212  ->getMock();
213  $mockArgument->method('getName')->willReturn('dummyName');
214  $mockObjectManager = $this->createMock(ObjectManagerInterface::class);
215  $mockObjectManager->expects(self::once())->method('get')->with(Argument::class)->willReturn($mockArgument);
216  $arguments = new ‪Arguments();
217  $arguments->injectObjectManager($mockObjectManager);
218  $addedArgument = $arguments->addNewArgument('dummyName');
219  self::assertInstanceOf(Argument::class, $addedArgument, 'addNewArgument() either did not add a new argument or did not return it.');
220  $retrievedArgument = $arguments['dummyName'];
221  self::assertSame($addedArgument, $retrievedArgument, 'The added and the retrieved argument are not the same.');
222  }
223 
228  {
229  $mockArgument = $this->getMockBuilder(Argument::class)
230  ->onlyMethods(['getName'])
231  ->disableOriginalConstructor()
232  ->getMock();
233  $mockArgument->method('getName')->willReturn('dummyName');
234  $mockObjectManager = $this->createMock(ObjectManagerInterface::class);
235  $mockObjectManager->expects(self::once())->method('get')->with(Argument::class, 'dummyName', 'Text')->willReturn($mockArgument);
236  $arguments = new ‪Arguments();
237  $arguments->injectObjectManager($mockObjectManager);
238  $arguments->addNewArgument('dummyName');
239  }
240 
245  {
246  $mockArgument = $this->getMockBuilder(Argument::class)
247  ->onlyMethods(['getName', 'setRequired'])
248  ->disableOriginalConstructor()
249  ->getMock();
250  $mockArgument->expects(self::once())->method('getName')->willReturn('dummyName');
251  $mockArgument->expects(self::once())->method('setRequired')->with(true);
252  $mockObjectManager = $this->createMock(ObjectManagerInterface::class);
253  $mockObjectManager->expects(self::once())->method('get')->with(Argument::class, 'dummyName', 'Text')->willReturn($mockArgument);
254  $arguments = new ‪Arguments();
255  $arguments->injectObjectManager($mockObjectManager);
256  $arguments->addNewArgument('dummyName', 'Text', true);
257  }
258 
263  {
264  $mockArgument = $this->getMockBuilder(Argument::class)
265  ->onlyMethods(['getName', 'setRequired', 'setDefaultValue'])
266  ->disableOriginalConstructor()
267  ->getMock();
268  $mockArgument->expects(self::once())->method('getName')->willReturn('dummyName');
269  $mockArgument->expects(self::once())->method('setRequired')->with(false);
270  $mockArgument->expects(self::once())->method('setDefaultValue')->with('someDefaultValue');
271  $mockObjectManager = $this->createMock(ObjectManagerInterface::class);
272  $mockObjectManager->expects(self::once())->method('get')->with(Argument::class, 'dummyName', 'Text')->willReturn($mockArgument);
273  $arguments = new ‪Arguments();
274  $arguments->injectObjectManager($mockObjectManager);
275  $arguments->addNewArgument('dummyName', 'Text', false, 'someDefaultValue');
276  }
277 
282  {
283  $this->expectException(\LogicException::class);
284  $this->expectExceptionCode(1210858451);
285  $arguments = new ‪Arguments();
286  $arguments->nonExistingMethod();
287  }
288 
292  public function ‪removeAllClearsAllArguments(): void
293  {
294  $mockArgument1 = $this->getMockBuilder(Argument::class)
295  ->onlyMethods(['getName', 'getShortName'])
296  ->disableOriginalConstructor()
297  ->getMock();
298  $mockArgument1->method('getName')->willReturn('argumentName1');
299  $mockArgument2 = $this->getMockBuilder(Argument::class)
300  ->onlyMethods(['getName', 'getShortName'])
301  ->disableOriginalConstructor()
302  ->getMock();
303  $mockArgument2->method('getName')->willReturn('argumentName2');
304  $arguments = new ‪Arguments();
305  $arguments[] = $mockArgument1;
306  $arguments[] = $mockArgument2;
307  self::assertTrue($arguments->hasArgument('argumentName2'));
308  $arguments->removeAll();
309  self::assertFalse($arguments->hasArgument('argumentName2'));
310  }
311 }
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\addNewArgumentCreatesAndAddsNewArgument
‪addNewArgumentCreatesAndAddsNewArgument()
Definition: ArgumentsTest.php:207
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\addingAnArgumentReplacesArgumentWithSameName
‪addingAnArgumentReplacesArgumentWithSameName()
Definition: ArgumentsTest.php:53
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\callingInvalidMethodThrowsException
‪callingInvalidMethodThrowsException()
Definition: ArgumentsTest.php:281
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\getArgumentNamesReturnsNamesOfAddedArguments
‪getArgumentNamesReturnsNamesOfAddedArguments()
Definition: ArgumentsTest.php:148
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller
Definition: ActionControllerTest.php:18
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\removeAllClearsAllArguments
‪removeAllClearsAllArguments()
Definition: ArgumentsTest.php:292
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\getArgumentShortNamesReturnsShortNamesOfAddedArguments
‪getArgumentShortNamesReturnsShortNamesOfAddedArguments()
Definition: ArgumentsTest.php:176
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments
Definition: Arguments.php:27
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\addingAnArgumentManuallyWorks
‪addingAnArgumentManuallyWorks()
Definition: ArgumentsTest.php:42
‪TYPO3\CMS\Extbase\Object\ObjectManagerInterface
Definition: ObjectManagerInterface.php:26
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\addNewArgumentCanAddArgumentsMarkedAsRequired
‪addNewArgumentCanAddArgumentsMarkedAsRequired()
Definition: ArgumentsTest.php:244
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\argumentsObjectIsOfScopePrototype
‪argumentsObjectIsOfScopePrototype()
Definition: ArgumentsTest.php:32
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\issetReturnsCorrectResult
‪issetReturnsCorrectResult()
Definition: ArgumentsTest.php:132
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\addNewArgumentCanAddArgumentsMarkedAsOptionalWithDefaultValues
‪addNewArgumentCanAddArgumentsMarkedAsOptionalWithDefaultValues()
Definition: ArgumentsTest.php:262
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\addingArgumentThroughArrayAccessWorks
‪addingArgumentThroughArrayAccessWorks()
Definition: ArgumentsTest.php:88
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\getArgumentWithNonExistingArgumentNameThrowsException
‪getArgumentWithNonExistingArgumentNameThrowsException()
Definition: ArgumentsTest.php:119
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\addNewArgumentProvidesFluentInterface
‪addNewArgumentProvidesFluentInterface()
Definition: ArgumentsTest.php:74
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest
Definition: ArgumentsTest.php:28
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\retrievingArgumentThroughArrayAccessWorks
‪retrievingArgumentThroughArrayAccessWorks()
Definition: ArgumentsTest.php:104
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument
Definition: Argument.php:28
‪TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException
Definition: NoSuchArgumentException.php:26
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\addNewArgumentAssumesTextDataTypeByDefault
‪addNewArgumentAssumesTextDataTypeByDefault()
Definition: ArgumentsTest.php:227