xref: /dokuwiki/_test/tests/Search/Index/MemoryIndexTest.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\Lock;
6*ede46466SAndreas Gohruse dokuwiki\Search\Index\MemoryIndex;
7*ede46466SAndreas Gohr
8*ede46466SAndreas Gohrclass MemoryIndexTest 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 MemoryIndex('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 = $this->getInaccessibleProperty($index, 'data');
28*ede46466SAndreas Gohr        $this->assertEquals(6, count($full));
29*ede46466SAndreas Gohr
30*ede46466SAndreas Gohr        $index->changeRow(3, 'foo');
31*ede46466SAndreas Gohr        $full = $this->getInaccessibleProperty($index, 'data');
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 = $this->getInaccessibleProperty($index, 'data');
38*ede46466SAndreas Gohr        $this->assertEquals(['', '', '', 'foo', '', 'bar', '', 'bang'], $full);
39*ede46466SAndreas Gohr
40*ede46466SAndreas Gohr        $index->save();
41*ede46466SAndreas Gohr        $full = file($index->getFilename(), FILE_IGNORE_NEW_LINES);
42*ede46466SAndreas Gohr        $this->assertEquals(['', '', '', 'foo', '', 'bar', '', 'bang'], $full);
43*ede46466SAndreas Gohr    }
44*ede46466SAndreas Gohr
45*ede46466SAndreas Gohr    public function testRetrieveRow()
46*ede46466SAndreas Gohr    {
47*ede46466SAndreas Gohr        $index = $this->getIndex();
48*ede46466SAndreas Gohr        $index->changeRow(5, 'test');
49*ede46466SAndreas Gohr        $this->assertEquals('test', $index->retrieveRow(5));
50*ede46466SAndreas Gohr
51*ede46466SAndreas Gohr        // out of bounds line should be empty, but pad the file
52*ede46466SAndreas Gohr        $this->assertEquals('', $index->retrieveRow(10));
53*ede46466SAndreas Gohr        $index->save();
54*ede46466SAndreas Gohr        $full = file($index->getFilename(), FILE_IGNORE_NEW_LINES);
55*ede46466SAndreas Gohr        $this->assertEquals(11, count($full));
56*ede46466SAndreas Gohr    }
57*ede46466SAndreas Gohr
58*ede46466SAndreas Gohr    public function testSave()
59*ede46466SAndreas Gohr    {
60*ede46466SAndreas Gohr        $index = $this->getIndex();
61*ede46466SAndreas Gohr        $this->assertFileDoesNotExist($index->getFilename());
62*ede46466SAndreas Gohr        $this->assertFalse($index->isDirty());
63*ede46466SAndreas Gohr
64*ede46466SAndreas Gohr        $index->changeRow(0, '');
65*ede46466SAndreas Gohr        $this->assertTrue($index->isDirty());
66*ede46466SAndreas Gohr        $index->save();
67*ede46466SAndreas Gohr        $this->assertFalse($index->isDirty());
68*ede46466SAndreas Gohr        $this->assertEquals(1, filesize($index->getFilename())); // new line
69*ede46466SAndreas Gohr
70*ede46466SAndreas Gohr        $index->changeRow(3, 'test');
71*ede46466SAndreas Gohr        $this->assertTrue($index->isDirty());
72*ede46466SAndreas Gohr        $index->save();
73*ede46466SAndreas Gohr        $this->assertFalse($index->isDirty());
74*ede46466SAndreas Gohr        $this->assertEquals(8, filesize($index->getFilename())); // 4 new lines + test
75*ede46466SAndreas Gohr
76*ede46466SAndreas Gohr        $index->getRowID('test'); // existing entry
77*ede46466SAndreas Gohr        $index->save();
78*ede46466SAndreas Gohr        $this->assertFalse($index->isDirty());
79*ede46466SAndreas Gohr    }
80*ede46466SAndreas Gohr
81*ede46466SAndreas Gohr}
82