Completed
Pull Request — master (#790)
by Guilherme
08:21 queued 04:10
created

RequestSubscriberTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 72
rs 10
c 0
b 0
f 0
wmc 5
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\TaskStackBundle\Tests\Event;
12
13
use LoginCidadao\CoreBundle\Event\LoginCidadaoCoreEvents;
14
use LoginCidadao\TaskStackBundle\Event\RequestSubscriber;
15
use LoginCidadao\TaskStackBundle\Service\TaskStackManagerInterface;
16
use PHPUnit\Framework\TestCase;
17
use Symfony\Component\HttpKernel\KernelEvents;
18
19
class RequestSubscriberTest extends TestCase
20
{
21
    /**
22
     * @return \PHPUnit_Framework_MockObject_MockObject|TaskStackManagerInterface
23
     */
24
    private function getStackManager()
25
    {
26
        return $this->createMock('LoginCidadao\TaskStackBundle\Service\TaskStackManagerInterface');
27
    }
28
29
    public function testGetSubscribedEvents()
30
    {
31
        $expected = [
32
            KernelEvents::REQUEST => 'onRequest',
33
            LoginCidadaoCoreEvents::AUTHENTICATION_ENTRY_POINT_START => 'onAuthenticationStart',
34
        ];
35
        $this->assertEquals($expected, RequestSubscriber::getSubscribedEvents());
36
    }
37
38
    public function testOnRequest()
39
    {
40
        $request = $this->createMock('Symfony\Component\HttpFoundation\Request');
41
        $response = $this->createMock('Symfony\Component\HttpFoundation\Response');
42
43
        $stackManager = $this->getStackManager();
44
        $stackManager->expects($this->once())->method('processRequest')->willReturn($response);
45
46
        $event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent')
47
            ->disableOriginalConstructor()
48
            ->getMock();
49
        $event->expects($this->once())->method('isMasterRequest')->willReturn(true);
50
        $event->expects($this->once())->method('getRequest')->willReturn($request);
51
        $event->expects($this->once())->method('getResponse')->willReturn($response);
52
        $event->expects($this->once())->method('setResponse')->with($response);
53
54
        $subscriber = new RequestSubscriber($stackManager);
55
        $subscriber->onRequest($event);
56
    }
57
58
    public function testOnRequestSkipNonMaster()
59
    {
60
        $stackManager = $this->getStackManager();
61
62
        $event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent')
63
            ->disableOriginalConstructor()
64
            ->getMock();
65
        $event->expects($this->once())->method('isMasterRequest')->willReturn(false);
66
67
        $subscriber = new RequestSubscriber($stackManager);
68
        $subscriber->onRequest($event);
69
    }
70
71
    public function testOnAuthenticationStart()
72
    {
73
        $stackManager = $this->getStackManager();
74
        $stackManager->expects($this->once())->method('emptyStack');
75
        $stackManager->expects($this->once())->method('addNotSkippedTaskOnce');
76
77
        $session = $this->createMock('Symfony\Component\HttpFoundation\Session\SessionInterface');
78
        $session->expects($this->once())->method('has')->willReturn(true);
79
        $session->expects($this->once())->method('remove');
80
81
        $request = $this->createMock('Symfony\Component\HttpFoundation\Request');
82
        $request->expects($this->once())->method('getSession')->willReturn($session);
83
84
        $event = $this->getMockBuilder('LoginCidadao\TaskStackBundle\Event\EntryPointStartEvent')
85
            ->disableOriginalConstructor()
86
            ->getMock();
87
        $event->expects($this->once())->method('getRequest')->willReturn($request);
88
89
        $subscriber = new RequestSubscriber($stackManager);
90
        $subscriber->onAuthenticationStart($event);
91
    }
92
}
93