diff --git a/Tests/Utils/FunctionDeclarations/IsArrowFunctionTest.php b/Tests/Utils/FunctionDeclarations/IsArrowFunctionTest.php index e358b709..a17c7fb3 100644 --- a/Tests/Utils/FunctionDeclarations/IsArrowFunctionTest.php +++ b/Tests/Utils/FunctionDeclarations/IsArrowFunctionTest.php @@ -695,12 +695,10 @@ public function testResultIsCached() // The test case used is specifically selected to be one which will always reach the cache check. $methodName = 'PHPCSUtils\\Utils\\FunctionDeclarations::getArrowFunctionOpenClose'; $cases = $this->dataArrowFunction(); - $testMarker = $cases['non-arrow-function-call-to-static-method']['testMarker']; - $expected = $cases['non-arrow-function-call-to-static-method']['expected']; - $targetContent = 'fn'; + $testMarker = $cases['live-coding']['testMarker']; + $expected = $cases['live-coding']['expected']; - $targets = Collections::arrowFunctionTokensBC(); - $stackPtr = $this->getTargetToken($testMarker, $targets, $targetContent); + $stackPtr = $this->getTargetToken($testMarker, Collections::arrowFunctionTokensBC(), 'fn'); // Change from offsets to absolute token positions. if ($expected['get'] !== false) { diff --git a/Tests/Utils/Lists/IsShortArrayOrListTest.php b/Tests/Utils/Lists/IsShortArrayOrListTest.php index 66481af2..244fb717 100644 --- a/Tests/Utils/Lists/IsShortArrayOrListTest.php +++ b/Tests/Utils/Lists/IsShortArrayOrListTest.php @@ -10,6 +10,7 @@ namespace PHPCSUtils\Tests\Utils\Arrays; +use PHPCSUtils\Internal\Cache; use PHPCSUtils\TestUtils\UtilityMethodTestCase; use PHPCSUtils\Utils\Arrays; use PHPCSUtils\Utils\Lists; @@ -438,4 +439,72 @@ public function dataIsShortArrayOrList() ], ]; } + + /** + * Verify that the build-in caching is used when caching is enabled. + * + * @covers \PHPCSUtils\Utils\Arrays::isShortArray + * + * @return void + */ + public function testIsShortArrayResultIsCached() + { + $methodName = 'PHPCSUtils\\Utils\\Arrays::isShortArray'; + $cases = $this->dataIsShortArrayOrList(); + $testMarker = $cases['short-array-in-foreach'][0]; + $expected = $cases['short-array-in-foreach'][1]['array']; + + $stackPtr = $this->getTargetToken($testMarker, \T_OPEN_SHORT_ARRAY); + + // Verify the caching works. + $origStatus = Cache::$enabled; + Cache::$enabled = true; + + $resultFirstRun = Arrays::isShortArray(self::$phpcsFile, $stackPtr); + $isCached = Cache::isCached(self::$phpcsFile, $methodName, $stackPtr); + $resultSecondRun = Arrays::isShortArray(self::$phpcsFile, $stackPtr); + + if ($origStatus === false) { + Cache::clear(); + } + Cache::$enabled = $origStatus; + + $this->assertSame($expected, $resultFirstRun, 'First result did not match expectation'); + $this->assertTrue($isCached, 'Cache::isCached() could not find the cached value'); + $this->assertSame($resultFirstRun, $resultSecondRun, 'Second result did not match first'); + } + + /** + * Verify that the build-in caching is used when caching is enabled. + * + * @covers \PHPCSUtils\Utils\Lists::isShortList + * + * @return void + */ + public function testIsShortListResultIsCached() + { + $methodName = 'PHPCSUtils\\Utils\\Lists::isShortList'; + $cases = $this->dataIsShortArrayOrList(); + $testMarker = $cases['short-list-with-nesting-and-keys'][0]; + $expected = $cases['short-list-with-nesting-and-keys'][1]['list']; + + $stackPtr = $this->getTargetToken($testMarker, \T_OPEN_SHORT_ARRAY); + + // Verify the caching works. + $origStatus = Cache::$enabled; + Cache::$enabled = true; + + $resultFirstRun = Lists::isShortList(self::$phpcsFile, $stackPtr); + $isCached = Cache::isCached(self::$phpcsFile, $methodName, $stackPtr); + $resultSecondRun = Lists::isShortList(self::$phpcsFile, $stackPtr); + + if ($origStatus === false) { + Cache::clear(); + } + Cache::$enabled = $origStatus; + + $this->assertSame($expected, $resultFirstRun, 'First result did not match expectation'); + $this->assertTrue($isCached, 'Cache::isCached() could not find the cached value'); + $this->assertSame($resultFirstRun, $resultSecondRun, 'Second result did not match first'); + } }