|
| 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\Arrays; |
| 12 | + |
| 13 | +use PHPCSUtils\TestUtils\UtilityMethodTestCase; |
| 14 | +use PHPCSUtils\Utils\Arrays; |
| 15 | +use PHPCSUtils\Utils\PassedParameters; |
| 16 | + |
| 17 | +/** |
| 18 | + * Tests for the \PHPCSUtils\Utils\Arrays::getDoubleArrowPtr() method. |
| 19 | + * |
| 20 | + * @covers \PHPCSUtils\Utils\Arrays::getDoubleArrowPtr |
| 21 | + * |
| 22 | + * @group arrays |
| 23 | + * |
| 24 | + * @since 1.0.0 |
| 25 | + */ |
| 26 | +class GetDoubleArrowPtrTest extends UtilityMethodTestCase |
| 27 | +{ |
| 28 | + |
| 29 | + /** |
| 30 | + * Cache for the parsed parameters array. |
| 31 | + * |
| 32 | + * @var array <string> => <int> |
| 33 | + */ |
| 34 | + private static $parameters = []; |
| 35 | + |
| 36 | + /** |
| 37 | + * Set up the parameters cache for the tests. |
| 38 | + * |
| 39 | + * Retrieves the parameters array only once and caches it as it won't change |
| 40 | + * between the tests anyway. |
| 41 | + * |
| 42 | + * @before |
| 43 | + * |
| 44 | + * @return void |
| 45 | + */ |
| 46 | + protected function setUpCache() |
| 47 | + { |
| 48 | + if (empty(self::$parameters) === true) { |
| 49 | + $target = $this->getTargetToken('/* testGetDoubleArrowPtr */', [\T_OPEN_SHORT_ARRAY]); |
| 50 | + $parameters = PassedParameters::getParameters(self::$phpcsFile, $target); |
| 51 | + |
| 52 | + foreach ($parameters as $index => $values) { |
| 53 | + \preg_match('`^(/\* test[^*]+ \*/)`', $values['raw'], $matches); |
| 54 | + if (empty($matches[1]) === false) { |
| 55 | + self::$parameters[$matches[1]] = $values; |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Test receiving an expected exception when an invalid start position is passed. |
| 63 | + * |
| 64 | + * @return void |
| 65 | + */ |
| 66 | + public function testInvalidStartPositionException() |
| 67 | + { |
| 68 | + $this->expectPhpcsException( |
| 69 | + 'Invalid start and/or end position passed to getDoubleArrowPtr(). Received: $start -10, $end 10' |
| 70 | + ); |
| 71 | + |
| 72 | + Arrays::getDoubleArrowPtr(self::$phpcsFile, -10, 10); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Test receiving an expected exception when an invalid end position is passed. |
| 77 | + * |
| 78 | + * @return void |
| 79 | + */ |
| 80 | + public function testInvalidEndPositionException() |
| 81 | + { |
| 82 | + $this->expectPhpcsException( |
| 83 | + 'Invalid start and/or end position passed to getDoubleArrowPtr(). Received: $start 0, $end 100000' |
| 84 | + ); |
| 85 | + |
| 86 | + Arrays::getDoubleArrowPtr(self::$phpcsFile, 0, 100000); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Test receiving an expected exception when the start position is after the end position. |
| 91 | + * |
| 92 | + * @return void |
| 93 | + */ |
| 94 | + public function testInvalidStartEndPositionException() |
| 95 | + { |
| 96 | + $this->expectPhpcsException( |
| 97 | + 'Invalid start and/or end position passed to getDoubleArrowPtr(). Received: $start 10, $end 5' |
| 98 | + ); |
| 99 | + |
| 100 | + Arrays::getDoubleArrowPtr(self::$phpcsFile, 10, 5); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Test retrieving the position of the double arrow for an array parameter. |
| 105 | + * |
| 106 | + * @dataProvider dataGetDoubleArrowPtr |
| 107 | + * |
| 108 | + * @param string $testMarker The comment which is part of the target array item in the test file. |
| 109 | + * @param array $expected The expected function call result. |
| 110 | + * |
| 111 | + * @return void |
| 112 | + */ |
| 113 | + public function testGetDoubleArrowPtr($testMarker, $expected) |
| 114 | + { |
| 115 | + if (isset(self::$parameters[$testMarker]) === false) { |
| 116 | + $this->fail('Test case not found for ' . $testMarker); |
| 117 | + } |
| 118 | + |
| 119 | + $start = self::$parameters[$testMarker]['start']; |
| 120 | + $end = self::$parameters[$testMarker]['end']; |
| 121 | + |
| 122 | + // Change double arrow position from offset to exact position. |
| 123 | + if ($expected !== false) { |
| 124 | + $expected += $start; |
| 125 | + } |
| 126 | + |
| 127 | + $result = Arrays::getDoubleArrowPtr(self::$phpcsFile, $start, $end); |
| 128 | + $this->assertSame($expected, $result); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Data provider. |
| 133 | + * |
| 134 | + * The double arrow positions are provided as offsets from the $start stackPtr. |
| 135 | + * |
| 136 | + * @see testGetDoubleArrowPtr() |
| 137 | + * |
| 138 | + * @return array |
| 139 | + */ |
| 140 | + public function dataGetDoubleArrowPtr() |
| 141 | + { |
| 142 | + return [ |
| 143 | + 'test-no-arrow' => [ |
| 144 | + '/* testValueNoArrow */', |
| 145 | + false, |
| 146 | + ], |
| 147 | + 'test-arrow-numeric-index' => [ |
| 148 | + '/* testArrowNumericIndex */', |
| 149 | + 8, |
| 150 | + ], |
| 151 | + 'test-arrow-string-index' => [ |
| 152 | + '/* testArrowStringIndex */', |
| 153 | + 8, |
| 154 | + ], |
| 155 | + 'test-arrow-multi-token-index' => [ |
| 156 | + '/* testArrowMultiTokenIndex */', |
| 157 | + 12, |
| 158 | + ], |
| 159 | + 'test-no-arrow-value-short-array' => [ |
| 160 | + '/* testNoArrowValueShortArray */', |
| 161 | + false, |
| 162 | + ], |
| 163 | + 'test-no-arrow-value-long-array' => [ |
| 164 | + '/* testNoArrowValueLongArray */', |
| 165 | + false, |
| 166 | + ], |
| 167 | + 'test-no-arrow-value-nested-arrays' => [ |
| 168 | + '/* testNoArrowValueNestedArrays */', |
| 169 | + false, |
| 170 | + ], |
| 171 | + 'test-no-arrow-value-closure' => [ |
| 172 | + '/* testNoArrowValueClosure */', |
| 173 | + false, |
| 174 | + ], |
| 175 | + 'test-arrow-value-short-array' => [ |
| 176 | + '/* testArrowValueShortArray */', |
| 177 | + 8, |
| 178 | + ], |
| 179 | + 'test-arrow-value-long-array' => [ |
| 180 | + '/* testArrowValueLongArray */', |
| 181 | + 8, |
| 182 | + ], |
| 183 | + 'test-arrow-value-closure' => [ |
| 184 | + '/* testArrowValueClosure */', |
| 185 | + 8, |
| 186 | + ], |
| 187 | + 'test-no-arrow-value-anon-class-with-foreach' => [ |
| 188 | + '/* testNoArrowValueAnonClassForeach */', |
| 189 | + false, |
| 190 | + ], |
| 191 | + 'test-no-arrow-value-closure-with-keyed-yield' => [ |
| 192 | + '/* testNoArrowValueClosureYieldWithKey */', |
| 193 | + false, |
| 194 | + ], |
| 195 | + 'test-arrow-key-closure-with-keyed-yield' => [ |
| 196 | + '/* testArrowKeyClosureYieldWithKey */', |
| 197 | + 24, |
| 198 | + ], |
| 199 | + ]; |
| 200 | + } |
| 201 | +} |
0 commit comments