Skip to content

Commit d36b0b8

Browse files
author
Oscar Otero
committed
improved tests and code coverage
1 parent 9d55ac2 commit d36b0b8

11 files changed

+147
-27
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
vendor
22
composer.lock
33
.php_cs.cache
4+
coverage

CONTRIBUTING.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,11 @@ Good pull requests – patches, improvements, new features – are a fantastic h
4545
```sh
4646
composer test
4747
```
48+
49+
To get code coverage information execute the following comand:
50+
51+
```sh
52+
composer coverage
53+
```
54+
55+
Then, open the `./coverage/index.html` file in your browser.

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@
4141
},
4242
"scripts": {
4343
"test": [
44-
"phpunit",
44+
"phpunit --coverage-text",
4545
"phpcs"
4646
],
47-
"cs-fix": "php-cs-fixer fix ."
47+
"cs-fix": "php-cs-fixer fix .",
48+
"coverage": "phpunit --coverage-html=coverage"
4849
}
4950
}

src/Factory.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ abstract class Factory
3939

4040
/**
4141
* Set a custom ResponseFactory.
42+
* @codeCoverageIgnore
4243
*/
4344
public static function setResponseFactory(ResponseFactoryInterface $responseFactory)
4445
{
@@ -47,6 +48,7 @@ public static function setResponseFactory(ResponseFactoryInterface $responseFact
4748

4849
/**
4950
* Set a custom StreamFactory.
51+
* @codeCoverageIgnore
5052
*/
5153
public static function setStreamFactory(StreamFactoryInterface $streamFactory)
5254
{
@@ -55,6 +57,7 @@ public static function setStreamFactory(StreamFactoryInterface $streamFactory)
5557

5658
/**
5759
* Set a custom UriFactory.
60+
* @codeCoverageIgnore
5861
*/
5962
public static function setUriFactory(UriFactoryInterface $uriFactory)
6063
{
@@ -63,6 +66,7 @@ public static function setUriFactory(UriFactoryInterface $uriFactory)
6366

6467
/**
6568
* Set a custom ServerRequestFactory.
69+
* @codeCoverageIgnore
6670
*/
6771
public static function setServerRequestFactory(ServerRequestFactoryInterface $serverRequestFactory)
6872
{

src/RequestHandlerContainer.php

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Psr\Http\Server\RequestHandlerInterface;
1111
use ReflectionClass;
1212
use ReflectionMethod;
13-
use RuntimeException;
1413

1514
/**
1615
* Resolve a callable using a container.
@@ -50,23 +49,19 @@ public function get($id)
5049
{
5150
try {
5251
$handler = $this->resolve($id);
52+
53+
if ($handler instanceof RequestHandlerInterface) {
54+
return $handler;
55+
}
56+
57+
return new CallableHandler($handler);
58+
} catch (NotFoundExceptionInterface $exception) {
59+
throw $exception;
5360
} catch (Exception $exception) {
5461
throw new class("Error getting the handler $id", 0, $exception)
5562
extends Exception implements ContainerExceptionInterface {
5663
};
5764
}
58-
59-
if ($handler instanceof RequestHandlerInterface) {
60-
return $handler;
61-
}
62-
63-
if (is_callable($handler)) {
64-
return new CallableHandler($handler);
65-
}
66-
67-
throw new class("Handler $id not found or has not valid type", 0, $exception)
68-
extends Exception implements NotFoundExceptionInterface {
69-
};
7065
}
7166

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

87-
if (!is_array($handler) || !is_string($handler[0])) {
88-
return $handler;
89-
}
90-
9182
list($class, $method) = $handler;
9283

9384
if ((new ReflectionMethod($class, $method))->isStatic()) {
@@ -105,7 +96,9 @@ protected function resolve(string $handler)
10596
protected function createClass(string $className)
10697
{
10798
if (!class_exists($className)) {
108-
throw new RuntimeException("The class {$className} does not exists");
99+
throw new class("The class $className does not exists")
100+
extends Exception implements NotFoundExceptionInterface {
101+
};
109102
}
110103

111104
$reflection = new ReflectionClass($className);
@@ -120,7 +113,7 @@ protected function createClass(string $className)
120113
/**
121114
* Slit a string to an array
122115
*
123-
* @return string|array
116+
* @return string|string[]
124117
*/
125118
protected function split(string $string)
126119
{

tests/ControllerStub.php renamed to tests/Assets/Controller.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
<?php
22
declare(strict_types = 1);
33

4-
namespace Middlewares\Tests;
4+
namespace Middlewares\Tests\Assets;
55

6-
final class ControllerStub
6+
final class Controller
77
{
8+
public static function staticAction()
9+
{
10+
}
11+
812
public function action()
913
{
1014
}

tests/Assets/ErrorController.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
namespace Middlewares\Tests\Assets;
5+
6+
use Exception;
7+
8+
final class ErrorController
9+
{
10+
public function __construct()
11+
{
12+
throw new Exception('Error Processing Request');
13+
}
14+
15+
public function __invoke()
16+
{
17+
}
18+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
namespace Middlewares\Tests\Assets;
5+
6+
use Psr\Http\Message\ResponseInterface;
7+
use Psr\Http\Message\ServerRequestInterface;
8+
use Psr\Http\Server\RequestHandlerInterface;
9+
10+
final class RequestHandlerController implements RequestHandlerInterface
11+
{
12+
public function handle(ServerRequestInterface $request): ResponseInterface
13+
{
14+
}
15+
}

tests/CallableHandlerTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ public function testExecute()
1919
$this->assertEquals('Hello World', (string) $response->getBody());
2020
}
2121

22+
public function testExecuteHandler()
23+
{
24+
$callable = new CallableHandler(function ($request) {
25+
echo $request->getHeaderLine('Foo');
26+
});
27+
28+
$request = Factory::createServerRequest()->withHeader('Foo', 'Bar');
29+
$response = $callable->handle($request);
30+
31+
$this->assertInstanceOf(ResponseInterface::class, $response);
32+
$this->assertEquals('Bar', (string) $response->getBody());
33+
}
34+
2235
public function testOb()
2336
{
2437
$callable = new CallableHandler(function () {

tests/FactoryTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,20 @@ public function testStream()
3636
$this->assertTrue($stream->isSeekable());
3737
}
3838

39+
public function testStreamWithResource()
40+
{
41+
$resource = fopen('php://temp', 'r+');
42+
fwrite($resource, 'Hello world');
43+
44+
$stream = Factory::createStream($resource);
45+
46+
$this->assertInstanceOf(StreamInterface::class, $stream);
47+
$this->assertInstanceOf(Stream::class, $stream);
48+
$this->assertTrue($stream->isWritable());
49+
$this->assertTrue($stream->isSeekable());
50+
$this->assertEquals('Hello world', (string) $stream);
51+
}
52+
3953
public function testUri()
4054
{
4155
$uri = Factory::createUri('http://example.com/my-path');

0 commit comments

Comments
 (0)