Completed
Pull Request — master (#126)
by Ryan
04:29 queued 01:40
created

ForbiddenNamesSniffTest::testForbiddenNames()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 38
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 6
eloc 17
c 2
b 1
f 0
nc 6
nop 1
dl 0
loc 38
rs 8.439
1
<?php
2
/**
3
 * Forbidden names sniff test file
4
 *
5
 * @package PHPCompatibility
6
 */
7
8
9
/**
10
 * Forbidden names sniff test
11
 *
12
 * @uses BaseSniffTest
13
 * @package PHPCompatibility
14
 * @author Jansen Price <[email protected]>
15
 */
16
class ForbiddenNamesSniffTest extends BaseSniffTest
17
{
18
    /**
19
     * setUp
20
     *
21
     * @return void
22
     */
23
    public function setUp()
24
    {
25
        parent::setUp();
26
27
    }
28
29
    /**
30
     * testNamespace
31
     *
32
     * @dataProvider usecaseProvider
33
     */
34
    public function testForbiddenNames($usecase)
35
    {
36
37
38
        // These use cases were generated using the PHP script
39
        // `generate-forbidden-names-test-files` in sniff-examples
40
        $filename = "sniff-examples/forbidden-names/$usecase.php";
41
42
        if (in_array($usecase, array('use', 'class-use-trait'))) {
43
            $file = $this->sniffFile($filename, '5.6');
44
45
            $this->assertNoViolation($file, 13);
46
            $this->assertNoViolation($file, 31);
47
48
            $file = $this->sniffFile($filename, '7.0');
49
50
            $lineCount = count(file($file->getFilename()));
51
52
            for ($i = 60; $i < $lineCount; $i++) {
53
                $this->assertError($file, $i, "Function name, class name, namespace name or constant name can not be reserved keyword");
54
            }
55
        }
56
57
        $file = $this->sniffFile($filename);
58
59
        $this->assertNoViolation($file, 2);
60
61
        $lineCount = count(file($file->getFilename()));
62
        // Each line of the use case files (starting at line 3) exhibits an
63
        // error.
64
        for ($i = 3; $i < $lineCount; $i++) {
65
            if (in_array($i, array(13,31)) && in_array($usecase, array('use', 'class-use-trait'))) {
66
                continue;
67
            }
68
            $this->assertError($file, $i, "Function name, class name, namespace name or constant name can not be reserved keyword");
69
        }
70
71
    }
72
73
    /**
74
     * Provides use cases to test with each keyword
75
     *
76
     * @return array
77
     */
78
    public function usecaseProvider()
79
    {
80
        return array(
81
            array('namespace'),
82
            array('use'),
83
            array('use-as'),
84
            array('class'),
85
            array('class-extends'),
86
            array('class-use-trait'),
87
            array('class-use-trait-alias-method'),
88
            array('trait'),
89
            array('function-declare'),
90
            array('const'),
91
            array('define'),
92
        );
93
    }
94
95
    /**
96
     * testCorrectUsageOfKeywords
97
     *
98
     * @return void
99
     */
100
    public function testCorrectUsageOfKeywords()
101
    {
102
        if (ini_get('date.timezone') == false) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing ini_get('date.timezone') of type string to the boolean false. If you are specifically checking for an empty string, consider using the more explicit === '' instead.
Loading history...
103
            ini_set('date.timezone', 'America/Chicago');
104
        }
105
        $file = $this->sniffFile("sniff-examples/forbidden_names_correct_usage.php");
106
107
        $this->assertNoViolation($file);
108
    }
109
110
    /**
111
     * Test setting test version option
112
     *
113
     * @return void
114
     */
115
    public function testSettingTestVersion()
116
    {
117
        $file = $this->sniffFile("sniff-examples/forbidden-names/class.php", "5.2");
118
119
        $this->assertNoViolation($file, 8);
120
    }
121
}
122