Completed
Push — master ( 3d39f9...d9b5c0 )
by Anthony
07:02 queued 04:32
created

GeneratorTest   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 170
Duplicated Lines 24.71 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 20
c 3
b 1
f 0
lcom 1
cbo 2
dl 42
loc 170
rs 10

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace RandomLib;
3
4
class GeneratorTest extends \PHPUnit_Framework_TestCase {
5
6
    protected $generator = null;
7
    protected $mixer = null;
8
    protected $sources = array();
9
10
    public static function provideGenerate() {
11
        return array(
12
            array(0, ''),
13
            array(1, chr(0)),
14
            array(2, chr(1) . chr(1)),
15
            array(3, chr(2) . chr(0) . chr(2)),
16
            array(4, chr(3) . chr(3) . chr(3) . chr(3)),
17
        );
18
    }
19
20
    public static function provideGenerateInt() {
21
        return array(
22
            array(1, 1, 1),
23
            array(0, 1, 0),
24
            array(0, 255, 0),
25
            array(400, 655, 400),
26
            array(0, 65535, 257),
27
            array(65535, 131070, 65792),
28
            array(0, 16777215, (2<<16) + 2),
29
            array(-10, 0, -10),
30
            array(-655, -400, -655),
31
            array(-131070, -65535, -130813),
32
        );
33
    }
34
35
    public static function provideGenerateIntRangeTest() {
36
        return array(
37
            array(0, 0),
38
            array(0, 1),
39
            array(1, 10000),
40
            array(100000, \PHP_INT_MAX),
41
        );
42
    }
43
44
    public static function provideGenerateStringTest() {
45
        return array(
46
            array(0, 'ab', ''),
47
            array(1, 'ab', 'a'),
48
            array(1, 'a', ''),
49
            array(2, 'ab', 'bb'),
50
            array(3, 'abc', 'cac'),
51
            array(8, '0123456789abcdef', '77777777'),
52
            array(16, '0123456789abcdef', 'ffffffffffffffff'),
53
            array(16, '', 'DDDDDDDDDDDDDDDD'),
54
        );
55
    }
56
57
    public function setUp() {
58
        $source1 = $this->getMock('RandomLib\Source');
59
        $source1->expects($this->any())
60
            ->method('generate')
61
            ->will($this->returnCallback(function ($size) {
62
                $r = '';
63
                for ($i = 0; $i < $size; $i++) {
64
                    $r .= chr($i);
65
                }
66
                return $r;
67
            }
68
        ));
69
        $source2 = $this->getMock('RandomLib\Source');
70
        $source2->expects($this->any())
71
            ->method('generate')
72
            ->will($this->returnCallback(function ($size) {
73
                $r = '';
74
                for ($i = $size - 1; $i >= 0; $i--) {
75
                    $r .= chr($i);
76
                }
77
                return $r;
78
            }
79
        ));
80
81
        $this->mixer = $this->getMock('RandomLib\Mixer');
82
        $this->mixer->expects($this->any())
83
            ->method('mix')
84
            ->will($this->returnCallback(function(array $sources) {
85
                if (empty($sources)) return '';
86
                $start = array_pop($sources);
87
                return array_reduce(
88
                    $sources,
89
                    function($el1, $el2) {
90
                        return $el1 ^ $el2;
91
                    },
92
                    $start
93
                );
94
            }));
95
96
        $this->sources = array($source1, $source2);
97
        $this->generator = new Generator($this->sources, $this->mixer);
98
    }
99
100
    public function testConstruct() {
101
        $this->assertTrue($this->generator instanceof Generator);
102
    }
103
104
    public function testGetMixer() {
105
        $this->assertSame($this->mixer, $this->generator->getMixer());
106
    }
107
108
    public function testGetSources() {
109
        $this->assertSame($this->sources, $this->generator->getSources());
110
    }
111
112
    /**
113
     * @dataProvider provideGenerate
114
     */
115
    public function testGenerate($size, $expect) {
116
        $this->assertEquals($expect, $this->generator->generate($size));
117
    }
118
119
    /**
120
     * @dataProvider provideGenerateInt
121
     */
122
    public function testGenerateInt($min, $max, $expect) {
123
        $this->assertEquals($expect, $this->generator->generateInt($min, $max));
124
    }
125
126
    /**
127
     * @dataProvider provideGenerateIntRangeTest
128
     */
129
    public function testGenerateIntRange($min, $max) {
130
        $n = $this->generator->generateInt($min, $max);
131
        $this->assertTrue($min <= $n);
132
        $this->assertTrue($max >= $n);
133
    }
134
135
    /**
136
     * @expectedException RangeException
137
     */
138
    public function testGenerateIntFail() {
139
        $n = $this->generator->generateInt(-1, PHP_INT_MAX);
140
    }
141
142
    /**
143
     */
144
    public function testGenerateIntLargeTest() {
145
        $bits = 30;
146
        $expected = 50529027;
147
        if (PHP_INT_MAX > 4000000000) {
148
            $bits = 55;
149
            $expected = 1693273676973062;
150
        }
151
        $n = $this->generator->generateInt(0, (int) pow(2, $bits));
152
        $this->assertEquals($expected, $n);
153
    }
154
    
155
    /**
156
     * @dataProvider provideGenerateStringTest
157
     */
158
    public function testGenerateString($length, $chars, $expected) {
159
        $n = $this->generator->generateString($length, $chars);
160
        $this->assertEquals($expected, $n);
161
    }
162
163
    /**
164
     * This test checks for issue #22:
165
     * @see https://github.com/ircmaxell/RandomLib/issues/22
166
     */
167
    public function testGenerateLargeRange() {
168
        if (PHP_INT_MAX < pow(2, 32)) {
169
            $this->markTestSkipped("Only test on 64 bit platforms");
170
        }
171
        $this->assertEquals(506381209866536711, $this->generator->generateInt(0, PHP_INT_MAX));
172
    }
173
}
174