From 29aa1e280484bb5d00f267e8817a8ba535670a0a Mon Sep 17 00:00:00 2001 From: Rougin Gutib Date: Sun, 17 Mar 2024 15:37:49 +0800 Subject: [PATCH] Add additional test cases for Builder, Middleware --- tests/Forward/Builder.php | 5 ++++ tests/Forward/ForwardTest.php | 14 ++++++++++ tests/Http/MessageTest.php | 14 +++++++--- tests/Middleware/DispatcherTestCases.php | 18 +++++++++++++ tests/Middleware/Multiple/Interop05.php | 34 ++++++++++++++++++++++++ tests/Middleware/Multiple/Slytherin.php | 31 +++++++++++++++++++++ 6 files changed, 112 insertions(+), 4 deletions(-) create mode 100644 tests/Middleware/Multiple/Interop05.php create mode 100644 tests/Middleware/Multiple/Slytherin.php diff --git a/tests/Forward/Builder.php b/tests/Forward/Builder.php index f6956fc..b6a4874 100644 --- a/tests/Forward/Builder.php +++ b/tests/Forward/Builder.php @@ -19,6 +19,11 @@ public function make() $app->get('/', 'Rougin\Slytherin\Fixture\Classes\NewClass@index'); + $app->get('/hello', function () + { + return 'Hello world!'; + }); + return $app; } diff --git a/tests/Forward/ForwardTest.php b/tests/Forward/ForwardTest.php index 003db7b..3aae073 100644 --- a/tests/Forward/ForwardTest.php +++ b/tests/Forward/ForwardTest.php @@ -36,4 +36,18 @@ public function test_default_route() $this->builder->make()->run(); } + + /** + * @runInSeparateProcess + * + * @return void + */ + public function test_route_as_callback() + { + $this->builder->setUrl('GET', '/hello'); + + $this->expectOutputString('Hello world!'); + + $this->builder->make()->run(); + } } diff --git a/tests/Http/MessageTest.php b/tests/Http/MessageTest.php index 18199c5..1d1a109 100644 --- a/tests/Http/MessageTest.php +++ b/tests/Http/MessageTest.php @@ -88,10 +88,6 @@ public function test_getting_protocol_version() */ public function test_adding_a_header() { - $message = $this->message->withAddedHeader('age', '18'); - - $this->assertTrue($message->hasHeader('age')); - $expected = array('Rougin', 'Royce', 'Gutib'); $message = $this->message->withAddedHeader('names', $expected); @@ -101,6 +97,16 @@ public function test_adding_a_header() $this->assertEquals($expected, $actual); } + /** + * @return void + */ + public function test_checking_a_header() + { + $message = $this->message->withAddedHeader('age', '18'); + + $this->assertTrue($message->hasHeader('age')); + } + /** * @return void */ diff --git a/tests/Middleware/DispatcherTestCases.php b/tests/Middleware/DispatcherTestCases.php index 8cd9138..f471b37 100644 --- a/tests/Middleware/DispatcherTestCases.php +++ b/tests/Middleware/DispatcherTestCases.php @@ -4,6 +4,8 @@ use Rougin\Slytherin\Http\ServerRequest; use Rougin\Slytherin\Middleware\Interop; +use Rougin\Slytherin\Middleware\Multiple\Interop05; +use Rougin\Slytherin\Middleware\Multiple\Slytherin; use Rougin\Slytherin\Middleware\Wrapper; use Rougin\Slytherin\System\Lastone; use Rougin\Slytherin\Testcase; @@ -179,6 +181,22 @@ public function test_getting_middlewares() $this->assertCount(1, $actual); } + /** + * @return void + */ + public function test_different_middleware_interfaces() + { + $expected = array('Rougin Gutib'); + + $items = array(new Slytherin, new Interop05); + + $this->dispatcher->push($items); + + $actual = $this->process()->getHeader('name'); + + $this->assertEquals($expected, $actual); + } + /** * Processes the defined middleware dispatcher and return its response. * diff --git a/tests/Middleware/Multiple/Interop05.php b/tests/Middleware/Multiple/Interop05.php new file mode 100644 index 0000000..9a9bff7 --- /dev/null +++ b/tests/Middleware/Multiple/Interop05.php @@ -0,0 +1,34 @@ + + */ +class Interop05 implements MiddlewareInterface +{ + /** + * @param \Psr\Http\Message\ServerRequestInterface $request + * @param \Interop\Http\ServerMiddleware\DelegateInterface $delegate + * @return \Psr\Http\Message\ResponseInterface + */ + public function process(ServerRequestInterface $request, DelegateInterface $delegate) + { + /** @var array */ + $parsed = $request->getParsedBody(); + + $response = $delegate->process($request); + + if (array_key_exists('name', $parsed)) + { + $response = $response->withHeader('name', $parsed['name']); + } + + return $response; + } +} diff --git a/tests/Middleware/Multiple/Slytherin.php b/tests/Middleware/Multiple/Slytherin.php new file mode 100644 index 0000000..0c27641 --- /dev/null +++ b/tests/Middleware/Multiple/Slytherin.php @@ -0,0 +1,31 @@ + + */ +class Slytherin implements MiddlewareInterface +{ + /** + * @param \Psr\Http\Message\ServerRequestInterface $request + * @param \Rougin\Slytherin\Middleware\HandlerInterface $handler + * @return \Psr\Http\Message\ResponseInterface + */ + public function process(ServerRequestInterface $request, HandlerInterface $handler) + { + /** @var array */ + $parsed = $request->getParsedBody(); + + $parsed['name'] = 'Rougin Gutib'; + + $request = $request->withParsedBody($parsed); + + return $handler->handle($request); + } +}