Completed
Push — master ( a85f01...3f72af )
by Ruud
23:55 queued 09:49
created

testContainerDeprecation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Kunstmaan\NodeSearchBundle\Tests\EventListener;
4
5
use Kunstmaan\NodeBundle\Entity\Node;
6
use Kunstmaan\NodeBundle\Entity\NodeTranslation;
7
use Kunstmaan\NodeBundle\Entity\StructureNode;
8
use Kunstmaan\NodeBundle\Event\NodeEvent;
9
use Kunstmaan\NodeSearchBundle\Configuration\NodePagesConfiguration;
10
use Kunstmaan\NodeSearchBundle\EventListener\NodeIndexUpdateEventListener;
11
use PHPUnit\Framework\TestCase;
12
13
class NodeIndexUpdateEventListenerTest extends TestCase
14
{
15 View Code Duplication
    public function testUpdateOfChildPageWithStructuredNodeParent()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
16
    {
17
        $parentNodeTranslation = $this->createMock(StructureNode::class);
18
        $parentNodeTranslation->method('isOnline')->willReturn(false);
19
20
        $parentNode = $this->createMock(Node::class);
21
        $parentNode->method('getNodeTranslation')->willReturn($parentNodeTranslation);
22
23
        $node = $this->createMock(Node::class);
24
        $node->method('getParents')->willReturn([
25
            $parentNode,
26
        ]);
27
28
        $nodeTranslation = $this->createMock(NodeTranslation::class);
29
        $nodeTranslation->method('getId')->willReturn(1);
30
        $nodeTranslation->method('getLang')->willReturn('nl');
31
        $nodeTranslation->method('getNode')->willReturn($node);
32
33
        $nodeEvent = $this->getMockBuilder(NodeEvent::class)->disableOriginalConstructor()->getMock();
34
35
        $nodeEvent->method('getNodeTranslation')->willReturn($nodeTranslation);
36
37
        $listener = new NodeIndexUpdateEventListener($this->getSearchConfiguration(true));
38
        $listener->onPostPersist($nodeEvent);
0 ignored issues
show
Documentation introduced by
$nodeEvent is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Kunstmaan\NodeBundle\Event\NodeEvent>.

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...
39
    }
40
41
    public function testUpdateOfChildPageWithStructuredNodeParentAndOfflineParent()
42
    {
43
        $parentNodeTranslation = $this->createMock(StructureNode::class);
44
        $parentNodeTranslation->method('isOnline')->willReturn(false);
45
46
        $parentNodeTranslation2 = $this->createMock(NodeTranslation::class);
47
        $parentNodeTranslation2->method('isOnline')->willReturn(false);
48
49
        $parentNode1 = $this->createMock(Node::class);
50
        $parentNode1->method('getNodeTranslation')->willReturn($parentNodeTranslation);
51
        $parentNode2 = $this->createMock(Node::class);
52
        $parentNode2->method('getNodeTranslation')->willReturn($parentNodeTranslation2);
53
54
        $node = $this->createMock(Node::class);
55
        $node->method('getParents')->willReturn([
56
            $parentNode1,
57
            $parentNode2,
58
        ]);
59
60
        $nodeTranslation = $this->createMock(NodeTranslation::class);
61
        $nodeTranslation->method('getId')->willReturn(1);
62
        $nodeTranslation->method('getLang')->willReturn('nl');
63
        $nodeTranslation->method('getNode')->willReturn($node);
64
65
        $nodeEvent = $this->getMockBuilder(NodeEvent::class)->disableOriginalConstructor()->getMock();
66
67
        $nodeEvent->method('getNodeTranslation')->willReturn($nodeTranslation);
68
69
        $listener = new NodeIndexUpdateEventListener($this->getSearchConfiguration(false));
70
        $listener->onPostPersist($nodeEvent);
0 ignored issues
show
Documentation introduced by
$nodeEvent is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Kunstmaan\NodeBundle\Event\NodeEvent>.

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...
71
    }
72
73 View Code Duplication
    public function testUpdateOfChildPageWithOfflineParent()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
    {
75
        $parentNodeTranslation = $this->createMock(NodeTranslation::class);
76
        $parentNodeTranslation->method('isOnline')->willReturn(false);
77
78
        $parentNode = $this->createMock(Node::class);
79
        $parentNode->method('getNodeTranslation')->willReturn($parentNodeTranslation);
80
81
        $node = $this->createMock(Node::class);
82
        $node->method('getParents')->willReturn([$parentNode]);
83
84
        $nodeTranslation = $this->createMock(NodeTranslation::class);
85
        $nodeTranslation->method('getId')->willReturn(1);
86
        $nodeTranslation->method('getLang')->willReturn('nl');
87
        $nodeTranslation->method('getNode')->willReturn($node);
88
89
        $nodeEvent = $this->getMockBuilder(NodeEvent::class)->disableOriginalConstructor()->getMock();
90
91
        $nodeEvent->method('getNodeTranslation')->willReturn($nodeTranslation);
92
93
        $listener = new NodeIndexUpdateEventListener($this->getSearchConfiguration(false));
94
        $listener->onPostPersist($nodeEvent);
0 ignored issues
show
Documentation introduced by
$nodeEvent is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Kunstmaan\NodeBundle\Event\NodeEvent>.

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...
95
    }
96
97 View Code Duplication
    public function testUpdateOfChildPageWithOnlineParent()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
    {
99
        $parentNodeTranslation = $this->createMock(NodeTranslation::class);
100
        $parentNodeTranslation->method('isOnline')->willReturn(true);
101
102
        $parentNode = $this->createMock(Node::class);
103
        $parentNode->method('getNodeTranslation')->willReturn($parentNodeTranslation);
104
105
        $node = $this->createMock(Node::class);
106
        $node->method('getParents')->willReturn([$parentNode]);
107
108
        $nodeTranslation = $this->createMock(NodeTranslation::class);
109
        $nodeTranslation->method('getId')->willReturn(1);
110
        $nodeTranslation->method('getLang')->willReturn('nl');
111
        $nodeTranslation->method('getNode')->willReturn($node);
112
113
        $nodeEvent = $this->getMockBuilder(NodeEvent::class)->disableOriginalConstructor()->getMock();
114
115
        $nodeEvent->method('getNodeTranslation')->willReturn($nodeTranslation);
116
117
        $listener = new NodeIndexUpdateEventListener($this->getSearchConfiguration(true));
118
        $listener->onPostPersist($nodeEvent);
0 ignored issues
show
Documentation introduced by
$nodeEvent is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Kunstmaan\NodeBundle\Event\NodeEvent>.

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...
119
    }
120
121
    /**
122
     * @group legacy
123
     * @expectedDeprecation Passing the container as the first argument of "Kunstmaan\NodeSearchBundle\EventListener\NodeIndexUpdateEventListener" is deprecated in KunstmaanNodeSearchBundle 5.2 and will be removed in KunstmaanNodeSearchBundle 6.0. Inject the "kunstmaan_node_search.search_configuration.node" service instead.
124
     */
125
    public function testContainerDeprecation()
126
    {
127
        $listener = new NodeIndexUpdateEventListener($this->getContainer($this->getSearchConfiguration(false)));
0 ignored issues
show
Unused Code introduced by
$listener is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
128
    }
129
130
    private function getContainer($searchConfigMock)
131
    {
132
        $container = $this->createMock('Symfony\Component\DependencyInjection\ContainerInterface');
133
134
        $container
135
            ->expects($this->any())
136
            ->method('get')
137
            ->with($this->equalTo('kunstmaan_node_search.search_configuration.node'))
138
            ->willReturn($searchConfigMock)
139
        ;
140
141
        return $container;
142
    }
143
144
    private function getSearchConfiguration($expectCall)
145
    {
146
        $searchConfig = $this->createMock(NodePagesConfiguration::class);
147
        $searchConfig
148
            ->expects($expectCall ? $this->once() : $this->never())
149
            ->method('indexNodeTranslation')
150
            ->willReturn(null)
151
        ;
152
153
        return $searchConfig;
154
    }
155
}
156