Passed
Push — issue#767 ( 902b86...e6d68a )
by Guilherme
05:21
created

SessionStateTest::testOnKernelResponseAddCookie()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 0
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\OpenIDBundle\Tests\Storage;
12
13
use LoginCidadao\OAuthBundle\Entity\Client;
14
use LoginCidadao\OpenIDBundle\Entity\ClientMetadata;
15
use LoginCidadao\OpenIDBundle\Manager\ClientManager;
16
use LoginCidadao\OpenIDBundle\Storage\SessionState;
17
use Symfony\Component\HttpFoundation\Response;
18
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
19
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
20
21
class SessionStateTest extends \PHPUnit_Framework_TestCase
22
{
23
    public function testOnKernelResponseAddCookie()
24
    {
25
        $headers = $this->getMock('Symfony\Component\HttpFoundation\ResponseHeaderBag');
26
        $headers->expects($this->once())->method('setCookie')
27
            ->with($this->isInstanceOf('Symfony\Component\HttpFoundation\Cookie'));
28
29
        $response = new Response();
30
        $response->headers = $headers;
0 ignored issues
show
Documentation Bug introduced by
It seems like $headers of type PHPUnit_Framework_MockObject_MockObject is incompatible with the declared type Symfony\Component\HttpFoundation\ResponseHeaderBag of property $headers.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
31
32
        $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
33
        $token->expects($this->once())->method('serialize')->willReturn('serialized');
34
35
        $tokenStorage = $this->getTokenStorage();
36
        $tokenStorage->expects($this->once())
37
            ->method('getToken')->willReturn($token);
38
39
        $event = $this->getFilterResponseEvent($response);
40
41
        $sessionState = new SessionState($this->getClientManager(), $tokenStorage);
42
        $sessionState->onKernelResponse($event);
43
    }
44
45
    public function testOnKernelResponseRemoveCookie()
46
    {
47
        $headers = $this->getMock('Symfony\Component\HttpFoundation\ResponseHeaderBag');
48
        $headers->expects($this->once())->method('removeCookie')->with('session_state');
49
50
        $response = new Response();
51
        $response->headers = $headers;
0 ignored issues
show
Documentation Bug introduced by
It seems like $headers of type PHPUnit_Framework_MockObject_MockObject is incompatible with the declared type Symfony\Component\HttpFoundation\ResponseHeaderBag of property $headers.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
52
53
        $event = $this->getFilterResponseEvent($response);
54
55
        $sessionState = new SessionState($this->getClientManager(), $this->getTokenStorage());
56
        $sessionState->onKernelResponse($event);
57
    }
58
59
    public function testGetSessionState()
60
    {
61
        $clientId = 'client_id';
62
        $sessionId = 'session_id';
63
        $client = (new Client())
64
            ->setMetadata((new ClientMetadata())
65
                ->setClientUri($url = 'https://example.com'));
66
67
        $clientManager = $this->getClientManager();
68
        $clientManager->expects($this->once())
69
            ->method('getClientById')->with($clientId)->willReturn($client);
70
71
        $sessionState = new SessionState($clientManager, $this->getTokenStorage());
72
73
        $state = $sessionState->getSessionState($clientId, $sessionId);
74
        $generatedSalt = explode('.', $state)[1];
75
76
        $expectedState = hash('sha256', $clientId.$url.$sessionId.$generatedSalt).".{$generatedSalt}";
77
        $this->assertSame($expectedState, $state);
78
    }
79
80
    /**
81
     * @return TokenStorageInterface|\PHPUnit_Framework_MockObject_MockObject
82
     */
83
    private function getTokenStorage()
84
    {
85
        return $this->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface');
86
    }
87
88
    /**
89
     * @return ClientManager|\PHPUnit_Framework_MockObject_MockObject
90
     */
91
    private function getClientManager()
92
    {
93
        return $this->getMockBuilder('LoginCidadao\OpenIDBundle\Manager\ClientManager')
94
            ->disableOriginalConstructor()->getMock();
95
    }
96
97
    /**
98
     * @return FilterResponseEvent|\PHPUnit_Framework_MockObject_MockObject
99
     */
100
    private function getFilterResponseEvent($response)
101
    {
102
        $event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\FilterResponseEvent')
103
            ->disableOriginalConstructor()->getMock();
104
        $event->expects($this->once())->method('isMasterRequest')->willReturn(true);
105
        $event->expects($this->once())->method('getResponse')->willReturn($response);
106
107
        return $event;
108
    }
109
}
110