PiwikStatisticBlockServiceTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 4
c 3
b 0
f 0
lcom 1
cbo 8
dl 0
loc 114
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 7 1
B testExecute() 0 37 1
B testExecuteOffline() 0 40 1
A testDefaultSettings() 0 20 1
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\PiwikBundle\Tests\Block\Service;
13
14
use Core23\PiwikBundle\Block\Service\PiwikStatisticBlockService;
15
use Core23\PiwikBundle\Client\ClientFactoryInterface;
16
use Core23\PiwikBundle\Client\ClientInterface;
17
use Core23\PiwikBundle\Exception\PiwikException;
18
use Psr\Log\LoggerInterface;
19
use Sonata\BlockBundle\Block\BlockContext;
20
use Sonata\BlockBundle\Model\Block;
21
use Sonata\BlockBundle\Model\BlockInterface;
22
use Sonata\BlockBundle\Test\AbstractBlockServiceTestCase;
23
24
final class PiwikStatisticBlockServiceTest extends AbstractBlockServiceTestCase
25
{
26
    private $logger;
27
28
    private $factory;
29
30
    protected function setUp(): void
31
    {
32
        parent::setUp();
33
34
        $this->logger  = $this->createMock(LoggerInterface::class);
35
        $this->factory = $this->createMock(ClientFactoryInterface::class);
36
    }
37
38
    public function testExecute(): void
39
    {
40
        $client = $this->createMock(ClientInterface::class);
41
        $client->expects($this->once())->method('call')
42
            ->with($this->equalTo('VisitsSummary.getVisits'), $this->equalTo([
43
                'idSite' => 'foo',
44
                'period' => 'day',
45
                'date'   => 'last30',
46
            ]))
47
            ->will($this->returnValue(['bar']));
48
49
        $this->factory->expects($this->once())->method('createPiwikClient')
50
            ->will($this->returnValue($client));
51
52
        $block = new Block();
53
54
        $blockContext = new BlockContext($block, [
55
            'title'    => null,
56
            'site'     => 'foo',
57
            'method'   => 'VisitsSummary.getVisits',
58
            'host'     => 'localhost',
59
            'token'    => '0815',
60
            'period'   => 'day',
61
            'date'     => 'last30',
62
            'template' => '@Core23Piwik/Block/block_piwik_statistic.html.twig',
63
        ]);
64
65
        $blockService = new PiwikStatisticBlockService('block.service', $this->templating, $this->factory);
0 ignored issues
show
Documentation introduced by
$this->factory is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Core23\PiwikBundl...ClientFactoryInterface>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
66
        $blockService->execute($blockContext);
67
68
        $this->assertSame('@Core23Piwik/Block/block_piwik_statistic.html.twig', $this->templating->view);
69
70
        $this->assertSame($blockContext, $this->templating->parameters['context']);
71
        $this->assertInternalType('array', $this->templating->parameters['settings']);
72
        $this->assertInstanceOf(BlockInterface::class, $this->templating->parameters['block']);
73
        $this->assertSame(['bar'], $this->templating->parameters['data']);
74
    }
75
76
    public function testExecuteOffline(): void
77
    {
78
        $client = $this->createMock(ClientInterface::class);
79
        $client->expects($this->once())->method('call')
80
            ->with($this->equalTo('VisitsSummary.getVisits'), $this->equalTo([
81
                'idSite' => 'foo',
82
                'period' => 'day',
83
                'date'   => 'last30',
84
            ]))
85
            ->willThrowException(new PiwikException('Offline'));
86
87
        $this->factory->expects($this->once())->method('createPiwikClient')
88
            ->will($this->returnValue($client));
89
90
        $this->logger->expects($this->once())->method('warning');
91
92
        $block = new Block();
93
94
        $blockContext = new BlockContext($block, [
95
            'title'    => null,
96
            'site'     => 'foo',
97
            'method'   => 'VisitsSummary.getVisits',
98
            'host'     => 'localhost',
99
            'token'    => '0815',
100
            'period'   => 'day',
101
            'date'     => 'last30',
102
            'template' => '@Core23Piwik/Block/block_piwik_statistic.html.twig',
103
        ]);
104
105
        $blockService = new PiwikStatisticBlockService('block.service', $this->templating, $this->factory);
0 ignored issues
show
Documentation introduced by
$this->factory is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Core23\PiwikBundl...ClientFactoryInterface>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
106
        $blockService->setLogger($this->logger);
0 ignored issues
show
Documentation introduced by
$this->logger is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Psr\Log\LoggerInterface>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
107
        $blockService->execute($blockContext);
108
109
        $this->assertSame('@Core23Piwik/Block/block_piwik_statistic.html.twig', $this->templating->view);
110
111
        $this->assertSame($blockContext, $this->templating->parameters['context']);
112
        $this->assertInternalType('array', $this->templating->parameters['settings']);
113
        $this->assertInstanceOf(BlockInterface::class, $this->templating->parameters['block']);
114
        $this->assertNull($this->templating->parameters['data']);
115
    }
116
117
    public function testDefaultSettings(): void
118
    {
119
        $blockService = new PiwikStatisticBlockService('block.service', $this->templating, $this->factory);
0 ignored issues
show
Documentation introduced by
$this->factory is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Core23\PiwikBundl...ClientFactoryInterface>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
120
        $blockService->setLogger($this->logger);
0 ignored issues
show
Documentation introduced by
$this->logger is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Psr\Log\LoggerInterface>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
121
        $blockContext = $this->getBlockContext($blockService);
122
123
        $this->assertSettings([
124
            'title'              => null,
125
            'translation_domain' => null,
126
            'icon'               => 'fa fa-bar-chart-o',
127
            'class'              => null,
128
            'site'               => null,
129
            'method'             => 'VisitsSummary.getVisits',
130
            'host'               => null,
131
            'token'              => null,
132
            'period'             => 'day',
133
            'date'               => 'last30',
134
            'template'           => '@Core23Piwik/Block/block_piwik_statistic.html.twig',
135
        ], $blockContext);
136
    }
137
}
138