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 9f5aeea commit 4095888
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
35 changes: 35 additions & 0 deletions Tests/Utils/Namespaces/DetermineNamespaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace PHPCSUtils\Tests\Utils\Namespaces;

use PHPCSUtils\Internal\Cache;
use PHPCSUtils\TestUtils\UtilityMethodTestCase;
use PHPCSUtils\Utils\Namespaces;

Expand Down Expand Up @@ -189,6 +190,40 @@ public function dataDetermineNamespace()
];
}

/**
* Verify that the build-in caching is used when caching is enabled.
*
* @return void
*/
public function testFindNamespacePtrResultIsCached()
{
// The test case used is specifically selected to be one which will always reach the cache check.
$methodName = 'PHPCSUtils\\Utils\\Namespaces::findNamespacePtr';
$cases = $this->dataDetermineNamespace();
$testMarker = $cases['non-scoped-namespace-2']['testMarker'];
$expected = $cases['non-scoped-namespace-2']['expected']['ptr'];

$stackPtr = $this->getTargetToken($testMarker, \T_ECHO);
$expected = $this->getTargetToken($expected, \T_NAMESPACE);

// Verify the caching works.
$origStatus = Cache::$enabled;
Cache::$enabled = true;

$resultFirstRun = Namespaces::findNamespacePtr(self::$phpcsFile, $stackPtr);
$isCached = Cache::isCached(self::$phpcsFile, $methodName, $stackPtr);
$resultSecondRun = Namespaces::findNamespacePtr(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');
}

/**
* Test that the namespace declaration itself is not regarded as being namespaced.
*
Expand Down
2 changes: 1 addition & 1 deletion Tests/Utils/PassedParameters/GetParametersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ public function test( $foo, $bar ) {
*
* @return void
*/
public function testResultIsCached()
public function testGetParametersResultIsCached()
{
// The test case used is specifically selected as the raw and the clean param values will be the same.
$methodName = 'PHPCSUtils\\Utils\\PassedParameters::getParameters';
Expand Down

0 comments on commit 4095888

Please sign in to comment.