-
-
Notifications
You must be signed in to change notification settings - Fork 708
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RunnableTestCase to run fixed code in a test
- added RunnableTestCase::assertOriginalAndFixedFileYieldSameResult($file) that takes a fixture file and expects fixture classes that implement the new RunnableInterface which exposes a run() method - the fixture class is dynamically renamed to avoid naming conflicts and loaded via eval() - fixtures that don't implement the RunnableInterface are ignored otherwise the run() method is called on the original class as well as on the fixed one and the output is expected to be equal (via assertSame) by default - the default assertion behavior can be changed via $assertion callable
- Loading branch information
Showing
5 changed files
with
116 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 26 additions & 6 deletions
32
...y/tests/Rector/FuncCall/ArrayKeysAndInArrayToArrayKeyExistsRector/Fixture/fixture.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,48 @@ | ||
<?php | ||
|
||
namespace Rector\CodeQuality\Tests\Rector\FuncCall\ArrayKeysAndInArrayToIssetRector\Fixture; | ||
namespace Rector\CodeQuality\Tests\Rector\FuncCall\ArrayKeysAndInArrayToArrayKeyExistsRector\Fixture; | ||
|
||
class SomeClass | ||
use Rector\CodeQuality\Tests\Rector\FuncCall\ArrayKeysAndInArrayToArrayKeyExistsRector\RunnableInterface; | ||
|
||
class SomeClass implements RunnableInterface | ||
{ | ||
public function run($packageName, $values) | ||
public function test($packageName, $values) | ||
{ | ||
$keys = array_keys($values); | ||
return in_array($packageName, $keys, true); | ||
} | ||
|
||
public function run() | ||
{ | ||
$packageName = "foo"; | ||
$values = ["foo" => "bar"]; | ||
|
||
return $this->test($packageName, $values); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\CodeQuality\Tests\Rector\FuncCall\ArrayKeysAndInArrayToIssetRector\Fixture; | ||
namespace Rector\CodeQuality\Tests\Rector\FuncCall\ArrayKeysAndInArrayToArrayKeyExistsRector\Fixture; | ||
|
||
class SomeClass | ||
use Rector\CodeQuality\Tests\Rector\FuncCall\ArrayKeysAndInArrayToArrayKeyExistsRector\RunnableInterface; | ||
|
||
class SomeClass implements RunnableInterface | ||
{ | ||
public function run($packageName, $values) | ||
public function test($packageName, $values) | ||
{ | ||
return array_key_exists($packageName, $values); | ||
} | ||
|
||
public function run() | ||
{ | ||
$packageName = "foo"; | ||
$values = ["foo" => "bar"]; | ||
|
||
return $this->test($packageName, $values); | ||
} | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
...ity/tests/Rector/FuncCall/ArrayKeysAndInArrayToArrayKeyExistsRector/RunnableInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\CodeQuality\Tests\Rector\FuncCall\ArrayKeysAndInArrayToArrayKeyExistsRector; | ||
|
||
interface RunnableInterface | ||
{ | ||
public function run(); | ||
} |
75 changes: 75 additions & 0 deletions
75
...lity/tests/Rector/FuncCall/ArrayKeysAndInArrayToArrayKeyExistsRector/RunnableTestCase.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\CodeQuality\Tests\Rector\FuncCall\ArrayKeysAndInArrayToArrayKeyExistsRector; | ||
|
||
use Nette\Utils\Strings; | ||
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Rector\Core\Testing\ValueObject\SplitLine; | ||
use ReflectionClass; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
class RunnableTestCase extends AbstractRectorTestCase | ||
{ | ||
protected function assertOriginalAndFixedFileYieldSameResult(string $file, ?callable $assertion = null): void | ||
{ | ||
$defaultAssertion = function (RunnableInterface $original, RunnableInterface $fixed): void { | ||
$actual = $original->run(); | ||
$expected = $fixed->run(); | ||
|
||
$this->assertSame($actual, $expected); | ||
}; | ||
|
||
$assertion ??= $defaultAssertion; | ||
|
||
/** | ||
* Todo: Duplicate from | ||
* | ||
* @see FixtureSplitter::splitContentToOriginalFileAndExpectedFile | ||
* ==> refactor in a method | ||
*/ | ||
$smartFileInfo = new SmartFileInfo($file); | ||
if (Strings::match($smartFileInfo->getContents(), SplitLine::SPLIT_LINE)) { | ||
[$originalContent, $expectedContent] = Strings::split($smartFileInfo->getContents(), SplitLine::SPLIT_LINE); | ||
} else { | ||
$originalContent = $smartFileInfo->getContents(); | ||
$expectedContent = $originalContent; | ||
} | ||
|
||
$originalInstance = $this->loadClass($originalContent); | ||
if ($originalInstance !== null) { | ||
$expectedInstance = $this->loadClass($expectedContent); | ||
if ($expectedInstance === null) { | ||
$assertion($originalInstance, $expectedInstance); | ||
} | ||
} | ||
} | ||
|
||
protected function getTemporaryClassName(): string | ||
{ | ||
$testName = (new ReflectionClass(static::class))->getShortName(); | ||
// Todo - pull in Ramsey UUID to generate temporay class names? | ||
// $uuid = Uuid::uuid4()->toString(); | ||
$uuid = md5((string) random_int(0, 100000000)); | ||
$className = $testName . '_' . $uuid; | ||
|
||
return (string) preg_replace('#[^0-9a-zA-Z]#', '_', $className); | ||
} | ||
|
||
protected function loadClass(string $classContent): ?RunnableInterface | ||
{ | ||
$className = $this->getTemporaryClassName(); | ||
$loadable = (string) preg_replace('#\\s*<\\?php#', '', $classContent); | ||
$loadable = (string) preg_replace('#\\s*namespace.*;#', '', $loadable); | ||
$loadable = (string) preg_replace('#class\\s+(\\S*)\\s+#', "class ${className} ", $loadable); | ||
eval($loadable); | ||
if (is_a($className, RunnableInterface::class, true)) { | ||
/** | ||
* @var RunnableInterface | ||
*/ | ||
return new $className(); | ||
} | ||
return null; | ||
} | ||
} |