Completed
Push — master ( 247290...01d05f )
by Michael
08:30
created

AbstractSourceTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 53
Duplicated Lines 18.87 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 10
loc 53
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 8 2
A getTestedClass() 0 4 1
A getExpectedStrength() 0 4 1
A provideGenerate() 10 10 3
A testGetStrength() 0 7 1
A testGenerate() 0 8 1

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
3
/*
4
 * The RandomLib library for securely generating random numbers and strings in PHP
5
 *
6
 * @author     Anthony Ferrara <[email protected]>
7
 * @copyright  2011 The Authors
8
 * @license    http://www.opensource.org/licenses/mit-license.html  MIT License
9
 * @version    Build @@version@@
10
 */
11
namespace RandomLib\Source;
12
13
use SecurityLib\Strength;
14
15
abstract class AbstractSourceTest extends \PHPUnit_Framework_TestCase
16
{
17
    public function setUp()
18
    {
19
        $class = static::getTestedClass();
20
21
        if (!$class::isSupported()) {
22
            $this->markTestSkipped();
23
        }
24
    }
25
26
    protected static function getTestedClass()
27
    {
28
        return preg_replace('/Test$/', '', get_called_class());
29
    }
30
31
    protected static function getExpectedStrength()
32
    {
33
        return new Strength(Strength::VERYLOW);
34
    }
35
36 View Code Duplication
    public static function provideGenerate()
37
    {
38
        $data = array();
39
        for ($i = 0; $i < 100; $i += 5) {
40
            $not = $i > 0 ? str_repeat(chr(0), $i) : chr(0);
41
            $data[] = array($i, $not);
42
        }
43
44
        return $data;
45
    }
46
47
    public function testGetStrength()
48
    {
49
        $class = static::getTestedClass();
50
        $strength = static::getExpectedStrength();
51
        $actual = $class::getStrength();
52
        $this->assertEquals($actual, $strength);
53
    }
54
55
    /**
56
     * @dataProvider provideGenerate
57
     * @group slow
58
     */
59
    public function testGenerate($length, $not)
60
    {
61
        $class = static::getTestedClass();
62
        $rand = new $class();
63
        $stub = $rand->generate($length);
64
        $this->assertEquals($length, strlen($stub));
65
        $this->assertNotEquals($not, $stub);
66
    }
67
}
68