|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * PHPCSUtils, utility functions and classes for PHP_CodeSniffer sniff developers. |
| 4 | + * |
| 5 | + * @package PHPCSUtils |
| 6 | + * @copyright 2019 PHPCSUtils Contributors |
| 7 | + * @license https://opensource.org/licenses/LGPL-3.0 LGPL3 |
| 8 | + * @link https://github.com/PHPCSStandards/PHPCSUtils |
| 9 | + */ |
| 10 | + |
| 11 | +namespace PHPCSUtils\Tests\Utils\TextStrings; |
| 12 | + |
| 13 | +use PHPCSUtils\TestUtils\UtilityMethodTestCase; |
| 14 | +use PHPCSUtils\Utils\TextStrings; |
| 15 | + |
| 16 | +/** |
| 17 | + * Tests for the \PHPCSUtils\Utils\TextStrings::getCompleteTextString() method. |
| 18 | + * |
| 19 | + * @covers \PHPCSUtils\Utils\TextStrings::getCompleteTextString |
| 20 | + * |
| 21 | + * @group textstrings |
| 22 | + * |
| 23 | + * @since 1.0.0 |
| 24 | + */ |
| 25 | +class GetCompleteTextStringTest extends UtilityMethodTestCase |
| 26 | +{ |
| 27 | + |
| 28 | + /** |
| 29 | + * Token types to target for these tests. |
| 30 | + * |
| 31 | + * @var array |
| 32 | + */ |
| 33 | + private $targets = [ |
| 34 | + \T_START_HEREDOC, |
| 35 | + \T_START_NOWDOC, |
| 36 | + \T_CONSTANT_ENCAPSED_STRING, |
| 37 | + \T_DOUBLE_QUOTED_STRING, |
| 38 | + ]; |
| 39 | + |
| 40 | + /** |
| 41 | + * Test passing a non-existent token pointer. |
| 42 | + * |
| 43 | + * @return void |
| 44 | + */ |
| 45 | + public function testNonExistentToken() |
| 46 | + { |
| 47 | + $this->expectPhpcsException( |
| 48 | + '$stackPtr must be of type T_START_HEREDOC, T_START_NOWDOC, T_CONSTANT_ENCAPSED_STRING' |
| 49 | + . ' or T_DOUBLE_QUOTED_STRING' |
| 50 | + ); |
| 51 | + |
| 52 | + TextStrings::getCompleteTextString(self::$phpcsFile, 100000); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Test receiving an expected exception when a non text string is passed. |
| 57 | + * |
| 58 | + * @return void |
| 59 | + */ |
| 60 | + public function testNotATextStringException() |
| 61 | + { |
| 62 | + $this->expectPhpcsException( |
| 63 | + '$stackPtr must be of type T_START_HEREDOC, T_START_NOWDOC, T_CONSTANT_ENCAPSED_STRING' |
| 64 | + . ' or T_DOUBLE_QUOTED_STRING' |
| 65 | + ); |
| 66 | + |
| 67 | + $next = $this->getTargetToken('/* testNotATextString */', \T_RETURN); |
| 68 | + TextStrings::getCompleteTextString(self::$phpcsFile, $next); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Test receiving an expected exception when a text string token is not the first token |
| 73 | + * of a multi-line text string. |
| 74 | + * |
| 75 | + * @return void |
| 76 | + */ |
| 77 | + public function testNotFirstTextStringException() |
| 78 | + { |
| 79 | + $this->expectPhpcsException('$stackPtr must be the start of the text string'); |
| 80 | + |
| 81 | + $next = $this->getTargetToken( |
| 82 | + '/* testNotFirstTextStringToken */', |
| 83 | + \T_CONSTANT_ENCAPSED_STRING, |
| 84 | + 'second line |
| 85 | +' |
| 86 | + ); |
| 87 | + TextStrings::getCompleteTextString(self::$phpcsFile, $next); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Test correctly retrieving the contents of a (potentially) multi-line text string. |
| 92 | + * |
| 93 | + * @dataProvider dataGetCompleteTextString |
| 94 | + * |
| 95 | + * @param string $testMarker The comment which prefaces the target token in the test file. |
| 96 | + * @param string $expected The expected function return value. |
| 97 | + * @param string $expectedWithQuotes The expected function return value when $stripQuotes is set to "false". |
| 98 | + * |
| 99 | + * @return void |
| 100 | + */ |
| 101 | + public function testGetCompleteTextString($testMarker, $expected, $expectedWithQuotes) |
| 102 | + { |
| 103 | + $stackPtr = $this->getTargetToken($testMarker, $this->targets); |
| 104 | + |
| 105 | + $result = TextStrings::getCompleteTextString(self::$phpcsFile, $stackPtr); |
| 106 | + $this->assertSame($expected, $result, 'Test failed getting the correct string with quotes stripped'); |
| 107 | + |
| 108 | + $result = TextStrings::getCompleteTextString(self::$phpcsFile, $stackPtr, false); |
| 109 | + $this->assertSame($expectedWithQuotes, $result, 'Test failed getting the correct string (unchanged)'); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Data provider. |
| 114 | + * |
| 115 | + * @see testGetCompleteTextString() For the array format. |
| 116 | + * |
| 117 | + * @return array |
| 118 | + */ |
| 119 | + public function dataGetCompleteTextString() |
| 120 | + { |
| 121 | + return [ |
| 122 | + 'single-line-constant-encapsed-string' => [ |
| 123 | + '/* testSingleLineConstantEncapsedString */', |
| 124 | + 'single line text string', |
| 125 | + "'single line text string'", |
| 126 | + ], |
| 127 | + 'multi-line-constant-encapsed-string' => [ |
| 128 | + '/* testMultiLineConstantEncapsedString */', |
| 129 | + 'first line |
| 130 | +second line |
| 131 | +third line |
| 132 | +fourth line', |
| 133 | + '"first line |
| 134 | +second line |
| 135 | +third line |
| 136 | +fourth line"', |
| 137 | + ], |
| 138 | + 'single-line-double-quoted-string' => [ |
| 139 | + '/* testSingleLineDoubleQuotedString */', |
| 140 | + 'single $line text string', |
| 141 | + '"single $line text string"', |
| 142 | + ], |
| 143 | + 'multi-line-double-quoted-string' => [ |
| 144 | + '/* testMultiLineDoubleQuotedString */', |
| 145 | + 'first line |
| 146 | +second $line |
| 147 | +third line |
| 148 | +fourth line', |
| 149 | + '"first line |
| 150 | +second $line |
| 151 | +third line |
| 152 | +fourth line"', |
| 153 | + ], |
| 154 | + 'heredoc' => [ |
| 155 | + '/* testHeredocString */', |
| 156 | + 'first line |
| 157 | +second $line |
| 158 | +third line |
| 159 | +fourth line |
| 160 | +', |
| 161 | + 'first line |
| 162 | +second $line |
| 163 | +third line |
| 164 | +fourth line |
| 165 | +', |
| 166 | + ], |
| 167 | + 'nowdoc' => [ |
| 168 | + '/* testNowdocString */', |
| 169 | + 'first line |
| 170 | +second line |
| 171 | +third line |
| 172 | +fourth line |
| 173 | +', |
| 174 | + 'first line |
| 175 | +second line |
| 176 | +third line |
| 177 | +fourth line |
| 178 | +', |
| 179 | + ], |
| 180 | + 'text-string-at-end-of-file' => [ |
| 181 | + '/* testTextStringAtEndOfFile */', |
| 182 | + 'first line |
| 183 | +last line', |
| 184 | + "'first line |
| 185 | +last line'", |
| 186 | + ], |
| 187 | + ]; |
| 188 | + } |
| 189 | +} |
0 commit comments