summaryrefslogtreecommitdiffstats
path: root/tests/ControllerTest.php
blob: b0f357f3726b61f346c480bcd3dbb2d584134d13 (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
<?php

namespace Jasny;

use Jasny\Controller;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Jasny\Controller\TestHelper as ControllerTestHelper;

/**
 * @covers Jasny\Controller
 */
class ControllerTest extends \PHPUnit_Framework_TestCase
{
    use ControllerTestHelper;
    
    /**
     * Test running controller
     */
    public function testInvoke()
    {
        $test = $this;
        
        $request = $this->createMock(ServerRequestInterface::class);
        $response = $this->createMock(ResponseInterface::class);
        $finalResponse = $this->createMock(ResponseInterface::class);

        $controller = $this->getController();
        $controller->expects($this->once())->method('run')
            ->willReturnCallback(\Closure::bind(function() use ($test, $request, $response, $finalResponse) {
                $test->assertSame($request, $this->getRequest());
                $test->assertSame($response, $this->getResponse());
                
                $this->setResponse($finalResponse);
                return null;
            }, $controller, Controller::class));

        $result = $controller($request, $response);

        $this->assertEquals($finalResponse, $result);
    }
    
    /**
     * @expectedException LogicException
     */
    public function testGetRequestUninvoked()
    {
        $this->getController()->getRequest();
    }
    
    /**
     * @expectedException LogicException
     */
    public function testGetResponseUninvoked()
    {
        $this->getController()->getResponse();
    }
    
    
    public function testUseSession()
    {
        $request = $this->createMock(ServerRequestInterface::class);
        $response = $this->createMock(ResponseInterface::class);

        $controller = $this->getController(['useSession']);
        $controller->expects($this->once())->method('useSession');
        
        $controller($request, $response);
    }
}