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\MatomoBundle\Tests\Block\Service; |
13
|
|
|
|
14
|
|
|
use Core23\MatomoBundle\Block\Service\MatomoStatisticBlockService; |
15
|
|
|
use Core23\MatomoBundle\Client\ClientFactoryInterface; |
16
|
|
|
use Core23\MatomoBundle\Client\ClientInterface; |
17
|
|
|
use Core23\MatomoBundle\Exception\MatomoException; |
18
|
|
|
use Psr\Log\LoggerInterface; |
19
|
|
|
use Sonata\AdminBundle\Form\FormMapper; |
20
|
|
|
use Sonata\BlockBundle\Block\BlockContext; |
21
|
|
|
use Sonata\BlockBundle\Model\Block; |
22
|
|
|
use Sonata\BlockBundle\Model\BlockInterface; |
23
|
|
|
use Sonata\BlockBundle\Test\AbstractBlockServiceTestCase; |
24
|
|
|
|
25
|
|
|
final class MatomoStatisticBlockServiceTest extends AbstractBlockServiceTestCase |
26
|
|
|
{ |
27
|
|
|
private $logger; |
28
|
|
|
|
29
|
|
|
private $factory; |
30
|
|
|
|
31
|
|
|
protected function setUp(): void |
32
|
|
|
{ |
33
|
|
|
parent::setUp(); |
34
|
|
|
|
35
|
|
|
$this->logger = $this->createMock(LoggerInterface::class); |
36
|
|
|
$this->factory = $this->createMock(ClientFactoryInterface::class); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testExecute(): void |
40
|
|
|
{ |
41
|
|
|
$client = $this->createMock(ClientInterface::class); |
42
|
|
|
$client->expects($this->once())->method('call') |
43
|
|
|
->with($this->equalTo('VisitsSummary.getVisits'), $this->equalTo([ |
44
|
|
|
'idSite' => 'foo', |
45
|
|
|
'period' => 'day', |
46
|
|
|
'date' => 'last30', |
47
|
|
|
])) |
48
|
|
|
->willReturn(['bar']) |
49
|
|
|
; |
50
|
|
|
|
51
|
|
|
$this->factory->expects($this->once())->method('createClient') |
52
|
|
|
->willReturn($client) |
53
|
|
|
; |
54
|
|
|
|
55
|
|
|
$block = new Block(); |
56
|
|
|
|
57
|
|
|
$blockContext = new BlockContext($block, [ |
58
|
|
|
'title' => null, |
59
|
|
|
'site' => 'foo', |
60
|
|
|
'method' => 'VisitsSummary.getVisits', |
61
|
|
|
'host' => 'localhost', |
62
|
|
|
'token' => '0815', |
63
|
|
|
'period' => 'day', |
64
|
|
|
'date' => 'last30', |
65
|
|
|
'template' => '@Core23Matomo/Block/block_matomo_statistic.html.twig', |
66
|
|
|
]); |
67
|
|
|
|
68
|
|
|
$blockService = new MatomoStatisticBlockService('block.service', $this->templating, $this->factory); |
|
|
|
|
69
|
|
|
$blockService->execute($blockContext); |
70
|
|
|
|
71
|
|
|
$this->assertSame('@Core23Matomo/Block/block_matomo_statistic.html.twig', $this->templating->view); |
72
|
|
|
|
73
|
|
|
$this->assertSame($blockContext, $this->templating->parameters['context']); |
74
|
|
|
$this->assertInternalType('array', $this->templating->parameters['settings']); |
|
|
|
|
75
|
|
|
$this->assertInstanceOf(BlockInterface::class, $this->templating->parameters['block']); |
76
|
|
|
$this->assertSame(['bar'], $this->templating->parameters['data']); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testExecuteOffline(): void |
80
|
|
|
{ |
81
|
|
|
$client = $this->createMock(ClientInterface::class); |
82
|
|
|
$client->expects($this->once())->method('call') |
83
|
|
|
->with($this->equalTo('VisitsSummary.getVisits'), $this->equalTo([ |
84
|
|
|
'idSite' => 'foo', |
85
|
|
|
'period' => 'day', |
86
|
|
|
'date' => 'last30', |
87
|
|
|
])) |
88
|
|
|
->willThrowException(new MatomoException('Offline')) |
89
|
|
|
; |
90
|
|
|
|
91
|
|
|
$this->factory->expects($this->once())->method('createClient') |
92
|
|
|
->willReturn($client) |
93
|
|
|
; |
94
|
|
|
|
95
|
|
|
$this->logger->expects($this->once())->method('warning'); |
96
|
|
|
|
97
|
|
|
$block = new Block(); |
98
|
|
|
|
99
|
|
|
$blockContext = new BlockContext($block, [ |
100
|
|
|
'title' => null, |
101
|
|
|
'site' => 'foo', |
102
|
|
|
'method' => 'VisitsSummary.getVisits', |
103
|
|
|
'host' => 'localhost', |
104
|
|
|
'token' => '0815', |
105
|
|
|
'period' => 'day', |
106
|
|
|
'date' => 'last30', |
107
|
|
|
'template' => '@Core23Matomo/Block/block_matomo_statistic.html.twig', |
108
|
|
|
]); |
109
|
|
|
|
110
|
|
|
$blockService = new MatomoStatisticBlockService('block.service', $this->templating, $this->factory); |
|
|
|
|
111
|
|
|
$blockService->setLogger($this->logger); |
|
|
|
|
112
|
|
|
$blockService->execute($blockContext); |
113
|
|
|
|
114
|
|
|
$this->assertSame('@Core23Matomo/Block/block_matomo_statistic.html.twig', $this->templating->view); |
115
|
|
|
|
116
|
|
|
$this->assertSame($blockContext, $this->templating->parameters['context']); |
117
|
|
|
$this->assertInternalType('array', $this->templating->parameters['settings']); |
|
|
|
|
118
|
|
|
$this->assertInstanceOf(BlockInterface::class, $this->templating->parameters['block']); |
119
|
|
|
$this->assertNull($this->templating->parameters['data']); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function testDefaultSettings(): void |
123
|
|
|
{ |
124
|
|
|
$blockService = new MatomoStatisticBlockService('block.service', $this->templating, $this->factory); |
|
|
|
|
125
|
|
|
$blockService->setLogger($this->logger); |
|
|
|
|
126
|
|
|
$blockContext = $this->getBlockContext($blockService); |
127
|
|
|
|
128
|
|
|
$this->assertSettings([ |
129
|
|
|
'title' => null, |
130
|
|
|
'translation_domain' => null, |
131
|
|
|
'icon' => 'fa fa-bar-chart-o', |
132
|
|
|
'class' => null, |
133
|
|
|
'site' => null, |
134
|
|
|
'method' => 'VisitsSummary.getVisits', |
135
|
|
|
'host' => null, |
136
|
|
|
'token' => null, |
137
|
|
|
'period' => 'day', |
138
|
|
|
'date' => 'last30', |
139
|
|
|
'template' => '@Core23Matomo/Block/block_matomo_statistic.html.twig', |
140
|
|
|
], $blockContext); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function testGetBlockMetadata(): void |
144
|
|
|
{ |
145
|
|
|
$blockService = new MatomoStatisticBlockService('block.service', $this->templating, $this->factory); |
|
|
|
|
146
|
|
|
|
147
|
|
|
$metadata = $blockService->getBlockMetadata('description'); |
148
|
|
|
|
149
|
|
|
$this->assertSame('block.service', $metadata->getTitle()); |
150
|
|
|
$this->assertSame('description', $metadata->getDescription()); |
151
|
|
|
$this->assertNotNull($metadata->getImage()); |
152
|
|
|
$this->assertStringStartsWith('data:image/png;base64,', $metadata->getImage() ?? ''); |
153
|
|
|
$this->assertSame('Core23MatomoBundle', $metadata->getDomain()); |
154
|
|
|
$this->assertSame([ |
155
|
|
|
'class' => 'fa fa-area-chart', |
156
|
|
|
], $metadata->getOptions()); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function testBuildEditForm(): void |
160
|
|
|
{ |
161
|
|
|
$blockService = new MatomoStatisticBlockService('block.service', $this->templating, $this->factory); |
|
|
|
|
162
|
|
|
|
163
|
|
|
$block = new Block(); |
164
|
|
|
|
165
|
|
|
$formMapper = $this->createMock(FormMapper::class); |
166
|
|
|
$formMapper->expects($this->once())->method('add'); |
167
|
|
|
|
168
|
|
|
$blockService->buildEditForm($formMapper, $block); |
|
|
|
|
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
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: