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\NullHTTPRequest; |
16
|
|
|
use SilverStripe\Core\Injector\Injector; |
17
|
|
|
use SilverStripe\Dev\SapphireTest; |
18
|
|
|
use SilverStripe\ORM\ArrayList; |
19
|
|
|
use Solarium\Core\Client\Client; |
20
|
|
|
|
21
|
|
|
class SolrCoreServiceTest extends SapphireTest |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var SolrCoreService |
25
|
|
|
*/ |
26
|
|
|
protected $service; |
27
|
|
|
|
28
|
|
|
public function testIndexes() |
29
|
|
|
{ |
30
|
|
|
$service = new SolrCoreService(); |
31
|
|
|
|
32
|
|
|
$expected = [ |
33
|
|
|
CircleCITestIndex::class, |
34
|
|
|
TestIndex::class, |
35
|
|
|
TestIndexTwo::class, |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
$this->assertEquals($expected, $service->getValidIndexes()); |
39
|
|
|
$this->assertEquals([CircleCITestIndex::class], $service->getValidIndexes(CircleCITestIndex::class)); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @expectedException \LogicException |
45
|
|
|
*/ |
46
|
|
|
public function testUpdateItemsFail() |
47
|
|
|
{ |
48
|
|
|
$this->service->updateItems(null, SolrCoreService::CREATE_TYPE); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @expectedException \LogicException |
53
|
|
|
*/ |
54
|
|
|
public function testUpdateItemsFailWrongCall() |
55
|
|
|
{ |
56
|
|
|
$this->service->updateItems(['test'], SolrCoreService::DELETE_TYPE_ALL); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testUpdateItems() |
60
|
|
|
{ |
61
|
|
|
$index = new CircleCITestIndex(); |
62
|
|
|
$query = new BaseQuery(); |
63
|
|
|
$query->addTerm('*:*'); |
64
|
|
|
$items = SiteTree::get(); |
65
|
|
|
|
66
|
|
|
$result = $this->service->updateItems($items, SolrCoreService::UPDATE_TYPE, CircleCITestIndex::class); |
67
|
|
|
$this->assertEquals(200, $result->getResponse()->getStatusCode()); |
68
|
|
|
|
69
|
|
|
$this->service->updateItems($items, SolrCoreService::DELETE_TYPE, CircleCITestIndex::class); |
70
|
|
|
$this->assertEquals(0, $index->doSearch($query)->getTotalItems()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testUpdateItemsEmptyArray() |
74
|
|
|
{ |
75
|
|
|
$index = new CircleCITestIndex(); |
76
|
|
|
$query = new BaseQuery(); |
77
|
|
|
$this->service->doManipulate(ArrayList::create(), SolrCoreService::DELETE_TYPE_ALL, $index); |
78
|
|
|
$this->assertEquals(0, $index->doSearch($query)->getTotalItems()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @expectedException \LogicException |
83
|
|
|
*/ |
84
|
|
|
public function testWrongUpdateItems() |
85
|
|
|
{ |
86
|
|
|
$items = SiteTree::get(); |
87
|
|
|
|
88
|
|
|
$this->service->updateItems($items, SolrCoreService::UPDATE_TYPE, 'NonExisting'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testCoreStatus() |
92
|
|
|
{ |
93
|
|
|
$status = $this->service->coreStatus(CircleCITestIndex::class); |
94
|
|
|
$deprecatedStatus = $this->service->coreIsActive(CircleCITestIndex::class); |
95
|
|
|
$this->assertEquals($status->getVersion(), $deprecatedStatus->getVersion()); |
96
|
|
|
$this->assertEquals($status->getNumberOfDocuments(), $deprecatedStatus->getNumberOfDocuments()); |
97
|
|
|
$this->assertEquals($status->getCoreName(), $deprecatedStatus->getCoreName()); |
98
|
|
|
|
99
|
|
|
$this->assertEquals('CircleCITestIndex', $status->getCoreName()); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function testCoreUnload() |
103
|
|
|
{ |
104
|
|
|
$status1 = $this->service->coreStatus(CircleCITestIndex::class); |
105
|
|
|
$this->service->coreUnload(CircleCITestIndex::class); |
106
|
|
|
$status2 = $this->service->coreStatus(CircleCITestIndex::class); |
107
|
|
|
$this->assertEquals(0, $status2->getUptime()); |
108
|
|
|
$this->assertNotEquals($status1->getUptime(), $status2->getUptime()); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function testGetSetClient() |
112
|
|
|
{ |
113
|
|
|
$client = $this->service->getClient(); |
114
|
|
|
$this->assertInstanceOf(Client::class, $client); |
115
|
|
|
$service = $this->service->setClient($client); |
116
|
|
|
$this->assertInstanceOf(SolrCoreService::class, $service); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function testGetSolrVersion() |
120
|
|
|
{ |
121
|
|
|
$this->assertEquals(5, $this->service->getSolrVersion()); |
122
|
|
|
$version4 = [ |
123
|
|
|
'responseHeader' => |
124
|
|
|
[ |
125
|
|
|
'status' => 0, |
126
|
|
|
'QTime' => 10, |
127
|
|
|
], |
128
|
|
|
'mode' => 'std', |
129
|
|
|
'solr_home' => '/var/solr/data', |
130
|
|
|
'lucene' => |
131
|
|
|
[ |
132
|
|
|
'solr-spec-version' => '4.3.2', |
133
|
|
|
] |
134
|
|
|
]; |
135
|
|
|
$mock = new MockHandler([ |
136
|
|
|
new Response(200, ['X-Foo' => 'Bar'], json_encode($version4)), |
137
|
|
|
]); |
138
|
|
|
$handler = HandlerStack::create($mock); |
139
|
|
|
|
140
|
|
|
$this->assertEquals(4, $this->service->getSolrVersion($handler)); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
protected function setUp() |
144
|
|
|
{ |
145
|
|
|
$this->service = new SolrCoreService(); |
146
|
|
|
|
147
|
|
|
return parent::setUp(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
protected function tearDown() |
151
|
|
|
{ |
152
|
|
|
/** @var SolrConfigureTask $task */ |
153
|
|
|
$task = Injector::inst()->get(SolrConfigureTask::class); |
154
|
|
|
$task->run(new NullHTTPRequest()); |
155
|
|
|
/** @var SolrIndexTask $task */ |
156
|
|
|
$task = Injector::inst()->get(SolrIndexTask::class); |
157
|
|
|
$task->run(new NullHTTPRequest()); |
158
|
|
|
parent::tearDown(); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|