Skip to content

Commit

Permalink
Corona: Separate parser for the sapi
Browse files Browse the repository at this point in the history
Issue: MOVE-14
  • Loading branch information
josemarper committed Feb 10, 2025
1 parent e8fa54b commit 5db80ef
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 0 deletions.
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';

}

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

Check failure on line 21 in src/Lunr/Corona/Parsers/Sapi/Tests/SapiParserGetTest.php

View workflow job for this annotation

GitHub Actions / php-tests / phpcs / PHPCS

Line indented incorrectly; expected 0 spaces, found 2
{

Check failure on line 22 in src/Lunr/Corona/Parsers/Sapi/Tests/SapiParserGetTest.php

View workflow job for this annotation

GitHub Actions / php-tests / phpcs / PHPCS

Expected 2 spaces before opening brace; 0 found

/**
* 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

Check failure on line 20 in src/Lunr/Corona/Parsers/Sapi/Tests/SapiParserTestCase.php

View workflow job for this annotation

GitHub Actions / php-tests / phpcs / PHPCS

Line indented incorrectly; expected 0 spaces, found 2
{

Check failure on line 21 in src/Lunr/Corona/Parsers/Sapi/Tests/SapiParserTestCase.php

View workflow job for this annotation

GitHub Actions / php-tests / phpcs / PHPCS

Expected 2 spaces before opening brace; 0 found

/**
* 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();
}

}

?>

0 comments on commit 5db80ef

Please sign in to comment.