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