1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Firesphere\SolrSearch\Tests; |
5
|
|
|
|
6
|
|
|
use CircleCITestIndex; |
7
|
|
|
use Firesphere\SolrSearch\Queries\BaseQuery; |
8
|
|
|
use Firesphere\SolrSearch\Services\SolrCoreService; |
9
|
|
|
use Firesphere\SolrSearch\Tasks\SolrConfigureTask; |
10
|
|
|
use Firesphere\SolrSearch\Tasks\SolrIndexTask; |
11
|
|
|
use GuzzleHttp\Handler\MockHandler; |
12
|
|
|
use GuzzleHttp\HandlerStack; |
13
|
|
|
use GuzzleHttp\Psr7\Response; |
14
|
|
|
use SilverStripe\CMS\Model\SiteTree; |
15
|
|
|
use SilverStripe\Control\HTTPRequestBuilder; |
16
|
|
|
use SilverStripe\Control\NullHTTPRequest; |
17
|
|
|
use SilverStripe\Core\Injector\Injector; |
18
|
|
|
use SilverStripe\Dev\SapphireTest; |
19
|
|
|
use SilverStripe\ORM\ArrayList; |
20
|
|
|
use SilverStripe\ORM\DatabaseAdmin; |
21
|
|
|
use SilverStripe\ORM\DB; |
22
|
|
|
use Solarium\Core\Client\Client; |
23
|
|
|
|
24
|
|
|
class SolrCoreServiceTest extends SapphireTest |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var SolrCoreService |
28
|
|
|
*/ |
29
|
|
|
protected $service; |
30
|
|
|
|
31
|
|
|
public static function setUpBeforeClass() |
32
|
|
|
{ |
33
|
|
|
parent::setUpBeforeClass(); |
34
|
|
|
// This is a hack on my local dev to make fixtures populate temporary database |
35
|
|
|
$conn = DB::get_conn(); |
36
|
|
|
$dbName = self::tempDB()->build(); |
37
|
|
|
$conn->selectDatabase($dbName); |
38
|
|
|
$dbAdmin = new DatabaseAdmin(); |
39
|
|
|
$dbAdmin->setRequest(HTTPRequestBuilder::createFromEnvironment()); |
40
|
|
|
$dbAdmin->doBuild(true, true, true); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testIndexes() |
44
|
|
|
{ |
45
|
|
|
$service = new SolrCoreService(); |
46
|
|
|
|
47
|
|
|
$expected = [ |
48
|
|
|
CircleCITestIndex::class, |
49
|
|
|
TestIndex::class, |
50
|
|
|
TestIndexTwo::class, |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
$this->assertEquals($expected, $service->getValidIndexes()); |
54
|
|
|
$this->assertEquals([CircleCITestIndex::class], $service->getValidIndexes(CircleCITestIndex::class)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @expectedException \LogicException |
60
|
|
|
*/ |
61
|
|
|
public function testUpdateItemsFail() |
62
|
|
|
{ |
63
|
|
|
$this->service->updateItems(null, SolrCoreService::CREATE_TYPE); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @expectedException \LogicException |
68
|
|
|
*/ |
69
|
|
|
public function testUpdateItemsFailWrongCall() |
70
|
|
|
{ |
71
|
|
|
$this->service->updateItems(['test'], SolrCoreService::DELETE_TYPE_ALL); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testUpdateItems() |
75
|
|
|
{ |
76
|
|
|
$index = new CircleCITestIndex(); |
77
|
|
|
$query = new BaseQuery(); |
78
|
|
|
$query->addTerm('*:*'); |
79
|
|
|
$items = SiteTree::get(); |
80
|
|
|
|
81
|
|
|
$result = $this->service->updateItems($items, SolrCoreService::UPDATE_TYPE, CircleCITestIndex::class); |
82
|
|
|
$this->assertEquals(200, $result->getResponse()->getStatusCode()); |
83
|
|
|
|
84
|
|
|
$this->service->updateItems($items, SolrCoreService::DELETE_TYPE, CircleCITestIndex::class); |
85
|
|
|
$this->assertEquals(0, $index->doSearch($query)->getTotalItems()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testUpdateItemsEmptyArray() |
89
|
|
|
{ |
90
|
|
|
$index = new CircleCITestIndex(); |
91
|
|
|
$query = new BaseQuery(); |
92
|
|
|
$this->service->doManipulate(ArrayList::create(), SolrCoreService::DELETE_TYPE_ALL, $index); |
93
|
|
|
$this->assertEquals(0, $index->doSearch($query)->getTotalItems()); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @expectedException \LogicException |
98
|
|
|
*/ |
99
|
|
|
public function testWrongUpdateItems() |
100
|
|
|
{ |
101
|
|
|
$items = SiteTree::get(); |
102
|
|
|
|
103
|
|
|
$this->service->updateItems($items, SolrCoreService::UPDATE_TYPE, 'NonExisting'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testCoreStatus() |
107
|
|
|
{ |
108
|
|
|
$status = $this->service->coreStatus(CircleCITestIndex::class); |
109
|
|
|
$deprecatedStatus = $this->service->coreIsActive(CircleCITestIndex::class); |
110
|
|
|
$this->assertEquals($status->getVersion(), $deprecatedStatus->getVersion()); |
111
|
|
|
$this->assertEquals($status->getNumberOfDocuments(), $deprecatedStatus->getNumberOfDocuments()); |
112
|
|
|
$this->assertEquals($status->getCoreName(), $deprecatedStatus->getCoreName()); |
113
|
|
|
|
114
|
|
|
$this->assertEquals('CircleCITestIndex', $status->getCoreName()); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testCoreUnload() |
118
|
|
|
{ |
119
|
|
|
$status1 = $this->service->coreStatus(CircleCITestIndex::class); |
120
|
|
|
$this->service->coreUnload(CircleCITestIndex::class); |
121
|
|
|
$status2 = $this->service->coreStatus(CircleCITestIndex::class); |
122
|
|
|
$this->assertEquals(0, $status2->getUptime()); |
123
|
|
|
$this->assertNotEquals($status1->getUptime(), $status2->getUptime()); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function testGetSetClient() |
127
|
|
|
{ |
128
|
|
|
$client = $this->service->getClient(); |
129
|
|
|
$this->assertInstanceOf(Client::class, $client); |
130
|
|
|
$service = $this->service->setClient($client); |
131
|
|
|
$this->assertInstanceOf(SolrCoreService::class, $service); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function testGetSolrVersion() |
135
|
|
|
{ |
136
|
|
|
$this->assertEquals(5, $this->service->getSolrVersion()); |
137
|
|
|
$version4 = [ |
138
|
|
|
'responseHeader' => |
139
|
|
|
[ |
140
|
|
|
'status' => 0, |
141
|
|
|
'QTime' => 10, |
142
|
|
|
], |
143
|
|
|
'mode' => 'std', |
144
|
|
|
'solr_home' => '/var/solr/data', |
145
|
|
|
'lucene' => |
146
|
|
|
[ |
147
|
|
|
'solr-spec-version' => '4.3.2', |
148
|
|
|
] |
149
|
|
|
]; |
150
|
|
|
$mock = new MockHandler([ |
151
|
|
|
new Response(200, ['X-Foo' => 'Bar'], json_encode($version4)), |
152
|
|
|
]); |
153
|
|
|
$handler = HandlerStack::create($mock); |
154
|
|
|
|
155
|
|
|
$this->assertEquals(4, $this->service->getSolrVersion($handler)); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
protected function setUp() |
159
|
|
|
{ |
160
|
|
|
$this->service = new SolrCoreService(); |
161
|
|
|
|
162
|
|
|
return parent::setUp(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
protected function tearDown() |
166
|
|
|
{ |
167
|
|
|
/** @var SolrConfigureTask $task */ |
168
|
|
|
$task = Injector::inst()->get(SolrConfigureTask::class); |
169
|
|
|
$task->run(new NullHTTPRequest()); |
170
|
|
|
/** @var SolrIndexTask $task */ |
171
|
|
|
$task = Injector::inst()->get(SolrIndexTask::class); |
172
|
|
|
$task->run(new NullHTTPRequest()); |
173
|
|
|
parent::tearDown(); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|