1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Stinger Entity 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\Model\Document; |
15
|
|
|
use StingerSoft\EntitySearchBundle\Services\AbstractSearchService; |
16
|
|
|
use StingerSoft\EntitySearchBundle\Services\DummySearchService; |
17
|
|
|
use StingerSoft\EntitySearchBundle\Services\Mapping\EntityToDocumentMapper; |
18
|
|
|
use StingerSoft\EntitySearchBundle\Tests\AbstractORMTestCase; |
19
|
|
|
use StingerSoft\EntitySearchBundle\Tests\DependencyInjection\StingerSoftEntitySearchExtensionTest; |
20
|
|
|
use StingerSoft\EntitySearchBundle\Tests\Fixtures\ORM\Beer; |
21
|
|
|
use StingerSoft\EntitySearchBundle\Tests\Fixtures\ORM\Potato; |
22
|
|
|
use StingerSoft\EntitySearchBundle\Tests\Fixtures\ORM\Whiskey; |
23
|
|
|
|
24
|
|
|
class EntityToDocumentMapperTest extends AbstractORMTestCase { |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* |
28
|
|
|
* {@inheritDoc} |
29
|
|
|
* |
30
|
|
|
* @see PHPUnit_Framework_TestCase::setUp() |
31
|
|
|
*/ |
32
|
|
|
public function setUp() { |
33
|
|
|
parent::setUp(); |
34
|
|
|
$this->getMockSqliteEntityManager(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* |
39
|
|
|
* @return \StingerSoft\EntitySearchBundle\Services\EntityToDocumentMapper |
40
|
|
|
*/ |
41
|
|
|
protected function getEntityToDocumentMapper() { |
42
|
|
|
$eh = new EntityToDocumentMapper(new DummySearchService(), StingerSoftEntitySearchExtensionTest::$mockConfiguration['stinger_soft.entity_search']['types']); |
43
|
|
|
return $eh; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @expectedException \InvalidArgumentException |
48
|
|
|
*/ |
49
|
|
|
public function testFuckedUpConfiguration() { |
50
|
|
|
$searchService = $this->getMockBuilder(AbstractSearchService::class)->getMockForAbstractClass(); |
51
|
|
|
new EntityToDocumentMapper($searchService, array( |
52
|
|
|
'beer' => array() |
53
|
|
|
)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @expectedException \InvalidArgumentException |
58
|
|
|
*/ |
59
|
|
|
public function testFuckedUpConfigurationWithoutMapping() { |
60
|
|
|
$searchService = $this->getMockBuilder(AbstractSearchService::class)->getMockForAbstractClass(); |
61
|
|
|
new EntityToDocumentMapper($searchService, array( |
62
|
|
|
'beer' => array( |
63
|
|
|
'persistence' => array( |
64
|
|
|
'model' => Beer::class |
65
|
|
|
) |
66
|
|
|
) |
67
|
|
|
)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @expectedException \InvalidArgumentException |
72
|
|
|
*/ |
73
|
|
View Code Duplication |
public function testFuckedUpConfigurationWithoutModel() { |
|
|
|
|
74
|
|
|
$searchService = $this->getMockBuilder(AbstractSearchService::class)->getMockForAbstractClass(); |
75
|
|
|
new EntityToDocumentMapper($searchService, array( |
76
|
|
|
'beer' => array( |
77
|
|
|
'mappings' => array( |
78
|
|
|
'title' => array( |
79
|
|
|
'propertyPath' => false |
80
|
|
|
) |
81
|
|
|
), |
82
|
|
|
'persistence' => array() |
83
|
|
|
) |
84
|
|
|
)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @expectedException \InvalidArgumentException |
89
|
|
|
*/ |
90
|
|
View Code Duplication |
public function testFuckedUpConfigurationWithoutPersistance() { |
|
|
|
|
91
|
|
|
$searchService = $this->getMockBuilder(AbstractSearchService::class)->getMockForAbstractClass(); |
92
|
|
|
new EntityToDocumentMapper($searchService, array( |
93
|
|
|
'beer' => array( |
94
|
|
|
'mappings' => array( |
95
|
|
|
'title' => array( |
96
|
|
|
'propertyPath' => false |
97
|
|
|
) |
98
|
|
|
) |
99
|
|
|
) |
100
|
|
|
)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testIsIndexable() { |
104
|
|
|
$eh = $this->getEntityToDocumentMapper(); |
105
|
|
|
$this->assertTrue($eh->isIndexable(new Beer())); |
106
|
|
|
$this->assertFalse($eh->isIndexable(new Potato())); |
107
|
|
|
$this->assertTrue($eh->isIndexable(new Whiskey())); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function testIsClassIndexable() { |
111
|
|
|
$eh = $this->getEntityToDocumentMapper(); |
112
|
|
|
$this->assertTrue($eh->isClassIndexable(Beer::class)); |
113
|
|
|
$this->assertFalse($eh->isClassIndexable(Potato::class)); |
114
|
|
|
$this->assertTrue($eh->isClassIndexable(Whiskey::class)); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testCreateDocument() { |
118
|
|
|
$eh = $this->getEntityToDocumentMapper(); |
119
|
|
|
|
120
|
|
|
$beer = new Beer(); |
121
|
|
|
$beer->setTitle('Hemelinger'); |
122
|
|
|
$this->em->persist($beer); |
123
|
|
|
$this->em->flush(); |
124
|
|
|
$document = $eh->createDocument($this->em, $beer); |
125
|
|
|
$this->assertInstanceOf(Document::class, $document); |
126
|
|
|
$this->assertEquals('Hemelinger', $document->getFieldValue(Document::FIELD_TITLE)); |
127
|
|
|
|
128
|
|
|
$whiskey = new Whiskey(); |
129
|
|
|
$whiskey->setTitle('Laphroaig'); |
130
|
|
|
$this->em->persist($whiskey); |
131
|
|
|
$this->em->flush(); |
132
|
|
|
$document = $eh->createDocument($this->em, $whiskey); |
133
|
|
|
$this->assertInstanceOf(Document::class, $document); |
134
|
|
|
$this->assertEquals('Laphroaig', $document->getFieldValue(Document::FIELD_TITLE)); |
135
|
|
|
|
136
|
|
|
$potato = new Potato(); |
137
|
|
|
$potato->setTitle('Erna'); |
138
|
|
|
$this->em->persist($potato); |
139
|
|
|
$this->em->flush(); |
140
|
|
|
$document = $eh->createDocument($this->em, $potato); |
141
|
|
|
$this->assertFalse($document); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* |
146
|
|
|
* {@inheritDoc} |
147
|
|
|
* |
148
|
|
|
* @see \StingerSoft\EntitySearchBundle\Tests\AbstractTestCase::getUsedEntityFixtures() |
149
|
|
|
*/ |
150
|
|
|
protected function getUsedEntityFixtures() { |
151
|
|
|
return array( |
152
|
|
|
Beer::class, |
153
|
|
|
Potato::class, |
154
|
|
|
Whiskey::class |
155
|
|
|
); |
156
|
|
|
} |
157
|
|
|
} |
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.