PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/Search/Lucene/LuceneTest.php

https://bitbucket.org/ksekar/campus
PHP | 576 lines | 382 code | 147 blank | 47 comment | 28 complexity | a34747be0e491ca09f14678017c3410b MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Search_Lucene
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: LuceneTest.php 24594 2012-01-05 21:27:01Z matthew $
  21. */
  22. if (!defined('PHPUnit_MAIN_METHOD')) {
  23. define('PHPUnit_MAIN_METHOD', 'Zend_Search_Lucene_LuceneTest::main');
  24. }
  25. /**
  26. * Zend_Search_Lucene
  27. */
  28. require_once 'Zend/Search/Lucene.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_Search_Lucene
  32. * @subpackage UnitTests
  33. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. * @group Zend_Search_Lucene
  36. */
  37. class Zend_Search_Lucene_LuceneTest extends PHPUnit_Framework_TestCase
  38. {
  39. public static function main()
  40. {
  41. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  42. $result = PHPUnit_TextUI_TestRunner::run($suite);
  43. }
  44. private function _clearDirectory($dirName)
  45. {
  46. if (!file_exists($dirName) || !is_dir($dirName)) {
  47. return;
  48. }
  49. // remove files from temporary direcytory
  50. $dir = opendir($dirName);
  51. while (($file = readdir($dir)) !== false) {
  52. if (!is_dir($dirName . '/' . $file)) {
  53. @unlink($dirName . '/' . $file);
  54. }
  55. }
  56. closedir($dir);
  57. }
  58. public function testCreate()
  59. {
  60. $index = Zend_Search_Lucene::create(dirname(__FILE__) . '/_index/_files');
  61. $this->assertTrue($index instanceof Zend_Search_Lucene_Interface);
  62. unset($index);
  63. $this->_clearDirectory(dirname(__FILE__) . '/_index/_files');
  64. }
  65. public function testOpen()
  66. {
  67. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_indexSample/_files');
  68. $this->assertTrue($index instanceof Zend_Search_Lucene_Interface);
  69. }
  70. public function testOpenNonCompound()
  71. {
  72. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_indexSample/_nonCompoundIndexFiles');
  73. $this->assertTrue($index instanceof Zend_Search_Lucene_Interface);
  74. }
  75. public function testDefaultSearchField()
  76. {
  77. $currentDefaultSearchField = Zend_Search_Lucene::getDefaultSearchField();
  78. $this->assertEquals($currentDefaultSearchField, null);
  79. Zend_Search_Lucene::setDefaultSearchField('anotherField');
  80. $this->assertEquals(Zend_Search_Lucene::getDefaultSearchField(), 'anotherField');
  81. Zend_Search_Lucene::setDefaultSearchField($currentDefaultSearchField);
  82. }
  83. public function testCount()
  84. {
  85. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_indexSample/_files');
  86. $this->assertEquals($index->count(), 10);
  87. }
  88. public function testMaxDoc()
  89. {
  90. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_indexSample/_files');
  91. $this->assertEquals($index->maxDoc(), 10);
  92. }
  93. public function testNumDocs()
  94. {
  95. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_indexSample/_files');
  96. $this->assertEquals($index->numDocs(), 9);
  97. }
  98. public function testIsDeleted()
  99. {
  100. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_indexSample/_files');
  101. $this->assertFalse($index->isDeleted(3));
  102. $this->assertTrue($index->isDeleted(6));
  103. }
  104. public function testMaxBufferedDocs()
  105. {
  106. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_indexSample/_files');
  107. $currentMaxBufferedDocs = $index->getMaxBufferedDocs();
  108. $index->setMaxBufferedDocs(234);
  109. $this->assertEquals($index->getMaxBufferedDocs(), 234);
  110. $index->setMaxBufferedDocs($currentMaxBufferedDocs);
  111. }
  112. public function testMaxMergeDocs()
  113. {
  114. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_indexSample/_files');
  115. $currentMaxMergeDocs = $index->getMaxMergeDocs();
  116. $index->setMaxMergeDocs(34);
  117. $this->assertEquals($index->getMaxMergeDocs(), 34);
  118. $index->setMaxMergeDocs($currentMaxMergeDocs);
  119. }
  120. public function testMergeFactor()
  121. {
  122. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_indexSample/_files');
  123. $currentMergeFactor = $index->getMergeFactor();
  124. $index->setMergeFactor(113);
  125. $this->assertEquals($index->getMergeFactor(), 113);
  126. $index->setMergeFactor($currentMergeFactor);
  127. }
  128. public function testFind()
  129. {
  130. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_indexSample/_files');
  131. $hits = $index->find('submitting');
  132. $this->assertEquals(count($hits), 3);
  133. }
  134. public function testGetFieldNames()
  135. {
  136. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_indexSample/_files');
  137. $this->assertTrue(array_values($index->getFieldNames()) == array('path', 'modified', 'contents'));
  138. }
  139. public function testGetDocument()
  140. {
  141. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_indexSample/_files');
  142. $doc = $index->getDocument(3);
  143. $this->assertTrue($doc instanceof Zend_Search_Lucene_Document);
  144. $this->assertEquals($doc->path, 'IndexSource/about-pear.html');
  145. }
  146. public function testHasTerm()
  147. {
  148. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_indexSample/_files');
  149. $this->assertTrue($index->hasTerm(new Zend_Search_Lucene_Index_Term('packages', 'contents')));
  150. $this->assertFalse($index->hasTerm(new Zend_Search_Lucene_Index_Term('nonusedword', 'contents')));
  151. }
  152. public function testTermDocs()
  153. {
  154. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_indexSample/_files');
  155. $this->assertTrue(array_values( $index->termDocs(new Zend_Search_Lucene_Index_Term('packages', 'contents')) ) ==
  156. array(0, 2, 6, 7, 8));
  157. }
  158. public function testTermPositions()
  159. {
  160. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_indexSample/_files');
  161. $this->assertTrue($index->termPositions(new Zend_Search_Lucene_Index_Term('packages', 'contents')) ==
  162. array(0 => array(174),
  163. 2 => array(40, 742),
  164. 6 => array(6, 156, 163),
  165. 7 => array(194),
  166. 8 => array(55, 190, 405)));
  167. }
  168. public function testDocFreq()
  169. {
  170. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_indexSample/_files');
  171. $this->assertEquals($index->docFreq(new Zend_Search_Lucene_Index_Term('packages', 'contents')), 5);
  172. }
  173. public function testGetSimilarity()
  174. {
  175. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_indexSample/_files');
  176. $this->assertTrue($index->getSimilarity() instanceof Zend_Search_Lucene_Search_Similarity);
  177. }
  178. public function testNorm()
  179. {
  180. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_indexSample/_files');
  181. $this->assertTrue(abs($index->norm(3, 'contents') - 0.054688) < 0.000001);
  182. }
  183. public function testHasDeletions()
  184. {
  185. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_indexSample/_files');
  186. $this->assertTrue($index->hasDeletions());
  187. }
  188. public function testDelete()
  189. {
  190. // Copy index sample into _files directory
  191. $sampleIndexDir = dirname(__FILE__) . '/_indexSample/_files';
  192. $tempIndexDir = dirname(__FILE__) . '/_files';
  193. if (!is_dir($tempIndexDir)) {
  194. mkdir($tempIndexDir);
  195. }
  196. $this->_clearDirectory($tempIndexDir);
  197. $indexDir = opendir($sampleIndexDir);
  198. while (($file = readdir($indexDir)) !== false) {
  199. if (!is_dir($sampleIndexDir . '/' . $file)) {
  200. copy($sampleIndexDir . '/' . $file, $tempIndexDir . '/' . $file);
  201. }
  202. }
  203. closedir($indexDir);
  204. $index = Zend_Search_Lucene::open($tempIndexDir);
  205. $this->assertFalse($index->isDeleted(2));
  206. $index->delete(2);
  207. $this->assertTrue($index->isDeleted(2));
  208. $index->commit();
  209. unset($index);
  210. $index1 = Zend_Search_Lucene::open($tempIndexDir);
  211. $this->assertTrue($index1->isDeleted(2));
  212. unset($index1);
  213. $this->_clearDirectory($tempIndexDir);
  214. }
  215. public function testAddDocument()
  216. {
  217. $index = Zend_Search_Lucene::create(dirname(__FILE__) . '/_index/_files');
  218. $indexSourceDir = dirname(__FILE__) . '/_indexSource/_files';
  219. $dir = opendir($indexSourceDir);
  220. while (($file = readdir($dir)) !== false) {
  221. if (is_dir($indexSourceDir . '/' . $file)) {
  222. continue;
  223. }
  224. if (strcasecmp(substr($file, strlen($file)-5), '.html') != 0) {
  225. continue;
  226. }
  227. // Create new Document from a file
  228. $doc = new Zend_Search_Lucene_Document();
  229. $doc->addField(Zend_Search_Lucene_Field::Text('path', 'IndexSource/' . $file));
  230. $doc->addField(Zend_Search_Lucene_Field::Keyword( 'modified', filemtime($indexSourceDir . '/' . $file) ));
  231. $f = fopen($indexSourceDir . '/' . $file,'rb');
  232. $byteCount = filesize($indexSourceDir . '/' . $file);
  233. $data = '';
  234. while ( $byteCount > 0 && ($nextBlock = fread($f, $byteCount)) != false ) {
  235. $data .= $nextBlock;
  236. $byteCount -= strlen($nextBlock);
  237. }
  238. fclose($f);
  239. $doc->addField(Zend_Search_Lucene_Field::Text('contents', $data, 'ISO-8859-1'));
  240. // Add document to the index
  241. $index->addDocument($doc);
  242. }
  243. closedir($dir);
  244. unset($index);
  245. $index1 = Zend_Search_Lucene::open(dirname(__FILE__) . '/_index/_files');
  246. $this->assertTrue($index1 instanceof Zend_Search_Lucene_Interface);
  247. unset($index1);
  248. $this->_clearDirectory(dirname(__FILE__) . '/_index/_files');
  249. }
  250. public function testOptimize()
  251. {
  252. $index = Zend_Search_Lucene::create(dirname(__FILE__) . '/_index/_files');
  253. $index->setMaxBufferedDocs(2);
  254. $indexSourceDir = dirname(__FILE__) . '/_indexSource/_files';
  255. $dir = opendir($indexSourceDir);
  256. while (($file = readdir($dir)) !== false) {
  257. if (is_dir($indexSourceDir . '/' . $file)) {
  258. continue;
  259. }
  260. if (strcasecmp(substr($file, strlen($file)-5), '.html') != 0) {
  261. continue;
  262. }
  263. // Create new Document from a file
  264. $doc = new Zend_Search_Lucene_Document();
  265. $doc->addField(Zend_Search_Lucene_Field::Keyword('path', 'IndexSource/' . $file));
  266. $doc->addField(Zend_Search_Lucene_Field::Keyword( 'modified', filemtime($indexSourceDir . '/' . $file) ));
  267. $f = fopen($indexSourceDir . '/' . $file,'rb');
  268. $byteCount = filesize($indexSourceDir . '/' . $file);
  269. $data = '';
  270. while ( $byteCount > 0 && ($nextBlock = fread($f, $byteCount)) != false ) {
  271. $data .= $nextBlock;
  272. $byteCount -= strlen($nextBlock);
  273. }
  274. fclose($f);
  275. $doc->addField(Zend_Search_Lucene_Field::Text('contents', $data, 'ISO-8859-1'));
  276. // Add document to the index
  277. $index->addDocument($doc);
  278. }
  279. closedir($dir);
  280. unset($index);
  281. $index1 = Zend_Search_Lucene::open(dirname(__FILE__) . '/_index/_files');
  282. $this->assertTrue($index1 instanceof Zend_Search_Lucene_Interface);
  283. $pathTerm = new Zend_Search_Lucene_Index_Term('IndexSource/contributing.html', 'path');
  284. $contributingDocs = $index1->termDocs($pathTerm);
  285. foreach ($contributingDocs as $id) {
  286. $index1->delete($id);
  287. }
  288. $index1->optimize();
  289. unset($index1);
  290. $index2 = Zend_Search_Lucene::open(dirname(__FILE__) . '/_index/_files');
  291. $this->assertTrue($index2 instanceof Zend_Search_Lucene_Interface);
  292. $hits = $index2->find('submitting');
  293. $this->assertEquals(count($hits), 3);
  294. unset($index2);
  295. $this->_clearDirectory(dirname(__FILE__) . '/_index/_files');
  296. }
  297. public function testTerms()
  298. {
  299. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_indexSample/_files');
  300. $this->assertEquals(count($index->terms()), 607);
  301. }
  302. public function testTermsStreamInterface()
  303. {
  304. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_indexSample/_files');
  305. $terms = array();
  306. $index->resetTermsStream();
  307. while ($index->currentTerm() !== null) {
  308. $terms[] = $index->currentTerm();
  309. $index->nextTerm();
  310. }
  311. $this->assertEquals(count($terms), 607);
  312. }
  313. public function testTermsStreamInterfaceSkipTo()
  314. {
  315. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_indexSample/_files');
  316. $terms = array();
  317. $index->resetTermsStream();
  318. $index->skipTo(new Zend_Search_Lucene_Index_Term('one', 'contents'));
  319. while ($index->currentTerm() !== null) {
  320. $terms[] = $index->currentTerm();
  321. $index->nextTerm();
  322. }
  323. $this->assertEquals(count($terms), 244);
  324. }
  325. public function testTermsStreamInterfaceSkipToTermsRetrieving()
  326. {
  327. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_indexSample/_files');
  328. $terms = array();
  329. $index->resetTermsStream();
  330. $index->skipTo(new Zend_Search_Lucene_Index_Term('one', 'contents'));
  331. $terms[] = $index->currentTerm();
  332. $terms[] = $index->nextTerm();
  333. $terms[] = $index->nextTerm();
  334. $index->closeTermsStream();
  335. $this->assertTrue($terms ==
  336. array(new Zend_Search_Lucene_Index_Term('one', 'contents'),
  337. new Zend_Search_Lucene_Index_Term('only', 'contents'),
  338. new Zend_Search_Lucene_Index_Term('open', 'contents'),
  339. ));
  340. }
  341. public function testTermsStreamInterfaceSkipToTermsRetrievingZeroTermsCase()
  342. {
  343. $index = Zend_Search_Lucene::create(dirname(__FILE__) . '/_index/_files');
  344. // Zero terms
  345. $doc = new Zend_Search_Lucene_Document();
  346. $doc->addField(Zend_Search_Lucene_Field::Text('contents', ''));
  347. $index->addDocument($doc);
  348. unset($index);
  349. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_index/_files');
  350. $index->resetTermsStream();
  351. $index->skipTo(new Zend_Search_Lucene_Index_Term('term', 'contents'));
  352. $this->assertTrue($index->currentTerm() === null);
  353. $index->closeTermsStream();
  354. unset($index);
  355. $this->_clearDirectory(dirname(__FILE__) . '/_index/_files');
  356. }
  357. public function testTermsStreamInterfaceSkipToTermsRetrievingOneTermsCase()
  358. {
  359. $index = Zend_Search_Lucene::create(dirname(__FILE__) . '/_index/_files');
  360. // Zero terms
  361. $doc = new Zend_Search_Lucene_Document();
  362. $doc->addField(Zend_Search_Lucene_Field::Text('contents', 'someterm'));
  363. $index->addDocument($doc);
  364. unset($index);
  365. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_index/_files');
  366. $index->resetTermsStream();
  367. $index->skipTo(new Zend_Search_Lucene_Index_Term('term', 'contents'));
  368. $this->assertTrue($index->currentTerm() === null);
  369. $index->closeTermsStream();
  370. unset($index);
  371. $this->_clearDirectory(dirname(__FILE__) . '/_index/_files');
  372. }
  373. public function testTermsStreamInterfaceSkipToTermsRetrievingTwoTermsCase()
  374. {
  375. $index = Zend_Search_Lucene::create(dirname(__FILE__) . '/_index/_files');
  376. // Zero terms
  377. $doc = new Zend_Search_Lucene_Document();
  378. $doc->addField(Zend_Search_Lucene_Field::Text('contents', 'someterm word'));
  379. $index->addDocument($doc);
  380. unset($index);
  381. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_index/_files');
  382. $index->resetTermsStream();
  383. $index->skipTo(new Zend_Search_Lucene_Index_Term('term', 'contents'));
  384. $this->assertTrue($index->currentTerm() == new Zend_Search_Lucene_Index_Term('word', 'contents'));
  385. $index->closeTermsStream();
  386. unset($index);
  387. $this->_clearDirectory(dirname(__FILE__) . '/_index/_files');
  388. }
  389. /**
  390. * @group ZF-7518
  391. */
  392. public function testTermsStreamInterfaceSkipToMatchedTerm()
  393. {
  394. $index = Zend_Search_Lucene::create(dirname(__FILE__) . '/_index/_files');
  395. $doc = new Zend_Search_Lucene_Document();
  396. $doc->addField(Zend_Search_Lucene_Field::Keyword('test', 'f'));
  397. $index->addDocument($doc);
  398. unset($index);
  399. $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_index/_files');
  400. $hits = $index->find('test:[a TO t]');
  401. $this->assertEquals(1, count($hits));
  402. $this->assertEquals(0, reset($hits)->id);
  403. $hits = $index->find('test:f');
  404. $this->assertEquals(1, count($hits));
  405. $this->assertEquals(0, reset($hits)->id);
  406. $hits = $index->find('test:g');
  407. $this->assertEquals(0, count($hits));
  408. unset($index);
  409. $this->_clearDirectory(dirname(__FILE__) . '/_index/_files');
  410. }
  411. /**
  412. * @group ZF-9680
  413. */
  414. public function testIsDeletedWithoutExplicitCommit()
  415. {
  416. //$this->_clearDirectory(dirname(__FILE__) . '/_index/_files');
  417. $index = Zend_Search_Lucene::create(dirname(__FILE__) . '/_index/_files');
  418. $document = new Zend_Search_Lucene_Document;
  419. $document->addField(Zend_Search_Lucene_Field::Keyword('_id', 'myId'));
  420. $document->addField(Zend_Search_Lucene_Field::Keyword('bla', 'blubb'));
  421. $index->addDocument($document);
  422. $this->assertFalse($index->isDeleted(0));
  423. unset($index);
  424. $this->_clearDirectory(dirname(__FILE__) . '/_index/_files');
  425. }
  426. }
  427. if (PHPUnit_MAIN_METHOD == 'Zend_Search_Lucene_LuceneTest::main') {
  428. Zend_Search_Lucene_LuceneTest::main();
  429. }