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\BlockBundle\Block\BlockContext; |
20
|
|
|
use Sonata\BlockBundle\Form\Mapper\FormMapper; |
21
|
|
|
use Sonata\BlockBundle\Model\Block; |
22
|
|
|
use Sonata\BlockBundle\Model\BlockInterface; |
23
|
|
|
use Sonata\BlockBundle\Test\BlockServiceTestCase; |
24
|
|
|
|
25
|
|
|
final class MatomoStatisticBlockServiceTest extends BlockServiceTestCase |
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(static::once())->method('call') |
43
|
|
|
->with(static::equalTo('VisitsSummary.getVisits'), static::equalTo([ |
44
|
|
|
'idSite' => 'foo', |
45
|
|
|
'period' => 'day', |
46
|
|
|
'date' => 'last30', |
47
|
|
|
])) |
48
|
|
|
->willReturn(['bar']) |
49
|
|
|
; |
50
|
|
|
|
51
|
|
|
$this->factory->expects(static::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
|
|
|
static::assertSame('@Core23Matomo/Block/block_matomo_statistic.html.twig', $this->templating->view); |
72
|
|
|
|
73
|
|
|
static::assertSame($blockContext, $this->templating->parameters['context']); |
74
|
|
|
static::assertIsArray($this->templating->parameters['settings']); |
75
|
|
|
static::assertInstanceOf(BlockInterface::class, $this->templating->parameters['block']); |
76
|
|
|
static::assertSame(['bar'], $this->templating->parameters['data']); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testExecuteOffline(): void |
80
|
|
|
{ |
81
|
|
|
$client = $this->createMock(ClientInterface::class); |
82
|
|
|
$client->expects(static::once())->method('call') |
83
|
|
|
->with(static::equalTo('VisitsSummary.getVisits'), static::equalTo([ |
84
|
|
|
'idSite' => 'foo', |
85
|
|
|
'period' => 'day', |
86
|
|
|
'date' => 'last30', |
87
|
|
|
])) |
88
|
|
|
->willThrowException(new MatomoException('Offline')) |
89
|
|
|
; |
90
|
|
|
|
91
|
|
|
$this->factory->expects(static::once())->method('createClient') |
92
|
|
|
->willReturn($client) |
93
|
|
|
; |
94
|
|
|
|
95
|
|
|
$this->logger->expects(static::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
|
|
|
static::assertSame('@Core23Matomo/Block/block_matomo_statistic.html.twig', $this->templating->view); |
115
|
|
|
|
116
|
|
|
static::assertSame($blockContext, $this->templating->parameters['context']); |
117
|
|
|
static::assertIsArray($this->templating->parameters['settings']); |
118
|
|
|
static::assertInstanceOf(BlockInterface::class, $this->templating->parameters['block']); |
119
|
|
|
static::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 testGetMetadata(): void |
144
|
|
|
{ |
145
|
|
|
$blockService = new MatomoStatisticBlockService('block.service', $this->templating, $this->factory); |
146
|
|
|
|
147
|
|
|
$metadata = $blockService->getMetadata(); |
148
|
|
|
|
149
|
|
|
static::assertSame('block.service', $metadata->getTitle()); |
150
|
|
|
static::assertNotNull($metadata->getImage()); |
151
|
|
|
static::assertStringStartsWith('data:image/png;base64,', $metadata->getImage() ?? ''); |
152
|
|
|
static::assertSame('Core23MatomoBundle', $metadata->getDomain()); |
153
|
|
|
static::assertSame([ |
154
|
|
|
'class' => 'fa fa-area-chart', |
155
|
|
|
], $metadata->getOptions()); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function testConfigureEditForm(): void |
159
|
|
|
{ |
160
|
|
|
$blockService = new MatomoStatisticBlockService('block.service', $this->templating, $this->factory); |
161
|
|
|
|
162
|
|
|
$block = new Block(); |
163
|
|
|
|
164
|
|
|
$formMapper = $this->createMock(FormMapper::class); |
165
|
|
|
$formMapper->expects(static::once())->method('add'); |
166
|
|
|
|
167
|
|
|
$blockService->configureEditForm($formMapper, $block); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|