ILIAS  trunk Revision v11.0_alpha-1689-g66c127b4ae8
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
RequestTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
26 use ILIAS\Data\URI;
27 
28 class RequestTest extends TestCase
29 {
30  protected function getURI(): URI
31  {
32  return $this->createMock(URI::class);
33  }
34 
35  protected function getEmptyRequest(): Request
36  {
37  return new Request($this->getURI(), Verb::NULL);
38  }
39 
40  public function testVerbAndBaseURL(): void
41  {
42  $url = $this->getURI();
43  $request = new Request($url, Verb::LIST_IDENTIFIERS);
44 
45  $this->assertSame($url, $request->baseURL());
46  $this->assertSame(Verb::LIST_IDENTIFIERS, $request->verb());
47  }
48 
49  public function testNoArgument(): void
50  {
51  $request = $this->getEmptyRequest();
52 
53  foreach (Argument::cases() as $argument) {
54  $this->assertFalse($request->hasArgument($argument));
55  $this->assertSame('', $request->argumentValue($argument));
56  }
57  }
58 
59  public function testSingleArgument(): void
60  {
61  $request = $this->getEmptyRequest()
62  ->withArgument(Argument::FROM_DATE, 'today');
63 
64  foreach (Argument::cases() as $argument) {
65  if ($argument === Argument::FROM_DATE) {
66  continue;
67  }
68  $this->assertFalse($request->hasArgument($argument));
69  $this->assertSame('', $request->argumentValue($argument));
70  }
71 
72  $this->assertTrue($request->hasArgument(Argument::FROM_DATE));
73  $this->assertSame('today', $request->argumentValue(Argument::FROM_DATE));
74  }
75 
76  public function testMultipleDifferentArguments(): void
77  {
78  $request = $this->getEmptyRequest()
79  ->withArgument(Argument::FROM_DATE, 'today')
80  ->withArgument(Argument::RESUMPTION_TOKEN, 'resume!');
81 
82  foreach (Argument::cases() as $argument) {
83  if (
84  $argument === Argument::FROM_DATE ||
85  $argument === Argument::RESUMPTION_TOKEN
86  ) {
87  continue;
88  }
89  $this->assertFalse($request->hasArgument($argument));
90  $this->assertSame('', $request->argumentValue($argument));
91  }
92 
93  $this->assertTrue($request->hasArgument(Argument::FROM_DATE));
94  $this->assertSame('today', $request->argumentValue(Argument::FROM_DATE));
95  $this->assertTrue($request->hasArgument(Argument::RESUMPTION_TOKEN));
96  $this->assertSame('resume!', $request->argumentValue(Argument::RESUMPTION_TOKEN));
97  }
98 
99  public function testArgumentKeysNoArgument(): void
100  {
101  $request = $this->getEmptyRequest();
102 
103  $this->assertNull($request->argumentKeys()->current());
104  }
105 
106  public function testArgumentKeys(): void
107  {
108  $request = $this->getEmptyRequest();
109 
110  $request = $request->withArgument(Argument::IDENTIFIER, 'some identifier');
111  $request = $request->withArgument(Argument::UNTIL_DATE, 'some date');
112 
113  $argument_keys = iterator_to_array($request->argumentKeys());
114  $this->assertCount(2, $argument_keys);
115  $this->assertContains(Argument::IDENTIFIER, $argument_keys);
116  $this->assertContains(Argument::UNTIL_DATE, $argument_keys);
117  }
118 
119  public function testHasCorrectArguments(): void
120  {
121  $expect_true = [];
122  $expect_false = [];
123 
124  $expect_true[] = $this->getEmptyRequest()
125  ->withArgument(Argument::IDENTIFIER, 'some identifier');
126 
127  $expect_true[] = $this->getEmptyRequest()
128  ->withArgument(Argument::IDENTIFIER, 'some identifier')
129  ->withArgument(Argument::RESUMPTION_TOKEN, 'token');
130 
131  $expect_true[] = $this->getEmptyRequest()
132  ->withArgument(Argument::IDENTIFIER, 'some identifier')
133  ->withArgument(Argument::RESUMPTION_TOKEN, 'token')
134  ->withArgument(Argument::FROM_DATE, 'date');
135 
136  $expect_true[] = $this->getEmptyRequest()
137  ->withArgument(Argument::MD_PREFIX, 'prefix');
138 
139  $expect_false[] = $this->getEmptyRequest();
140 
141  $expect_false[] = $this->getEmptyRequest()
142  ->withArgument(Argument::UNTIL_DATE, 'date');
143 
144  $expect_false[] = $this->getEmptyRequest()
145  ->withArgument(Argument::FROM_DATE, 'date');
146 
147  $expect_false[] = $this->getEmptyRequest()
148  ->withArgument(Argument::IDENTIFIER, 'some identifier')
149  ->withArgument(Argument::UNTIL_DATE, 'date');
150 
151  $expect_false[] = $this->getEmptyRequest()
152  ->withArgument(Argument::IDENTIFIER, 'some identifier')
153  ->withArgument(Argument::MD_PREFIX, 'prefix');
154 
155  $expect_false[] = $this->getEmptyRequest()
156  ->withArgument(Argument::FROM_DATE, 'date')
157  ->withArgument(Argument::MD_PREFIX, 'prefix');
158 
159  foreach ($expect_true as $request) {
160  $this->assertTrue($request->hasCorrectArguments(
161  [Argument::IDENTIFIER],
162  [Argument::RESUMPTION_TOKEN, Argument::FROM_DATE],
163  [Argument::MD_PREFIX]
164  ));
165  }
166  foreach ($expect_false as $request) {
167  $this->assertFalse($request->hasCorrectArguments(
168  [Argument::IDENTIFIER],
169  [Argument::RESUMPTION_TOKEN, Argument::FROM_DATE],
170  [Argument::MD_PREFIX]
171  ));
172  }
173  }
174 
175  public function testHasCorrectArgumentsNoArguments(): void
176  {
177  $request_no_arguments = $this->getEmptyRequest();
178 
179  $request_one_argument = $this->getEmptyRequest()
180  ->withArgument(Argument::IDENTIFIER, 'some identifier');
181 
182  $request_two_arguments = $this->getEmptyRequest()
183  ->withArgument(Argument::IDENTIFIER, 'some identifier')
184  ->withArgument(Argument::UNTIL_DATE, 'some date');
185 
186  $this->assertTrue($request_no_arguments->hasCorrectArguments([], [], []));
187  $this->assertFalse($request_one_argument->hasCorrectArguments([], [], []));
188  $this->assertFalse($request_two_arguments->hasCorrectArguments([], [], []));
189  }
190 
191  public function testHasCorrectArgumentsRequired(): void
192  {
193  $request_no_arguments = $this->getEmptyRequest();
194 
195  $request_one_argument = $this->getEmptyRequest()
196  ->withArgument(Argument::IDENTIFIER, 'some identifier');
197 
198  $request_two_arguments = $this->getEmptyRequest()
199  ->withArgument(Argument::IDENTIFIER, 'some identifier')
200  ->withArgument(Argument::UNTIL_DATE, 'some date');
201 
202  $this->assertFalse($request_no_arguments->hasCorrectArguments([Argument::IDENTIFIER], [], []));
203  $this->assertTrue($request_one_argument->hasCorrectArguments([Argument::IDENTIFIER], [], []));
204  $this->assertFalse($request_two_arguments->hasCorrectArguments([Argument::IDENTIFIER], [], []));
205  }
206 
207  public function testHasCorrectArgumentsOptional(): void
208  {
209  $request_no_arguments = $this->getEmptyRequest();
210 
211  $request_one_argument = $this->getEmptyRequest()
212  ->withArgument(Argument::IDENTIFIER, 'some identifier');
213 
214  $request_two_arguments = $this->getEmptyRequest()
215  ->withArgument(Argument::IDENTIFIER, 'some identifier')
216  ->withArgument(Argument::UNTIL_DATE, 'some date');
217 
218  $this->assertTrue($request_no_arguments->hasCorrectArguments([], [Argument::IDENTIFIER], []));
219  $this->assertTrue($request_one_argument->hasCorrectArguments([], [Argument::IDENTIFIER], []));
220  $this->assertFalse($request_two_arguments->hasCorrectArguments([], [Argument::IDENTIFIER], []));
221  }
222 
223  public function testHasCorrectArgumentsExclusive(): void
224  {
225  $expect_true = [];
226  $expect_false = [];
227 
228  $expect_false[] = $this->getEmptyRequest();
229 
230  $expect_true[] = $this->getEmptyRequest()
231  ->withArgument(Argument::IDENTIFIER, 'some identifier');
232 
233  $expect_true[] = $this->getEmptyRequest()
234  ->withArgument(Argument::UNTIL_DATE, 'some date');
235 
236  $expect_false[] = $this->getEmptyRequest()
237  ->withArgument(Argument::FROM_DATE, 'some date');
238 
239  $expect_false[] = $this->getEmptyRequest()
240  ->withArgument(Argument::IDENTIFIER, 'some identifier')
241  ->withArgument(Argument::UNTIL_DATE, 'some date');
242 
243  $expect_false[] = $this->getEmptyRequest()
244  ->withArgument(Argument::FROM_DATE, 'some date')
245  ->withArgument(Argument::UNTIL_DATE, 'some date');
246 
247  foreach ($expect_true as $request) {
248  $this->assertTrue($request->hasCorrectArguments(
249  [Argument::UNTIL_DATE],
250  [],
251  [Argument::IDENTIFIER]
252  ));
253  }
254  foreach ($expect_false as $request) {
255  $this->assertFalse($request->hasCorrectArguments(
256  [Argument::UNTIL_DATE],
257  [],
258  [Argument::IDENTIFIER]
259  ));
260  }
261  }
262 }
$url
Definition: shib_logout.php:66