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