|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * PHPCompatibility, an external standard for PHP_CodeSniffer. |
| 4 | + * |
| 5 | + * @package PHPCompatibility |
| 6 | + * @copyright 2012-2022 PHPCompatibility Contributors |
| 7 | + * @license https://opensource.org/licenses/LGPL-3.0 LGPL3 |
| 8 | + * @link https://github.com/PHPCompatibility/PHPCompatibility |
| 9 | + */ |
| 10 | + |
| 11 | +namespace Magento2\Tests\PHPCompatibility; |
| 12 | + |
| 13 | +/** |
| 14 | + * Test the RemovedDollarBraceStringEmbeds sniff. |
| 15 | + * |
| 16 | + * @group removedDollarBraceStringEmbeds |
| 17 | + * @group textstrings |
| 18 | + * |
| 19 | + * @covers \Magento2\Sniffs\PHPCompatibility\RemovedDollarBraceStringEmbedsSniff |
| 20 | + * |
| 21 | + * @since 10.0.0 |
| 22 | + */ |
| 23 | +class RemovedDollarBraceStringEmbedsUnitTest extends BaseSniffTest |
| 24 | +{ |
| 25 | + |
| 26 | + /** |
| 27 | + * The name of the primary test case file. |
| 28 | + * |
| 29 | + * @var string |
| 30 | + */ |
| 31 | + const TEST_FILE = 'RemovedDollarBraceStringEmbedsUnitTest.1.inc'; |
| 32 | + |
| 33 | + /** |
| 34 | + * The name of a secondary test case file containing PHP 7.3+ indented heredocs. |
| 35 | + * |
| 36 | + * @var string |
| 37 | + */ |
| 38 | + const TEST_FILE_PHP73HEREDOCS = 'RemovedDollarBraceStringEmbedsUnitTest.2.inc'; |
| 39 | + |
| 40 | + /** |
| 41 | + * Test that variable embeds of "type 3" - Braces after the dollar sign (�${foo}�) - |
| 42 | + * are correctly detected. |
| 43 | + * |
| 44 | + * @dataProvider dataRemovedDollarBraceStringEmbedsType3 |
| 45 | + * |
| 46 | + * @param int $line The line number. |
| 47 | + * @param string $found The embedded variable found. |
| 48 | + * |
| 49 | + * @return void |
| 50 | + */ |
| 51 | + public function testRemovedDollarBraceStringEmbedsType3($line, $found) |
| 52 | + { |
| 53 | + $file = $this->sniffFile(__DIR__ . '/' . self::TEST_FILE, '8.2'); |
| 54 | + $this->assertWarning($file, $line, 'Using ${var} in strings is deprecated since PHP 8.2, use {$var} instead. Found: ' . $found); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Data provider. |
| 59 | + * |
| 60 | + * @see testRemovedDollarBraceStringEmbedsType3() |
| 61 | + * |
| 62 | + * @return array |
| 63 | + */ |
| 64 | + public function dataRemovedDollarBraceStringEmbedsType3() |
| 65 | + { |
| 66 | + return [ |
| 67 | + [57, '${foo}'], |
| 68 | + [58, '${foo[\'bar\']}'], |
| 69 | + [59, '${foo}'], |
| 70 | + [59, '${text}'], |
| 71 | + [62, '${foo}'], |
| 72 | + [65, '${foo}'], |
| 73 | + ]; |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + /** |
| 78 | + * Test that variable embeds of "type 4" - Variable variables (�${expr}�, equivalent to |
| 79 | + * (string) ${expr}) - are correctly detected. |
| 80 | + * |
| 81 | + * @dataProvider dataRemovedDollarBraceStringEmbedsType4 |
| 82 | + * |
| 83 | + * @param int $line The line number. |
| 84 | + * @param string $found The embedded expression found. |
| 85 | + * |
| 86 | + * @return void |
| 87 | + */ |
| 88 | + public function testRemovedDollarBraceStringEmbedsType4($line, $found) |
| 89 | + { |
| 90 | + $file = $this->sniffFile(__DIR__ . '/' . self::TEST_FILE, '8.2'); |
| 91 | + $this->assertWarning($file, $line, "Using {$found} (variable variables) in strings is deprecated since PHP 8.2, use {\${expr}} instead."); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Data provider. |
| 96 | + * |
| 97 | + * @see testRemovedDollarBraceStringEmbedsType4() |
| 98 | + * |
| 99 | + * @return array |
| 100 | + */ |
| 101 | + public function dataRemovedDollarBraceStringEmbedsType4() |
| 102 | + { |
| 103 | + return [ |
| 104 | + [68, '${$bar}'], |
| 105 | + [69, '${(foo)}'], |
| 106 | + [70, '${foo->bar}'], |
| 107 | + [71, '${$object->getMethod()}'], |
| 108 | + [72, '${(foo)}'], |
| 109 | + [73, '${substr(\'laruence\', 0, 2)}'], |
| 110 | + [75, '${foo["${bar}"]}'], |
| 111 | + [76, '${foo["${bar[\'baz\']}"]}'], |
| 112 | + [77, '${foo->{$baz}}'], |
| 113 | + [78, '${foo->{${\'a\'}}}'], |
| 114 | + [79, '${foo->{"${\'a\'}"}}'], |
| 115 | + [83, '${foo["${bar}"]}'], |
| 116 | + [84, '${foo["${bar[\'baz\']}"]}'], |
| 117 | + [85, '${foo->{${\'a\'}}}'], |
| 118 | + [89, '${(foo)}'], |
| 119 | + ]; |
| 120 | + } |
| 121 | + |
| 122 | + |
| 123 | + /** |
| 124 | + * Test that variable embeds of "type 3" - Braces after the dollar sign (�${foo}�) - |
| 125 | + * are correctly detected in PHP 7.3+ indented heredocs. |
| 126 | + * |
| 127 | + * @dataProvider dataRemovedDollarBraceStringEmbedsType3InIndentedHeredoc |
| 128 | + * |
| 129 | + * @param int $line The line number. |
| 130 | + * @param string $found The embedded variable found. |
| 131 | + * |
| 132 | + * @return void |
| 133 | + */ |
| 134 | + public function testRemovedDollarBraceStringEmbedsType3InIndentedHeredoc($line, $found) |
| 135 | + { |
| 136 | + if (\PHP_VERSION_ID < 70300) { |
| 137 | + $this->markTestSkipped('Test code involving PHP 7.3 heredocs will not tokenize correctly on PHP < 7.3'); |
| 138 | + } |
| 139 | + |
| 140 | + $file = $this->sniffFile(__DIR__ . '/' . self::TEST_FILE_PHP73HEREDOCS, '8.2'); |
| 141 | + $this->assertWarning($file, $line, 'Using ${var} in strings is deprecated since PHP 8.2, use {$var} instead. Found: ' . $found); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Data provider. |
| 146 | + * |
| 147 | + * @see testRemovedDollarBraceStringEmbedsType3InIndentedHeredoc() |
| 148 | + * |
| 149 | + * @return array |
| 150 | + */ |
| 151 | + public function dataRemovedDollarBraceStringEmbedsType3InIndentedHeredoc() |
| 152 | + { |
| 153 | + return [ |
| 154 | + [33, '${foo[\'bar\']}'], |
| 155 | + ]; |
| 156 | + } |
| 157 | + |
| 158 | + |
| 159 | + /** |
| 160 | + * Test that variable embeds of "type 4" - Variable variables (�${expr}�, equivalent to |
| 161 | + * (string) ${expr}) - are correctly detected in PHP 7.3+ indented heredocs. |
| 162 | + * |
| 163 | + * @dataProvider dataRemovedDollarBraceStringEmbedsType4InIndentedHeredoc |
| 164 | + * |
| 165 | + * @param int $line The line number. |
| 166 | + * @param string $found The embedded expression found. |
| 167 | + * |
| 168 | + * @return void |
| 169 | + */ |
| 170 | + public function testRemovedDollarBraceStringEmbedsType4InIndentedHeredoc($line, $found) |
| 171 | + { |
| 172 | + if (\PHP_VERSION_ID < 70300) { |
| 173 | + $this->markTestSkipped('Test code involving PHP 7.3 heredocs will not tokenize correctly on PHP < 7.3'); |
| 174 | + } |
| 175 | + |
| 176 | + $file = $this->sniffFile(__DIR__ . '/' . self::TEST_FILE_PHP73HEREDOCS, '8.2'); |
| 177 | + $this->assertWarning($file, $line, "Using {$found} (variable variables) in strings is deprecated since PHP 8.2, use {\${expr}} instead."); |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Data provider. |
| 182 | + * |
| 183 | + * @see testRemovedDollarBraceStringEmbedsType4InIndentedHeredoc() |
| 184 | + * |
| 185 | + * @return array |
| 186 | + */ |
| 187 | + public function dataRemovedDollarBraceStringEmbedsType4InIndentedHeredoc() |
| 188 | + { |
| 189 | + return [ |
| 190 | + [39, '${$object->getMethod()}'], |
| 191 | + [40, '${foo["${bar[\'baz\']}"]}'], |
| 192 | + [41, '${foo->{${\'a\'}}}'], |
| 193 | + ]; |
| 194 | + } |
| 195 | + |
| 196 | + |
| 197 | + /** |
| 198 | + * Verify the sniff does not throw false positives for valid code. |
| 199 | + * |
| 200 | + * @dataProvider dataTestFiles |
| 201 | + * |
| 202 | + * @param string $testFile File name for the test case file to use. |
| 203 | + * @param int $lines Number of lines at the top of the file for which we don't expect errors. |
| 204 | + * |
| 205 | + * @return void |
| 206 | + */ |
| 207 | + public function testNoFalsePositives($testFile, $lines) |
| 208 | + { |
| 209 | + if ($testFile === self::TEST_FILE_PHP73HEREDOCS && \PHP_VERSION_ID < 70300) { |
| 210 | + $this->markTestSkipped('Test code involving PHP 7.3 heredocs will not tokenize correctly on PHP < 7.3'); |
| 211 | + } |
| 212 | + |
| 213 | + $file = $this->sniffFile(__DIR__ . '/' . $testFile, '8.2'); |
| 214 | + |
| 215 | + // No errors expected on the first # lines. |
| 216 | + for ($line = 1; $line <= $lines; $line++) { |
| 217 | + $this->assertNoViolation($file, $line); |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + |
| 222 | + /** |
| 223 | + * Verify no notices are thrown at all. |
| 224 | + * |
| 225 | + * @dataProvider dataTestFiles |
| 226 | + * |
| 227 | + * @param string $testFile File name for the test case file to use. |
| 228 | + * |
| 229 | + * @return void |
| 230 | + */ |
| 231 | + public function testNoViolationsInFileOnValidVersion($testFile) |
| 232 | + { |
| 233 | + if ($testFile === self::TEST_FILE_PHP73HEREDOCS && \PHP_VERSION_ID < 70300) { |
| 234 | + $this->markTestSkipped('Test code involving PHP 7.3 heredocs will not tokenize correctly on PHP < 7.3'); |
| 235 | + } |
| 236 | + |
| 237 | + $file = $this->sniffFile(__DIR__ . '/' . $testFile, '8.1'); |
| 238 | + $this->assertNoViolation($file); |
| 239 | + } |
| 240 | + |
| 241 | + |
| 242 | + /** |
| 243 | + * Data provider. |
| 244 | + * |
| 245 | + * @return array |
| 246 | + */ |
| 247 | + public function dataTestFiles() |
| 248 | + { |
| 249 | + return [ |
| 250 | + [self::TEST_FILE, 51], |
| 251 | + [self::TEST_FILE_PHP73HEREDOCS, 26], |
| 252 | + ]; |
| 253 | + } |
| 254 | +} |
0 commit comments