Completed
Branch feature/scrutinizer-run-tests (072b5a)
by Juliette
10:12
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
c 2
b 1
f 0
dl 0
loc 38
rs 8.439
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
     * @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
        $data = array(
81
            array('use'),
82
            array('use-as'),
83
            array('class'),
84
            array('class-extends'),
85
            array('class-use-trait'),
86
            array('class-use-trait-alias-method'),
87
            array('trait'),
88
            array('function-declare'),
89
            array('const'),
90
            array('define'),
91
        );
92
        if (version_compare(phpversion(), '5.3', '>=')) {
93
            $data[] = array('namespace');
94
        }
95
96
        return $data;
97
    }
98
99
    /**
100
     * testCorrectUsageOfKeywords
101
     *
102
     * @return void
103
     */
104
    public function testCorrectUsageOfKeywords()
105
    {
106
        if (ini_get('date.timezone') == false) {
107
            ini_set('date.timezone', 'America/Chicago');
108
        }
109
        $file = $this->sniffFile("sniff-examples/forbidden_names_correct_usage.php");
110
111
        $this->assertNoViolation($file);
112
    }
113
114
    /**
115
     * Test setting test version option
116
     *
117
     * @return void
118
     */
119
    public function testSettingTestVersion()
120
    {
121
        $file = $this->sniffFile("sniff-examples/forbidden-names/class.php", "5.2");
122
123
        $this->assertNoViolation($file, 8);
124
    }
125
}
126