Completed
Pull Request — 5.2 (#2400)
by
unknown
17:37 queued 05:03
created

testUpdateOfChildPageWithStructuredNodeParentAndOfflineParent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 9.328
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Kunstmaan\NodeSearchBundle\Tests\EventListener;
4
5
use Doctrine\ORM\EntityManager;
6
use Kunstmaan\NodeBundle\Entity\AbstractPage;
7
use Kunstmaan\NodeBundle\Entity\Node;
8
use Kunstmaan\NodeBundle\Entity\NodeTranslation;
9
use Kunstmaan\NodeBundle\Entity\AbstractStructurePage;
10
use Kunstmaan\NodeBundle\Event\NodeEvent;
11
use Kunstmaan\NodeSearchBundle\Configuration\NodePagesConfiguration;
12
use Kunstmaan\NodeSearchBundle\EventListener\NodeIndexUpdateEventListener;
13
use PHPUnit\Framework\TestCase;
14
15
class NodeIndexUpdateEventListenerTest extends TestCase
16
{
17 View Code Duplication
    public function testUpdateOfChildPageWithStructuredPageParent()
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...
18
    {
19
        $parentPage = $this->createMock(AbstractStructurePage::class);
20
21
        $parentNodeTranslation = $this->createMock(NodeTranslation::class);
22
        $parentNodeTranslation->method('getRef')->willReturn($parentPage);
23
        $parentNodeTranslation->method('isOnline')->willReturn(true);
24
25
        $parentNode = $this->createMock(Node::class);
26
        $parentNode->method('getNodeTranslation')->willReturn($parentNodeTranslation);
27
28
        $node = $this->createMock(Node::class);
29
        $node->method('getParents')->willReturn([
30
            $parentNode,
31
        ]);
32
33
        $nodeTranslation = $this->createMock(NodeTranslation::class);
34
        $nodeTranslation->method('getId')->willReturn(1);
35
        $nodeTranslation->method('getLang')->willReturn('nl');
36
        $nodeTranslation->method('getNode')->willReturn($node);
37
38
        $nodeEvent = $this->getMockBuilder(NodeEvent::class)->disableOriginalConstructor()->getMock();
39
40
        $nodeEvent->method('getNodeTranslation')->willReturn($nodeTranslation);
41
42
        $em = $this->createMock(EntityManager::class);
43
        $listener = new NodeIndexUpdateEventListener($this->getSearchConfiguration(true), $em);
44
        $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...
45
    }
46
47
    public function testUpdateOfChildPageWithStructuredPageParentAndOfflineParent()
48
    {
49
        $parentPage = $this->createMock(AbstractStructurePage::class);
50
51
        $parentNodeTranslation = $this->createMock(NodeTranslation::class);
52
        $parentNodeTranslation->method('getRef')->willReturn($parentPage);
53
        $parentNodeTranslation->method('isOnline')->willReturn(true);
54
55
        $parentPageNotStructured = $this->createMock(AbstractPage::class);
56
        $parentNodeTranslation2 = $this->createMock(NodeTranslation::class);
57
        $parentNodeTranslation2->method('isOnline')->willReturn(false);
58
        $parentNodeTranslation->method('getRef')->willReturn($parentPageNotStructured);
59
60
        $parentNode1 = $this->createMock(Node::class);
61
        $parentNode1->method('getNodeTranslation')->willReturn($parentNodeTranslation);
62
        $parentNode2 = $this->createMock(Node::class);
63
        $parentNode2->method('getNodeTranslation')->willReturn($parentNodeTranslation2);
64
65
        $node = $this->createMock(Node::class);
66
        $node->method('getParents')->willReturn([
67
            $parentNode1,
68
            $parentNode2,
69
        ]);
70
71
        $nodeTranslation = $this->createMock(NodeTranslation::class);
72
        $nodeTranslation->method('getId')->willReturn(1);
73
        $nodeTranslation->method('getLang')->willReturn('nl');
74
        $nodeTranslation->method('getNode')->willReturn($node);
75
76
        $nodeEvent = $this->getMockBuilder(NodeEvent::class)->disableOriginalConstructor()->getMock();
77
78
        $nodeEvent->method('getNodeTranslation')->willReturn($nodeTranslation);
79
80
        $em = $this->createMock(EntityManager::class);
81
        $listener = new NodeIndexUpdateEventListener($this->getSearchConfiguration(false), $em);
82
        $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...
83
    }
84
85 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...
86
    {
87
        $parentPage = $this->createMock(AbstractPage::class);
88
        $parentNodeTranslation = $this->createMock(NodeTranslation::class);
89
        $parentNodeTranslation->method('isOnline')->willReturn(false);
90
        $parentNodeTranslation->method('getRef')->willReturn($parentPage);
91
92
        $parentNode = $this->createMock(Node::class);
93
        $parentNode->method('getNodeTranslation')->willReturn($parentNodeTranslation);
94
95
        $node = $this->createMock(Node::class);
96
        $node->method('getParents')->willReturn([$parentNode]);
97
98
        $nodeTranslation = $this->createMock(NodeTranslation::class);
99
        $nodeTranslation->method('getId')->willReturn(1);
100
        $nodeTranslation->method('getLang')->willReturn('nl');
101
        $nodeTranslation->method('getNode')->willReturn($node);
102
103
        $nodeEvent = $this->getMockBuilder(NodeEvent::class)->disableOriginalConstructor()->getMock();
104
105
        $nodeEvent->method('getNodeTranslation')->willReturn($nodeTranslation);
106
107
        $em = $this->createMock(EntityManager::class);
108
        $listener = new NodeIndexUpdateEventListener($this->getSearchConfiguration(false), $em);
109
        $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...
110
    }
111
112 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...
113
    {
114
        $parentPage = $this->createMock(AbstractPage::class);
115
        $parentNodeTranslation = $this->createMock(NodeTranslation::class);
116
        $parentNodeTranslation->method('isOnline')->willReturn(true);
117
        $parentNodeTranslation->method('getRef')->willReturn($parentPage);
118
119
        $parentNode = $this->createMock(Node::class);
120
        $parentNode->method('getNodeTranslation')->willReturn($parentNodeTranslation);
121
122
        $node = $this->createMock(Node::class);
123
        $node->method('getParents')->willReturn([$parentNode]);
124
125
        $nodeTranslation = $this->createMock(NodeTranslation::class);
126
        $nodeTranslation->method('getId')->willReturn(1);
127
        $nodeTranslation->method('getLang')->willReturn('nl');
128
        $nodeTranslation->method('getNode')->willReturn($node);
129
130
        $nodeEvent = $this->getMockBuilder(NodeEvent::class)->disableOriginalConstructor()->getMock();
131
132
        $nodeEvent->method('getNodeTranslation')->willReturn($nodeTranslation);
133
134
        $em = $this->createMock(EntityManager::class);
135
        $listener = new NodeIndexUpdateEventListener($this->getSearchConfiguration(true), $em);
136
        $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...
137
    }
138
139
    /**
140
     * @group legacy
141
     * @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.
142
     */
143
    public function testContainerDeprecation()
144
    {
145
        $em = $this->createMock(EntityManager::class);
146
        $listener = new NodeIndexUpdateEventListener($this->getContainer($this->getSearchConfiguration(false)), $em);
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...
147
    }
148
149
    private function getContainer($searchConfigMock)
150
    {
151
        $container = $this->createMock('Symfony\Component\DependencyInjection\ContainerInterface');
152
153
        $container
154
            ->expects($this->any())
155
            ->method('get')
156
            ->with($this->equalTo('kunstmaan_node_search.search_configuration.node'))
157
            ->willReturn($searchConfigMock)
158
        ;
159
160
        return $container;
161
    }
162
163
    private function getSearchConfiguration($expectCall)
164
    {
165
        $searchConfig = $this->createMock(NodePagesConfiguration::class);
166
        $searchConfig
167
            ->expects($expectCall ? $this->once() : $this->never())
168
            ->method('indexNodeTranslation')
169
            ->willReturn(null)
170
        ;
171
172
        return $searchConfig;
173
    }
174
}
175