1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Stinger Enity Search package. |
5
|
|
|
* |
6
|
|
|
* (c) Oliver Kotte <[email protected]> |
7
|
|
|
* (c) Florian Meyer <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
namespace StingerSoft\EntitySearchBundle\Tests\Services; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\EventManager; |
15
|
|
|
use StingerSoft\EntitySearchBundle\Services\DoctrineListener; |
16
|
|
|
use StingerSoft\EntitySearchBundle\Tests\AbstractORMTestCase; |
17
|
|
|
use StingerSoft\EntitySearchBundle\Tests\Fixtures\ORM\Beer; |
18
|
|
|
use StingerSoft\EntitySearchBundle\Services\SearchService; |
19
|
|
|
use StingerSoft\EntitySearchBundle\Services\AbstractSearchService; |
20
|
|
|
use StingerSoft\EntitySearchBundle\Tests\Fixtures\ORM\Potato; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
*/ |
24
|
|
|
class DoctrineListenerTest extends AbstractORMTestCase { |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param number $save Number of expected saves |
28
|
|
|
* @param number $delete Number of expected deletions |
29
|
|
|
*/ |
30
|
|
|
protected function registerDoctrineListener($save = 0, $delete = 0) { |
31
|
|
|
$listenerMock = $this->getMockBuilder(DoctrineListener::class)->setMethods(array( |
32
|
|
|
'updateEntity', |
33
|
|
|
'removeEntity' |
34
|
|
|
))->disableOriginalConstructor()->getMock(); |
35
|
|
|
|
36
|
|
|
$listenerMock->expects($this->exactly($save))->method('updateEntity')->will($this->returnValue(null)); |
37
|
|
|
$listenerMock->expects($this->exactly($delete))->method('removeEntity')->will($this->returnValue(null)); |
38
|
|
|
|
39
|
|
|
$evm = new EventManager(); |
40
|
|
|
$evm->addEventSubscriber($listenerMock); |
41
|
|
|
$this->getMockSqliteEntityManager($evm); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param number $save Number of expected saves |
46
|
|
|
* @param number $delete Number of expected deletions |
47
|
|
|
*/ |
48
|
|
|
protected function registerSearchService($save = 0, $delete = 0) { |
49
|
|
|
$searchMock = $this->getMockBuilder(AbstractSearchService::class)->setMethods(array( |
50
|
|
|
'saveDocument', |
51
|
|
|
'removeDocument', |
52
|
|
|
'clearIndex', |
53
|
|
|
'autocomplete', |
54
|
|
|
'search', |
55
|
|
|
))->disableOriginalConstructor()->getMock(); |
56
|
|
|
|
57
|
|
|
$searchMock->expects($this->exactly($save))->method('saveDocument')->will($this->returnValue(null)); |
58
|
|
|
$searchMock->expects($this->exactly($delete))->method('removeDocument')->will($this->returnValue(null)); |
59
|
|
|
|
60
|
|
|
$evm = new EventManager(); |
61
|
|
|
$evm->addEventSubscriber(new DoctrineListener($searchMock)); |
62
|
|
|
$this->getMockSqliteEntityManager($evm); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testMockedPersist() { |
66
|
|
|
$this->registerDoctrineListener(1, 0); |
67
|
|
|
$beer = new Beer(); |
68
|
|
|
$beer->setTitle('Hemelinger'); |
69
|
|
|
$this->em->persist($beer); |
70
|
|
|
$this->em->flush(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
View Code Duplication |
public function testMockedUpdate() { |
|
|
|
|
74
|
|
|
$this->registerDoctrineListener(2, 0); |
75
|
|
|
$beer = new Beer(); |
76
|
|
|
$beer->setTitle('Haake Bäck'); |
77
|
|
|
$this->em->persist($beer); |
78
|
|
|
$this->em->flush(); |
79
|
|
|
|
80
|
|
|
$beer->setTitle('Haake Beck'); |
81
|
|
|
$this->em->persist($beer); |
82
|
|
|
$this->em->flush(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
View Code Duplication |
public function testMockedDelete() { |
|
|
|
|
86
|
|
|
$this->registerDoctrineListener(1, 1); |
87
|
|
|
$beer = new Beer(); |
88
|
|
|
$beer->setTitle('Haake Beck'); |
89
|
|
|
$this->em->persist($beer); |
90
|
|
|
$this->em->flush(); |
91
|
|
|
|
92
|
|
|
$this->em->remove($beer); |
93
|
|
|
$this->em->flush(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function testPersist() { |
97
|
|
|
$this->registerSearchService(1, 0); |
98
|
|
|
$beer = new Beer(); |
99
|
|
|
$beer->setTitle('Hemelinger'); |
100
|
|
|
$this->em->persist($beer); |
101
|
|
|
$this->em->flush(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
View Code Duplication |
public function testUpdate() { |
|
|
|
|
105
|
|
|
$this->registerSearchService(2, 0); |
106
|
|
|
$beer = new Beer(); |
107
|
|
|
$beer->setTitle('Haake Bäck'); |
108
|
|
|
$this->em->persist($beer); |
109
|
|
|
$this->em->flush(); |
110
|
|
|
|
111
|
|
|
$beer->setTitle('Haake Beck'); |
112
|
|
|
$this->em->persist($beer); |
113
|
|
|
$this->em->flush(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function testReturnFalse() { |
117
|
|
|
|
118
|
|
|
$this->registerSearchService(1, 1); |
119
|
|
|
$beer = new Beer(); |
120
|
|
|
$beer->setTitle('Haake Bäck'); |
121
|
|
|
$this->em->persist($beer); |
122
|
|
|
$this->em->flush(); |
123
|
|
|
|
124
|
|
|
Beer::$index = false; |
125
|
|
|
$beer->setTitle('Haake Beck'); |
126
|
|
|
$this->em->persist($beer); |
127
|
|
|
$this->em->flush(); |
128
|
|
|
Beer::$index = true; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
View Code Duplication |
public function testDelete() { |
|
|
|
|
132
|
|
|
$this->registerSearchService(1, 1); |
133
|
|
|
$beer = new Beer(); |
134
|
|
|
$beer->setTitle('Haake Beck'); |
135
|
|
|
$this->em->persist($beer); |
136
|
|
|
$this->em->flush(); |
137
|
|
|
|
138
|
|
|
$this->em->remove($beer); |
139
|
|
|
$this->em->flush(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
View Code Duplication |
public function testMockedNonIndexablePersist() { |
|
|
|
|
143
|
|
|
$this->registerSearchService(0, 0); |
144
|
|
|
$potato = new Potato(); |
145
|
|
|
$potato->setTitle('Lisa'); |
146
|
|
|
$this->em->persist($potato); |
147
|
|
|
$this->em->flush(); |
148
|
|
|
|
149
|
|
|
$potato->setTitle('Erna'); |
150
|
|
|
$this->em->persist($potato); |
151
|
|
|
$this->em->flush(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function testMockedNonIndexableUpdate() { |
155
|
|
|
$this->registerSearchService(0, 0); |
156
|
|
|
$potato = new Potato(); |
157
|
|
|
$potato->setTitle('Lisa'); |
158
|
|
|
$this->em->persist($potato); |
159
|
|
|
$this->em->flush(); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
View Code Duplication |
public function testMockedNonIndexableDelete() { |
|
|
|
|
163
|
|
|
$this->registerSearchService(0, 0); |
164
|
|
|
$potato = new Potato(); |
165
|
|
|
$potato->setTitle('Lisa'); |
166
|
|
|
$this->em->persist($potato); |
167
|
|
|
$this->em->flush(); |
168
|
|
|
|
169
|
|
|
$this->em->remove($potato); |
170
|
|
|
$this->em->flush(); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
protected function getUsedEntityFixtures() { |
174
|
|
|
return array( |
175
|
|
|
Beer::class, |
176
|
|
|
Potato::class, |
177
|
|
|
); |
178
|
|
|
} |
179
|
|
|
} |
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.