xref: /dokuwiki/_test/tests/Search/Index/MemoryIndexTest.php (revision 06053dca2fac9a1da4eb1accf8c2488942da5d2a)
1ede46466SAndreas Gohr<?php
2ede46466SAndreas Gohr
3ede46466SAndreas Gohrnamespace dokuwiki\test\Search\Index;
4ede46466SAndreas Gohr
5ede46466SAndreas Gohruse dokuwiki\Search\Index\Lock;
6ede46466SAndreas Gohruse dokuwiki\Search\Index\MemoryIndex;
7ede46466SAndreas Gohr
8ede46466SAndreas Gohrclass MemoryIndexTest extends AbstractIndexTestCase
9ede46466SAndreas Gohr{
10ede46466SAndreas Gohr    protected function getIndex()
11ede46466SAndreas Gohr    {
12ede46466SAndreas Gohr        static $count = 0;
13ede46466SAndreas Gohr        return new MemoryIndex('index', $count++, true);
14ede46466SAndreas Gohr    }
15ede46466SAndreas Gohr
16ede46466SAndreas Gohr    public function tearDown(): void
17ede46466SAndreas Gohr    {
18ede46466SAndreas Gohr        Lock::releaseAll();
19ede46466SAndreas Gohr        parent::tearDown();
20ede46466SAndreas Gohr    }
21ede46466SAndreas Gohr
22ede46466SAndreas Gohr    public function testChangeRow()
23ede46466SAndreas Gohr    {
24ede46466SAndreas Gohr        $index = $this->getIndex();
25ede46466SAndreas Gohr
26ede46466SAndreas Gohr        $index->changeRow(5, 'test');
27ede46466SAndreas Gohr        $full = $this->getInaccessibleProperty($index, 'data');
28ede46466SAndreas Gohr        $this->assertEquals(6, count($full));
29ede46466SAndreas Gohr
30ede46466SAndreas Gohr        $index->changeRow(3, 'foo');
31ede46466SAndreas Gohr        $full = $this->getInaccessibleProperty($index, 'data');
32ede46466SAndreas Gohr        $this->assertEquals(6, count($full));
33ede46466SAndreas Gohr
34ede46466SAndreas Gohr        $index->changeRow(5, 'bar');
35ede46466SAndreas Gohr        $index->changeRow(7, 'bang');
36ede46466SAndreas Gohr
37ede46466SAndreas Gohr        $full = $this->getInaccessibleProperty($index, 'data');
38ede46466SAndreas Gohr        $this->assertEquals(['', '', '', 'foo', '', 'bar', '', 'bang'], $full);
39ede46466SAndreas Gohr
40ede46466SAndreas Gohr        $index->save();
41ede46466SAndreas Gohr        $full = file($index->getFilename(), FILE_IGNORE_NEW_LINES);
42ede46466SAndreas Gohr        $this->assertEquals(['', '', '', 'foo', '', 'bar', '', 'bang'], $full);
43ede46466SAndreas Gohr    }
44ede46466SAndreas Gohr
45ede46466SAndreas Gohr    public function testRetrieveRow()
46ede46466SAndreas Gohr    {
47ede46466SAndreas Gohr        $index = $this->getIndex();
48ede46466SAndreas Gohr        $index->changeRow(5, 'test');
49ede46466SAndreas Gohr        $this->assertEquals('test', $index->retrieveRow(5));
50ede46466SAndreas Gohr
51*06053dcaSAndreas Gohr        // out of bounds line should be empty and not modify the index
52ede46466SAndreas Gohr        $this->assertEquals('', $index->retrieveRow(10));
53ede46466SAndreas Gohr        $index->save();
54ede46466SAndreas Gohr        $full = file($index->getFilename(), FILE_IGNORE_NEW_LINES);
55*06053dcaSAndreas Gohr        $this->assertEquals(6, count($full));
56ede46466SAndreas Gohr    }
57ede46466SAndreas Gohr
58ede46466SAndreas Gohr    public function testSave()
59ede46466SAndreas Gohr    {
60ede46466SAndreas Gohr        $index = $this->getIndex();
61ede46466SAndreas Gohr        $this->assertFileDoesNotExist($index->getFilename());
62ede46466SAndreas Gohr        $this->assertFalse($index->isDirty());
63ede46466SAndreas Gohr
64ede46466SAndreas Gohr        $index->changeRow(0, '');
65ede46466SAndreas Gohr        $this->assertTrue($index->isDirty());
66ede46466SAndreas Gohr        $index->save();
67ede46466SAndreas Gohr        $this->assertFalse($index->isDirty());
68ede46466SAndreas Gohr        $this->assertEquals(1, filesize($index->getFilename())); // new line
69ede46466SAndreas Gohr
70ede46466SAndreas Gohr        $index->changeRow(3, 'test');
71ede46466SAndreas Gohr        $this->assertTrue($index->isDirty());
72ede46466SAndreas Gohr        $index->save();
73ede46466SAndreas Gohr        $this->assertFalse($index->isDirty());
74ede46466SAndreas Gohr        $this->assertEquals(8, filesize($index->getFilename())); // 4 new lines + test
75ede46466SAndreas Gohr
76ede46466SAndreas Gohr        $index->getRowID('test'); // existing entry
77ede46466SAndreas Gohr        $index->save();
78ede46466SAndreas Gohr        $this->assertFalse($index->isDirty());
79ede46466SAndreas Gohr    }
80ede46466SAndreas Gohr
81ede46466SAndreas Gohr}
82