xref: /dokuwiki/_test/tests/Search/Index/FileIndexTest.php (revision 21fbd01b3c3eea88b767376b7b158f31f0f63127)
1<?php
2
3namespace dokuwiki\test\Search\Index;
4
5use dokuwiki\Search\Index\FileIndex;
6use dokuwiki\Search\Index\Lock;
7
8class FileIndexTest extends AbstractIndexTestCase
9{
10    protected function getIndex()
11    {
12        static $count = 0;
13        return new FileIndex('index', $count++, true);
14    }
15
16    public function tearDown(): void
17    {
18        Lock::releaseAll();
19        parent::tearDown();
20    }
21
22    public function testChangeRow()
23    {
24        $index = $this->getIndex();
25
26        $index->changeRow(5, 'test');
27        $full = file($index->getFilename(), FILE_IGNORE_NEW_LINES);
28        $this->assertEquals(6, count($full));
29
30        $index->changeRow(3, 'foo');
31        $full = file($index->getFilename(), FILE_IGNORE_NEW_LINES);
32        $this->assertEquals(6, count($full));
33
34        $index->changeRow(5, 'bar');
35        $index->changeRow(7, 'bang');
36
37        $full = file($index->getFilename(), FILE_IGNORE_NEW_LINES);
38        $this->assertEquals(['', '', '', 'foo', '', 'bar', '', 'bang'], $full);
39    }
40
41    public function testRetrieveRow()
42    {
43        $index = $this->getIndex();
44        $index->changeRow(5, 'test');
45        $this->assertEquals('test', $index->retrieveRow(5));
46
47        // out of bounds line should be empty, but pad the file
48        $this->assertEquals('', $index->retrieveRow(10));
49        $full = file($index->getFilename(), FILE_IGNORE_NEW_LINES);
50        $this->assertEquals(11, count($full));
51    }
52
53}
54