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 StingerSoft\EntitySearchBundle\Tests\AbstractORMTestCase; |
15
|
|
|
use StingerSoft\EntitySearchBundle\Services\DummySearchService; |
16
|
|
|
use StingerSoft\EntitySearchBundle\Tests\Fixtures\ORM\Beer; |
17
|
|
|
use StingerSoft\EntitySearchBundle\Tests\Fixtures\ORM\Potato; |
18
|
|
|
|
19
|
|
|
class DummySearchServiceTest extends AbstractORMTestCase { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* |
23
|
|
|
* {@inheritDoc} |
24
|
|
|
* |
25
|
|
|
* @see PHPUnit_Framework_TestCase::setUp() |
26
|
|
|
*/ |
27
|
|
|
public function setUp() { |
28
|
|
|
parent::setUp(); |
29
|
|
|
$this->getMockSqliteEntityManager(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* |
34
|
|
|
* @return \StingerSoft\EntitySearchBundle\Services\DummySearchService |
35
|
|
|
*/ |
36
|
|
|
protected function getSearchService() { |
37
|
|
|
$service = new DummySearchService(); |
38
|
|
|
$service->setObjectManager($this->em); |
39
|
|
|
return $service; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
View Code Duplication |
public function testSaveDocument() { |
|
|
|
|
43
|
|
|
$beer = new Beer(); |
44
|
|
|
$beer->setTitle('Hemelinger'); |
45
|
|
|
$this->em->persist($beer); |
46
|
|
|
$this->em->flush(); |
47
|
|
|
|
48
|
|
|
$service = $this->getSearchService(); |
49
|
|
|
$document = $service->createEmptyDocumentFromEntity($beer); |
50
|
|
|
$this->assertAttributeCount(0, 'index', $service); |
51
|
|
|
$service->saveDocument($document); |
52
|
|
|
$this->assertAttributeCount(1, 'index', $service); |
53
|
|
|
|
54
|
|
|
$service->clearIndex(); |
55
|
|
|
$this->assertAttributeCount(0, 'index', $service); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
View Code Duplication |
public function testRemoveDocument() { |
|
|
|
|
59
|
|
|
$beer = new Beer(); |
60
|
|
|
$beer->setTitle('Hemelinger'); |
61
|
|
|
$this->em->persist($beer); |
62
|
|
|
$this->em->flush(); |
63
|
|
|
|
64
|
|
|
$service = $this->getSearchService(); |
65
|
|
|
$document = $service->createEmptyDocumentFromEntity($beer); |
66
|
|
|
$this->assertAttributeCount(0, 'index', $service); |
67
|
|
|
$service->saveDocument($document); |
68
|
|
|
$this->assertAttributeCount(1, 'index', $service); |
69
|
|
|
|
70
|
|
|
$service->removeDocument($document); |
71
|
|
|
$this->assertAttributeCount(0, 'index', $service); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testAutocompletion() { |
75
|
|
|
$beer = new Beer(); |
76
|
|
|
$beer->setTitle('Hemelinger'); |
77
|
|
|
$this->em->persist($beer); |
78
|
|
|
$this->em->flush(); |
79
|
|
|
|
80
|
|
|
$service = $this->getSearchService(); |
81
|
|
|
$document = $service->createEmptyDocumentFromEntity($beer); |
82
|
|
|
$this->assertAttributeCount(0, 'index', $service); |
83
|
|
|
$beer->indexEntity($document); |
84
|
|
|
$service->saveDocument($document); |
85
|
|
|
$this->assertAttributeCount(1, 'index', $service); |
86
|
|
|
|
87
|
|
|
$suggests = $service->autocomplete('He'); |
88
|
|
|
$this->count(1, $suggests); |
|
|
|
|
89
|
|
|
$this->assertContains($beer->getTitle(), $suggests); |
90
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* |
95
|
|
|
* {@inheritDoc} |
96
|
|
|
* |
97
|
|
|
* @see \StingerSoft\EntitySearchBundle\Tests\AbstractTestCase::getUsedEntityFixtures() |
98
|
|
|
*/ |
99
|
|
|
protected function getUsedEntityFixtures() { |
100
|
|
|
return array( |
101
|
|
|
Beer::class, |
102
|
|
|
Potato::class |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
} |
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.