Skip to content

Commit

Permalink
Add additional test cases for Builder, Middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
rougin committed Mar 17, 2024
1 parent a3e83f8 commit 29aa1e2
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 4 deletions.
5 changes: 5 additions & 0 deletions tests/Forward/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public function make()

$app->get('/', 'Rougin\Slytherin\Fixture\Classes\NewClass@index');

$app->get('/hello', function ()
{
return 'Hello world!';
});

return $app;
}

Expand Down
14 changes: 14 additions & 0 deletions tests/Forward/ForwardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
14 changes: 10 additions & 4 deletions tests/Http/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
*/
Expand Down
18 changes: 18 additions & 0 deletions tests/Middleware/DispatcherTestCases.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down
34 changes: 34 additions & 0 deletions tests/Middleware/Multiple/Interop05.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Rougin\Slytherin\Middleware\Multiple;

use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface;

/**
* @package Slytherin
* @author Rougin Gutib <rougingutib@gmail.com>
*/
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<string, string> */
$parsed = $request->getParsedBody();

$response = $delegate->process($request);

if (array_key_exists('name', $parsed))
{
$response = $response->withHeader('name', $parsed['name']);
}

return $response;
}
}
31 changes: 31 additions & 0 deletions tests/Middleware/Multiple/Slytherin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rougin\Slytherin\Middleware\Multiple;

use Psr\Http\Message\ServerRequestInterface;
use Rougin\Slytherin\Middleware\HandlerInterface;
use Rougin\Slytherin\Middleware\MiddlewareInterface;

/**
* @package Slytherin
* @author Rougin Gutib <rougingutib@gmail.com>
*/
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<string, string> */
$parsed = $request->getParsedBody();

$parsed['name'] = 'Rougin Gutib';

$request = $request->withParsedBody($parsed);

return $handler->handle($request);
}
}

0 comments on commit 29aa1e2

Please sign in to comment.