Completed
Push — feature/update_all_the_things ( 324e2a...b9ab83 )
by Lucas
30:14 queued 13:01
created

testOnAuthenticationSuccess()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
c 2
b 2
f 0
dl 0
loc 35
rs 8.8571
cc 1
eloc 27
nc 1
nop 0
1
<?php
2
/**
3
 * Validates the behavior of the AuthenticationLogger event listener.
4
 */
5
namespace Graviton\SecurityBundle\Tests\Listener;
6
7
use Graviton\SecurityBundle\Listener\AuthenticationLogger;
8
9
/**
10
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
11
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
12
 * @link     http://swisscom.ch
13
 */
14
class AuthenticationLoggerTest extends \PHPUnit_Framework_TestCase
15
{
16
    /** @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject logger */
17
    private $logger;
18
19
    /**
20
     * @return void
21
     */
22
    protected function setUp()
23
    {
24
        /** @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject logger */
25
        $this->logger = $this->getMockBuilder('\Psr\Log\LoggerInterface')
26
            ->setMethods(array('warning', 'info'))
27
            ->getMockForAbstractClass();
28
    }
29
30
    /**
31
     * @return void
32
     */
33
    public function testOnAuthenticationFailure()
34
    {
35
        $exceptionDouble = $this->getMockBuilder('\Symfony\Component\Security\Core\Exception\AuthenticationException')
36
            ->disableOriginalConstructor()
37
            ->setMethods(array('getMessageKey'))
38
            ->getMock();
39
        $exceptionDouble
40
            ->expects($this->once())
41
            ->method('getMessageKey')
42
            ->will($this->returnValue('An authentication exception occurred.'));
43
44
        $eventDouble = $this->getMockBuilder('\Symfony\Component\Security\Core\Event\AuthenticationFailureEvent')
45
            ->disableOriginalConstructor()
46
            ->setMethods(array('getAuthenticationException'))
47
            ->getMock();
48
        $eventDouble
49
            ->expects($this->once())
50
            ->method('getAuthenticationException')
51
            ->willReturn($exceptionDouble);
52
53
        $this->logger
54
            ->expects($this->once())
55
            ->method('warning')
56
            ->with(
57
                $this->equalTo('An authentication exception occurred.'),
58
                $this->isType('array')
59
            );
60
61
        $logger = new AuthenticationLogger($this->logger);
62
        $logger->onAuthenticationFailure($eventDouble);
63
    }
64
65
    /**
66
     * @return void
67
     */
68
    public function testOnAuthenticationSuccess()
69
    {
70
        $this->logger
71
            ->expects($this->once())
72
            ->method('info')
73
            ->with($this->equalTo('Entity (Jon Doe) was successfully recognized.'));
74
75
        $tokenDouble = $this->getMockBuilder('\Symfony\Component\Security\Core\Authentication\Token\TokenInterface')
76
            ->setMethods(array('getUsername'))
77
            ->getMockForAbstractClass();
78
        $tokenDouble
79
            ->expects($this->once())
80
            ->method('getUsername')
81
            ->will($this->returnValue('Jon Doe'));
82
83
        $eventDouble = $this->getMockBuilder('\Symfony\Component\Security\Core\Event\AuthenticationEvent')
84
            ->disableOriginalConstructor()
85
            ->setMethods(array('getAuthenticationToken'))
86
            ->getMock();
87
        $eventDouble
88
            ->expects($this->once())
89
            ->method('getAuthenticationToken')
90
            ->willReturn($tokenDouble);
91
92
        $this->logger
93
            ->expects($this->once())
94
            ->method('info')
95
            ->with(
96
                $this->equalTo('Entity (Jon Doe) was successfully recognized.')
97
            );
98
99
        $logger = new AuthenticationLogger($this->logger);
100
101
        $logger->onAuthenticationSuccess($eventDouble);
102
    }
103
104
    /**
105
     * @return void
106
     */
107
    public function testOnAuthenticationSuccessNonContract()
108
    {
109
        $tokenDouble = $this->getMockBuilder('\Symfony\Component\Security\Core\Authentication\Token\TokenInterface')
110
                            ->setMethods(array('getUser'))
111
                            ->getMockForAbstractClass();
112
        $tokenDouble
113
            ->expects($this->once())
114
            ->method('getUsername')
115
            ->willReturn('johnny');
116
117
        $eventDouble = $this->getMockBuilder('\Symfony\Component\Security\Core\Event\AuthenticationEvent')
118
                            ->disableOriginalConstructor()
119
                            ->setMethods(array('getAuthenticationToken'))
120
                            ->getMock();
121
        $eventDouble
122
            ->expects($this->once())
123
            ->method('getAuthenticationToken')
124
            ->willReturn($tokenDouble);
125
126
        $this->logger
127
            ->expects($this->once())
128
            ->method('info')
129
            ->with(
130
                $this->equalTo('Entity (johnny) was successfully recognized.')
131
            );
132
133
        $logger = new AuthenticationLogger($this->logger);
134
135
        $logger->onAuthenticationSuccess($eventDouble);
136
    }
137
}
138