-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
AuthErrorActionTest.php
120 lines (95 loc) 路 3.42 KB
/
AuthErrorActionTest.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
/*
* (c) Christian Gripp <mail@core23.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Core23\LastFmBundle\Tests\Action;
use Core23\LastFmBundle\Action\AuthErrorAction;
use Core23\LastFmBundle\Core23LastFmEvents;
use Core23\LastFmBundle\Event\AuthFailedEvent;
use Core23\LastFmBundle\Session\SessionManagerInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Twig\Environment;
class AuthErrorActionTest extends TestCase
{
private $twig;
private $router;
private $sessionManager;
private $eventDispatcher;
protected function setUp(): void
{
$this->twig = $this->prophesize(Environment::class);
$this->router = $this->prophesize(RouterInterface::class);
$this->sessionManager = $this->prophesize(SessionManagerInterface::class);
$this->eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
}
public function testExecute(): void
{
$this->sessionManager->isAuthenticated()
->willReturn(false)
;
$this->sessionManager->clear()
->shouldBeCalled()
;
$this->eventDispatcher->dispatch(Argument::type(AuthFailedEvent::class), Core23LastFmEvents::AUTH_ERROR)
->shouldBeCalled()
;
$action = new AuthErrorAction(
$this->twig->reveal(),
$this->router->reveal(),
$this->sessionManager->reveal(),
$this->eventDispatcher->reveal()
);
$response = $action();
static::assertNotInstanceOf(RedirectResponse::class, $response);
static::assertSame(200, $response->getStatusCode());
}
public function testExecuteWithCaughtEvent(): void
{
$this->sessionManager->isAuthenticated()
->willReturn(false)
;
$this->sessionManager->clear()
->shouldBeCalled()
;
$eventResponse = new Response();
$this->eventDispatcher->dispatch(Argument::type(AuthFailedEvent::class), Core23LastFmEvents::AUTH_ERROR)
->will(function ($args) use ($eventResponse) {
$args[1]->setResponse($eventResponse);
})
;
$action = new AuthErrorAction(
$this->twig->reveal(),
$this->router->reveal(),
$this->sessionManager->reveal(),
$this->eventDispatcher->reveal()
);
$response = $action();
static::assertSame($eventResponse, $response);
}
public function testExecuteWithNoAuth(): void
{
$this->sessionManager->isAuthenticated()
->willReturn(true)
;
$this->router->generate('core23_lastfm_success', [], UrlGeneratorInterface::ABSOLUTE_PATH)
->willReturn('/success')
->shouldBeCalled()
;
$action = new AuthErrorAction(
$this->twig->reveal(),
$this->router->reveal(),
$this->sessionManager->reveal(),
$this->eventDispatcher->reveal()
);
static::assertInstanceOf(RedirectResponse::class, $action());
}
}