Completed
Pull Request — release-v2.1 (#15)
by Quentin
08:46 queued 01:14
created

RandomTokenGeneratorTest::generateCaseProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace {
4
    $mockRandomPseudoBytes = false;
5
}
6
7
namespace Majora\Component\OAuth\Generator {
8
    /**
9
     * override global method to tests if function cannot get a strong hash.
10
     */
11
    function openssl_random_pseudo_bytes($length, &$strong)
12
    {
13
        global $mockRandomPseudoBytes;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
14
        if (isset($mockRandomPseudoBytes) && $mockRandomPseudoBytes === true) {
15
            $strong = false;
16
17
            return 12345;
18
        } else {
19
            return \openssl_random_pseudo_bytes($length, $strong);
20
        }
21
    }
22
}
23
24
namespace Majora\Component\OAuth\Tests\Generator {
25
26
    use Majora\Component\OAuth\Generator\RandomTokenGenerator;
0 ignored issues
show
Coding Style introduced by
USE declarations must go after the first namespace declaration
Loading history...
27
28
    /**
29
     * Unit test class for Majora\Component\OAuth\Generator\RandomTokenGenerator.
30
     */
31
    class RandomTokenGeneratorTest extends \PHPUnit_Framework_TestCase
32
    {
33
        /**
34
         * SetUp method overriden to reset global mock boolean.
35
         */
36
        public function setUp()
37
        {
38
            global $mockRandomPseudoBytes;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
39
40
            $mockRandomPseudoBytes = false;
41
        }
42
43
        /**
44
         * Cases provider for token generation.
45
         */
46
        public function generateCaseProvider()
47
        {
48
            return array(
49
                'openssl_pseudo_bytes' => array(false, 50, 100),
50
                'mt_rand_sha_512' => array(true, 64, 100),
51
            );
52
        }
53
54
        /**
55
         * Tests generate() method.
56
         *
57
         * @dataProvider generateCaseProvider
58
         */
59
        public function testGenerate($enableMock, $tokenLength, $iterations)
60
        {
61
            global $mockRandomPseudoBytes;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
62
63
            $mockRandomPseudoBytes = $enableMock;
64
65
            $generator = new RandomTokenGenerator('mocked_secret');
66
            $alreadyGenerated = array();
67
68
            for ($i = 0; $i < $iterations; ++$i) {
69
                $token = $generator->generate('test_seed');
70
71
                $this->assertEquals($tokenLength, strlen($token));
72
                $this->assertNotContains($token, $alreadyGenerated);
73
74
                $alreadyGenerated[] = $token;
75
            }
76
        }
77
    }
78
}
79