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\CheckAuthAction; |
13
|
|
|
use Core23\FacebookBundle\Session\SessionInterface; |
14
|
|
|
use Core23\FacebookBundle\Session\SessionManagerInterface; |
15
|
|
|
use DateTime; |
16
|
|
|
use Facebook\Authentication\AccessToken; |
17
|
|
|
use Facebook\Exceptions\FacebookSDKException; |
18
|
|
|
use Facebook\Facebook; |
19
|
|
|
use Facebook\FacebookResponse; |
20
|
|
|
use Facebook\GraphNodes\GraphUser; |
21
|
|
|
use Facebook\Helpers\FacebookRedirectLoginHelper; |
22
|
|
|
use PHPUnit\Framework\TestCase; |
23
|
|
|
use Prophecy\Argument; |
24
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
25
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
26
|
|
|
use Symfony\Component\Routing\RouterInterface; |
27
|
|
|
|
28
|
|
|
class CheckAuthActionTest extends TestCase |
29
|
|
|
{ |
30
|
|
|
private $router; |
31
|
|
|
|
32
|
|
|
private $facebook; |
33
|
|
|
|
34
|
|
|
private $sessionManager; |
35
|
|
|
|
36
|
|
|
private $helper; |
37
|
|
|
|
38
|
|
|
protected function setUp(): void |
39
|
|
|
{ |
40
|
|
|
$this->router = $this->prophesize(RouterInterface::class); |
41
|
|
|
$this->sessionManager = $this->prophesize(SessionManagerInterface::class); |
42
|
|
|
|
43
|
|
|
$this->helper = $this->prophesize(FacebookRedirectLoginHelper::class); |
44
|
|
|
|
45
|
|
|
$this->facebook = $this->prophesize(Facebook::class); |
46
|
|
|
$this->facebook->getRedirectLoginHelper() |
47
|
|
|
->willReturn($this->helper) |
48
|
|
|
; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
View Code Duplication |
public function testExecute(): void |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$accessToken = $this->prepareAccessToken(); |
54
|
|
|
|
55
|
|
|
$graphUser = $this->prophesize(GraphUser::class); |
56
|
|
|
|
57
|
|
|
$response = $this->prophesize(FacebookResponse::class); |
58
|
|
|
$response->getGraphUser() |
59
|
|
|
->willReturn($graphUser) |
60
|
|
|
; |
61
|
|
|
|
62
|
|
|
$this->facebook->get('/me?fields=id,name', $accessToken) |
63
|
|
|
->willReturn($response) |
64
|
|
|
; |
65
|
|
|
|
66
|
|
|
$this->sessionManager->store(Argument::type(SessionInterface::class)) |
67
|
|
|
->shouldBeCalled() |
68
|
|
|
; |
69
|
|
|
|
70
|
|
|
$this->router->generate('core23_facebook_success', [], UrlGeneratorInterface::ABSOLUTE_PATH) |
71
|
|
|
->willReturn('/success') |
72
|
|
|
->shouldBeCalled() |
73
|
|
|
; |
74
|
|
|
|
75
|
|
|
$action = new CheckAuthAction( |
76
|
|
|
$this->router->reveal(), |
77
|
|
|
$this->facebook->reveal(), |
78
|
|
|
$this->sessionManager->reveal() |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
$response = $action(); |
82
|
|
|
|
83
|
|
|
$this->assertSame('/success', $response->getTargetUrl()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
View Code Duplication |
public function testExecuteWithApiError(): void |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
$accessToken = $this->prepareAccessToken(); |
89
|
|
|
|
90
|
|
|
$graphUser = $this->prophesize(GraphUser::class); |
91
|
|
|
|
92
|
|
|
$response = $this->prophesize(FacebookResponse::class); |
93
|
|
|
$response->getGraphUser() |
94
|
|
|
->willReturn($graphUser) |
95
|
|
|
; |
96
|
|
|
|
97
|
|
|
$this->facebook->get('/me?fields=id,name', $accessToken) |
98
|
|
|
->willThrow(FacebookSDKException::class) |
99
|
|
|
; |
100
|
|
|
|
101
|
|
|
$this->router->generate('core23_facebook_error', [], UrlGeneratorInterface::ABSOLUTE_PATH) |
102
|
|
|
->willReturn('/error') |
103
|
|
|
->shouldBeCalled() |
104
|
|
|
; |
105
|
|
|
|
106
|
|
|
$action = new CheckAuthAction( |
107
|
|
|
$this->router->reveal(), |
108
|
|
|
$this->facebook->reveal(), |
109
|
|
|
$this->sessionManager->reveal() |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
$response = $action(); |
113
|
|
|
|
114
|
|
|
$this->assertSame('/error', $response->getTargetUrl()); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testExecuteWithNoAccessToken(): void |
118
|
|
|
{ |
119
|
|
|
$this->helper = $this->prophesize(FacebookRedirectLoginHelper::class); |
120
|
|
|
$this->helper->getAccessToken() |
121
|
|
|
->willReturn(null) |
122
|
|
|
; |
123
|
|
|
|
124
|
|
|
$this->router->generate('core23_facebook_error', [], UrlGeneratorInterface::ABSOLUTE_PATH) |
125
|
|
|
->willReturn('/error') |
126
|
|
|
->shouldBeCalled() |
127
|
|
|
; |
128
|
|
|
|
129
|
|
|
$action = new CheckAuthAction( |
130
|
|
|
$this->router->reveal(), |
131
|
|
|
$this->facebook->reveal(), |
132
|
|
|
$this->sessionManager->reveal() |
133
|
|
|
); |
134
|
|
|
|
135
|
|
|
$response = $action(); |
136
|
|
|
|
137
|
|
|
$this->assertSame('/error', $response->getTargetUrl()); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return ObjectProphecy |
142
|
|
|
*/ |
143
|
|
|
private function prepareAccessToken(): ObjectProphecy |
144
|
|
|
{ |
145
|
|
|
$accessToken = $this->prophesize(AccessToken::class); |
146
|
|
|
$accessToken->getValue() |
147
|
|
|
->willReturn('TOKEN_VALUE') |
148
|
|
|
; |
149
|
|
|
$accessToken->getExpiresAt() |
150
|
|
|
->willReturn(new DateTime('tomorrow')) |
151
|
|
|
; |
152
|
|
|
|
153
|
|
|
$this->helper->getAccessToken() |
154
|
|
|
->willReturn($accessToken) |
155
|
|
|
; |
156
|
|
|
|
157
|
|
|
return $accessToken; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
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.