Completed
Pull Request — master (#384)
by
unknown
13:01 queued 01:16
created

UniqueIdentifierGeneratorTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 1
dl 0
loc 76
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testGeneratesUniqueIdentifiers() 0 7 1
A testGeneratesUniqueIdentifiersByName() 0 14 2
A testGeneratesValidIdentifiers() 0 7 1
A testGeneratedIdentifierEntropy() 0 4 1
A getBaseIdentifierNames() 0 12 1
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license.
17
 */
18
19
declare(strict_types=1);
20
21
namespace ProxyManagerTest\Generator\Util;
22
23
use PHPUnit_Framework_TestCase;
24
use ProxyManager\Generator\Util\UniqueIdentifierGenerator;
25
26
/**
27
 * Tests for {@see \ProxyManager\Generator\Util\UniqueIdentifierGenerator}
28
 *
29
 * @author Marco Pivetta <[email protected]>
30
 * @license MIT
31
 *
32
 * @group Coverage
33
 * @covers \ProxyManager\Generator\Util\UniqueIdentifierGenerator
34
 */
35
class UniqueIdentifierGeneratorTest extends PHPUnit_Framework_TestCase
36
{
37
    /**
38
     * @dataProvider getBaseIdentifierNames
39
     *
40
     * @param string $name
41
     */
42
    public function testGeneratesUniqueIdentifiers(string $name) : void
43
    {
44
        self::assertNotSame(
45
            UniqueIdentifierGenerator::getIdentifier($name),
46
            UniqueIdentifierGenerator::getIdentifier($name)
47
        );
48
    }
49
50
    /**
51
     * @dataProvider getBaseIdentifierNames
52
     *
53
     * @param string $name
54
     */
55
    public function testGeneratesUniqueIdentifiersByName(string $name) : void
56
    {
57
        if (preg_match(UniqueIdentifierGenerator::VALID_IDENTIFIER_FORMAT, $name)) {
58
            self::assertSame(
59
                UniqueIdentifierGenerator::getIdentifier($name, true),
60
                UniqueIdentifierGenerator::getIdentifier($name, true)
61
            );
62
        } else {
63
            self::assertNotSame(
64
                UniqueIdentifierGenerator::getIdentifier($name, true),
65
                UniqueIdentifierGenerator::getIdentifier($name, true)
66
            );
67
        }
68
    }
69
70
    /**
71
     * @dataProvider getBaseIdentifierNames
72
     *
73
     * @param string $name
74
     */
75
    public function testGeneratesValidIdentifiers(string $name) : void
76
    {
77
        self::assertRegExp(
78
            '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]+$/',
79
            UniqueIdentifierGenerator::getIdentifier($name)
80
        );
81
    }
82
83
    /**
84
     * @dataProvider getBaseIdentifierNames
85
     *
86
     * @param string $name
87
     */
88
    public function testGeneratedIdentifierEntropy(string $name) : void
89
    {
90
        self::assertGreaterThan(14, strlen(UniqueIdentifierGenerator::getIdentifier($name)));
91
    }
92
93
    /**
94
     * Data provider generating identifier names to be checked
95
     *
96
     * @return string[][]
97
     */
98
    public function getBaseIdentifierNames() : array
99
    {
100
        return [
101
            [''],
102
            ['1'],
103
            ['foo'],
104
            ['Foo'],
105
            ['bar'],
106
            ['Bar'],
107
            ['foo_bar'],
108
        ];
109
    }
110
}
111