1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* (c) Christian Gripp <[email protected]> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Core23\LastFmBundle\Tests\Action; |
11
|
|
|
|
12
|
|
|
use Core23\LastFm\Service\AuthServiceInterface; |
13
|
|
|
use Core23\LastFm\Session\SessionInterface; |
14
|
|
|
use Core23\LastFmBundle\Action\CheckAuthAction; |
15
|
|
|
use Core23\LastFmBundle\Session\SessionManagerInterface; |
16
|
|
|
use PHPUnit\Framework\TestCase; |
17
|
|
|
use Prophecy\Argument; |
18
|
|
|
use Symfony\Component\HttpFoundation\Request; |
19
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
20
|
|
|
use Symfony\Component\Routing\RouterInterface; |
21
|
|
|
|
22
|
|
|
class CheckAuthActionTest extends TestCase |
23
|
|
|
{ |
24
|
|
|
private $router; |
25
|
|
|
|
26
|
|
|
private $sessionManager; |
27
|
|
|
|
28
|
|
|
private $authService; |
29
|
|
|
|
30
|
|
|
protected function setUp(): void |
31
|
|
|
{ |
32
|
|
|
$this->router = $this->prophesize(RouterInterface::class); |
33
|
|
|
$this->sessionManager = $this->prophesize(SessionManagerInterface::class); |
34
|
|
|
$this->authService = $this->prophesize(AuthServiceInterface::class); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testExecute(): void |
38
|
|
|
{ |
39
|
|
|
$this->sessionManager->store(Argument::type(SessionInterface::class)) |
40
|
|
|
->shouldBeCalled() |
41
|
|
|
; |
42
|
|
|
|
43
|
|
|
$this->router->generate('core23_lastfm_success', [], UrlGeneratorInterface::ABSOLUTE_PATH) |
44
|
|
|
->willReturn('/success') |
45
|
|
|
; |
46
|
|
|
|
47
|
|
|
$this->authService->createSession('MY_TOKEN') |
48
|
|
|
->willReturn($this->prophesize(SessionInterface::class)) |
49
|
|
|
; |
50
|
|
|
|
51
|
|
|
$request = new Request(); |
52
|
|
|
$request->query->set('token', 'MY_TOKEN'); |
53
|
|
|
|
54
|
|
|
$action = new CheckAuthAction( |
55
|
|
|
$this->router->reveal(), |
56
|
|
|
$this->sessionManager->reveal(), |
57
|
|
|
$this->authService->reveal() |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
$response = $action($request); |
61
|
|
|
|
62
|
|
|
$this->assertSame('/success', $response->getTargetUrl()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testExecuteWithNoToken(): void |
66
|
|
|
{ |
67
|
|
|
$this->router->generate('core23_lastfm_auth', [], UrlGeneratorInterface::ABSOLUTE_PATH) |
68
|
|
|
->willReturn('/auth') |
69
|
|
|
; |
70
|
|
|
|
71
|
|
|
$request = new Request(); |
72
|
|
|
$request->query->set('token', null); |
73
|
|
|
|
74
|
|
|
$action = new CheckAuthAction( |
75
|
|
|
$this->router->reveal(), |
76
|
|
|
$this->sessionManager->reveal(), |
77
|
|
|
$this->authService->reveal() |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
$response = $action($request); |
81
|
|
|
|
82
|
|
|
$this->assertSame('/auth', $response->getTargetUrl()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testExecuteWithNoSession(): void |
86
|
|
|
{ |
87
|
|
|
$this->router->generate('core23_lastfm_error', [], UrlGeneratorInterface::ABSOLUTE_PATH) |
88
|
|
|
->willReturn('/error') |
89
|
|
|
; |
90
|
|
|
$this->authService->createSession('MY_TOKEN') |
91
|
|
|
->willReturn(null) |
92
|
|
|
; |
93
|
|
|
|
94
|
|
|
$request = new Request(); |
95
|
|
|
$request->query->set('token', 'MY_TOKEN'); |
96
|
|
|
|
97
|
|
|
$action = new CheckAuthAction( |
98
|
|
|
$this->router->reveal(), |
99
|
|
|
$this->sessionManager->reveal(), |
100
|
|
|
$this->authService->reveal() |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
$response = $action($request); |
104
|
|
|
|
105
|
|
|
$this->assertSame('/error', $response->getTargetUrl()); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|