Passed
Push — master ( 160c08...0f35aa )
by Gordon
04:48 queued 02:17
created

SearchFixturesTest::testORSearch()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 10
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 13
rs 9.9332
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\Helper\BulkIndexingHelper;
8
use Suilven\FreeTextSearch\Indexes;
9
use Suilven\FreeTextSearch\Types\SearchParamTypes;
0 ignored issues
show
Bug introduced by
The type Suilven\FreeTextSearch\Types\SearchParamTypes was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Suilven\ManticoreSearch\Helper\ReconfigureIndexesHelper;
11
use Suilven\ManticoreSearch\Service\Searcher;
12
use Suilven\ManticoreSearch\Tests\Models\FlickrAuthor;
13
use Suilven\ManticoreSearch\Tests\Models\FlickrPhoto;
14
use Suilven\ManticoreSearch\Tests\Models\FlickrSet;
15
use Suilven\ManticoreSearch\Tests\Models\FlickrTag;
16
17
class SearchFixturesTest extends SapphireTest
18
{
19
    protected static $fixture_file = ['tests/fixtures/sitetree.yml', 'tests/fixtures/flickrphotos.yml'];
20
21
    protected static $extra_dataobjects = [
22
        FlickrPhoto::class,
23
        FlickrTag::class,
24
        FlickrSet::class,
25
        FlickrAuthor::class,
26
    ];
27
28
    /** @var int */
29
    private static $pageID;
0 ignored issues
show
introduced by
The private property $pageID is not used, and could be removed.
Loading history...
30
31
    public function setUp(): void
32
    {
33
        parent::setUp();
34
35
        /** @var \Suilven\FreeTextSearch\Indexes $indexesService */
36
        $indexesService = new Indexes();
37
        $indexesArray = $indexesService->getIndexes();
38
        $helper = new ReconfigureIndexesHelper();
39
        $helper->reconfigureIndexes($indexesArray);
40
41
        \error_log('INDEXING');
42
        $helper = new BulkIndexingHelper();
43
        $helper->bulkIndex('sitetree');
44
        \error_log('/INDEXING');
45
    }
46
47
48
    public function testSimilar(): void
49
    {
50
        $page = $this->objFromFixture(SiteTree::class, 'sitetree_49');
51
        $searcher = new Searcher();
52
        $searcher->setIndexName('sitetree');
53
        $result = $searcher->searchForSimilar($page);
54
55
        // 1 is the original doc, 3 others contain sheep, 1 contains mussy an 1 contains shuttlecock
56
        $this->assertEquals(6, $result->getTotaNumberOfResults());
57
        $hits = $result->getRecords();
58
        $ids = [];
59
        foreach ($hits as $hit) {
60
            $ids[] = $hit->ID;
61
        }
62
63
        $this->assertEquals([49, 40, 45, 21, 36, 47], $ids);
64
    }
65
66
67
    public function testAndSearch(): void
68
    {
69
        $searcher = new Searcher();
70
        $searcher->setIndexName('sitetree');
71
        $searcher->setSearchType(SearchParamTypes::AND);
0 ignored issues
show
Bug introduced by
The method setSearchType() does not exist on Suilven\ManticoreSearch\Service\Searcher. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

71
        $searcher->/** @scrutinizer ignore-call */ 
72
                   setSearchType(SearchParamTypes::AND);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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
        $this->assertEquals([49, 45, 21, 36, 47], $ids);
96
    }
97
}
98