Passed
Branch master (733912)
by Woody
03:06
created

anonymous//tests/LazyMiddlewareTest.php$0   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 10
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 10
c 0
b 0
f 0
wmc 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Northwoods\Middleware;
5
6
use InvalidArgumentException;
7
use Northwoods\Middleware\Fixture\Handler;
8
use Northwoods\Middleware\Fixture\Middleware;
9
use Nyholm\Psr7\ServerRequest;
10
use PHPUnit\Framework\TestCase;
11
use Psr\Container\ContainerInterface;
12
use Psr\Http\Server\MiddlewareInterface;
13
14
class LazyMiddlewareTest extends TestCase
15
{
16
    /** @var ContainerInterface */
17
    private $container;
18
19
    public function setUp()
20
    {
21
        $this->container = new class implements ContainerInterface
22
        {
23
            public function has($id)
24
            {
25
                return class_exists($id);
26
            }
27
28
            public function get($id)
29
            {
30
                return new $id();
31
            }
32
        };
33
    }
34
35
    public function testVerifiesMiddlewareImplementation(): void
36
    {
37
        $this->expectException(InvalidArgumentException::class);
38
        $this->expectExceptionMessage(self::class);
39
40
        $middleware = new LazyMiddleware($this->container, self::class);
0 ignored issues
show
Unused Code introduced by
The assignment to $middleware is dead and can be removed.
Loading history...
41
    }
42
43
    public function testDefersToMiddlewareImplemention(): void
44
    {
45
        $middleware = new LazyMiddleware($this->container, Middleware::class);
46
47
        $handler = new Handler();
48
        $request = new ServerRequest('GET', 'https://example.com/');
49
50
        $response = $middleware->process($request, $handler);
51
52
        $this->assertEquals(200, $response->getStatusCode());
53
        $this->assertEquals(Middleware::class, (string) $response->getBody());
54
    }
55
}
56