summaryrefslogtreecommitdiffstats
path: root/tests/Router/Middleware/ErrorHandlerTest.php
blob: c4b63134d785bc083e170e71dd589700d3bc8d1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php

use Jasny\Router\Middleware\ErrorHandler;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;

class ErrorHandlerTest extends PHPUnit_Framework_TestCase
{
    /**
     * Test invoke with invalid 'next' param
     */
    public function testInvokeInvalidNext()
    {
        $middleware = new ErrorHandler();
        list($request, $response) = $this->getRequests();

        $this->expectException(\InvalidArgumentException::class);

        $result = $middleware($request, $response, 'not_callable');
    }

    /**
     * Test that exception in 'next' callback is caught
     */
    public function testInvokeCatchError()
    {
        $middleware = new ErrorHandler();
        list($request, $response) = $this->getRequests();

        $this->expectCatchError($response);

        $result = $middleware($request, $response, function($request, $response) {
            throw new Exception('Test exception'); 
        });

        $this->assertEquals(get_class($response), get_class($result), "Middleware should return response object");        
    }

    /**
     * Test case when there is no error
     */
    public function testInvokeNoError()
    {
        $middleware = new ErrorHandler();
        list($request, $response) = $this->getRequests();

        $result = $middleware($request, $response, function($request, $response) {
            $response->nextCalled = true;

            return $response;
        });        

        $this->assertEquals(get_class($response), get_class($result), "Middleware should return response object");        
        $this->assertTrue($result->nextCalled, "'next' was not called");   
    }

    /**
     * Get requests for testing
     *
     * @return array
     */
    public function getRequests()
    {
        $request = $this->createMock(ServerRequestInterface::class);
        $response = $this->createMock(ResponseInterface::class);

        return [$request, $response];
    }

    /**
     * Expect for error
     *
     * @param ResponseInterface $response 
     */
    public function expectCatchError($response)
    {
        $stream = $this->createMock(StreamInterface::class);
        $stream->expects($this->once())->method('rewind');
        $stream->expects($this->once())->method('write')->with($this->equalTo('Unexpected error'));

        $response->method('getBody')->will($this->returnValue($stream));
        $response->expects($this->once())->method('withBody')->with($this->equalTo($stream))->will($this->returnSelf());
        $response->expects($this->once())->method('withStatus')->with($this->equalTo(500), $this->equalTo('Internal Server Error'))->will($this->returnSelf());
    }
}