FileByteStreamAcceptanceTest.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. require_once 'Swift/Tests/SwiftUnitTestCase.php';
  3. require_once 'Swift/InputByteStream.php';
  4. require_once 'Swift/ByteStream/FileByteStream.php';
  5. require_once 'Swift/StreamFilters/StringReplacementFilter.php';
  6. class Swift_ByteStream_FileByteStreamAcceptanceTest
  7. extends Swift_Tests_SwiftUnitTestCase
  8. {
  9. private $_tmpDir;
  10. private $_testFile;
  11. public function skip()
  12. {
  13. $this->skipUnless(
  14. SWIFT_TMP_DIR, 'Cannot run test without a writable directory to use (' .
  15. 'define SWIFT_TMP_DIR in tests/config.php if you wish to run this test)'
  16. );
  17. }
  18. public function setUp()
  19. {
  20. $this->_tmpDir = SWIFT_TMP_DIR;
  21. $this->_testFile = $this->_tmpDir . '/swift-test-file' . __CLASS__;
  22. file_put_contents($this->_testFile, 'abcdefghijklm');
  23. }
  24. public function tearDown()
  25. {
  26. unlink($this->_testFile);
  27. }
  28. public function testFileDataCanBeRead()
  29. {
  30. $file = $this->_createFileStream($this->_testFile);
  31. $str = '';
  32. while (false !== $bytes = $file->read(8192))
  33. {
  34. $str .= $bytes;
  35. }
  36. $this->assertEqual('abcdefghijklm', $str);
  37. }
  38. public function testFileDataCanBeReadSequentially()
  39. {
  40. $file = $this->_createFileStream($this->_testFile);
  41. $this->assertEqual('abcde', $file->read(5));
  42. $this->assertEqual('fghijklm', $file->read(8));
  43. $this->assertFalse($file->read(1));
  44. }
  45. public function testFilenameIsReturned()
  46. {
  47. $file = $this->_createFileStream($this->_testFile);
  48. $this->assertEqual($this->_testFile, $file->getPath());
  49. }
  50. public function testFileCanBeWrittenTo()
  51. {
  52. $file = $this->_createFileStream(
  53. $this->_testFile, true
  54. );
  55. $file->write('foobar');
  56. $this->assertEqual('foobar', $file->read(8192));
  57. }
  58. public function testReadingFromThenWritingToFile()
  59. {
  60. $file = $this->_createFileStream(
  61. $this->_testFile, true
  62. );
  63. $file->write('foobar');
  64. $this->assertEqual('foobar', $file->read(8192));
  65. $file->write('zipbutton');
  66. $this->assertEqual('zipbutton', $file->read(8192));
  67. }
  68. public function testWritingToFileWithCanonicalization()
  69. {
  70. $file = $this->_createFileStream(
  71. $this->_testFile, true
  72. );
  73. $file->addFilter($this->_createFilter(array("\r\n", "\r"), "\n"), 'allToLF');
  74. $file->write("foo\r\nbar\r");
  75. $file->write("\nzip\r\ntest\r");
  76. $file->flushBuffers();
  77. $this->assertEqual("foo\nbar\nzip\ntest\n", file_get_contents($this->_testFile));
  78. }
  79. public function testBindingOtherStreamsMirrorsWriteOperations()
  80. {
  81. $file = $this->_createFileStream(
  82. $this->_testFile, true
  83. );
  84. $is1 = $this->_createMockInputStream();
  85. $is2 = $this->_createMockInputStream();
  86. $this->_checking(Expectations::create()
  87. -> one($is1)->write('x')
  88. -> one($is2)->write('x')
  89. -> one($is1)->write('y')
  90. -> one($is2)->write('y')
  91. );
  92. $file->bind($is1);
  93. $file->bind($is2);
  94. $file->write('x');
  95. $file->write('y');
  96. }
  97. public function testBindingOtherStreamsMirrorsFlushOperations()
  98. {
  99. $file = $this->_createFileStream(
  100. $this->_testFile, true
  101. );
  102. $is1 = $this->_createMockInputStream();
  103. $is2 = $this->_createMockInputStream();
  104. $this->_checking(Expectations::create()
  105. -> one($is1)->flushBuffers()
  106. -> one($is2)->flushBuffers()
  107. );
  108. $file->bind($is1);
  109. $file->bind($is2);
  110. $file->flushBuffers();
  111. }
  112. public function testUnbindingStreamPreventsFurtherWrites()
  113. {
  114. $file = $this->_createFileStream(
  115. $this->_testFile, true
  116. );
  117. $is1 = $this->_createMockInputStream();
  118. $is2 = $this->_createMockInputStream();
  119. $this->_checking(Expectations::create()
  120. -> one($is1)->write('x')
  121. -> one($is2)->write('x')
  122. -> one($is1)->write('y')
  123. );
  124. $file->bind($is1);
  125. $file->bind($is2);
  126. $file->write('x');
  127. $file->unbind($is2);
  128. $file->write('y');
  129. }
  130. // -- Creation methods
  131. private function _createFilter($search, $replace)
  132. {
  133. return new Swift_StreamFilters_StringReplacementFilter($search, $replace);
  134. }
  135. private function _createMockInputStream()
  136. {
  137. return $this->_mock('Swift_InputByteStream');
  138. }
  139. private function _createFileStream($file, $writable = false)
  140. {
  141. return new Swift_ByteStream_FileByteStream($file, $writable);
  142. }
  143. }