Passed
Push — master ( 37eb29...5c91bf )
by Felipe
02:04
created

StatusCodeHandlerTest::provideStatusCodes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of coisa/http.
4
 *
5
 * (c) Felipe Sayão Lobato Abreu <[email protected]>
6
 *
7
 * This source file is subject to the license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace CoiSA\Http\Test\Handler;
12
13
use CoiSA\Http\Handler\StatusCodeHandler;
14
use Fig\Http\Message\StatusCodeInterface;
15
use PHPUnit\Framework\TestCase;
16
use Prophecy\Prophecy\ObjectProphecy;
17
use Psr\Http\Message\ResponseInterface;
18
use Psr\Http\Message\ServerRequestInterface;
19
use Psr\Http\Server\RequestHandlerInterface;
20
21
/**
22
 * Class StatusCodeHandlerTest
23
 *
24
 * @package CoiSA\Http\Test\Handler
25
 */
26
final class StatusCodeHandlerTest extends TestCase
27
{
28
    /** @var ObjectProphecy|RequestHandlerInterface */
29
    private $handler;
30
31
    /** @var ObjectProphecy|ServerRequestInterface */
32
    private $serverRequest;
33
34
    /** @var ObjectProphecy|ResponseInterface */
35
    private $response;
36
37
    public function setUp(): void
38
    {
39
        $this->handler       = $this->prophesize(RequestHandlerInterface::class);
40
        $this->serverRequest = $this->prophesize(ServerRequestInterface::class);
41
        $this->response      = $this->prophesize(ResponseInterface::class);
42
43
        $this->handler->handle($this->serverRequest->reveal())->will([$this->response, 'reveal']);
44
    }
45
46
    public function testInvalidStatusThrowsUnexpectedValueException(): void
47
    {
48
        $this->expectException(\UnexpectedValueException::class);
49
        new StatusCodeHandler($this->handler->reveal(), rand(600, 700));
50
    }
51
52
    /**
53
     * @dataProvider provideStatusCodes
54
     */
55
    public function testResponseHaveSameGivenStatusCode($statusCode)
56
    {
57
        $this->response->withStatus($statusCode)->will([$this->response, 'reveal']);
58
        $this->response->getStatusCode()->willReturn($statusCode);
59
60
        $handler = new StatusCodeHandler($this->handler->reveal(), $statusCode);
61
        $response = $handler->handle($this->serverRequest->reveal());
62
63
        $this->assertEquals($statusCode, $response->getStatusCode());
64
    }
65
66
    public function provideStatusCodes()
67
    {
68
        $reflection    = new \ReflectionClass(StatusCodeInterface::class);
69
        $allowedStatus = $reflection->getConstants();
70
71
        return array_chunk($allowedStatus, 1);
72
    }
73
}
74