|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace SlevomatCodingStandard\Sniffs\Strings; |
| 4 | + |
| 5 | +use SlevomatCodingStandard\Sniffs\TestCase; |
| 6 | +use UnexpectedValueException; |
| 7 | + |
| 8 | +class DisallowVariableParsingSniffTest extends TestCase |
| 9 | +{ |
| 10 | + |
| 11 | + public function testNoErrors(): void |
| 12 | + { |
| 13 | + $report = self::checkFile(__DIR__ . '/data/disallowVariableParsingNoError.php'); |
| 14 | + self::assertNoSniffErrorInFile($report); |
| 15 | + } |
| 16 | + |
| 17 | + public function testErrorsDollarCurlySyntax(): void |
| 18 | + { |
| 19 | + $report = self::checkFile( |
| 20 | + __DIR__ . '/data/disallowVariableParsingErrors.php', |
| 21 | + [ |
| 22 | + 'disallowDollarCurlySyntax' => true, |
| 23 | + 'disallowCurlyDollarSyntax' => false, |
| 24 | + 'disallowSimpleSyntax' => false, |
| 25 | + ] |
| 26 | + ); |
| 27 | + |
| 28 | + self::assertSame(4, $report->getErrorCount()); |
| 29 | + |
| 30 | + self::assertSniffError( |
| 31 | + $report, |
| 32 | + 10, |
| 33 | + DisallowVariableParsingSniff::CODE_DISALLOWED_DOLLAR_CURLY_SYNTAX, |
| 34 | + 'Using variable syntax "${...}" inside string is disallowed as syntax "${...}" is deprecated as of PHP 8.2, found "${simpleString}".' |
| 35 | + ); |
| 36 | + |
| 37 | + self::assertSniffError( |
| 38 | + $report, |
| 39 | + 11, |
| 40 | + DisallowVariableParsingSniff::CODE_DISALLOWED_DOLLAR_CURLY_SYNTAX, |
| 41 | + 'Using variable syntax "${...}" inside string is disallowed as syntax "${...}" is deprecated as of PHP 8.2, found "${array[1]}".' |
| 42 | + ); |
| 43 | + |
| 44 | + self::assertSniffError( |
| 45 | + $report, |
| 46 | + 17, |
| 47 | + DisallowVariableParsingSniff::CODE_DISALLOWED_DOLLAR_CURLY_SYNTAX, |
| 48 | + 'Using variable syntax "${...}" inside string is disallowed as syntax "${...}" is deprecated as of PHP 8.2, found "${simpleString}".' |
| 49 | + ); |
| 50 | + |
| 51 | + self::assertSniffError( |
| 52 | + $report, |
| 53 | + 18, |
| 54 | + DisallowVariableParsingSniff::CODE_DISALLOWED_DOLLAR_CURLY_SYNTAX, |
| 55 | + 'Using variable syntax "${...}" inside string is disallowed as syntax "${...}" is deprecated as of PHP 8.2, found "${array[1]}".' |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + public function testErrorsCurlyDollarSyntax(): void |
| 60 | + { |
| 61 | + $report = self::checkFile( |
| 62 | + __DIR__ . '/data/disallowVariableParsingErrors.php', |
| 63 | + [ |
| 64 | + 'disallowCurlyDollarSyntax' => true, |
| 65 | + 'disallowDollarCurlySyntax' => false, |
| 66 | + 'disallowSimpleSyntax' => false, |
| 67 | + ] |
| 68 | + ); |
| 69 | + |
| 70 | + self::assertSame(6, $report->getErrorCount()); |
| 71 | + |
| 72 | + self::assertSniffError( |
| 73 | + $report, |
| 74 | + 22, |
| 75 | + DisallowVariableParsingSniff::CODE_DISALLOWED_CURLY_DOLLAR_SYNTAX, |
| 76 | + 'Using variable syntax "{$...}" inside string is disallowed, found "{$simpleString}".' |
| 77 | + ); |
| 78 | + |
| 79 | + self::assertSniffError( |
| 80 | + $report, |
| 81 | + 23, |
| 82 | + DisallowVariableParsingSniff::CODE_DISALLOWED_CURLY_DOLLAR_SYNTAX, |
| 83 | + 'Using variable syntax "{$...}" inside string is disallowed, found "{$array[1]}".' |
| 84 | + ); |
| 85 | + |
| 86 | + self::assertSniffError( |
| 87 | + $report, |
| 88 | + 24, |
| 89 | + DisallowVariableParsingSniff::CODE_DISALLOWED_CURLY_DOLLAR_SYNTAX, |
| 90 | + 'Using variable syntax "{$...}" inside string is disallowed, found "{$object->name}".' |
| 91 | + ); |
| 92 | + |
| 93 | + self::assertSniffError( |
| 94 | + $report, |
| 95 | + 31, |
| 96 | + DisallowVariableParsingSniff::CODE_DISALLOWED_CURLY_DOLLAR_SYNTAX, |
| 97 | + 'Using variable syntax "{$...}" inside string is disallowed, found "{$simpleString}".' |
| 98 | + ); |
| 99 | + |
| 100 | + self::assertSniffError( |
| 101 | + $report, |
| 102 | + 32, |
| 103 | + DisallowVariableParsingSniff::CODE_DISALLOWED_CURLY_DOLLAR_SYNTAX, |
| 104 | + 'Using variable syntax "{$...}" inside string is disallowed, found "{$array[1]}".' |
| 105 | + ); |
| 106 | + |
| 107 | + self::assertSniffError( |
| 108 | + $report, |
| 109 | + 33, |
| 110 | + DisallowVariableParsingSniff::CODE_DISALLOWED_CURLY_DOLLAR_SYNTAX, |
| 111 | + 'Using variable syntax "{$...}" inside string is disallowed, found "{$object->name}".' |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + public function testErrorsSimpleSyntax(): void |
| 116 | + { |
| 117 | + $report = self::checkFile( |
| 118 | + __DIR__ . '/data/disallowVariableParsingErrors.php', |
| 119 | + [ |
| 120 | + 'disallowSimpleSyntax' => true, |
| 121 | + 'disallowDollarCurlySyntax' => false, |
| 122 | + 'disallowCurlyDollarSyntax' => false, |
| 123 | + ] |
| 124 | + ); |
| 125 | + |
| 126 | + self::assertSame(6, $report->getErrorCount()); |
| 127 | + |
| 128 | + self::assertSniffError( |
| 129 | + $report, |
| 130 | + 37, |
| 131 | + DisallowVariableParsingSniff::CODE_DISALLOWED_SIMPLE_SYNTAX, |
| 132 | + 'Using variable syntax "$..." inside string is disallowed, found "$simpleString".' |
| 133 | + ); |
| 134 | + |
| 135 | + self::assertSniffError( |
| 136 | + $report, |
| 137 | + 38, |
| 138 | + DisallowVariableParsingSniff::CODE_DISALLOWED_SIMPLE_SYNTAX, |
| 139 | + 'Using variable syntax "$..." inside string is disallowed, found "$array[1]".' |
| 140 | + ); |
| 141 | + |
| 142 | + self::assertSniffError( |
| 143 | + $report, |
| 144 | + 39, |
| 145 | + DisallowVariableParsingSniff::CODE_DISALLOWED_SIMPLE_SYNTAX, |
| 146 | + 'Using variable syntax "$..." inside string is disallowed, found "$object->name".' |
| 147 | + ); |
| 148 | + |
| 149 | + self::assertSniffError( |
| 150 | + $report, |
| 151 | + 46, |
| 152 | + DisallowVariableParsingSniff::CODE_DISALLOWED_SIMPLE_SYNTAX, |
| 153 | + 'Using variable syntax "$..." inside string is disallowed, found "$simpleString".' |
| 154 | + ); |
| 155 | + |
| 156 | + self::assertSniffError( |
| 157 | + $report, |
| 158 | + 47, |
| 159 | + DisallowVariableParsingSniff::CODE_DISALLOWED_SIMPLE_SYNTAX, |
| 160 | + 'Using variable syntax "$..." inside string is disallowed, found "$array[1]".' |
| 161 | + ); |
| 162 | + |
| 163 | + self::assertSniffError( |
| 164 | + $report, |
| 165 | + 48, |
| 166 | + DisallowVariableParsingSniff::CODE_DISALLOWED_SIMPLE_SYNTAX, |
| 167 | + 'Using variable syntax "$..." inside string is disallowed, found "$object->name".' |
| 168 | + ); |
| 169 | + } |
| 170 | + |
| 171 | + public function testNoOptionIsSet(): void |
| 172 | + { |
| 173 | + $this->expectException(UnexpectedValueException::class); |
| 174 | + $this->expectExceptionMessage('No option is set.'); |
| 175 | + |
| 176 | + self::checkFile(__DIR__ . '/data/disallowVariableParsingNoError.php', [ |
| 177 | + 'disallowDollarCurlySyntax' => false, |
| 178 | + 'disallowCurlyDollarSyntax' => false, |
| 179 | + 'disallowSimpleSyntax' => false, |
| 180 | + ]); |
| 181 | + } |
| 182 | + |
| 183 | +} |
0 commit comments