From 669fc7220cc9e0e368fb03d12f2564178c90f182 Mon Sep 17 00:00:00 2001 From: Josemar Pereira Date: Mon, 10 Feb 2025 14:34:32 +0100 Subject: [PATCH] Corona: Separate parser for the sapi Issue: MOVE-14 --- src/Lunr/Corona/Parsers/Sapi/SapiParser.php | 76 +++++++++++++++++++ src/Lunr/Corona/Parsers/Sapi/SapiValue.php | 27 +++++++ .../Parsers/Sapi/Tests/SapiParserGetTest.php | 61 +++++++++++++++ .../Parsers/Sapi/Tests/SapiParserTestCase.php | 51 +++++++++++++ src/Lunr/Corona/RequestParser.php | 1 - .../RequestParserStaticRequestTestTrait.php | 16 ---- src/Lunr/Corona/Tests/RequestTestCase.php | 1 - tests/phpunit.xml | 1 + 8 files changed, 216 insertions(+), 18 deletions(-) create mode 100644 src/Lunr/Corona/Parsers/Sapi/SapiParser.php create mode 100644 src/Lunr/Corona/Parsers/Sapi/SapiValue.php create mode 100644 src/Lunr/Corona/Parsers/Sapi/Tests/SapiParserGetTest.php create mode 100644 src/Lunr/Corona/Parsers/Sapi/Tests/SapiParserTestCase.php diff --git a/src/Lunr/Corona/Parsers/Sapi/SapiParser.php b/src/Lunr/Corona/Parsers/Sapi/SapiParser.php new file mode 100644 index 00000000..612f496f --- /dev/null +++ b/src/Lunr/Corona/Parsers/Sapi/SapiParser.php @@ -0,0 +1,76 @@ + $this->parse(), + default => throw new RuntimeException('Unsupported request value type "' . $key::class . '"'), + }; + } + + /** + * Parse SAPI + * + * @return string|null + */ + public function parse(): ?string + { + return PHP_SAPI; + } + +} + +?> diff --git a/src/Lunr/Corona/Parsers/Sapi/SapiValue.php b/src/Lunr/Corona/Parsers/Sapi/SapiValue.php new file mode 100644 index 00000000..ef3d7b92 --- /dev/null +++ b/src/Lunr/Corona/Parsers/Sapi/SapiValue.php @@ -0,0 +1,27 @@ + diff --git a/src/Lunr/Corona/Parsers/Sapi/Tests/SapiParserGetTest.php b/src/Lunr/Corona/Parsers/Sapi/Tests/SapiParserGetTest.php new file mode 100644 index 00000000..c37e0c53 --- /dev/null +++ b/src/Lunr/Corona/Parsers/Sapi/Tests/SapiParserGetTest.php @@ -0,0 +1,61 @@ +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); + } + +} + +?> diff --git a/src/Lunr/Corona/Parsers/Sapi/Tests/SapiParserTestCase.php b/src/Lunr/Corona/Parsers/Sapi/Tests/SapiParserTestCase.php new file mode 100644 index 00000000..14bf4623 --- /dev/null +++ b/src/Lunr/Corona/Parsers/Sapi/Tests/SapiParserTestCase.php @@ -0,0 +1,51 @@ +class = new SapiParser(); + + parent::baseSetUp($this->class); + } + + /** + * TestCase Destructor. + */ + public function tearDown(): void + { + unset($this->class); + + parent::tearDown(); + } + +} + +?> diff --git a/src/Lunr/Corona/RequestParser.php b/src/Lunr/Corona/RequestParser.php index c474498d..bdda8a19 100644 --- a/src/Lunr/Corona/RequestParser.php +++ b/src/Lunr/Corona/RequestParser.php @@ -54,7 +54,6 @@ public function parse_request() { $request = []; - $request['sapi'] = PHP_SAPI; $request['host'] = gethostname(); $request['application_path'] = $this->config['default_application_path']; diff --git a/src/Lunr/Corona/Tests/Helpers/RequestParserStaticRequestTestTrait.php b/src/Lunr/Corona/Tests/Helpers/RequestParserStaticRequestTestTrait.php index ac7c7565..b550576d 100644 --- a/src/Lunr/Corona/Tests/Helpers/RequestParserStaticRequestTestTrait.php +++ b/src/Lunr/Corona/Tests/Helpers/RequestParserStaticRequestTestTrait.php @@ -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. */ diff --git a/src/Lunr/Corona/Tests/RequestTestCase.php b/src/Lunr/Corona/Tests/RequestTestCase.php index dbe07842..18f63b3f 100644 --- a/src/Lunr/Corona/Tests/RequestTestCase.php +++ b/src/Lunr/Corona/Tests/RequestTestCase.php @@ -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' ], diff --git a/tests/phpunit.xml b/tests/phpunit.xml index 32bd1af0..394bc12f 100644 --- a/tests/phpunit.xml +++ b/tests/phpunit.xml @@ -23,6 +23,7 @@ ../src/Lunr/Corona/Tests/ ../src/Lunr/Corona/Exceptions/Tests/ ../src/Lunr/Corona/Parsers/BearerToken/Tests/ + ../src/Lunr/Corona/Parsers/Sapi/Tests ../src/Lunr/Ray/Tests/