1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* (c) Christian Gripp <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Core23\FacebookBundle\Tests\Block\Service; |
13
|
|
|
|
14
|
|
|
use Core23\FacebookBundle\Block\Service\PageFeedBlockService; |
15
|
|
|
use Facebook\Authentication\AccessToken; |
16
|
|
|
use Facebook\Exceptions\FacebookSDKException; |
17
|
|
|
use Facebook\Facebook; |
18
|
|
|
use Facebook\FacebookApp; |
19
|
|
|
use Facebook\FacebookResponse; |
20
|
|
|
use Facebook\GraphNodes\GraphEdge; |
21
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
22
|
|
|
use Sonata\AdminBundle\Form\FormMapper; |
23
|
|
|
use Sonata\BlockBundle\Block\BlockContext; |
24
|
|
|
use Sonata\BlockBundle\Model\Block; |
25
|
|
|
use Sonata\BlockBundle\Model\BlockInterface; |
26
|
|
|
use Sonata\BlockBundle\Test\AbstractBlockServiceTestCase; |
27
|
|
|
|
28
|
|
|
final class PageFeedBlockServiceTest extends AbstractBlockServiceTestCase |
29
|
|
|
{ |
30
|
|
|
private $facebook; |
31
|
|
|
|
32
|
|
|
protected function setUp(): void |
33
|
|
|
{ |
34
|
|
|
parent::setUp(); |
35
|
|
|
|
36
|
|
|
$this->facebook = $this->createMock(Facebook::class); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testExecute(): void |
40
|
|
|
{ |
41
|
|
|
$token = $this->createMock(AccessToken::class); |
42
|
|
|
|
43
|
|
|
$app = $this->createMock(FacebookApp::class); |
44
|
|
|
$app->expects($this->once())->method('getAccessToken') |
45
|
|
|
->willReturn($token) |
46
|
|
|
; |
47
|
|
|
|
48
|
|
|
$this->facebook->expects($this->once())->method('getApp') |
49
|
|
|
->willReturn($app) |
50
|
|
|
; |
51
|
|
|
|
52
|
|
|
$feedResponse = [ |
53
|
|
|
['foo' => 'bar'], |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
$edge = $this->createMock(GraphEdge::class); |
57
|
|
|
$edge->expects($this->once())->method('asArray') |
58
|
|
|
->willReturn($feedResponse) |
59
|
|
|
; |
60
|
|
|
|
61
|
|
|
$response = $this->createMock(FacebookResponse::class); |
62
|
|
|
$response->expects($this->once())->method('getGraphEdge') |
63
|
|
|
->willReturn($edge) |
64
|
|
|
; |
65
|
|
|
|
66
|
|
|
$this->facebook->method('get') |
67
|
|
|
->with($this->equalTo('/0815/feed?fields=type,message,description,permalink_url,picture,created_time'), $this->equalTo($token)) |
68
|
|
|
->willReturn($response) |
69
|
|
|
; |
70
|
|
|
|
71
|
|
|
$block = new Block(); |
72
|
|
|
|
73
|
|
|
$blockContext = new BlockContext($block, [ |
74
|
|
|
'title' => null, |
75
|
|
|
'translation_domain' => null, |
76
|
|
|
'template' => '@Core23Facebook/Block/block_page_feed.html.twig', |
77
|
|
|
'id' => '0815', |
78
|
|
|
'fields' => 'type,message,description,permalink_url,picture,created_time', |
79
|
|
|
]); |
80
|
|
|
|
81
|
|
|
$blockService = new PageFeedBlockService('block.service', $this->templating, $this->facebook); |
|
|
|
|
82
|
|
|
$blockService->execute($blockContext); |
83
|
|
|
|
84
|
|
|
$this->assertSame('@Core23Facebook/Block/block_page_feed.html.twig', $this->templating->view); |
85
|
|
|
|
86
|
|
|
$this->assertSame($blockContext, $this->templating->parameters['context']); |
87
|
|
|
$this->assertInternalType('array', $this->templating->parameters['settings']); |
|
|
|
|
88
|
|
|
$this->assertInstanceOf(BlockInterface::class, $this->templating->parameters['block']); |
89
|
|
|
|
90
|
|
|
$this->assertSame($feedResponse, $this->templating->parameters['feed']); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testExecuteThrowsFacebookException(): void |
94
|
|
|
{ |
95
|
|
|
$token = $this->createMock(AccessToken::class); |
96
|
|
|
|
97
|
|
|
$app = $this->createMock(FacebookApp::class); |
98
|
|
|
$app->expects($this->once())->method('getAccessToken') |
99
|
|
|
->willReturn($token) |
100
|
|
|
; |
101
|
|
|
|
102
|
|
|
$this->facebook->expects($this->once())->method('getApp') |
103
|
|
|
->willReturn($app) |
104
|
|
|
; |
105
|
|
|
|
106
|
|
|
$this->facebook->method('get') |
107
|
|
|
->with($this->equalTo('/0815/feed?fields=type,message,description,permalink_url,picture,created_time'), $this->equalTo($token)) |
108
|
|
|
->willThrowException(new FacebookSDKException()) |
109
|
|
|
; |
110
|
|
|
|
111
|
|
|
$block = new Block(); |
112
|
|
|
|
113
|
|
|
$blockContext = new BlockContext($block, [ |
114
|
|
|
'title' => null, |
115
|
|
|
'translation_domain' => null, |
116
|
|
|
'template' => '@Core23Facebook/Block/block_page_feed.html.twig', |
117
|
|
|
'id' => '0815', |
118
|
|
|
'fields' => 'type,message,description,permalink_url,picture,created_time', |
119
|
|
|
]); |
120
|
|
|
|
121
|
|
|
$blockService = new PageFeedBlockService('block.service', $this->templating, $this->facebook); |
|
|
|
|
122
|
|
|
$blockService->execute($blockContext); |
123
|
|
|
|
124
|
|
|
$this->assertSame('@Core23Facebook/Block/block_page_feed.html.twig', $this->templating->view); |
125
|
|
|
|
126
|
|
|
$this->assertSame($blockContext, $this->templating->parameters['context']); |
127
|
|
|
$this->assertInternalType('array', $this->templating->parameters['settings']); |
|
|
|
|
128
|
|
|
$this->assertInstanceOf(BlockInterface::class, $this->templating->parameters['block']); |
129
|
|
|
|
130
|
|
|
$this->assertSame([], $this->templating->parameters['feed']); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function testDefaultSettings(): void |
134
|
|
|
{ |
135
|
|
|
$blockService = new PageFeedBlockService('block.service', $this->templating, $this->facebook); |
|
|
|
|
136
|
|
|
$blockContext = $this->getBlockContext($blockService); |
137
|
|
|
|
138
|
|
|
$this->assertSettings([ |
139
|
|
|
'title' => null, |
140
|
|
|
'translation_domain' => null, |
141
|
|
|
'icon' => 'fa fa-facebook-official', |
142
|
|
|
'class' => null, |
143
|
|
|
'id' => null, |
144
|
|
|
'limit' => 10, |
145
|
|
|
'fields' => 'type,message,description,permalink_url,picture,created_time', |
146
|
|
|
'template' => '@Core23Facebook/Block/block_page_feed.html.twig', |
147
|
|
|
], $blockContext); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function testGetBlockMetadata(): void |
151
|
|
|
{ |
152
|
|
|
$blockService = new PageFeedBlockService('block.service', $this->templating, $this->facebook); |
|
|
|
|
153
|
|
|
|
154
|
|
|
$metadata = $blockService->getBlockMetadata('description'); |
155
|
|
|
|
156
|
|
|
$this->assertSame('block.service', $metadata->getTitle()); |
157
|
|
|
$this->assertSame('description', $metadata->getDescription()); |
158
|
|
|
$this->assertNotNull($metadata->getImage()); |
159
|
|
|
$this->assertStringStartsWith('data:image/png;base64,', $metadata->getImage() ?? ''); |
160
|
|
|
$this->assertSame('Core23FacebookBundle', $metadata->getDomain()); |
161
|
|
|
$this->assertSame([ |
162
|
|
|
'class' => 'fa fa-facebook-official', |
163
|
|
|
], $metadata->getOptions()); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function testBuildEditForm(): void |
167
|
|
|
{ |
168
|
|
|
$blockService = new PageFeedBlockService('block.service', $this->templating, $this->facebook); |
|
|
|
|
169
|
|
|
|
170
|
|
|
$block = new Block(); |
171
|
|
|
|
172
|
|
|
/** @var MockObject&FormMapper $formMapper */ |
|
|
|
|
173
|
|
|
$formMapper = $this->createMock(FormMapper::class); |
174
|
|
|
$formMapper->expects($this->once())->method('add'); |
175
|
|
|
|
176
|
|
|
$blockService->buildEditForm($formMapper, $block); |
|
|
|
|
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: