File: UnitTest.php

package info (click to toggle)
php-horde-lz4 1.0.10-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 264 kB
  • sloc: ansic: 1,745; xml: 266; php: 103; makefile: 10; sh: 3
file content (173 lines) | stat: -rw-r--r-- 5,105 bytes parent folder | download
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
/**
 * Copyright 2013-2016 Horde LLC (http://www.horde.org/)
 *
 * See the enclosed file COPYING for license information (PHP). If you
 * did not receive this file, see http://www.php.net/license/3_01.txt.
 *
 * @category   Horde
 * @copyright  2013 Horde LLC
 * @license    http://www.php.net/license/3_01.txt PHP 3.01
 * @package    horde_lz4
 * @subpackage UnitTests
 */

/**
 * Tests for the horde_lz4 extension.
 *
 * @author     Michael Slusarz <slusarz@horde.org>
 * @category   Horde
 * @copyright  2013 Horde LLC
 * @ignore
 * @license    http://www.php.net/license/3_01.txt PHP 3.01
 * @package    horde_lz4
 * @subpackage UnitTests
 */
class horde_lz4_UnitTest extends Horde_Test_Case
{
    private $data;

    public function setUp(): void
    {
        if (!extension_loaded('horde_lz4')) {
            $this->markTestSkipped('horde_lz4 extension not installed.');
        }

        $this->data = file_get_contents(__DIR__ . '/fixtures/data.txt');
    }

    // Test horde_lz4_compress() function : basic functionality
    public function testCompressBasic()
    {
        // Compressing a big string
        $output = horde_lz4_compress($this->data);
        $this->assertEquals(
            0,
            strcmp(horde_lz4_uncompress($output), $this->data)
        );

        // Compressing a smaller string
        $smallstring = "A small string to compress\n";
        $output = horde_lz4_compress($smallstring);
        $this->assertEquals(
            0,
            strcmp(horde_lz4_uncompress($output), $smallstring)
        );
    }

    // Test horde_lz4_compress() function : error conditions
    public function testCompressErrorConditionOne()
    {
        $this->expectException('PHPUnit\Framework\Error\Warning');

        // Zero arguments
        horde_lz4_compress();
    }

    // Test horde_lz4_compress() function : error conditions
    public function testCompressErrorConditionTwo()
    {
        $this->expectException('PHPUnit\Framework\Error\Warning');

        // Test horde_lz4_compress with one more than the expected number
        // of arguments
        $data = 'string_val';
        $extra_arg = 10;
        horde_lz4_compress($data, false, $extra_arg);
    }

    // Test horde_lz4_compress() function : error conditions
    public function testCompressErrorConditionThree()
    {
        $this->expectException('PHPUnit\Framework\Error\Warning');

        horde_lz4_compress(new stdClass);
    }

    // Test horde_lz4_compress() function : variation
    public function testCompressVariation()
    {
        $output = horde_lz4_compress($this->data);

        $this->assertNotEquals(
            md5($output),
            md5(horde_lz4_compress($output))
        );
    }

    // Test horde_lz4_uncompress() function : basic functionality
    public function testUncompressBasic()
    {
        $compressed = horde_lz4_compress($this->data);
        $this->assertEquals(
            0,
            strcmp($this->data, horde_lz4_uncompress($compressed))
        );
    }

    // Test horde_lz4_uncompress() function : error conditions
    public function testUncompressErrorConditionOne()
    {
        $this->expectException('PHPUnit\Framework\Error\Warning');

        // Zero arguments
        horde_lz4_uncompress();
    }

    // Test horde_lz4_uncompress() function : error conditions
    public function testUncompressErrorConditionTwo()
    {
        $this->expectException('PHPUnit\Framework\Error\Warning');

        // Test horde_lz4_uncompress with one more than the expected number
        // of arguments
        $data = 'string_val';
        $extra_arg = 10;
        horde_lz4_uncompress($data, $extra_arg);
    }

    // Test horde_lz4_uncompress() function : error conditions
    public function testUncompressErrorConditionThree()
    {
        $this->expectException('PHPUnit\Framework\Error\Warning');

        horde_lz4_uncompress(new stdClass);
    }

    // Test horde_lz4_uncompress() function : error conditions
    public function testUncompressErrorConditionFour()
    {
        $this->expectException('PHPUnit\Framework\Error\Warning');

        // Testing with incorrect arguments
        horde_lz4_uncompress(123);
    }

    // Test horde_lz4_compress() function : high compression
    public function testHighCompression()
    {
        // Compressing a big string
        $output = horde_lz4_compress($this->data, true);
        $this->assertEquals(
            0,
            strcmp(horde_lz4_uncompress($output), $this->data)
        );

        // Compressing a smaller string
        $smallstring = "A small string to compress\n";
        $output = horde_lz4_compress($smallstring, true);
        $this->assertEquals(
            0,
            strcmp(horde_lz4_uncompress($output), $smallstring)
        );
    }

    // Test horde_lz4_uncompress() function : bad input (non-lz4 data)
    public function testUncompressBadInput()
    {
        // Bad data is missing the Horde-LZ4 header and is not LZ4 data.
        $bad_data = "12345678";
        $this->assertFalse(horde_lz4_uncompress($bad_data));
    }

}