xref: /dokuwiki/_test/tests/Search/Index/FileIndexTest.php (revision 06053dca2fac9a1da4eb1accf8c2488942da5d2a)
1ede46466SAndreas Gohr<?php
2ede46466SAndreas Gohr
3ede46466SAndreas Gohrnamespace dokuwiki\test\Search\Index;
4ede46466SAndreas Gohr
5ede46466SAndreas Gohruse dokuwiki\Search\Index\FileIndex;
6ede46466SAndreas Gohruse dokuwiki\Search\Index\Lock;
7ede46466SAndreas Gohr
8ede46466SAndreas Gohrclass FileIndexTest extends AbstractIndexTestCase
9ede46466SAndreas Gohr{
10ede46466SAndreas Gohr    protected function getIndex()
11ede46466SAndreas Gohr    {
12ede46466SAndreas Gohr        static $count = 0;
13ede46466SAndreas Gohr        return new FileIndex('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 = file($index->getFilename(), FILE_IGNORE_NEW_LINES);
28ede46466SAndreas Gohr        $this->assertEquals(6, count($full));
29ede46466SAndreas Gohr
30ede46466SAndreas Gohr        $index->changeRow(3, 'foo');
31ede46466SAndreas Gohr        $full = file($index->getFilename(), FILE_IGNORE_NEW_LINES);
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 = file($index->getFilename(), FILE_IGNORE_NEW_LINES);
38ede46466SAndreas Gohr        $this->assertEquals(['', '', '', 'foo', '', 'bar', '', 'bang'], $full);
39ede46466SAndreas Gohr    }
40ede46466SAndreas Gohr
41ede46466SAndreas Gohr    public function testRetrieveRow()
42ede46466SAndreas Gohr    {
43ede46466SAndreas Gohr        $index = $this->getIndex();
44ede46466SAndreas Gohr        $index->changeRow(5, 'test');
45ede46466SAndreas Gohr        $this->assertEquals('test', $index->retrieveRow(5));
46ede46466SAndreas Gohr
47*06053dcaSAndreas Gohr        // out of bounds line should be empty and not modify the file
48ede46466SAndreas Gohr        $this->assertEquals('', $index->retrieveRow(10));
49ede46466SAndreas Gohr        $full = file($index->getFilename(), FILE_IGNORE_NEW_LINES);
50*06053dcaSAndreas Gohr        $this->assertEquals(6, count($full));
51ede46466SAndreas Gohr    }
52ede46466SAndreas Gohr
53ede46466SAndreas Gohr}
54