/
RemovedAlternativePHPTagsUnitTest.php
185 lines (165 loc) · 4.63 KB
/
RemovedAlternativePHPTagsUnitTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
/**
* PHPCompatibility, an external standard for PHP_CodeSniffer.
*
* @package PHPCompatibility
* @copyright 2012-2020 PHPCompatibility Contributors
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
* @link https://github.com/PHPCompatibility/PHPCompatibility
*/
namespace PHPCompatibility\Tests\Miscellaneous;
use PHPCompatibility\Tests\BaseSniffTestCase;
/**
* Test the RemovedAlternativePHPTags sniff.
*
* @group removedAlternativePHPTags
* @group miscellaneous
*
* @covers \PHPCompatibility\Sniffs\Miscellaneous\RemovedAlternativePHPTagsSniff
*
* @since 7.0.4
*/
class RemovedAlternativePHPTagsUnitTest extends BaseSniffTestCase
{
/**
* Whether or not ASP tags are on.
*
* @var bool
*/
protected static $aspTags = false;
/**
* Set up skip condition.
*
* @beforeClass
*
* @since 7.0.4
* @since 10.0.0 Renamed the method from `setUpBeforeClass()` to `setUpSkipCondition()` and
* now using the `@beforeClass` annotation to allow for
* PHPUnit cross-version compatibility.
*
* @return void
*/
public static function setUpSkipCondition()
{
// Run the parent `@beforeClass` method.
parent::resetSniffFiles();
if (\PHP_VERSION_ID < 70000) {
// phpcs:ignore PHPCompatibility.IniDirectives.RemovedIniDirectives.asp_tagsRemoved
self::$aspTags = (bool) \ini_get('asp_tags');
}
}
/**
* testAlternativePHPTags
*
* @dataProvider dataAlternativePHPTags
*
* @param string $type The type of opening tags, either ASP or Script.
* @param string $snippet The text string found.
* @param int $line The line number.
*
* @return void
*/
public function testAlternativePHPTags($type, $snippet, $line)
{
if ($type === 'ASP' && self::$aspTags === false) {
$this->markTestSkipped('ASP tags are unavailable (PHP 7+) or disabled.');
return;
}
$file = $this->sniffFile(__FILE__, '7.0');
$this->assertError($file, $line, "{$type} style opening tags have been removed in PHP 7.0. Found \"{$snippet}\"");
}
/**
* Data provider.
*
* @see testAlternativePHPTags()
*
* @return array
*/
public static function dataAlternativePHPTags()
{
return [
['Script', '<script language="php">', 7],
['Script', "<script language='php'>", 10],
['Script', '<script type="text/php" language="php">', 13],
['Script', "<script language='PHP' type='text/php'>", 16],
['ASP', '<%', 21],
['ASP', '<%', 22],
['ASP', '<%=', 23],
['ASP', '<%=', 24],
];
}
/**
* testMaybeASPOpenTag
*
* @dataProvider dataMaybeASPOpenTag
*
* @param int $line The line number.
* @param string $snippet Part of the text string found.
*
* @return void
*/
public function testMaybeASPOpenTag($line, $snippet)
{
if (self::$aspTags === true) {
$this->markTestSkipped('ASP tags are unavailable (PHP 7+) or disabled.');
return;
}
$file = $this->sniffFile(__FILE__, '7.0');
$warning = "Possible use of ASP style opening tags detected. ASP style opening tags have been removed in PHP 7.0. Found: {$snippet}";
$this->assertWarning($file, $line, $warning);
}
/**
* Data provider.
*
* @see testMaybeASPOpenTag()
*
* @return array
*/
public static function dataMaybeASPOpenTag()
{
return [
[21, '<% echo $var; %>'],
[22, '<% echo $var; %> and some m...'],
[23, '<%= $var . \' and some more ...'],
[24, '<%= $var %> and some more t...'],
];
}
/**
* testNoFalsePositives
*
* @dataProvider dataNoFalsePositives
*
* @param int $line The line number.
*
* @return void
*/
public function testNoFalsePositives($line)
{
$file = $this->sniffFile(__FILE__, '7.0');
$this->assertNoViolation($file, $line);
}
/**
* Data provider.
*
* @see testNoFalsePositives()
*
* @return array
*/
public static function dataNoFalsePositives()
{
return [
[3],
[28],
];
}
/**
* Verify no notices are thrown at all.
*
* @return void
*/
public function testNoViolationsInFileOnValidVersion()
{
$file = $this->sniffFile(__FILE__, '5.6');
$this->assertNoViolation($file);
}
}