xref: /dokuwiki/_test/tests/Search/Index/AbstractIndexTestCase.php (revision 21fbd01b3c3eea88b767376b7b158f31f0f63127)
1<?php
2
3namespace dokuwiki\test\Search\Index;
4
5use dokuwiki\Search\Index\AbstractIndex;
6
7abstract class AbstractIndexTestCase extends \DokuWikiTest
8{
9
10    /**
11     * Return a new writable index
12     *
13     * @return AbstractIndex
14     */
15    abstract protected function getIndex();
16
17    public function testGetRowID()
18    {
19        $index = $this->getIndex();
20        $result = $index->getRowID('foo');
21        $index->save();
22        $this->assertEquals(0, $result);
23
24        $result = $index->getRowID('bar');
25        $index->save();
26        $this->assertEquals(1, $result);
27
28        $result = $index->getRowID('foo');
29        $index->save();
30        $this->assertEquals(0, $result);
31    }
32
33    public function testGetRowIDs()
34    {
35        $index = $this->getIndex();
36        $result = $index->getRowIDs(['foo', 'bar', 'baz']);
37        $index->save();
38        $this->assertEquals(['foo' => 0, 'bar' => 1, 'baz' => 2], $result);
39
40        $result = $index->getRowIDs(['foo', 'bang', 'baz']);
41        $index->save();
42        $this->assertEquals(['foo' => 0, 'baz' => 2, 'bang' => 3], $result);
43    }
44
45    public function testRetrieve()
46    {
47        $index = $this->getIndex();
48        $index->getRowIDs(['foo', 'bar', 'baz']); // add data
49        $index->save();
50
51        $this->assertEquals('bar', $index->retrieveRow(1));
52        $this->assertEquals('', $index->retrieveRow(5)); // non existent, but will be created with padding
53        $index->save();
54
55        // rows up to 5 exist now, 7 does not and is ignored
56        $this->assertEquals([0 => 'foo', 2 => 'baz', 4 => ''], $index->retrieveRows([0, 2, 4, 7]));
57        $index->save();
58    }
59
60    public function testSearch()
61    {
62        $index = $this->getIndex();
63        $index->getRowIDs(['foo', 'bar', 'baz', 'bazzel']);
64        $index->save();
65
66        $result = $index->search('/^ba.$/');
67        $this->assertEquals(
68            [1 => 'bar', 2 => 'baz'],
69            $result
70        );
71    }
72
73    public function testIterable()
74    {
75        $index = $this->getIndex();
76        $index->getRowIDs(['foo', 'bar', 'baz']);
77        $index->save();
78
79        $result = iterator_to_array($index);
80        $this->assertEquals([0 => 'foo', 1 => 'bar', 2 => 'baz'], $result);
81    }
82
83    public function testCountEmpty()
84    {
85        $index = $this->getIndex();
86        $this->assertEquals(0, count($index));
87    }
88
89    public function testCountWithData()
90    {
91        $index = $this->getIndex();
92        $index->getRowIDs(['foo', 'bar', 'baz']);
93        $index->save();
94        $this->assertEquals(3, count($index));
95    }
96
97    public function testCountWithGaps()
98    {
99        $index = $this->getIndex();
100        $index->changeRow(5, 'test');
101        $index->save();
102        $this->assertEquals(6, count($index)); // lines 0-5
103    }
104}
105