Completed
Push — master ( 514dbf...ba76d3 )
by Wim
11s
created

ForbiddenNamesSniffTest::testForbiddenNames()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 37
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 8.439
c 0
b 0
f 0
cc 6
eloc 17
nc 6
nop 1
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
     * @group forbiddenNames
33
     *
34
     * @dataProvider usecaseProvider
35
     */
36
    public function testForbiddenNames($usecase)
37
    {
38
39
        // These use cases were generated using the PHP script
40
        // `generate-forbidden-names-test-files` in sniff-examples
41
        $filename = "sniff-examples/forbidden-names/$usecase.php";
42
43
        if (in_array($usecase, array('use', 'class-use-trait'))) {
44
            $file = $this->sniffFile($filename, '5.6');
45
46
            $this->assertNoViolation($file, 13);
47
            $this->assertNoViolation($file, 31);
48
49
            $file = $this->sniffFile($filename, '7.0');
50
51
            $lineCount = count(file($file->getFilename()));
52
53
            for ($i = 60; $i < $lineCount; $i++) {
54
                $this->assertError($file, $i, "Function name, class name, namespace name or constant name can not be reserved keyword");
55
            }
56
        }
57
58
        $file = $this->sniffFile($filename);
59
60
        $this->assertNoViolation($file, 2);
61
62
        $lineCount = count(file($file->getFilename()));
63
        // Each line of the use case files (starting at line 3) exhibits an
64
        // error.
65
        for ($i = 3; $i < $lineCount; $i++) {
66
            if (in_array($i, array(13,31)) && in_array($usecase, array('use', 'class-use-trait'))) {
67
                continue;
68
            }
69
            $this->assertError($file, $i, "Function name, class name, namespace name or constant name can not be reserved keyword");
70
        }
71
72
    }
73
74
    /**
75
     * Provides use cases to test with each keyword
76
     *
77
     * @return array
78
     */
79
    public function usecaseProvider()
80
    {
81
        $data = array(
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
            array('interface'),
93
            array('interface-extends'),
94
        );
95
        if (version_compare(phpversion(), '5.3', '>=')) {
96
            $data[] = array('namespace');
97
        }
98
99
        return $data;
100
    }
101
102
    /**
103
     * testCorrectUsageOfKeywords
104
     *
105
     * @group forbiddenNames
106
     *
107
     * @return void
108
     */
109
    public function testCorrectUsageOfKeywords()
110
    {
111
        if (ini_get('date.timezone') == false) {
112
            ini_set('date.timezone', 'America/Chicago');
113
        }
114
        $file = $this->sniffFile("sniff-examples/forbidden_names_correct_usage.php");
115
116
        $this->assertNoViolation($file);
117
    }
118
119
    /**
120
     * Test setting test version option
121
     *
122
     * @group forbiddenNames
123
     *
124
     * @return void
125
     */
126
    public function testSettingTestVersion()
127
    {
128
        $file = $this->sniffFile("sniff-examples/forbidden-names/class.php", '4.4');
129
130
        $this->assertNoViolation($file, 3);
131
    }
132
}
133