‪TYPO3CMS  ‪main
ArgumentsTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\Attributes\Test;
24 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25 
26 final class ‪ArgumentsTest extends UnitTestCase
27 {
28  #[Test]
29  public function ‪argumentsObjectIsOfScopePrototype(): void
30  {
31  $arguments1 = new ‪Arguments();
32  $arguments2 = new ‪Arguments();
33  self::assertNotSame($arguments1, $arguments2, 'The arguments object is not of scope prototype!');
34  }
35 
36  #[Test]
37  public function ‪addingAnArgumentManuallyWorks(): void
38  {
39  $arguments = new ‪Arguments();
40  $newArgument = new ‪Argument('argumentName1234', 'dummyValue');
41  $arguments->addArgument($newArgument);
42  self::assertSame($newArgument, $arguments->getArgument('argumentName1234'), 'The added and retrieved argument is not the same.');
43  }
44 
45  #[Test]
47  {
48  $arguments = new ‪Arguments();
49  $mockFirstArgument = $this->getMockBuilder(Argument::class)
50  ->onlyMethods(['getName'])
51  ->disableOriginalConstructor()
52  ->getMock();
53  $mockFirstArgument->method('getName')->willReturn('argumentName1234');
54  $arguments->addArgument($mockFirstArgument);
55  $mockSecondArgument = $this->getMockBuilder(Argument::class)
56  ->onlyMethods(['getName'])
57  ->disableOriginalConstructor()
58  ->getMock();
59  $mockSecondArgument->method('getName')->willReturn('argumentName1234');
60  $arguments->addArgument($mockSecondArgument);
61  self::assertSame($mockSecondArgument, $arguments->getArgument('argumentName1234'), 'The added and retrieved argument is not the same.');
62  }
63 
64  #[Test]
66  {
67  $arguments = new ‪Arguments();
68  $newArgument = $arguments->addNewArgument('someArgument');
69  self::assertInstanceOf(Argument::class, $newArgument);
70  }
71 
72  #[Test]
74  {
75  $mockArgument = $this->getMockBuilder(Argument::class)
76  ->onlyMethods(['getName'])
77  ->disableOriginalConstructor()
78  ->getMock();
79  $mockArgument->method('getName')->willReturn('argumentName1234');
80  $arguments = new ‪Arguments();
81  $arguments[] = $mockArgument;
82  self::assertTrue($arguments->hasArgument('argumentName1234'), 'Added argument does not exist.');
83  self::assertSame($mockArgument, $arguments->getArgument('argumentName1234'), 'Added and retrieved arguments are not the same.');
84  }
85 
86  #[Test]
88  {
89  $mockArgument = $this->getMockBuilder(Argument::class)
90  ->onlyMethods(['getName'])
91  ->disableOriginalConstructor()
92  ->getMock();
93  $mockArgument->method('getName')->willReturn('argumentName1234');
94  $arguments = new ‪Arguments();
95  $arguments[] = $mockArgument;
96  self::assertSame($mockArgument, $arguments['argumentName1234'], 'Argument retrieved by array access is not the one we added.');
97  }
98 
99  #[Test]
101  {
102  $arguments = new ‪Arguments();
103  try {
104  $arguments->getArgument('someArgument');
105  self::fail('getArgument() did not throw an exception although the specified argument does not exist.');
106  } catch (‪NoSuchArgumentException $exception) {
107  }
108  }
109 
110  #[Test]
111  public function ‪issetReturnsCorrectResult(): void
112  {
113  $mockArgument = $this->getMockBuilder(Argument::class)
114  ->onlyMethods(['getName'])
115  ->disableOriginalConstructor()
116  ->getMock();
117  $mockArgument->method('getName')->willReturn('argumentName1234');
118  $arguments = new ‪Arguments();
119  self::assertFalse(isset($arguments['argumentName1234']), 'isset() did not return FALSE.');
120  $arguments[] = $mockArgument;
121  self::assertTrue(isset($arguments['argumentName1234']), 'isset() did not return TRUE.');
122  }
123 
124  #[Test]
126  {
127  $mockArgument1 = $this->getMockBuilder(Argument::class)
128  ->onlyMethods(['getName'])
129  ->disableOriginalConstructor()
130  ->getMock();
131  $mockArgument1->method('getName')->willReturn('argumentName1');
132  $mockArgument2 = $this->getMockBuilder(Argument::class)
133  ->onlyMethods(['getName'])
134  ->disableOriginalConstructor()
135  ->getMock();
136  $mockArgument2->method('getName')->willReturn('argumentName2');
137  $mockArgument3 = $this->getMockBuilder(Argument::class)
138  ->onlyMethods(['getName'])
139  ->disableOriginalConstructor()
140  ->getMock();
141  $mockArgument3->method('getName')->willReturn('argumentName3');
142  $arguments = new ‪Arguments();
143  $arguments[] = $mockArgument1;
144  $arguments[] = $mockArgument2;
145  $arguments[] = $mockArgument3;
146  $expectedArgumentNames = ['argumentName1', 'argumentName2', 'argumentName3'];
147  self::assertEquals($expectedArgumentNames, $arguments->getArgumentNames(), 'Returned argument names were not as expected.');
148  }
149 
150  #[Test]
152  {
153  $mockArgument1 = $this->getMockBuilder(Argument::class)
154  ->onlyMethods(['getName', 'getShortName'])
155  ->disableOriginalConstructor()
156  ->getMock();
157  $mockArgument1->method('getName')->willReturn('argumentName1');
158  $mockArgument1->method('getShortName')->willReturn('a');
159  $mockArgument2 = $this->getMockBuilder(Argument::class)
160  ->onlyMethods(['getName', 'getShortName'])
161  ->disableOriginalConstructor()
162  ->getMock();
163  $mockArgument2->method('getName')->willReturn('argumentName2');
164  $mockArgument2->method('getShortName')->willReturn('b');
165  $mockArgument3 = $this->getMockBuilder(Argument::class)
166  ->onlyMethods(['getName', 'getShortName'])
167  ->disableOriginalConstructor()
168  ->getMock();
169  $mockArgument3->method('getName')->willReturn('argumentName3');
170  $mockArgument3->method('getShortName')->willReturn('c');
171  $arguments = new ‪Arguments();
172  $arguments[] = $mockArgument1;
173  $arguments[] = $mockArgument2;
174  $arguments[] = $mockArgument3;
175  $expectedShortNames = ['a', 'b', 'c'];
176  self::assertEquals($expectedShortNames, $arguments->getArgumentShortNames(), 'Returned argument short names were not as expected.');
177  }
178 
179  #[Test]
181  {
182  $mockArgument = $this->getMockBuilder(Argument::class)
183  ->onlyMethods(['getName'])
184  ->disableOriginalConstructor()
185  ->getMock();
186  $mockArgument->method('getName')->willReturn('dummyName');
187  $arguments = new ‪Arguments();
188  $addedArgument = $arguments->addNewArgument('dummyName');
189  self::assertInstanceOf(Argument::class, $addedArgument, 'addNewArgument() either did not add a new argument or did not return it.');
190  $retrievedArgument = $arguments['dummyName'];
191  self::assertSame($addedArgument, $retrievedArgument, 'The added and the retrieved argument are not the same.');
192  }
193 
194  #[Test]
196  {
197  $mockArgument = $this->getMockBuilder(Argument::class)
198  ->onlyMethods(['getName'])
199  ->disableOriginalConstructor()
200  ->getMock();
201  $mockArgument->method('getName')->willReturn('dummyName');
202  $arguments = new ‪Arguments();
203  $arguments->addNewArgument('dummyName');
204  }
205 
206  #[Test]
208  {
209  $arguments = new ‪Arguments();
210  $argument = $arguments->addNewArgument('dummyName', 'Text', true);
211  self::assertTrue($argument->isRequired());
212  }
213 
214  #[Test]
216  {
217  $arguments = new ‪Arguments();
218  $argument = $arguments->addNewArgument('dummyName', 'Text', false, 'someDefaultValue');
219  self::assertFalse($argument->isRequired());
220  }
221 
222  #[Test]
224  {
225  $this->expectException(\LogicException::class);
226  $this->expectExceptionCode(1210858451);
227  $arguments = new ‪Arguments();
228  $arguments->nonExistingMethod();
229  }
230 
231  #[Test]
232  public function ‪removeAllClearsAllArguments(): void
233  {
234  $mockArgument1 = $this->getMockBuilder(Argument::class)
235  ->onlyMethods(['getName', 'getShortName'])
236  ->disableOriginalConstructor()
237  ->getMock();
238  $mockArgument1->method('getName')->willReturn('argumentName1');
239  $mockArgument2 = $this->getMockBuilder(Argument::class)
240  ->onlyMethods(['getName', 'getShortName'])
241  ->disableOriginalConstructor()
242  ->getMock();
243  $mockArgument2->method('getName')->willReturn('argumentName2');
244  $arguments = new ‪Arguments();
245  $arguments[] = $mockArgument1;
246  $arguments[] = $mockArgument2;
247  self::assertTrue($arguments->hasArgument('argumentName2'));
248  $arguments->removeAll();
249  self::assertFalse($arguments->hasArgument('argumentName2'));
250  }
251 }
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\addNewArgumentCreatesAndAddsNewArgument
‪addNewArgumentCreatesAndAddsNewArgument()
Definition: ArgumentsTest.php:180
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\addingAnArgumentReplacesArgumentWithSameName
‪addingAnArgumentReplacesArgumentWithSameName()
Definition: ArgumentsTest.php:46
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\callingInvalidMethodThrowsException
‪callingInvalidMethodThrowsException()
Definition: ArgumentsTest.php:223
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\getArgumentNamesReturnsNamesOfAddedArguments
‪getArgumentNamesReturnsNamesOfAddedArguments()
Definition: ArgumentsTest.php:125
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller
Definition: ArgumentsTest.php:18
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\removeAllClearsAllArguments
‪removeAllClearsAllArguments()
Definition: ArgumentsTest.php:232
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\getArgumentShortNamesReturnsShortNamesOfAddedArguments
‪getArgumentShortNamesReturnsShortNamesOfAddedArguments()
Definition: ArgumentsTest.php:151
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments
Definition: Arguments.php:29
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\addingAnArgumentManuallyWorks
‪addingAnArgumentManuallyWorks()
Definition: ArgumentsTest.php:37
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\addNewArgumentCanAddArgumentsMarkedAsRequired
‪addNewArgumentCanAddArgumentsMarkedAsRequired()
Definition: ArgumentsTest.php:207
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\argumentsObjectIsOfScopePrototype
‪argumentsObjectIsOfScopePrototype()
Definition: ArgumentsTest.php:29
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\issetReturnsCorrectResult
‪issetReturnsCorrectResult()
Definition: ArgumentsTest.php:111
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\addNewArgumentCanAddArgumentsMarkedAsOptionalWithDefaultValues
‪addNewArgumentCanAddArgumentsMarkedAsOptionalWithDefaultValues()
Definition: ArgumentsTest.php:215
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\addingArgumentThroughArrayAccessWorks
‪addingArgumentThroughArrayAccessWorks()
Definition: ArgumentsTest.php:73
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\getArgumentWithNonExistingArgumentNameThrowsException
‪getArgumentWithNonExistingArgumentNameThrowsException()
Definition: ArgumentsTest.php:100
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\addNewArgumentProvidesFluentInterface
‪addNewArgumentProvidesFluentInterface()
Definition: ArgumentsTest.php:65
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest
Definition: ArgumentsTest.php:27
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\retrievingArgumentThroughArrayAccessWorks
‪retrievingArgumentThroughArrayAccessWorks()
Definition: ArgumentsTest.php:87
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument
Definition: Argument.php:27
‪TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException
Definition: NoSuchArgumentException.php:25
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\ArgumentsTest\addNewArgumentAssumesTextDataTypeByDefault
‪addNewArgumentAssumesTextDataTypeByDefault()
Definition: ArgumentsTest.php:195