FileTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Zend_Search_Lucene_Storage_File_Filesystem
  24. */
  25. require_once 'Zend/Search/Lucene/Storage/File/Filesystem.php';
  26. /**
  27. * Zend_Search_Lucene_Storage_File_Memory
  28. */
  29. require_once 'Zend/Search/Lucene/Storage/File/Memory.php';
  30. /**
  31. * PHPUnit test case
  32. */
  33. require_once 'PHPUnit/Framework/TestCase.php';
  34. /**
  35. * @category Zend
  36. * @package Zend_Search_Lucene
  37. * @subpackage UnitTests
  38. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. * @group Zend_Search_Lucene
  41. */
  42. class Zend_Search_Lucene_Storage_FileTest extends PHPUnit_Framework_TestCase
  43. {
  44. public function testFilesystem()
  45. {
  46. $file = new Zend_Search_Lucene_Storage_File_Filesystem(dirname(__FILE__) . '/_files/sample_data'); // open file object for reading
  47. $this->assertTrue($file instanceof Zend_Search_Lucene_Storage_File);
  48. $fileSize = filesize(dirname(__FILE__) . '/_files/sample_data');
  49. $this->assertEquals($file->size(), $fileSize);
  50. $file->seek(0, SEEK_END);
  51. $this->assertEquals($file->tell(), $fileSize);
  52. $file->seek(2, SEEK_SET);
  53. $this->assertEquals($file->tell(), 2);
  54. $file->seek(3, SEEK_CUR);
  55. $this->assertEquals($file->tell(), 5);
  56. $file->seek(0, SEEK_SET);
  57. $this->assertEquals($file->tell(), 0);
  58. $this->assertEquals($file->readByte(), 10);
  59. $this->assertEquals($file->readBytes(8), "\xFF\x00\xAA\x11\xBB\x44\x66\x99");
  60. $this->assertEquals($file->readInt(), 49057123);
  61. $this->assertEquals($file->readLong(), 753823522);
  62. $this->assertEquals($file->readVInt(), 234586758);
  63. $this->assertEquals($file->readString(), "UTF-8 string with non-ascii (Cyrillic) symbols\nUTF-8 строка с не-ASCII (кириллическими) символами");
  64. $this->assertEquals($file->readBinary(), "\xFF\x00\xAA\x11\xBB\x44\x66\x99");
  65. $file->seek(0);
  66. $fileData = $file->readBytes($file->size());
  67. $file->close();
  68. unset($file);
  69. $testFName = dirname(__FILE__) . '/_files/sample_data_1';
  70. $file = new Zend_Search_Lucene_Storage_File_Filesystem($testFName, 'wb');
  71. $file->lock(LOCK_EX);
  72. $file->writeByte(10);
  73. $file->writeBytes("\xFF\x00\xAA\x11\xBB\x44\x66\x99");
  74. $file->writeInt(49057123);
  75. $file->writeLong(753823522);
  76. $file->writeVInt(234586758);
  77. $file->writeString("UTF-8 string with non-ascii (Cyrillic) symbols\nUTF-8 строка с не-ASCII (кириллическими) символами");
  78. $file->writeVInt(8); $file->writeBytes("\xFF\x00\xAA\x11\xBB\x44\x66\x99");
  79. $file->flush();
  80. $file->unlock();
  81. $file->close();
  82. $fh = fopen($testFName, 'rb');
  83. $this->assertEquals($fileData, fread($fh, filesize($testFName)));
  84. fclose($fh);
  85. unlink($testFName);
  86. }
  87. public function testMemory()
  88. {
  89. $file = new Zend_Search_Lucene_Storage_File_Filesystem(dirname(__FILE__) . '/_files/sample_data');
  90. $fileData = $file->readBytes($file->size());
  91. $file->close();
  92. unset($file);
  93. $file = new Zend_Search_Lucene_Storage_File_Memory($fileData);
  94. $this->assertTrue($file instanceof Zend_Search_Lucene_Storage_File);
  95. $fileSize = strlen($fileData);
  96. $file->seek(0, SEEK_END);
  97. $this->assertEquals($file->tell(), $fileSize);
  98. $file->seek(2, SEEK_SET);
  99. $this->assertEquals($file->tell(), 2);
  100. $file->seek(3, SEEK_CUR);
  101. $this->assertEquals($file->tell(), 5);
  102. $file->seek(0, SEEK_SET);
  103. $this->assertEquals($file->tell(), 0);
  104. $this->assertEquals($file->readByte(), 10);
  105. $this->assertEquals($file->readBytes(8), "\xFF\x00\xAA\x11\xBB\x44\x66\x99");
  106. $this->assertEquals($file->readInt(), 49057123);
  107. $this->assertEquals($file->readLong(), 753823522);
  108. $this->assertEquals($file->readVInt(), 234586758);
  109. $this->assertEquals($file->readString(), "UTF-8 string with non-ascii (Cyrillic) symbols\nUTF-8 строка с не-ASCII (кириллическими) символами");
  110. $this->assertEquals($file->readBinary(), "\xFF\x00\xAA\x11\xBB\x44\x66\x99");
  111. // these methods do nothing, but should be provided by object
  112. $file->lock(LOCK_EX);
  113. $file->unlock();
  114. }
  115. }