1<?php 2 3namespace dokuwiki\test\Search\Index; 4 5use dokuwiki\Search\Index\Lock; 6use dokuwiki\Search\Index\MemoryIndex; 7 8class MemoryIndexTest extends AbstractIndexTestCase 9{ 10 protected function getIndex() 11 { 12 static $count = 0; 13 return new MemoryIndex('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 = $this->getInaccessibleProperty($index, 'data'); 28 $this->assertEquals(6, count($full)); 29 30 $index->changeRow(3, 'foo'); 31 $full = $this->getInaccessibleProperty($index, 'data'); 32 $this->assertEquals(6, count($full)); 33 34 $index->changeRow(5, 'bar'); 35 $index->changeRow(7, 'bang'); 36 37 $full = $this->getInaccessibleProperty($index, 'data'); 38 $this->assertEquals(['', '', '', 'foo', '', 'bar', '', 'bang'], $full); 39 40 $index->save(); 41 $full = file($index->getFilename(), FILE_IGNORE_NEW_LINES); 42 $this->assertEquals(['', '', '', 'foo', '', 'bar', '', 'bang'], $full); 43 } 44 45 public function testRetrieveRow() 46 { 47 $index = $this->getIndex(); 48 $index->changeRow(5, 'test'); 49 $this->assertEquals('test', $index->retrieveRow(5)); 50 51 // out of bounds line should be empty and not modify the index 52 $this->assertEquals('', $index->retrieveRow(10)); 53 $index->save(); 54 $full = file($index->getFilename(), FILE_IGNORE_NEW_LINES); 55 $this->assertEquals(6, count($full)); 56 } 57 58 public function testSave() 59 { 60 $index = $this->getIndex(); 61 $this->assertFileDoesNotExist($index->getFilename()); 62 $this->assertFalse($index->isDirty()); 63 64 $index->changeRow(0, ''); 65 $this->assertTrue($index->isDirty()); 66 $index->save(); 67 $this->assertFalse($index->isDirty()); 68 $this->assertEquals(1, filesize($index->getFilename())); // new line 69 70 $index->changeRow(3, 'test'); 71 $this->assertTrue($index->isDirty()); 72 $index->save(); 73 $this->assertFalse($index->isDirty()); 74 $this->assertEquals(8, filesize($index->getFilename())); // 4 new lines + test 75 76 $index->getRowID('test'); // existing entry 77 $index->save(); 78 $this->assertFalse($index->isDirty()); 79 } 80 81} 82