summaryrefslogtreecommitdiff
path: root/inc/mailgun/clue/stream-filter/tests/FunZlibTest.php
blob: 752c8a2c831b54f0e522b2925eec5bb6f6b04351 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php

use Clue\StreamFilter;

class BuiltInZlibTest extends PHPUnit_Framework_TestCase
{
    public function testFunZlibDeflateHelloWorld()
    {
        $deflate = StreamFilter\fun('zlib.deflate');

        $data = $deflate('hello') . $deflate(' ') . $deflate('world') . $deflate();

        $this->assertEquals(gzdeflate('hello world'), $data);
    }

    public function testFunZlibDeflateEmpty()
    {
        if (PHP_VERSION >= 7) $this->markTestSkipped('Not supported on PHP7 (empty string does not invoke filter)');

        $deflate = StreamFilter\fun('zlib.deflate');

        //$data = gzdeflate('');
        $data = $deflate();

        $this->assertEquals("\x03\x00", $data);
    }

    public function testFunZlibDeflateBig()
    {
        $deflate = StreamFilter\fun('zlib.deflate');

        $n = 1000;
        $expected = str_repeat('hello', $n);

        $bytes = '';
        for ($i = 0; $i < $n; ++$i) {
            $bytes .= $deflate('hello');
        }
        $bytes .= $deflate();

        $this->assertEquals($expected, gzinflate($bytes));
    }

    public function testFunZlibInflateHelloWorld()
    {
        $inflate = StreamFilter\fun('zlib.inflate');

        $data = $inflate(gzdeflate('hello world')) . $inflate();

        $this->assertEquals('hello world', $data);
    }

    public function testFunZlibInflateEmpty()
    {
        $inflate = StreamFilter\fun('zlib.inflate');

        $data = $inflate("\x03\x00") . $inflate();

        $this->assertEquals('', $data);
    }

    public function testFunZlibInflateBig()
    {
        if (defined('HHVM_VERSION')) $this->markTestSkipped('Not supported on HHVM (final chunk will not be emitted)');

        $inflate = StreamFilter\fun('zlib.inflate');

        $expected = str_repeat('hello', 10);
        $bytes = gzdeflate($expected);

        $ret = '';
        foreach (str_split($bytes, 2) as $chunk) {
            $ret .= $inflate($chunk);
        }
        $ret .= $inflate();

        $this->assertEquals($expected, $ret);
    }
}