summaryrefslogtreecommitdiffstats
path: root/tests/Controller/SessionTest.php
blob: a5fb5a61bbe1dd2527f2aa2146490a02e5978e63 (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
87
<?php

namespace Jasny\Controller;

use Jasny\Controller\Session;
use Jasny\Controller\Session\Flash;
use Psr\Http\Message\ServerRequestInterface;
use Jasny\Controller\TestHelper;

/**
 * @covers Jasny\Controller\Session
 * @internal There is some tight coupling between the Session trait and Flash class.
 */
class SessionTest extends \PHPUnit_Framework_TestCase
{
    use TestHelper;

    /**
     * Get the controller class
     * 
     * @return string
     */
    protected function getControllerClass()
    {
        return Session::class;
    }
   
    
    public function testUseSession()
    {
        $session = new \ArrayObject();
        
        $request = $this->createMock(ServerRequestInterface::class);
        $request->expects($this->once())->method('getAttribute')->with('session')->willReturn($session);
        
        $controller = $this->getController();
        $controller->expects($this->once())->method('getRequest')->willReturn($request);
        
        $controller->useSession();
        
        $this->assertAttributeSame($session, 'session', $controller);
    }
    
    public function testUseGlobalSession()
    {
        $request = $this->createMock(ServerRequestInterface::class);
        $request->expects($this->once())->method('getAttribute')->with('session')->willReturn(null);
        
        $controller = $this->getController();
        $controller->expects($this->once())->method('getRequest')->willReturn($request);
        
        $controller->useSession();
        
        $_SESSION['foo'] = 'bar'; // Change the global session to make sure it's set by reference
        
        $this->assertAttributeEquals(['foo' => 'bar'], 'session', $controller);
    }
    
    public function testGetFlash()
    {
        $session = new \ArrayObject();
        
        $controller = $this->getController();
        $this->setPrivateProperty($controller, 'session', $session);
        
        $flash = $controller->flash();
        $this->assertInstanceOf(Flash::class, $flash);
        $this->assertEmpty($flash->get());
        
        $this->assertAttributeSame($session, 'session', $flash);
        
        $this->assertSame($flash, $controller->flash());
    }
   
    public function testSetFlash()
    {
        $flash = $this->createMock(Flash::class);
        $flash->expects($this->once())->method('set')->with('notice', 'hello world');
        
        $controller = $this->getController();
        $this->setPrivateProperty($controller, 'flash', $flash);
        
        $result = $controller->flash('notice', 'hello world');
        
        $this->assertSame($flash, $result);
    }
}