Skip to content

Commit 827ed4e

Browse files
committed
Add exception to zlib dependant streams when zlib is not enabled
Add stream behavior Improve zlib tests Remove php-mock dependency Add zlib extension check to InflateStream
1 parent 6478a77 commit 827ed4e

15 files changed

+98
-40
lines changed

spec/Encoding/ChunkStreamSpec.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
class ChunkStreamSpec extends ObjectBehavior
1010
{
11+
use StreamBehavior;
12+
1113
function let(StreamInterface $stream)
1214
{
1315
if (defined('HHVM_VERSION')) {
@@ -22,11 +24,6 @@ function it_is_initializable()
2224
$this->shouldHaveType('Http\Message\Encoding\ChunkStream');
2325
}
2426

25-
function it_is_a_stream()
26-
{
27-
$this->shouldImplement('Psr\Http\Message\StreamInterface');
28-
}
29-
3027
function it_chunks_content()
3128
{
3229
$stream = new MemoryStream('This is a stream');

spec/Encoding/CompressStreamSpec.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
class CompressStreamSpec extends ObjectBehavior
1010
{
11+
use StreamBehavior, ZlibStreamBehavior;
12+
1113
function let(StreamInterface $stream)
1214
{
1315
if (defined('HHVM_VERSION')) {
@@ -22,11 +24,6 @@ function it_is_initializable()
2224
$this->shouldHaveType('Http\Message\Encoding\CompressStream');
2325
}
2426

25-
function it_is_a_stream()
26-
{
27-
$this->shouldImplement('Psr\Http\Message\StreamInterface');
28-
}
29-
3027
function it_reads()
3128
{
3229
$stream = new MemoryStream('This is a test stream');

spec/Encoding/DechunkStreamSpec.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
class DechunkStreamSpec extends ObjectBehavior
1010
{
11+
use StreamBehavior;
12+
1113
function let(StreamInterface $stream)
1214
{
1315
if (defined('HHVM_VERSION')) {
@@ -22,11 +24,6 @@ function it_is_initializable()
2224
$this->shouldHaveType('Http\Message\Encoding\DechunkStream');
2325
}
2426

25-
function it_is_a_stream()
26-
{
27-
$this->shouldImplement('Psr\Http\Message\StreamInterface');
28-
}
29-
3027
function it_reads()
3128
{
3229
$stream = new MemoryStream("4\r\ntest\r\n4\r\ntest\r\n0\r\n\r\n\0");

spec/Encoding/DecompressStreamSpec.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
class DecompressStreamSpec extends ObjectBehavior
1010
{
11+
use StreamBehavior, ZlibStreamBehavior;
12+
1113
function let(StreamInterface $stream)
1214
{
1315
if (defined('HHVM_VERSION')) {
@@ -22,11 +24,6 @@ function it_is_initializable()
2224
$this->shouldHaveType('Http\Message\Encoding\DecompressStream');
2325
}
2426

25-
function it_is_a_stream()
26-
{
27-
$this->shouldImplement('Psr\Http\Message\StreamInterface');
28-
}
29-
3027
function it_reads()
3128
{
3229
// "This is a test stream" | deflate

spec/Encoding/DeflateStreamSpec.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
class DeflateStreamSpec extends ObjectBehavior
99
{
10+
use StreamBehavior;
11+
1012
function let(StreamInterface $stream)
1113
{
1214
$this->beConstructedWith($stream);
@@ -17,11 +19,6 @@ function it_is_initializable()
1719
$this->shouldHaveType('Http\Message\Encoding\DeflateStream');
1820
}
1921

20-
function it_is_a_stream()
21-
{
22-
$this->shouldImplement('Psr\Http\Message\StreamInterface');
23-
}
24-
2522
function it_reads()
2623
{
2724
$stream = new MemoryStream('This is a test stream');

spec/Encoding/GzipDecodeStreamSpec.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
class GzipDecodeStreamSpec extends ObjectBehavior
1010
{
11+
use StreamBehavior, ZlibStreamBehavior;
12+
1113
function let(StreamInterface $stream)
1214
{
1315
if (defined('HHVM_VERSION')) {
@@ -22,11 +24,6 @@ function it_is_initializable()
2224
$this->shouldHaveType('Http\Message\Encoding\GzipDecodeStream');
2325
}
2426

25-
function it_is_a_stream()
26-
{
27-
$this->shouldImplement('Psr\Http\Message\StreamInterface');
28-
}
29-
3027
function it_reads()
3128
{
3229
// "This is a test stream" | deflate

spec/Encoding/GzipEncodeStreamSpec.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
class GzipEncodeStreamSpec extends ObjectBehavior
1010
{
11+
use StreamBehavior, ZlibStreamBehavior;
12+
1113
function let(StreamInterface $stream)
1214
{
1315
if (defined('HHVM_VERSION')) {
@@ -22,11 +24,6 @@ function it_is_initializable()
2224
$this->shouldHaveType('Http\Message\Encoding\GzipEncodeStream');
2325
}
2426

25-
function it_is_a_stream()
26-
{
27-
$this->shouldImplement('Psr\Http\Message\StreamInterface');
28-
}
29-
3027
function it_reads()
3128
{
3229
$stream = new MemoryStream('This is a test stream');

spec/Encoding/InflateStreamSpec.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
class InflateStreamSpec extends ObjectBehavior
99
{
10+
use StreamBehavior, ZlibStreamBehavior;
11+
1012
function let(StreamInterface $stream)
1113
{
1214
$this->beConstructedWith($stream);
@@ -17,11 +19,6 @@ function it_is_initializable()
1719
$this->shouldHaveType('Http\Message\Encoding\InflateStream');
1820
}
1921

20-
function it_is_a_stream()
21-
{
22-
$this->shouldImplement('Psr\Http\Message\StreamInterface');
23-
}
24-
2522
function it_reads()
2623
{
2724
// "This is a test stream" | deflate

spec/Encoding/StreamBehavior.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace spec\Http\Message\Encoding;
4+
5+
trait StreamBehavior
6+
{
7+
function it_is_a_stream()
8+
{
9+
$this->shouldImplement('Psr\Http\Message\StreamInterface');
10+
}
11+
}

spec/Encoding/ZlibStreamBehavior.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace spec\Http\Message\Encoding {
4+
5+
use Psr\Http\Message\StreamInterface;
6+
use PhpSpec\Exception\Example\SkippingException;
7+
8+
trait ZlibStreamBehavior
9+
{
10+
function it_throws_an_exception_when_zlib_is_not_enabled()
11+
{
12+
ZlibExtensionLoadedMock::$enabled = true;
13+
14+
$this->shouldThrow('RuntimeException')->duringInstantiation();
15+
16+
ZlibExtensionLoadedMock::$enabled = false;
17+
}
18+
}
19+
20+
class ZlibExtensionLoadedMock
21+
{
22+
/**
23+
* If enabled, the mocked method is used.
24+
*
25+
* @var bool
26+
*/
27+
public static $enabled = false;
28+
29+
/**
30+
* extension_loaded function mock.
31+
*/
32+
public static function extensionLoaded()
33+
{
34+
if (static::$enabled) {
35+
return false;
36+
} else {
37+
return call_user_func_array('\extension_loaded', func_get_args());
38+
}
39+
}
40+
}
41+
}
42+
43+
namespace Http\Message\Encoding {
44+
45+
use spec\Http\Message\Encoding\ZlibExtensionLoadedMock;
46+
47+
function extension_loaded($extension)
48+
{
49+
return ZlibExtensionLoadedMock::extensionLoaded($extension);
50+
}
51+
}

src/Encoding/CompressStream.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ class CompressStream extends FilteredStream
1313
{
1414
public function __construct(StreamInterface $stream, $level = -1)
1515
{
16+
if (!extension_loaded('zlib')) {
17+
throw new \RuntimeException('The zlib extension must be enabled to use this stream');
18+
}
19+
1620
parent::__construct($stream, ['window' => 15, 'level' => $level], ['window' => 15]);
1721
}
1822

src/Encoding/DecompressStream.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ class DecompressStream extends FilteredStream
1313
{
1414
public function __construct(StreamInterface $stream, $level = -1)
1515
{
16+
if (!extension_loaded('zlib')) {
17+
throw new \RuntimeException('The zlib extension must be enabled to use this stream');
18+
}
19+
1620
parent::__construct($stream, ['window' => 15], ['window' => 15, 'level' => $level]);
1721
}
1822

src/Encoding/GzipDecodeStream.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ class GzipDecodeStream extends FilteredStream
1313
{
1414
public function __construct(StreamInterface $stream, $level = -1)
1515
{
16+
if (!extension_loaded('zlib')) {
17+
throw new \RuntimeException('The zlib extension must be enabled to use this stream');
18+
}
19+
1620
parent::__construct($stream, ['window' => 31], ['window' => 31, 'level' => $level]);
1721
}
1822

src/Encoding/GzipEncodeStream.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ class GzipEncodeStream extends FilteredStream
1313
{
1414
public function __construct(StreamInterface $stream, $level = -1)
1515
{
16+
if (!extension_loaded('zlib')) {
17+
throw new \RuntimeException('The zlib extension must be enabled to use this stream');
18+
}
19+
1620
parent::__construct($stream, ['window' => 31, 'level' => $level], ['window' => 31]);
1721
}
1822

src/Encoding/InflateStream.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ class InflateStream extends FilteredStream
1313
{
1414
public function __construct(StreamInterface $stream, $level = -1)
1515
{
16+
if (!extension_loaded('zlib')) {
17+
throw new \RuntimeException('The zlib extension must be enabled to use this stream');
18+
}
19+
1620
parent::__construct($stream, ['window' => -15], ['window' => -15, 'level' => $level]);
1721
}
1822

0 commit comments

Comments
 (0)