-
Notifications
You must be signed in to change notification settings - Fork 91
/
AuthenticatorTest.php
79 lines (60 loc) · 3.66 KB
/
AuthenticatorTest.php
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
<?php
namespace Tests\FrameworkBundle\Unit\Model\Security;
use PHPUnit\Framework\TestCase;
use Shopsys\FrameworkBundle\Model\Security\Authenticator;
use stdClass;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class AuthenticatorTest extends TestCase
{
public function testCheckLoginProcessWithRequestError()
{
$authenticator = $this->getAuthenticator();
/** @var \Symfony\Component\HttpFoundation\Request|\PHPUnit\Framework\MockObject\MockObject $requestMock */
$requestMock = $this->createMock('\Symfony\Component\HttpFoundation\Request');
$requestMock->expects($this->never())->method('getSession');
$requestMock->attributes = $this->createMock('\Symfony\Component\HttpFoundation\ParameterBag');
$requestMock->attributes->expects($this->once())->method('has')->willReturn(true);
$requestMock->attributes->expects($this->once())->method('get')->willReturn(new stdClass());
$this->expectException('Shopsys\FrameworkBundle\Model\Security\Exception\LoginFailedException');
$authenticator->checkLoginProcess($requestMock);
}
public function testCheckLoginProcessWithSessionError()
{
$authenticator = $this->getAuthenticator();
$sessionMock = $this->createMock('\Symfony\Component\HttpFoundation\Session\SessionInterface');
$sessionMock->expects($this->atLeastOnce())->method('get')->willReturn(new stdClass());
$sessionMock->expects($this->atLeastOnce())->method('remove');
/** @var \Symfony\Component\HttpFoundation\Request|\PHPUnit\Framework\MockObject\MockObject $requestMock */
$requestMock = $this->createMock('\Symfony\Component\HttpFoundation\Request');
$requestMock->expects($this->once())->method('getSession')->willReturn($sessionMock);
$requestMock->attributes = $this->createMock('\Symfony\Component\HttpFoundation\ParameterBag');
$requestMock->attributes->expects($this->once())->method('has')->willReturn(false);
$requestMock->attributes->expects($this->never())->method('get');
$this->expectException('Shopsys\FrameworkBundle\Model\Security\Exception\LoginFailedException');
$authenticator->checkLoginProcess($requestMock);
}
public function testCheckLoginProcessWithoutSessionError()
{
$authenticator = $this->getAuthenticator();
$sessionMock = $this->createMock('\Symfony\Component\HttpFoundation\Session\SessionInterface');
$sessionMock->expects($this->once())->method('get')->willReturn(null);
$sessionMock->expects($this->once())->method('remove');
/** @var \Symfony\Component\HttpFoundation\Request|\PHPUnit\Framework\MockObject\MockObject $requestMock */
$requestMock = $this->createMock('\Symfony\Component\HttpFoundation\Request');
$requestMock->expects($this->once())->method('getSession')->willReturn($sessionMock);
$requestMock->attributes = $this->createMock('\Symfony\Component\HttpFoundation\ParameterBag');
$requestMock->attributes->expects($this->once())->method('has')->willReturn(false);
$requestMock->attributes->expects($this->never())->method('get');
$this->assertSame(true, $authenticator->checkLoginProcess($requestMock));
}
/**
* @return \Shopsys\FrameworkBundle\Model\Security\Authenticator
*/
private function getAuthenticator()
{
$tokenStorageMock = $this->createMock(TokenStorageInterface::class);
$eventDispatcherMock = $this->createMock(EventDispatcherInterface::class);
return new Authenticator($tokenStorageMock, $eventDispatcherMock);
}
}