1ede46466SAndreas Gohr<?php 2ede46466SAndreas Gohr 3ede46466SAndreas Gohrnamespace dokuwiki\test\Search\Index; 4ede46466SAndreas Gohr 5ede46466SAndreas Gohruse dokuwiki\Search\Index\AbstractIndex; 6ede46466SAndreas Gohr 7ede46466SAndreas Gohrabstract class AbstractIndexTestCase extends \DokuWikiTest 8ede46466SAndreas Gohr{ 9ede46466SAndreas Gohr 10ede46466SAndreas Gohr /** 11ede46466SAndreas Gohr * Return a new writable index 12ede46466SAndreas Gohr * 13ede46466SAndreas Gohr * @return AbstractIndex 14ede46466SAndreas Gohr */ 15ede46466SAndreas Gohr abstract protected function getIndex(); 16ede46466SAndreas Gohr 17ede46466SAndreas Gohr public function testGetRowID() 18ede46466SAndreas Gohr { 19ede46466SAndreas Gohr $index = $this->getIndex(); 20ede46466SAndreas Gohr $result = $index->getRowID('foo'); 21ede46466SAndreas Gohr $index->save(); 22ede46466SAndreas Gohr $this->assertEquals(0, $result); 23ede46466SAndreas Gohr 24ede46466SAndreas Gohr $result = $index->getRowID('bar'); 25ede46466SAndreas Gohr $index->save(); 26ede46466SAndreas Gohr $this->assertEquals(1, $result); 27ede46466SAndreas Gohr 28ede46466SAndreas Gohr $result = $index->getRowID('foo'); 29ede46466SAndreas Gohr $index->save(); 30ede46466SAndreas Gohr $this->assertEquals(0, $result); 31ede46466SAndreas Gohr } 32ede46466SAndreas Gohr 33ede46466SAndreas Gohr public function testGetRowIDs() 34ede46466SAndreas Gohr { 35ede46466SAndreas Gohr $index = $this->getIndex(); 36ede46466SAndreas Gohr $result = $index->getRowIDs(['foo', 'bar', 'baz']); 37ede46466SAndreas Gohr $index->save(); 38ede46466SAndreas Gohr $this->assertEquals(['foo' => 0, 'bar' => 1, 'baz' => 2], $result); 39ede46466SAndreas Gohr 40ede46466SAndreas Gohr $result = $index->getRowIDs(['foo', 'bang', 'baz']); 41ede46466SAndreas Gohr $index->save(); 42ede46466SAndreas Gohr $this->assertEquals(['foo' => 0, 'baz' => 2, 'bang' => 3], $result); 43ede46466SAndreas Gohr } 44ede46466SAndreas Gohr 45ede46466SAndreas Gohr public function testRetrieve() 46ede46466SAndreas Gohr { 47ede46466SAndreas Gohr $index = $this->getIndex(); 48ede46466SAndreas Gohr $index->getRowIDs(['foo', 'bar', 'baz']); // add data 49ede46466SAndreas Gohr $index->save(); 50ede46466SAndreas Gohr 51ede46466SAndreas Gohr $this->assertEquals('bar', $index->retrieveRow(1)); 52*06053dcaSAndreas Gohr $this->assertEquals('', $index->retrieveRow(5)); // non existent 53ede46466SAndreas Gohr $index->save(); 54ede46466SAndreas Gohr 55*06053dcaSAndreas Gohr // only rows 0-2 exist (from getRowIDs), 4 and 7 do not and are ignored 56*06053dcaSAndreas Gohr $this->assertEquals([0 => 'foo', 2 => 'baz'], $index->retrieveRows([0, 2, 4, 7])); 57ede46466SAndreas Gohr $index->save(); 58ede46466SAndreas Gohr } 59ede46466SAndreas Gohr 60ede46466SAndreas Gohr public function testSearch() 61ede46466SAndreas Gohr { 62ede46466SAndreas Gohr $index = $this->getIndex(); 63ede46466SAndreas Gohr $index->getRowIDs(['foo', 'bar', 'baz', 'bazzel']); 64ede46466SAndreas Gohr $index->save(); 65ede46466SAndreas Gohr 66ede46466SAndreas Gohr $result = $index->search('/^ba.$/'); 67ede46466SAndreas Gohr $this->assertEquals( 68ede46466SAndreas Gohr [1 => 'bar', 2 => 'baz'], 69ede46466SAndreas Gohr $result 70ede46466SAndreas Gohr ); 71ede46466SAndreas Gohr } 72ede46466SAndreas Gohr 73ede46466SAndreas Gohr public function testIterable() 74ede46466SAndreas Gohr { 75ede46466SAndreas Gohr $index = $this->getIndex(); 76ede46466SAndreas Gohr $index->getRowIDs(['foo', 'bar', 'baz']); 77ede46466SAndreas Gohr $index->save(); 78ede46466SAndreas Gohr 79ede46466SAndreas Gohr $result = iterator_to_array($index); 80ede46466SAndreas Gohr $this->assertEquals([0 => 'foo', 1 => 'bar', 2 => 'baz'], $result); 81ede46466SAndreas Gohr } 8221fbd01bSAndreas Gohr 8321fbd01bSAndreas Gohr public function testCountEmpty() 8421fbd01bSAndreas Gohr { 8521fbd01bSAndreas Gohr $index = $this->getIndex(); 8621fbd01bSAndreas Gohr $this->assertEquals(0, count($index)); 8721fbd01bSAndreas Gohr } 8821fbd01bSAndreas Gohr 8921fbd01bSAndreas Gohr public function testCountWithData() 9021fbd01bSAndreas Gohr { 9121fbd01bSAndreas Gohr $index = $this->getIndex(); 9221fbd01bSAndreas Gohr $index->getRowIDs(['foo', 'bar', 'baz']); 9321fbd01bSAndreas Gohr $index->save(); 9421fbd01bSAndreas Gohr $this->assertEquals(3, count($index)); 9521fbd01bSAndreas Gohr } 9621fbd01bSAndreas Gohr 9721fbd01bSAndreas Gohr public function testCountWithGaps() 9821fbd01bSAndreas Gohr { 9921fbd01bSAndreas Gohr $index = $this->getIndex(); 10021fbd01bSAndreas Gohr $index->changeRow(5, 'test'); 10121fbd01bSAndreas Gohr $index->save(); 10221fbd01bSAndreas Gohr $this->assertEquals(6, count($index)); // lines 0-5 10321fbd01bSAndreas Gohr } 104ede46466SAndreas Gohr} 105