Skip to content

Commit e9ba138

Browse files
committed
Added more tests to allow for files to be opened too which will be helpful in my other project I'm integrating this library with.
1 parent d3523d8 commit e9ba138

3 files changed

Lines changed: 82 additions & 14 deletions

File tree

src/GzStreamGuzzle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function readOnlyStream()
4545
return new self($this->stream, true, $this->level);
4646
}
4747

48-
public function read($length)
48+
public function read($length = 65536)
4949
{
5050
$ret = $this->stream->read($length);
5151
return $ret;

src/String.php

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,23 @@ class String
1313
protected $stream = null;
1414
protected $streamObject = null;
1515
protected $compressionLevel = 6;
16+
protected $isRealFile = false;
1617

17-
public function __construct($readOnly = false, $compressionLevel = 6)
18+
public function __construct($readOnly = false, $compressionLevel = 6, $filepath = 'php://memory')
1819
{
19-
$this->compressionLevel = $compressionLevel;
20-
$this->stream = fopen('php://memory', 'w');
20+
$this->replaceStream($readOnly, $compressionLevel, $filepath);
21+
}
22+
23+
public function replaceStream($readOnly = false, $compressionLevel = 6, $filepath = 'php://memory')
24+
{
25+
if (substr($filepath, 0, 6) !== 'php://') {
26+
$this->isRealFile = true;
27+
}
28+
$this->compressionLevel = $compressionLevel;
29+
$this->stream = fopen($filepath, 'r+');
2130
$this->streamObject = Psr7\stream_for($this->stream);
2231
$this->gzStream = new GzStreamGuzzle($this->streamObject, $readOnly, $this->compressionLevel);
23-
}
32+
}
2433

2534
public function write($string, $options = 0, $depth = 512)
2635
{
@@ -31,19 +40,22 @@ public function write($string, $options = 0, $depth = 512)
3140
return $this->getGzStream()->write($string);
3241
}
3342

43+
public function read($length = 65536)
44+
{
45+
$ret = $this->getGzStream()->read($length);
46+
return $ret;
47+
}
48+
3449
public function prepend($string, $compressionLevel = 6)
3550
{
3651
$this->prepareForRead();
3752
$gzStreamReadOnly = $this->getGzStream()->readOnlyStream();
3853

39-
$this->compressionLevel = $compressionLevel;
40-
$this->stream = fopen('php://memory', 'w');
41-
$this->streamObject = Psr7\stream_for($this->stream);
42-
$this->gzStream = new GzStreamGuzzle($this->streamObject, false, $compressionLevel);
54+
$this->replaceStream(false, $compressionLevel, 'php://memory');
4355

4456
$this->getGzStream()->write($string);
4557

46-
while ($buffer = $gzStreamReadOnly->read(4096)) {
58+
while ($buffer = $gzStreamReadOnly->read()) {
4759
$this->getGzStream()->write($buffer);
4860
}
4961
}
@@ -102,6 +114,11 @@ public function getStreamObject()
102114

103115
public function getReadOnlyStream()
104116
{
117+
if ($this->isRealFile) {
118+
return $this->getGzStream();
119+
}
120+
121+
// More specifically for the in-memory streams:
105122
$this->prepareForRead();
106123
$gzStreamReadOnly = $this->getGzStream()->readOnlyStream();
107124

tests/StringTest.php

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ public function testPrependStream()
7272

7373
$this->memoryUsage(__METHOD__, 'Finish');
7474

75-
$compressedString->writeDecompressedContents('tests/files/tmp/prepended_test_decompressed.txt');
76-
$compressedString->writeCompressedContents('tests/files/tmp/prepended_test_compressed.gz');
75+
$compressedString->writeDecompressedContents(__DIR__.'/files/tmp/prepended_test_decompressed.txt');
76+
$compressedString->writeCompressedContents(__DIR__.'/files/tmp/prepended_test_compressed.gz');
7777
}
7878

7979
public function testPrependAndWriteStream()
@@ -90,8 +90,8 @@ public function testPrependAndWriteStream()
9090

9191
$this->assertEquals($textToPrepend.$content.$textToAppend, $compressedString->getDecompressedContents());
9292

93-
$compressedString->writeDecompressedContents('tests/files/tmp/appended_test_decompressed.txt');
94-
$compressedString->writeCompressedContents('tests/files/tmp/appended_test_compressed.gz');
93+
$compressedString->writeDecompressedContents(__DIR__.'/files/tmp/appended_test_decompressed.txt');
94+
$compressedString->writeCompressedContents(__DIR__.'/files/tmp/appended_test_compressed.gz');
9595
}
9696

9797
public function testDifferentCompressionModes()
@@ -115,6 +115,57 @@ public function testDifferentCompressionModes()
115115
$this->assertGreaterThan($size2, $size1, 'Gzip Mode 1 should result in the larger file');
116116
}
117117

118+
public function testReadGzipFile()
119+
{
120+
$this->memoryUsage(__METHOD__, 'Start');
121+
$compressedStringFile = new String(true, 6, __DIR__.'/files/companies_first_10.gz');
122+
123+
$readOnlyStream = $compressedStringFile->getReadOnlyStream();
124+
$i = 0;
125+
$firstLineOutput = '';
126+
while($buffer = $readOnlyStream->read()) {
127+
if ($i < 1) {
128+
$firstLineOutput = $buffer;
129+
}
130+
131+
$i++;
132+
}
133+
134+
$this->assertContains('_id', $firstLineOutput);
135+
136+
$this->log(__METHOD__, false);
137+
$this->memoryUsage(__METHOD__, 'Finish');
138+
139+
}
140+
141+
public function testWriteFileAndReadStream()
142+
{
143+
$this->memoryUsage(__METHOD__, 'Start');
144+
145+
$compressedString = new String();
146+
147+
$content = file_get_contents(__DIR__.'/files/companies_first_10.json');
148+
$compressedString->write($content);
149+
150+
$compressedString->writeCompressedContents(__DIR__.'/files/tmp/write_test.gz');
151+
152+
$readOnlyStream = $compressedString->getReadOnlyStream();
153+
$i = 0;
154+
$firstLineOutput = '';
155+
while($buffer = $readOnlyStream->read()) {
156+
if ($i < 1) {
157+
$firstLineOutput = $buffer;
158+
}
159+
160+
$i++;
161+
}
162+
163+
$this->assertContains('_id', $firstLineOutput);
164+
165+
$this->log(__METHOD__, false);
166+
$this->memoryUsage(__METHOD__, 'Finish');
167+
}
168+
118169
/*public function testWriteAfterReadOnlyStream()
119170
{
120171
$compressedString = new String();

0 commit comments

Comments
 (0)