Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] Corona: Separate parser for the sapi #38

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions src/Lunr/Corona/Parsers/Sapi/SapiParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

/**
* This file contains the request value parser for the sapi
*
* SPDX-FileCopyrightText: Copyright 2025 Move Agency Group B.V., Zwolle, The Netherlands
* SPDX-License-Identifier: MIT
*/

namespace Lunr\Corona\Parsers\Sapi;

use BackedEnum;
use Lunr\Corona\RequestValueInterface;
use Lunr\Corona\RequestValueParserInterface;
use RuntimeException;

/**
* Request Value Parser for sapi
*/
class SapiParser implements RequestValueParserInterface
{

/**
* Constructor.
*/
public function __construct()
{
// no-op
}

/**
* Destructor.
*/
public function __destruct()
{
// no-op
}

/**
* Return the request value type the parser handles.
*
* @return class-string The FQDN of the type enum the parser handles
*/
public function getRequestValueType(): string
{
return SapiValue::class;
}

/**
* Get a request value.
*
* @param BackedEnum&RequestValueInterface $key The identifier/name of the request value to get
*
* @return string|null
*/
public function get(BackedEnum&RequestValueInterface $key): ?string
{
return match ($key) {
SapiValue::Sapi => $this->parse(),
default => throw new RuntimeException('Unsupported request value type "' . $key::class . '"'),
};
}

/**
* Parse SAPI
*
* @return string|null
*/
public function parse(): ?string
{
return PHP_SAPI;
}

}

?>
27 changes: 27 additions & 0 deletions src/Lunr/Corona/Parsers/Sapi/SapiValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/**
* This file contains the sapi value.
*
* SPDX-FileCopyrightText: Copyright 2025 Move Agency Group B.V., Zwolle, The Netherlands
* SPDX-License-Identifier: MIT
*/

namespace Lunr\Corona\Parsers\Sapi;

use Lunr\Corona\RequestValueInterface;

/**
* Request Data Enums
*/
enum SapiValue: string implements RequestValueInterface
{

/**
* Sapi
*/
case Sapi = 'sapi';

}

?>
61 changes: 61 additions & 0 deletions src/Lunr/Corona/Parsers/Sapi/Tests/SapiParserGetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/**
* This file contains the SapiParserGetTest class.
*
* SPDX-FileCopyrightText: Copyright 2025 Move Agency Group B.V., Zwolle, The Netherlands
* SPDX-License-Identifier: MIT
*/

namespace Lunr\Corona\Parsers\Sapi\Tests;

use Lunr\Corona\Parsers\Sapi\SapiValue;
use Lunr\Corona\Tests\Helpers\MockRequestValue;
use RuntimeException;

/**
* This class contains test methods for the SapiParser class.
*
* @covers Lunr\Corona\Parsers\Sapi\SapiParser
*/
class SapiParserGetTest extends SapiParserTestCase
{

/**
* Test that getRequestValueType() returns the correct type.
*
* @covers Lunr\Corona\Parsers\Sapi\SapiParser::getRequestValueType
*/
public function testGetRequestValueType()
{
$this->assertEquals(SapiValue::class, $this->class->getRequestValueType());
}

/**
* Test getting an unsupported value.
*
* @covers Lunr\Corona\Parsers\Sapi\SapiParser::get
*/
public function testGetUnsupportedValue()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Unsupported request value type "Lunr\Corona\Tests\Helpers\MockRequestValue"');

$this->class->get(MockRequestValue::Foo);
}

/**
* Test getting a parsed sapi
*/
public function testGetSapi()
{
$key = SapiValue::Sapi;

$value = $this->class->get($key);

$this->assertEquals(PHP_SAPI, $value);
}

}

?>
51 changes: 51 additions & 0 deletions src/Lunr/Corona/Parsers/Sapi/Tests/SapiParserTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/**
* This file contains the SapiParserTestCase class.
*
* SPDX-FileCopyrightText: Copyright 2025 Move Agency Group B.V., Zwolle, The Netherlands
* SPDX-License-Identifier: MIT
*/

namespace Lunr\Corona\Parsers\Sapi\Tests;

use Lunr\Corona\Parsers\Sapi\SapiParser;
use Lunr\Halo\LunrBaseTestCase;

/**
* This class contains test methods for the SapiParser class.
*
* @covers Lunr\Corona\Parsers\Sapi\SapiParser
*/
abstract class SapiParserTestCase extends LunrBaseTestCase
{

/**
* Instance of the tested class.
* @var SapiParser
*/
protected SapiParser $class;

/**
* TestCase Constructor.
*/
public function setUp(): void
{
$this->class = new SapiParser();

parent::baseSetUp($this->class);
}

/**
* TestCase Destructor.
*/
public function tearDown(): void
{
unset($this->class);

parent::tearDown();
}

}

?>
1 change: 0 additions & 1 deletion src/Lunr/Corona/RequestParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public function parse_request()
{
$request = [];

$request['sapi'] = PHP_SAPI;
$request['host'] = gethostname();

$request['application_path'] = $this->config['default_application_path'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,22 +108,6 @@ public function testRequestHost(): void
$this->cleanup_request_test();
}

/**
* Test that the sapi is stored correctly.
*/
public function testRequestSapi(): void
{
$this->prepare_request_test();

$request = $this->class->parse_request();

$this->assertIsArray($request);
$this->assertArrayHasKey('sapi', $request);
$this->assertEquals(PHP_SAPI, $request['sapi']);

$this->cleanup_request_test();
}

/**
* Test that the application_path is constructed and stored correctly.
*/
Expand Down
1 change: 0 additions & 1 deletion src/Lunr/Corona/Tests/RequestTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ protected function getRequestValues(): array
'port' => '443',
'base_path' => '/path/to/',
'base_url' => 'https://www.domain.com/path/to/',
'sapi' => 'cli',
'controller' => 'controller',
'method' => 'method',
'params' => [ 'param1', 'param2' ],
Expand Down
1 change: 1 addition & 0 deletions tests/phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<directory>../src/Lunr/Corona/Tests/</directory>
<directory>../src/Lunr/Corona/Exceptions/Tests/</directory>
<directory>../src/Lunr/Corona/Parsers/BearerToken/Tests/</directory>
<directory>../src/Lunr/Corona/Parsers/Sapi/Tests</directory>
</testsuite>
<testsuite name="Ray">
<directory>../src/Lunr/Ray/Tests/</directory>
Expand Down