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() |
|
|
|
|
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); |
|
|
|
|
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); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
View Code Duplication |
public function testUpdateOfChildPageWithOfflineParent() |
|
|
|
|
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); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
View Code Duplication |
public function testUpdateOfChildPageWithOnlineParent() |
|
|
|
|
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); |
|
|
|
|
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))); |
|
|
|
|
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
|
|
|
|
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.