|
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 Sonata\BlockBundle\Block\BlockContext; |
|
22
|
|
|
use Sonata\BlockBundle\Form\Mapper\FormMapper; |
|
23
|
|
|
use Sonata\BlockBundle\Model\Block; |
|
24
|
|
|
use Sonata\BlockBundle\Model\BlockInterface; |
|
25
|
|
|
use Sonata\BlockBundle\Test\BlockServiceTestCase; |
|
26
|
|
|
|
|
27
|
|
|
final class PageFeedBlockServiceTest extends BlockServiceTestCase |
|
28
|
|
|
{ |
|
29
|
|
|
private $facebook; |
|
30
|
|
|
|
|
31
|
|
|
protected function setUp(): void |
|
32
|
|
|
{ |
|
33
|
|
|
parent::setUp(); |
|
34
|
|
|
|
|
35
|
|
|
$this->facebook = $this->createMock(Facebook::class); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function testExecute(): void |
|
39
|
|
|
{ |
|
40
|
|
|
$token = $this->createMock(AccessToken::class); |
|
41
|
|
|
|
|
42
|
|
|
$app = $this->createMock(FacebookApp::class); |
|
43
|
|
|
$app->expects(static::once())->method('getAccessToken') |
|
44
|
|
|
->willReturn($token) |
|
45
|
|
|
; |
|
46
|
|
|
|
|
47
|
|
|
$this->facebook->expects(static::once())->method('getApp') |
|
48
|
|
|
->willReturn($app) |
|
49
|
|
|
; |
|
50
|
|
|
|
|
51
|
|
|
$feedResponse = [ |
|
52
|
|
|
['foo' => 'bar'], |
|
53
|
|
|
]; |
|
54
|
|
|
|
|
55
|
|
|
$edge = $this->createMock(GraphEdge::class); |
|
56
|
|
|
$edge->expects(static::once())->method('asArray') |
|
57
|
|
|
->willReturn($feedResponse) |
|
58
|
|
|
; |
|
59
|
|
|
|
|
60
|
|
|
$response = $this->createMock(FacebookResponse::class); |
|
61
|
|
|
$response->expects(static::once())->method('getGraphEdge') |
|
62
|
|
|
->willReturn($edge) |
|
63
|
|
|
; |
|
64
|
|
|
|
|
65
|
|
|
$this->facebook->method('get') |
|
66
|
|
|
->with(static::equalTo('/0815/feed?fields=type,message,description,permalink_url,picture,created_time'), static::equalTo($token)) |
|
67
|
|
|
->willReturn($response) |
|
68
|
|
|
; |
|
69
|
|
|
|
|
70
|
|
|
$block = new Block(); |
|
71
|
|
|
|
|
72
|
|
|
$blockContext = new BlockContext($block, [ |
|
73
|
|
|
'title' => null, |
|
74
|
|
|
'translation_domain' => null, |
|
75
|
|
|
'template' => '@Core23Facebook/Block/block_page_feed.html.twig', |
|
76
|
|
|
'id' => '0815', |
|
77
|
|
|
'fields' => 'type,message,description,permalink_url,picture,created_time', |
|
78
|
|
|
]); |
|
79
|
|
|
|
|
80
|
|
|
$blockService = new PageFeedBlockService('block.service', $this->templating, $this->facebook); |
|
|
|
|
|
|
81
|
|
|
$blockService->execute($blockContext); |
|
82
|
|
|
|
|
83
|
|
|
static::assertSame('@Core23Facebook/Block/block_page_feed.html.twig', $this->templating->view); |
|
84
|
|
|
|
|
85
|
|
|
static::assertSame($blockContext, $this->templating->parameters['context']); |
|
86
|
|
|
static::assertIsArray($this->templating->parameters['settings']); |
|
87
|
|
|
static::assertInstanceOf(BlockInterface::class, $this->templating->parameters['block']); |
|
88
|
|
|
|
|
89
|
|
|
static::assertSame($feedResponse, $this->templating->parameters['feed']); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function testExecuteThrowsFacebookException(): void |
|
93
|
|
|
{ |
|
94
|
|
|
$token = $this->createMock(AccessToken::class); |
|
95
|
|
|
|
|
96
|
|
|
$app = $this->createMock(FacebookApp::class); |
|
97
|
|
|
$app->expects(static::once())->method('getAccessToken') |
|
98
|
|
|
->willReturn($token) |
|
99
|
|
|
; |
|
100
|
|
|
|
|
101
|
|
|
$this->facebook->expects(static::once())->method('getApp') |
|
102
|
|
|
->willReturn($app) |
|
103
|
|
|
; |
|
104
|
|
|
|
|
105
|
|
|
$this->facebook->method('get') |
|
106
|
|
|
->with(static::equalTo('/0815/feed?fields=type,message,description,permalink_url,picture,created_time'), static::equalTo($token)) |
|
107
|
|
|
->willThrowException(new FacebookSDKException()) |
|
108
|
|
|
; |
|
109
|
|
|
|
|
110
|
|
|
$block = new Block(); |
|
111
|
|
|
|
|
112
|
|
|
$blockContext = new BlockContext($block, [ |
|
113
|
|
|
'title' => null, |
|
114
|
|
|
'translation_domain' => null, |
|
115
|
|
|
'template' => '@Core23Facebook/Block/block_page_feed.html.twig', |
|
116
|
|
|
'id' => '0815', |
|
117
|
|
|
'fields' => 'type,message,description,permalink_url,picture,created_time', |
|
118
|
|
|
]); |
|
119
|
|
|
|
|
120
|
|
|
$blockService = new PageFeedBlockService('block.service', $this->templating, $this->facebook); |
|
|
|
|
|
|
121
|
|
|
$blockService->execute($blockContext); |
|
122
|
|
|
|
|
123
|
|
|
static::assertSame('@Core23Facebook/Block/block_page_feed.html.twig', $this->templating->view); |
|
124
|
|
|
|
|
125
|
|
|
static::assertSame($blockContext, $this->templating->parameters['context']); |
|
126
|
|
|
static::assertIsArray($this->templating->parameters['settings']); |
|
127
|
|
|
static::assertInstanceOf(BlockInterface::class, $this->templating->parameters['block']); |
|
128
|
|
|
|
|
129
|
|
|
static::assertSame([], $this->templating->parameters['feed']); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public function testDefaultSettings(): void |
|
133
|
|
|
{ |
|
134
|
|
|
$blockService = new PageFeedBlockService('block.service', $this->templating, $this->facebook); |
|
|
|
|
|
|
135
|
|
|
$blockContext = $this->getBlockContext($blockService); |
|
136
|
|
|
|
|
137
|
|
|
$this->assertSettings([ |
|
138
|
|
|
'title' => null, |
|
139
|
|
|
'translation_domain' => null, |
|
140
|
|
|
'icon' => 'fa fa-facebook-official', |
|
141
|
|
|
'class' => null, |
|
142
|
|
|
'id' => null, |
|
143
|
|
|
'limit' => 10, |
|
144
|
|
|
'fields' => 'type,message,description,permalink_url,picture,created_time', |
|
145
|
|
|
'template' => '@Core23Facebook/Block/block_page_feed.html.twig', |
|
146
|
|
|
], $blockContext); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
public function testGetMetadata(): void |
|
150
|
|
|
{ |
|
151
|
|
|
$blockService = new PageFeedBlockService('block.service', $this->templating, $this->facebook); |
|
|
|
|
|
|
152
|
|
|
|
|
153
|
|
|
$metadata = $blockService->getMetadata(); |
|
154
|
|
|
|
|
155
|
|
|
static::assertSame('core23_facebook.block.page_feed', $metadata->getTitle()); |
|
156
|
|
|
static::assertNull($metadata->getImage()); |
|
157
|
|
|
static::assertSame('Core23FacebookBundle', $metadata->getDomain()); |
|
158
|
|
|
static::assertSame([ |
|
159
|
|
|
'class' => 'fa fa-facebook-official', |
|
160
|
|
|
], $metadata->getOptions()); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
public function testConfigureEditForm(): void |
|
164
|
|
|
{ |
|
165
|
|
|
$blockService = new PageFeedBlockService('block.service', $this->templating, $this->facebook); |
|
|
|
|
|
|
166
|
|
|
|
|
167
|
|
|
$block = new Block(); |
|
168
|
|
|
|
|
169
|
|
|
$formMapper = $this->createMock(FormMapper::class); |
|
170
|
|
|
$formMapper->expects(static::once())->method('add'); |
|
171
|
|
|
|
|
172
|
|
|
$blockService->configureEditForm($formMapper, $block); |
|
|
|
|
|
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|
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: