ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
ParserTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
24 use ILIAS\Data\URI;
27 
28 class ParserTest extends TestCase
29 {
30  protected function getURI(string $string): URI
31  {
32  $url = $this->createMock(URI::class);
33  $url->method('__toString')->willReturn($string);
34  return $url;
35  }
36 
41  protected function getHTTPWrapper(
42  array $arguments_with_values
43  ): WrapperInterface {
44  return new class ($arguments_with_values) extends NullWrapper {
45  public function __construct(
46  protected array $arguments_with_values
47  ) {
48  }
49 
50  public function requestHasArgument(Argument $argument): bool
51  {
52  return in_array(
53  $argument->value,
54  array_keys($this->arguments_with_values)
55  );
56  }
57 
58  public function retrieveArgumentFromRequest(Argument $argument): string
59  {
60  if (!$this->requestHasArgument($argument)) {
61  return '';
62  }
63  return $this->arguments_with_values[$argument->value];
64  }
65  };
66  }
67 
68  public function getParser(
69  WrapperInterface $wrapper
70  ): Parser {
71  return new class ($wrapper) extends Parser {
72  protected function getEmptyRequest(
73  Verb $verb,
74  URI $base_url
75  ): RequestInterface {
76  return new class ($verb, $base_url) extends NullRequest {
77  public array $exposed_arguments = [];
78 
79  public function __construct(
80  public Verb $exposed_verb,
81  public URI $exposed_base_url
82  ) {
83  }
84 
85  public function withArgument(
86  Argument $key,
87  string $value
88  ): RequestInterface {
89  $clone = clone $this;
90  $clone->exposed_arguments[$key->value] = $value;
91  return $clone;
92  }
93  };
94  }
95  };
96  }
97 
98  public function testParseFromHTTP(): void
99  {
100  $parser = $this->getParser($this->getHTTPWrapper(
101  [
102  Argument::IDENTIFIER->value => 'some identifier',
103  Argument::VERB->value => Verb::LIST_IDENTIFIERS->value,
104  Argument::FROM_DATE->value => 'some date'
105  ]
106  ));
107 
108  $request = $parser->parseFromHTTP($this->getURI('some url'));
109 
110  $this->assertSame('some url', (string) $request->exposed_base_url);
111  $this->assertSame(Verb::LIST_IDENTIFIERS, $request->exposed_verb);
112  $this->assertEquals(
113  [
114  Argument::IDENTIFIER->value => 'some identifier',
115  Argument::FROM_DATE->value => 'some date'
116  ],
117  $request->exposed_arguments
118  );
119  }
120 
122  {
123  $string_with_reserved_chars = ':/?#[]@!$&' . "'" . ' ()*+,;=';
124  $parser = $this->getParser($this->getHTTPWrapper(
125  [
126  Argument::IDENTIFIER->value => rawurlencode($string_with_reserved_chars)
127  ]
128  ));
129 
130  $request = $parser->parseFromHTTP($this->getURI('some url'));
131 
132  $this->assertEquals(
133  [Argument::IDENTIFIER->value => $string_with_reserved_chars],
134  $request->exposed_arguments
135  );
136  }
137 
138  public function testParseFromHTTPNoArgumentsNoVerb(): void
139  {
140  $parser = $this->getParser($this->getHTTPWrapper([]));
141 
142  $request = $parser->parseFromHTTP($this->getURI('some url'));
143 
144  $this->assertSame(Verb::NULL, $request->exposed_verb);
145  $this->assertEmpty($request->exposed_arguments);
146  }
147 
148  public function testParseFromHTTPNoArguments(): void
149  {
150  $parser = $this->getParser($this->getHTTPWrapper(
151  [Argument::VERB->value => Verb::LIST_IDENTIFIERS->value,]
152  ));
153 
154  $request = $parser->parseFromHTTP($this->getURI('some url'));
155 
156  $this->assertEmpty($request->exposed_arguments);
157  }
158 
159  public function testParseFromHTTPNoVerb(): void
160  {
161  $parser = $this->getParser($this->getHTTPWrapper(
162  [Argument::IDENTIFIER->value => 'some identifier']
163  ));
164 
165  $request = $parser->parseFromHTTP($this->getURI('some url'));
166 
167  $this->assertSame(Verb::NULL, $request->exposed_verb);
168  }
169 
170  public function testParseFromHTTPInvalidVerb(): void
171  {
172  $parser = $this->getParser($this->getHTTPWrapper(
173  [Argument::VERB->value => 'nonsense verb']
174  ));
175 
176  $request = $parser->parseFromHTTP($this->getURI('some url'));
177 
178  $this->assertSame(Verb::NULL, $request->exposed_verb);
179  $this->assertEmpty($request->exposed_arguments);
180  }
181 
182  public function testParseFromHTTPInvalidArgument(): void
183  {
184  $parser = $this->getParser($this->getHTTPWrapper(
185  ['nonsense argument' => 'nonsense verb']
186  ));
187 
188  $request = $parser->parseFromHTTP($this->getURI('some url'));
189 
190  $this->assertSame(Verb::NULL, $request->exposed_verb);
191  $this->assertEmpty($request->exposed_arguments);
192  }
193 }
$url
Definition: shib_logout.php:66
getHTTPWrapper(array $arguments_with_values)
If verb should be found, also pass it explicitely as an argument.
Definition: ParserTest.php:41
__construct()
Constructor setup ILIAS global object public.
Definition: class.ilias.php:76