SearchFixturesTest   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 199
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 129
c 3
b 0
f 0
dl 0
loc 199
rs 10
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testAndSearch() 0 13 2
A setUp() 0 14 1
A testORSearch() 0 14 2
A testSimilar() 0 15 2
A testFlickrFacetsIncludeSearchTerm() 0 51 2
A checkSumDocumentCount() 0 11 2
A testFlickrFacetsEmptySearchTerm() 0 49 2
1
<?php declare(strict_types = 1);
2
3
namespace Suilven\ManticoreSearch\Tests\Service;
4
5
use SilverStripe\CMS\Model\SiteTree;
6
use SilverStripe\Dev\SapphireTest;
7
use Suilven\FreeTextSearch\Container\Facet;
8
use Suilven\FreeTextSearch\Helper\BulkIndexingHelper;
9
use Suilven\FreeTextSearch\Indexes;
10
use Suilven\FreeTextSearch\Types\SearchParamTypes;
11
use Suilven\ManticoreSearch\Helper\ReconfigureIndexesHelper;
12
use Suilven\ManticoreSearch\Service\Searcher;
13
use Suilven\ManticoreSearch\Tests\Models\FlickrAuthor;
14
use Suilven\ManticoreSearch\Tests\Models\FlickrPhoto;
15
use Suilven\ManticoreSearch\Tests\Models\FlickrSet;
16
use Suilven\ManticoreSearch\Tests\Models\FlickrTag;
17
18
class SearchFixturesTest extends SapphireTest
19
{
20
    protected static $fixture_file = ['tests/fixtures/sitetree.yml', 'tests/fixtures/flickrphotos.yml'];
21
22
    protected static $extra_dataobjects = [
23
        FlickrPhoto::class,
24
        FlickrTag::class,
25
        FlickrSet::class,
26
        FlickrAuthor::class,
27
    ];
28
29
    /** @var int */
30
    private static $pageID;
0 ignored issues
show
introduced by
The private property $pageID is not used, and could be removed.
Loading history...
31
32
    public function setUp(): void
33
    {
34
        parent::setUp();
35
36
        /** @var \Suilven\FreeTextSearch\Indexes $indexesService */
37
        $indexesService = new Indexes();
38
        $indexesArray = $indexesService->getIndexes();
39
        $helper = new ReconfigureIndexesHelper();
40
        $helper->reconfigureIndexes($indexesArray);
41
42
        $helper = new BulkIndexingHelper();
43
        $helper->bulkIndex('sitetree');
44
        $helper->bulkIndex('flickrphotos');
45
        $helper->bulkIndex('members');
46
    }
47
48
49
    public function testSimilar(): void
50
    {
51
        $page = $this->objFromFixture(SiteTree::class, 'sitetree_49');
52
        $searcher = new Searcher();
53
        $searcher->setIndexName('sitetree');
54
        $result = $searcher->searchForSimilar($page);
55
56
        // 1 is the original doc, 3 others contain sheep, 1 contains mussy an 1 contains shuttlecock
57
        $this->assertEquals(6, $result->getTotaNumberOfResults());
58
        $hits = $result->getRecords();
59
        $ids = [];
60
        foreach ($hits as $hit) {
61
            $ids[] = $hit->ID;
62
        }
63
        $this->assertEquals([49, 45, 40, 47, 21, 36], $ids);
64
    }
65
66
67
    public function testAndSearch(): void
68
    {
69
        $searcher = new Searcher();
70
        $searcher->setIndexName('sitetree');
71
        $searcher->setSearchType(SearchParamTypes::AND);
72
        $result = $searcher->search('sheep shuttlecock');
73
        $this->assertEquals(1, $result->getTotaNumberOfResults());
74
        $hits = $result->getRecords();
75
        $ids = [];
76
        foreach ($hits as $hit) {
77
            $ids[] = $hit->ID;
78
        }
79
        $this->assertEquals([49], $ids);
80
    }
81
82
83
    public function testORSearch(): void
84
    {
85
        $searcher = new Searcher();
86
        $searcher->setIndexName('sitetree');
87
        $searcher->setSearchType(SearchParamTypes::OR);
88
        $result = $searcher->search('sheep shuttlecock');
89
        $this->assertEquals(5, $result->getTotaNumberOfResults());
90
        $hits = $result->getRecords();
91
        $ids = [];
92
        foreach ($hits as $hit) {
93
            $ids[] = $hit->ID;
94
        }
95
96
        $this->assertEquals([49, 45, 47, 21, 36], $ids);
97
    }
98
99
100
    public function testFlickrFacetsEmptySearchTerm(): void
101
    {
102
        $searcher = new Searcher();
103
        $searcher->setIndexName('flickrphotos');
104
        $searcher->setSearchType(SearchParamTypes::AND);
105
        $searcher->setFacettedTokens(['ISO', 'Aperture', 'Orientation']);
106
        $result = $searcher->search('*');
107
        $this->assertEquals(50, $result->getTotaNumberOfResults());
108
        $hits = $result->getRecords();
109
        $ids = [];
110
        foreach ($hits as $hit) {
111
            $ids[] = $hit->ID;
112
        }
113
114
        // TODO What is the default search order - this was 1 to 15 consecutively
115
        $this->assertEquals([10, 49, 7, 33, 18, 37, 27, 29, 31, 6, 14, 35, 48, 22, 25], $ids);
116
117
118
        $facets = $result->getFacets();
119
        $this->assertEquals([
120
            1600 => 7,
121
            800 => 11,
122
            400 => 6,
123
            200 => 8,
124
            100 => 10,
125
            64 => 4,
126
            25 => 4,
127
        ], $facets[0]->asKeyValueArray());
128
129
        $this->assertEquals([
130
            32 => 1,
131
            27 => 9,
132
            22 => 6,
133
            16 => 7,
134
            11 => 7,
135
            8 => 5,
136
            5 => 6,
137
            4 => 3,
138
            2 => 6,
139
        ], $facets[1]->asKeyValueArray());
140
141
        $this->assertEquals([
142
            90 => 20,
143
            0 => 30,
144
        ], $facets[2]->asKeyValueArray());
145
146
        $this->checkSumDocumentCount($facets[0], 50);
147
        $this->checkSumDocumentCount($facets[1], 50);
148
        $this->checkSumDocumentCount($facets[2], 50);
149
    }
150
151
152
    public function testFlickrFacetsIncludeSearchTerm(): void
153
    {
154
        $searcher = new Searcher();
155
        $searcher->setIndexName('flickrphotos');
156
        $searcher->setSearchType(SearchParamTypes::AND);
157
        $searcher->setFacettedTokens(['ISO', 'Aperture', 'Orientation']);
158
        $result = $searcher->search('Tom');
159
        $this->assertEquals(13, $result->getTotaNumberOfResults());
160
        $hits = $result->getRecords();
161
        $ids = [];
162
        foreach ($hits as $hit) {
163
            $ids[] = $hit->ID;
164
        }
165
166
        $this->assertEquals([48, 29, 47, 23, 9, 32, 34, 16, 12, 5, 24, 43, 46], $ids);
167
168
        /** @var array<\Suilven\FreeTextSearch\Container\Facet> $facets */
169
        $facets = $result->getFacets();
170
171
        $this->assertEquals('ISO', $facets[0]->getName());
172
        $this->assertEquals('Aperture', $facets[1]->getName());
173
        $this->assertEquals('Orientation', $facets[2]->getName());
174
175
176
        $this->assertEquals([
177
            1600 => 2,
178
            800 => 3,
179
            400 => 1,
180
            200 => 2,
181
            100 => 2,
182
            25 => 3,
183
        ], $facets[0]->asKeyValueArray());
184
185
186
        $this->assertEquals([
187
            27 => 1,
188
            16 => 3,
189
            11 => 2,
190
            8 => 3,
191
            5 => 1,
192
            2 => 3,
193
        ], $facets[1]->asKeyValueArray());
194
195
        $this->assertEquals([
196
            90 => 5,
197
            0 => 8,
198
        ], $facets[2]->asKeyValueArray());
199
200
        $this->checkSumDocumentCount($facets[0], 13);
201
        $this->checkSumDocumentCount($facets[1], 13);
202
        $this->checkSumDocumentCount($facets[2], 13);
203
    }
204
205
206
    private function checkSumDocumentCount(Facet $facet, int $expectedCount): void
207
    {
208
        $sum = 0;
209
        $kvArray = $facet->asKeyValueArray();
210
211
        /** @var \Suilven\FreeTextSearch\Container\FacetCount $key */
212
        foreach (\array_keys($kvArray) as $key) {
213
            $sum += $kvArray[$key];
214
        }
215
216
        $this->assertEquals($expectedCount, $sum);
217
    }
218
}
219