Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Commit

Permalink
Added some more tests for Dispatcher and CallableMiddleware
Browse files Browse the repository at this point in the history
  • Loading branch information
prisis committed Jan 24, 2018
1 parent 59acc66 commit d18c417
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/TestingHelper/Middleware/CallableMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private function execute($callable, array $arguments = []): ResponseInterface
$response = $instance->createResponse();
} else {
throw new UnexpectedValueException(
'The value returned must be scalar or an object with __toString method'
'The value returned must be "scalar" or an object with "__toString" method.'
);
}

Expand Down
44 changes: 44 additions & 0 deletions tests/Middleware/CallableMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
declare(strict_types=1);
namespace Narrowspark\TestingHelper\Tests\Middleware;

use Http\Factory\Guzzle\ResponseFactory;
use Http\Factory\Guzzle\ServerRequestFactory;
use Narrowspark\TestingHelper\Middleware\CallableMiddleware;
use Narrowspark\TestingHelper\Middleware\RequestHandlerMiddleware;
use PHPUnit\Framework\TestCase;

class CallableMiddlewareTest extends TestCase
{
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage Class name don't implements [Interop\Http\Factory\ResponseFactoryInterface] interface; [Narrowspark\TestingHelper\Tests\Middleware\CallableMiddlewareTest] given.
*/
public function testConstructorThrowErrorOnWrongResponseFactory(): void
{
Expand All @@ -18,4 +22,44 @@ function (): void {
CallableMiddlewareTest::class
);
}

/**
* @expectedException \UnexpectedValueException
* @expectedExceptionMessage The value returned must be "scalar" or an object with "__toString" method.
*/
public function testMiddlewareThrowErrorOnScalarType(): void
{
$middleware = new CallableMiddleware(
function () {
return new class() {
};
},
ResponseFactory::class
);

$middleware->process(
(new ServerRequestFactory())->createServerRequest('GET', '/'),
new RequestHandlerMiddleware(function (): void {
})
);
}

/**
* @expectedException \RuntimeException
* @expectedExceptionMessage No ResponseFactory class found.
*/
public function testConstructorThrowErrorOnWrongResponseFactory2(): void
{
$middleware = new CallableMiddleware(
function () {
return '';
}
);

$middleware->process(
(new ServerRequestFactory())->createServerRequest('GET', '/'),
new RequestHandlerMiddleware(function (): void {
})
);
}
}
10 changes: 10 additions & 0 deletions tests/Middleware/DispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ protected function setUp(): void
$this->serverRequest = (new ServerRequestFactory())->createServerRequest('GET', '/');
}

/**
* @expectedException \LogicException
* @expectedExceptionMessage Unresolved request: middleware stack exhausted with no result.
*/
public function testDispatcherWithEmptyStack(): void
{
$dispatcher = new Dispatcher([]);
$dispatcher->dispatch($this->serverRequest);
}

public function testDispatcher(): void
{
$dispatcher = new Dispatcher([
Expand Down

0 comments on commit d18c417

Please sign in to comment.