Skip to content

Commit

Permalink
improved tests and code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Oscar Otero committed Jan 24, 2018
1 parent 9d55ac2 commit d36b0b8
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
vendor
composer.lock
.php_cs.cache
coverage
8 changes: 8 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,11 @@ Good pull requests – patches, improvements, new features – are a fantastic h
```sh
composer test
```

To get code coverage information execute the following comand:

```sh
composer coverage
```

Then, open the `./coverage/index.html` file in your browser.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@
},
"scripts": {
"test": [
"phpunit",
"phpunit --coverage-text",
"phpcs"
],
"cs-fix": "php-cs-fixer fix ."
"cs-fix": "php-cs-fixer fix .",
"coverage": "phpunit --coverage-html=coverage"
}
}
4 changes: 4 additions & 0 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ abstract class Factory

/**
* Set a custom ResponseFactory.
* @codeCoverageIgnore
*/
public static function setResponseFactory(ResponseFactoryInterface $responseFactory)
{
Expand All @@ -47,6 +48,7 @@ public static function setResponseFactory(ResponseFactoryInterface $responseFact

/**
* Set a custom StreamFactory.
* @codeCoverageIgnore
*/
public static function setStreamFactory(StreamFactoryInterface $streamFactory)
{
Expand All @@ -55,6 +57,7 @@ public static function setStreamFactory(StreamFactoryInterface $streamFactory)

/**
* Set a custom UriFactory.
* @codeCoverageIgnore
*/
public static function setUriFactory(UriFactoryInterface $uriFactory)
{
Expand All @@ -63,6 +66,7 @@ public static function setUriFactory(UriFactoryInterface $uriFactory)

/**
* Set a custom ServerRequestFactory.
* @codeCoverageIgnore
*/
public static function setServerRequestFactory(ServerRequestFactoryInterface $serverRequestFactory)
{
Expand Down
31 changes: 12 additions & 19 deletions src/RequestHandlerContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Psr\Http\Server\RequestHandlerInterface;
use ReflectionClass;
use ReflectionMethod;
use RuntimeException;

/**
* Resolve a callable using a container.
Expand Down Expand Up @@ -50,23 +49,19 @@ public function get($id)
{
try {
$handler = $this->resolve($id);

if ($handler instanceof RequestHandlerInterface) {
return $handler;
}

return new CallableHandler($handler);
} catch (NotFoundExceptionInterface $exception) {
throw $exception;
} catch (Exception $exception) {
throw new class("Error getting the handler $id", 0, $exception)
extends Exception implements ContainerExceptionInterface {
};
}

if ($handler instanceof RequestHandlerInterface) {
return $handler;
}

if (is_callable($handler)) {
return new CallableHandler($handler);
}

throw new class("Handler $id not found or has not valid type", 0, $exception)
extends Exception implements NotFoundExceptionInterface {
};
}

/**
Expand All @@ -84,10 +79,6 @@ protected function resolve(string $handler)
return function_exists($handler) ? $handler : $this->createClass($handler);
}

if (!is_array($handler) || !is_string($handler[0])) {
return $handler;
}

list($class, $method) = $handler;

if ((new ReflectionMethod($class, $method))->isStatic()) {
Expand All @@ -105,7 +96,9 @@ protected function resolve(string $handler)
protected function createClass(string $className)
{
if (!class_exists($className)) {
throw new RuntimeException("The class {$className} does not exists");
throw new class("The class $className does not exists")
extends Exception implements NotFoundExceptionInterface {
};
}

$reflection = new ReflectionClass($className);
Expand All @@ -120,7 +113,7 @@ protected function createClass(string $className)
/**
* Slit a string to an array
*
* @return string|array
* @return string|string[]
*/
protected function split(string $string)
{
Expand Down
8 changes: 6 additions & 2 deletions tests/ControllerStub.php → tests/Assets/Controller.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<?php
declare(strict_types = 1);

namespace Middlewares\Tests;
namespace Middlewares\Tests\Assets;

final class ControllerStub
final class Controller
{
public static function staticAction()
{
}

public function action()
{
}
Expand Down
18 changes: 18 additions & 0 deletions tests/Assets/ErrorController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
declare(strict_types = 1);

namespace Middlewares\Tests\Assets;

use Exception;

final class ErrorController
{
public function __construct()
{
throw new Exception('Error Processing Request');
}

public function __invoke()
{
}
}
15 changes: 15 additions & 0 deletions tests/Assets/RequestHandlerController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
declare(strict_types = 1);

namespace Middlewares\Tests\Assets;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

final class RequestHandlerController implements RequestHandlerInterface
{
public function handle(ServerRequestInterface $request): ResponseInterface
{
}
}
13 changes: 13 additions & 0 deletions tests/CallableHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ public function testExecute()
$this->assertEquals('Hello World', (string) $response->getBody());
}

public function testExecuteHandler()
{
$callable = new CallableHandler(function ($request) {
echo $request->getHeaderLine('Foo');
});

$request = Factory::createServerRequest()->withHeader('Foo', 'Bar');
$response = $callable->handle($request);

$this->assertInstanceOf(ResponseInterface::class, $response);
$this->assertEquals('Bar', (string) $response->getBody());
}

public function testOb()
{
$callable = new CallableHandler(function () {
Expand Down
14 changes: 14 additions & 0 deletions tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ public function testStream()
$this->assertTrue($stream->isSeekable());
}

public function testStreamWithResource()
{
$resource = fopen('php://temp', 'r+');
fwrite($resource, 'Hello world');

$stream = Factory::createStream($resource);

$this->assertInstanceOf(StreamInterface::class, $stream);
$this->assertInstanceOf(Stream::class, $stream);
$this->assertTrue($stream->isWritable());
$this->assertTrue($stream->isSeekable());
$this->assertEquals('Hello world', (string) $stream);
}

public function testUri()
{
$uri = Factory::createUri('http://example.com/my-path');
Expand Down
57 changes: 53 additions & 4 deletions tests/RequestHandlerContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@

namespace Middlewares\Tests;

use Middlewares\Tests\Assets\Controller;
use Middlewares\Tests\Assets\ErrorController;
use Middlewares\Tests\Assets\RequestHandlerController;
use Middlewares\Utils\RequestHandlerContainer;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Server\RequestHandlerInterface;

final class ReflectionResolverTest extends TestCase
Expand All @@ -13,9 +18,9 @@ public function testResolveClass()
{
$resolver = new RequestHandlerContainer();

$this->assertTrue($resolver->has(ControllerStub::class));
$this->assertTrue($resolver->has(Controller::class));

$callable = $resolver->get(ControllerStub::class);
$callable = $resolver->get(Controller::class);

$this->assertInstanceOf(RequestHandlerInterface::class, $callable);
}
Expand All @@ -24,9 +29,31 @@ public function testResolveClassCallable()
{
$resolver = new RequestHandlerContainer();

$this->assertTrue($resolver->has(ControllerStub::class.'::action'));
$this->assertTrue($resolver->has(Controller::class.'::action'));

$callable = $resolver->get(ControllerStub::class.'::action');
$callable = $resolver->get(Controller::class.'::action');

$this->assertInstanceOf(RequestHandlerInterface::class, $callable);
}

public function testResolveClassStaticCallable()
{
$resolver = new RequestHandlerContainer();

$this->assertTrue($resolver->has(Controller::class.'::staticAction'));

$callable = $resolver->get(Controller::class.'::staticAction');

$this->assertInstanceOf(RequestHandlerInterface::class, $callable);
}

public function testResolveRequestHandler()
{
$resolver = new RequestHandlerContainer();

$this->assertTrue($resolver->has(RequestHandlerController::class));

$callable = $resolver->get(RequestHandlerController::class);

$this->assertInstanceOf(RequestHandlerInterface::class, $callable);
}
Expand All @@ -41,4 +68,26 @@ public function testResolveFunction()

$this->assertInstanceOf(RequestHandlerInterface::class, $callable);
}

public function testContainerExceptionInterface()
{
$resolver = new RequestHandlerContainer();

$this->assertTrue($resolver->has(ErrorController::class));

$this->expectException(ContainerExceptionInterface::class);

$callable = $resolver->get(ErrorController::class);
}

public function testNotFoundException()
{
$resolver = new RequestHandlerContainer();

$this->assertFalse($resolver->has('foo'));

$this->expectException(NotFoundExceptionInterface::class);

$resolver->get('foo');
}
}

0 comments on commit d36b0b8

Please sign in to comment.