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\FacebookBundle\Tests\Action; |
11
|
|
|
|
12
|
|
|
use Core23\FacebookBundle\Action\AuthSuccessAction; |
13
|
|
|
use Core23\FacebookBundle\Core23FacebookEvents; |
14
|
|
|
use Core23\FacebookBundle\Event\AuthSuccessEvent; |
15
|
|
|
use Core23\FacebookBundle\Session\SessionInterface; |
16
|
|
|
use Core23\FacebookBundle\Session\SessionManagerInterface; |
17
|
|
|
use PHPUnit\Framework\TestCase; |
18
|
|
|
use Prophecy\Argument; |
19
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
20
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
21
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
22
|
|
|
use Symfony\Component\Routing\RouterInterface; |
23
|
|
|
use Twig\Environment; |
24
|
|
|
|
25
|
|
|
class AuthSuccessActionTest extends TestCase |
26
|
|
|
{ |
27
|
|
|
private $twig; |
28
|
|
|
|
29
|
|
|
private $router; |
30
|
|
|
|
31
|
|
|
private $sessionManager; |
32
|
|
|
|
33
|
|
|
private $eventDispatcher; |
34
|
|
|
|
35
|
|
View Code Duplication |
protected function setUp(): void |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
$this->twig = $this->prophesize(Environment::class); |
38
|
|
|
$this->router = $this->prophesize(RouterInterface::class); |
39
|
|
|
$this->sessionManager = $this->prophesize(SessionManagerInterface::class); |
40
|
|
|
$this->eventDispatcher = $this->prophesize(EventDispatcherInterface::class); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testExecute(): void |
44
|
|
|
{ |
45
|
|
|
$session = $this->prophesize(SessionInterface::class); |
46
|
|
|
|
47
|
|
|
$this->sessionManager->isAuthenticated() |
48
|
|
|
->willReturn(true) |
49
|
|
|
; |
50
|
|
|
$this->sessionManager->getSession() |
51
|
|
|
->willReturn($session) |
52
|
|
|
; |
53
|
|
|
$this->sessionManager->getUsername() |
54
|
|
|
->willReturn('FooUser') |
55
|
|
|
; |
56
|
|
|
|
57
|
|
|
$this->eventDispatcher->dispatch(Core23FacebookEvents::AUTH_SUCCESS, Argument::type(AuthSuccessEvent::class)) |
58
|
|
|
->shouldBeCalled() |
59
|
|
|
; |
60
|
|
|
|
61
|
|
|
$this->twig->render('@Core23Facebook/Auth/success.html.twig', [ |
62
|
|
|
'name' => 'FooUser', |
63
|
|
|
])->shouldBeCalled(); |
64
|
|
|
|
65
|
|
|
$action = new AuthSuccessAction( |
66
|
|
|
$this->twig->reveal(), |
67
|
|
|
$this->router->reveal(), |
68
|
|
|
$this->sessionManager->reveal(), |
69
|
|
|
$this->eventDispatcher->reveal() |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
$response = $action(); |
73
|
|
|
|
74
|
|
|
$this->assertNotInstanceOf(RedirectResponse::class, $response); |
75
|
|
|
$this->assertSame(200, $response->getStatusCode()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
View Code Duplication |
public function testExecuteNoAuth(): void |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$this->sessionManager->isAuthenticated() |
81
|
|
|
->willReturn(false) |
82
|
|
|
; |
83
|
|
|
|
84
|
|
|
$this->router->generate('core23_facebook_error', [], UrlGeneratorInterface::ABSOLUTE_PATH) |
85
|
|
|
->willReturn('/success') |
86
|
|
|
->shouldBeCalled() |
87
|
|
|
; |
88
|
|
|
|
89
|
|
|
$action = new AuthSuccessAction( |
90
|
|
|
$this->twig->reveal(), |
91
|
|
|
$this->router->reveal(), |
92
|
|
|
$this->sessionManager->reveal(), |
93
|
|
|
$this->eventDispatcher->reveal() |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
$this->assertInstanceOf(RedirectResponse::class, $action()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
View Code Duplication |
public function testExecuteNoSession(): void |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
$this->sessionManager->isAuthenticated() |
102
|
|
|
->willReturn(true) |
103
|
|
|
; |
104
|
|
|
$this->sessionManager->getSession() |
105
|
|
|
->willReturn(null) |
106
|
|
|
; |
107
|
|
|
|
108
|
|
|
$this->router->generate('core23_facebook_error', [], UrlGeneratorInterface::ABSOLUTE_PATH) |
109
|
|
|
->willReturn('/success') |
110
|
|
|
->shouldBeCalled() |
111
|
|
|
; |
112
|
|
|
|
113
|
|
|
$action = new AuthSuccessAction( |
114
|
|
|
$this->twig->reveal(), |
115
|
|
|
$this->router->reveal(), |
116
|
|
|
$this->sessionManager->reveal(), |
117
|
|
|
$this->eventDispatcher->reveal() |
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
$this->assertInstanceOf(RedirectResponse::class, $action()); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.