Skip to content

Commit

Permalink
ADD TO: implement
Browse files Browse the repository at this point in the history
  • Loading branch information
jrfnl committed Jun 30, 2022
1 parent c0960a1 commit e1cdc0b
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 5 deletions.
8 changes: 3 additions & 5 deletions Tests/Utils/FunctionDeclarations/IsArrowFunctionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
69 changes: 69 additions & 0 deletions Tests/Utils/Lists/IsShortArrayOrListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
}
}

0 comments on commit e1cdc0b

Please sign in to comment.