Completed
Pull Request — master (#385)
by Marco
10:52
created

IdentifierSuffixerTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 1
dl 0
loc 66
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testGeneratesSuffixedIdentifiers() 0 7 1
A testGeneratedSuffixDependsOnPackageInstalledVersions() 0 7 1
A testGeneratesValidIdentifiers() 0 7 1
A testGeneratedIdentifierSuffix() 0 5 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 PackageVersions\Versions;
24
use PHPUnit_Framework_TestCase;
25
use ProxyManager\Generator\Util\IdentifierSuffixer;
26
27
/**
28
 * Tests for {@see \ProxyManager\Generator\Util\IdentifierSuffixer}
29
 *
30
 * @author Marco Pivetta <[email protected]>
31
 * @license MIT
32
 *
33
 * @group Coverage
34
 * @covers \ProxyManager\Generator\Util\IdentifierSuffixer
35
 */
36
class IdentifierSuffixerTest extends PHPUnit_Framework_TestCase
37
{
38
    /**
39
     * @dataProvider getBaseIdentifierNames
40
     */
41
    public function testGeneratesSuffixedIdentifiers(string $name) : void
42
    {
43
        self::assertSame(
44
            IdentifierSuffixer::getIdentifier($name),
45
            IdentifierSuffixer::getIdentifier($name)
46
        );
47
    }
48
49
    /**
50
     * @dataProvider getBaseIdentifierNames
51
     */
52
    public function testGeneratedSuffixDependsOnPackageInstalledVersions(string $name) : void
53
    {
54
        self::assertStringEndsWith(
55
            \substr(sha1($name . sha1(serialize(Versions::VERSIONS))), 0, 5),
56
            IdentifierSuffixer::getIdentifier($name)
57
        );
58
    }
59
60
    /**
61
     * @dataProvider getBaseIdentifierNames
62
     *
63
     * @param string $name
64
     */
65
    public function testGeneratesValidIdentifiers(string $name) : void
66
    {
67
        self::assertRegExp(
68
            '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]+$/',
69
            IdentifierSuffixer::getIdentifier($name)
70
        );
71
    }
72
73
    /**
74
     * @dataProvider getBaseIdentifierNames
75
     *
76
     * @param string $name
77
     */
78
    public function testGeneratedIdentifierSuffix(string $name) : void
79
    {
80
        // 5 generated characters are enough to keep idiots from tampering with these properties "the easy way"
81
        self::assertGreaterThan(5, strlen(IdentifierSuffixer::getIdentifier($name)));
82
    }
83
84
    /**
85
     * Data provider generating identifier names to be checked
86
     *
87
     * @return string[][]
88
     */
89
    public function getBaseIdentifierNames() : array
90
    {
91
        return [
92
            [''],
93
            ['1'],
94
            ['foo'],
95
            ['Foo'],
96
            ['bar'],
97
            ['Bar'],
98
            ['foo_bar'],
99
        ];
100
    }
101
}
102