PageRenderTime 24ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/standard/tags/release-1.0.0/tests/Zend/Search/Lucene/LuceneTest.php

https://github.com/jorgenils/zend-framework
PHP | 315 lines | 215 code | 80 blank | 20 comment | 15 complexity | 208540eae46f2126758f17e7e9f86b08 MD5 | raw file
  1. <?php
  2. /**
  3. * @category Zend
  4. * @package Zend_Search_Lucene
  5. * @subpackage UnitTests
  6. */
  7. /**
  8. * Zend_Search_Lucene
  9. */
  10. require_once 'Zend/Search/Lucene.php';
  11. /**
  12. * PHPUnit test case
  13. */
  14. require_once 'PHPUnit/Framework/TestCase.php';
  15. /**
  16. * @category Zend
  17. * @package Zend_Search_Lucene
  18. * @subpackage UnitTests
  19. */
  20. class Zend_Search_Lucene_LuceneTest extends PHPUnit_Framework_TestCase
  21. {
  22. public function testCreate()
  23. {
  24. $index = Zend_Search_Lucene::create(dirname(__FILE__) . '/_files/_index');
  25. $this->assertTrue($index instanceof Zend_Search_Lucene_Interface);
  26. }
  27. public function testOpen()
  28. {
  29. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_files/_indexSample');
  30. $this->assertTrue($index instanceof Zend_Search_Lucene_Interface);
  31. }
  32. public function testDefaultSearchField()
  33. {
  34. $currentDefaultSearchField = Zend_Search_Lucene::getDefaultSearchField();
  35. $this->assertEquals($currentDefaultSearchField, null);
  36. Zend_Search_Lucene::setDefaultSearchField('anotherField');
  37. $this->assertEquals(Zend_Search_Lucene::getDefaultSearchField(), 'anotherField');
  38. Zend_Search_Lucene::setDefaultSearchField($currentDefaultSearchField);
  39. }
  40. public function testCount()
  41. {
  42. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_files/_indexSample');
  43. $this->assertEquals($index->count(), 10);
  44. }
  45. public function testMaxDoc()
  46. {
  47. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_files/_indexSample');
  48. $this->assertEquals($index->maxDoc(), 10);
  49. }
  50. public function testNumDocs()
  51. {
  52. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_files/_indexSample');
  53. $this->assertEquals($index->numDocs(), 9);
  54. }
  55. public function testIsDeleted()
  56. {
  57. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_files/_indexSample');
  58. $this->assertFalse($index->isDeleted(3));
  59. $this->assertTrue($index->isDeleted(6));
  60. }
  61. public function testMaxBufferedDocs()
  62. {
  63. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_files/_indexSample');
  64. $currentMaxBufferedDocs = $index->getMaxBufferedDocs();
  65. $index->setMaxBufferedDocs(234);
  66. $this->assertEquals($index->getMaxBufferedDocs(), 234);
  67. $index->setMaxBufferedDocs($currentMaxBufferedDocs);
  68. }
  69. public function testMaxMergeDocs()
  70. {
  71. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_files/_indexSample');
  72. $currentMaxMergeDocs = $index->getMaxMergeDocs();
  73. $index->setMaxMergeDocs(34);
  74. $this->assertEquals($index->getMaxMergeDocs(), 34);
  75. $index->setMaxMergeDocs($currentMaxMergeDocs);
  76. }
  77. public function testMergeFactor()
  78. {
  79. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_files/_indexSample');
  80. $currentMergeFactor = $index->getMergeFactor();
  81. $index->setMergeFactor(113);
  82. $this->assertEquals($index->getMergeFactor(), 113);
  83. $index->setMergeFactor($currentMergeFactor);
  84. }
  85. public function testFind()
  86. {
  87. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_files/_indexSample');
  88. $hits = $index->find('submitting');
  89. $this->assertEquals(count($hits), 3);
  90. }
  91. public function testGetFieldNames()
  92. {
  93. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_files/_indexSample');
  94. $this->assertTrue(array_values($index->getFieldNames()) == array('path', 'modified', 'contents'));
  95. }
  96. public function testGetDocument()
  97. {
  98. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_files/_indexSample');
  99. $doc = $index->getDocument(3);
  100. $this->assertTrue($doc instanceof Zend_Search_Lucene_Document);
  101. $this->assertEquals($doc->path, 'IndexSource/about-pear.html');
  102. }
  103. public function testHasTerm()
  104. {
  105. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_files/_indexSample');
  106. $this->assertTrue($index->hasTerm(new Zend_Search_Lucene_Index_Term('packages', 'contents')));
  107. $this->assertFalse($index->hasTerm(new Zend_Search_Lucene_Index_Term('nonusedword', 'contents')));
  108. }
  109. public function testTermDocs()
  110. {
  111. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_files/_indexSample');
  112. $this->assertTrue(array_values( $index->termDocs(new Zend_Search_Lucene_Index_Term('packages', 'contents')) ) ==
  113. array(0, 2, 6, 7, 8));
  114. }
  115. public function testTermPositions()
  116. {
  117. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_files/_indexSample');
  118. $this->assertTrue($index->termPositions(new Zend_Search_Lucene_Index_Term('packages', 'contents')) ==
  119. array(0 => array(174),
  120. 2 => array(40, 742),
  121. 6 => array(6, 156, 163),
  122. 7 => array(194),
  123. 8 => array(55, 190, 405)));
  124. }
  125. public function testDocFreq()
  126. {
  127. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_files/_indexSample');
  128. $this->assertEquals($index->docFreq(new Zend_Search_Lucene_Index_Term('packages', 'contents')), 5);
  129. }
  130. public function testGetSimilarity()
  131. {
  132. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_files/_indexSample');
  133. $this->assertTrue($index->getSimilarity() instanceof Zend_Search_Lucene_Search_Similarity);
  134. }
  135. public function testNorm()
  136. {
  137. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_files/_indexSample');
  138. $this->assertTrue(abs($index->norm(3, 'contents') - 0.054688) < 0.000001);
  139. }
  140. public function testHasDeletions()
  141. {
  142. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_files/_indexSample');
  143. $this->assertTrue($index->hasDeletions());
  144. }
  145. public function testDelete()
  146. {
  147. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_files/_indexSample');
  148. $this->assertFalse($index->isDeleted(2));
  149. $index->delete(2);
  150. $this->assertTrue($index->isDeleted(2));
  151. unset($index);
  152. $index1 = Zend_Search_Lucene::open(dirname(__FILE__) . '/_files/_indexSample');
  153. $this->assertTrue($index1->isDeleted(2));
  154. unset($index1);
  155. unlink(dirname(__FILE__) . '/_files/_indexSample/_1.del');
  156. }
  157. public function testAddDocument()
  158. {
  159. $index = Zend_Search_Lucene::create(dirname(__FILE__) . '/_files/_index');
  160. $indexSourceDir = dirname(__FILE__) . '/_files/_indexSource';
  161. $dir = opendir($indexSourceDir);
  162. while (($file = readdir($dir)) !== false) {
  163. if (is_dir($indexSourceDir . '/' . $file)) {
  164. continue;
  165. }
  166. if (strcasecmp(substr($file, strlen($file)-5), '.html') != 0) {
  167. continue;
  168. }
  169. // Create new Document from a file
  170. $doc = new Zend_Search_Lucene_Document();
  171. $doc->addField(Zend_Search_Lucene_Field::Text('path', 'IndexSource/' . $file));
  172. $doc->addField(Zend_Search_Lucene_Field::Keyword( 'modified', filemtime($indexSourceDir . '/' . $file) ));
  173. $f = fopen($indexSourceDir . '/' . $file,'rb');
  174. $byteCount = filesize($indexSourceDir . '/' . $file);
  175. $data = '';
  176. while ( $byteCount > 0 && ($nextBlock = fread($f, $byteCount)) != false ) {
  177. $data .= $nextBlock;
  178. $byteCount -= strlen($nextBlock);
  179. }
  180. fclose($f);
  181. $doc->addField(Zend_Search_Lucene_Field::Text('contents', $data, 'ISO-8859-1'));
  182. // Add document to the index
  183. $index->addDocument($doc);
  184. }
  185. closedir($dir);
  186. unset($index);
  187. $index1 = Zend_Search_Lucene::open(dirname(__FILE__) . '/_files/_index');
  188. $this->assertTrue($index1 instanceof Zend_Search_Lucene_Interface);
  189. }
  190. public function testOptimize()
  191. {
  192. $index = Zend_Search_Lucene::create(dirname(__FILE__) . '/_files/_index');
  193. $index->setMaxBufferedDocs(2);
  194. $indexSourceDir = dirname(__FILE__) . '/_files/_indexSource';
  195. $dir = opendir($indexSourceDir);
  196. while (($file = readdir($dir)) !== false) {
  197. if (is_dir($indexSourceDir . '/' . $file)) {
  198. continue;
  199. }
  200. if (strcasecmp(substr($file, strlen($file)-5), '.html') != 0) {
  201. continue;
  202. }
  203. // Create new Document from a file
  204. $doc = new Zend_Search_Lucene_Document();
  205. $doc->addField(Zend_Search_Lucene_Field::Text('path', 'IndexSource/' . $file));
  206. $doc->addField(Zend_Search_Lucene_Field::Keyword( 'modified', filemtime($indexSourceDir . '/' . $file) ));
  207. $f = fopen($indexSourceDir . '/' . $file,'rb');
  208. $byteCount = filesize($indexSourceDir . '/' . $file);
  209. $data = '';
  210. while ( $byteCount > 0 && ($nextBlock = fread($f, $byteCount)) != false ) {
  211. $data .= $nextBlock;
  212. $byteCount -= strlen($nextBlock);
  213. }
  214. fclose($f);
  215. $doc->addField(Zend_Search_Lucene_Field::Text('contents', $data, 'ISO-8859-1'));
  216. // Add document to the index
  217. $index->addDocument($doc);
  218. }
  219. closedir($dir);
  220. unset($index);
  221. $index1 = Zend_Search_Lucene::open(dirname(__FILE__) . '/_files/_index');
  222. $this->assertTrue($index1 instanceof Zend_Search_Lucene_Interface);
  223. $index1->delete(6);
  224. $index1->optimize();
  225. $index2 = Zend_Search_Lucene::open(dirname(__FILE__) . '/_files/_index');
  226. $this->assertTrue($index2 instanceof Zend_Search_Lucene_Interface);
  227. $hits = $index2->find('submitting');
  228. $this->assertEquals(count($hits), 3);
  229. }
  230. public function testTerms()
  231. {
  232. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_files/_indexSample');
  233. $this->assertEquals(count($index->terms()), 607);
  234. }
  235. }