-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Corona: Add support for custom, scoped request parsers
Next to one parser to handle every supported value, also support custom, smaller scoped parsers for specific values. This allows splitting up the existing parsers and eventually only loading the parsers you actually need (and thus not wasting resources parsing something you don't need)
- Loading branch information
Showing
6 changed files
with
271 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
/** | ||
* This file contains the request value interface. | ||
* | ||
* SPDX-FileCopyrightText: Copyright 2024 Move Agency Group B.V., Zwolle, The Netherlands | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
namespace Lunr\Corona; | ||
|
||
/** | ||
* Request Value Interface. | ||
*/ | ||
interface RequestValueInterface | ||
{ | ||
|
||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
/** | ||
* This file contains the request value parser interface. | ||
* | ||
* SPDX-FileCopyrightText: Copyright 2024 Move Agency Group B.V., Zwolle, The Netherlands | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
namespace Lunr\Corona; | ||
|
||
use BackedEnum; | ||
|
||
/** | ||
* Request Value Parser Interface. | ||
*/ | ||
interface RequestValueParserInterface | ||
{ | ||
|
||
/** | ||
* Return the request value type the parser handles. | ||
* | ||
* @return class-string The FQDN of the type enum the parser handles | ||
*/ | ||
public function get_request_value_type(): string; | ||
|
||
/** | ||
* Get a request value. | ||
* | ||
* @param BackedEnum&RequestValueInterface $key The identifier/name of the request value to get | ||
* | ||
* @return scalar The requested value | ||
*/ | ||
public function get(BackedEnum&RequestValueInterface $key): bool|float|int|string|null; | ||
|
||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
/** | ||
* This file contains mock request value types. | ||
* | ||
* SPDX-FileCopyrightText: Copyright 2024 Move Agency Group B.V., Zwolle, The Netherlands | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
namespace Lunr\Corona\Tests\Helpers; | ||
|
||
use Lunr\Corona\RequestValueInterface; | ||
|
||
/** | ||
* Request Data Enums | ||
*/ | ||
enum MockRequestValue: string implements RequestValueInterface | ||
{ | ||
|
||
/** | ||
* Mock value. | ||
*/ | ||
case Foo = 'foo'; | ||
|
||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
|
||
/** | ||
* This file contains the RequestGetValueTest class. | ||
* | ||
* SPDX-FileCopyrightText: Copyright 2024 Move Agency Group B.V., Zwolle, The Netherlands | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
namespace Lunr\Corona\Tests; | ||
|
||
use Lunr\Corona\RequestValueParserInterface; | ||
use Lunr\Corona\Tests\Helpers\MockRequestValue; | ||
use RuntimeException; | ||
|
||
/** | ||
* Tests for getting request values. | ||
* | ||
* @covers Lunr\Corona\Request | ||
*/ | ||
class RequestGetValueTest extends RequestTest | ||
{ | ||
|
||
/** | ||
* Check that get() returns cached values. | ||
* | ||
* @covers Lunr\Corona\Request::get | ||
*/ | ||
public function testGetWithCachedValue(): void | ||
{ | ||
$cache = [ | ||
'foo' => 'bar', | ||
]; | ||
|
||
$this->set_reflection_property_value('request', $cache); | ||
|
||
$value = $this->class->get(MockRequestValue::Foo); | ||
|
||
$this->assertEquals('bar', $value); | ||
} | ||
|
||
/** | ||
* Check that get() returns mocked values. | ||
* | ||
* @covers Lunr\Corona\Request::get | ||
*/ | ||
public function testGetWithMockedValue(): void | ||
{ | ||
$cache = [ | ||
'foo' => 'bar', | ||
]; | ||
|
||
$mock = [ | ||
'foo' => 'baz', | ||
]; | ||
|
||
$this->set_reflection_property_value('request', $cache); | ||
$this->set_reflection_property_value('mock', $mock); | ||
|
||
$value = $this->class->get(MockRequestValue::Foo); | ||
|
||
$this->assertEquals('baz', $value); | ||
} | ||
|
||
/** | ||
* Check that get() throws an exception for a value with an unregistered parser. | ||
* | ||
* @covers Lunr\Corona\Request::get | ||
*/ | ||
public function testGetWithUncachedValueAndUnregisteredParserThrowsException(): void | ||
{ | ||
$this->expectException(RuntimeException::class); | ||
$this->expectExceptionMessage('No parser registered for requested value ("foo")!'); | ||
|
||
$this->class->get(MockRequestValue::Foo); | ||
} | ||
|
||
/** | ||
* Check that get() returns uncached values. | ||
* | ||
* @covers Lunr\Corona\Request::get | ||
*/ | ||
public function testGetWithUncachedValue(): void | ||
{ | ||
$parser = $this->getMockBuilder(RequestValueParserInterface::class) | ||
->getMock(); | ||
|
||
$parsers = [ | ||
MockRequestValue::class => $parser, | ||
]; | ||
|
||
$this->set_reflection_property_value('parsers', $parsers); | ||
|
||
$parser->expects($this->once()) | ||
->method('get') | ||
->with(MockRequestValue::Foo) | ||
->willReturn('bar'); | ||
|
||
$value = $this->class->get(MockRequestValue::Foo); | ||
|
||
$this->assertEquals('bar', $value); | ||
} | ||
|
||
} | ||
|
||
?> |