Passed
Pull Request — master (#123)
by
unknown
05:29
created

SolrSearchTest::setUpData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
/**
4
 * (c) Kitodo. Key to digital objects e.V. <[email protected]>
5
 *
6
 * This file is part of the Kitodo and TYPO3 projects.
7
 *
8
 * @license GNU General Public License version 3 or later.
9
 * For the full copyright and license information, please read the
10
 * LICENSE.txt file that was distributed with this source code.
11
 */
12
13
namespace Kitodo\Dlf\Tests\Functional\Common;
14
15
use Kitodo\Dlf\Common\Solr\Solr;
16
use Kitodo\Dlf\Common\Solr\SolrSearch;
17
use Kitodo\Dlf\Domain\Repository\DocumentRepository;
18
use Kitodo\Dlf\Domain\Repository\SolrCoreRepository;
19
use Kitodo\Dlf\Tests\Functional\FunctionalTestCase;
20
21
class SolrSearchTest extends FunctionalTestCase
22
{
23
    private static array $databaseFixtures = [
24
        __DIR__ . '/../../Fixtures/Common/solrcores.csv'
25
    ];
26
27
    private static array $solrFixtures = [
28
        __DIR__ . '/../../Fixtures/Common/documents_1.solr.json'
29
    ];
30
31
    private Solr $solr;
32
    private SolrCoreRepository $solrCoreRepository;
33
34
    public function setUp(): void
35
    {
36
        parent::setUp();
37
        $this->setUpData(self::$databaseFixtures);
38
        $this->solr = $this->setUpSolr(5, 0, self::$solrFixtures);
39
    }
40
41
    /**
42
     * @test
43
     */
44
    public function canPrepareAndSubmit()
45
    {
46
        $documentRepository = $this->initializeRepository(DocumentRepository::class, 0);
47
        $solrCoreName = $this->solrCoreRepository->findByUid(5)->getIndexName();
48
        $settings = ['solrcore' => $solrCoreName, 'storagePid' => 0];
49
50
        $resultSet = $this->solr->searchRaw(['core' => 5, 'collection' => 1]);
51
        $this->assertCount(33, $resultSet);
52
53
        $params1 = ['query' => '*'];
54
        $search = new SolrSearch($documentRepository, null, $settings, $params1);
55
        $search->prepare();
56
        $this->assertEquals(33, $search->getNumFound());
57
        $this->assertEquals(3, $search->getSolrResults()['numberOfToplevels']);
58
        $this->assertCount(15, $search->getSolrResults()['documents']);
59
60
        $params2 = ['query' => '10 Keyboard pieces'];
61
        $search2 = new SolrSearch($documentRepository, null, $settings, $params2);
62
        $search2->prepare();
63
        $this->assertEquals(1, $search2->getNumFound());
64
        $this->assertEquals(1, $search2->getSolrResults()['numberOfToplevels']);
65
        $this->assertCount(1, $search2->getSolrResults()['documents']);
66
67
        $params3 = ['query' => 'foobar'];
68
        $search3 = new SolrSearch($documentRepository, null, $settings, $params3);
69
        $search3->prepare();
70
        $this->assertEquals(0, $search3->getNumFound());
71
        $this->assertEquals(0, $search3->getSolrResults()['numberOfToplevels']);
72
        $this->assertCount(0, $search3->getSolrResults()['documents']);
73
    }
74
75
    protected function setUpData($databaseFixtures): void
76
    {
77
        foreach ($databaseFixtures as $filePath) {
78
            $this->importCSVDataSet($filePath);
79
        }
80
        $this->initializeRepository(DocumentRepository::class, 0);
81
    }
82
83
    protected function setUpSolr($uid, $storagePid, $solrFixtures)
84
    {
85
        $this->solrCoreRepository = $this->initializeRepository(SolrCoreRepository::class, $storagePid);
86
87
        // Setup Solr only once for all tests in this suite
88
        static $solr = null;
89
90
        if ($solr === null) {
91
            $coreName = Solr::createCore();
92
            $solr = Solr::getInstance($coreName);
93
            foreach ($solrFixtures as $filePath) {
94
                $this->importSolrDocuments($solr, $filePath);
95
            }
96
        }
97
98
        $coreModel = $this->solrCoreRepository->findByUid($uid);
99
        $coreModel->setIndexName($solr->core);
100
        $this->solrCoreRepository->update($coreModel);
101
        $this->persistenceManager->persistAll();
102
        return $solr;
103
    }
104
}
105