Skip to content

Commit

Permalink
Add RunnableTestCase to run fixed code in a test
Browse files Browse the repository at this point in the history
- 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
paslandau committed Mar 28, 2020
1 parent 23b08fc commit 7939a04
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@

use Iterator;
use Rector\CodeQuality\Rector\FuncCall\ArrayKeysAndInArrayToArrayKeyExistsRector;
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;

final class ArrayKeysAndInArrayToArrayKeyExistsRectorTest extends AbstractRectorTestCase
final class ArrayKeysAndInArrayToArrayKeyExistsRectorTest extends RunnableTestCase
{
/**
* @dataProvider provideData()
*/
public function test(string $file): void
{
$this->doTestFile($file);
$this->assertOriginalAndFixedFileYieldSameResult($file);
}

public function provideData(): Iterator
Expand Down
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);
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Rector\CodeQuality\Tests\Rector\FuncCall\ArrayKeysAndInArrayToIssetRector\Fixture;

use Rector\CodeQuality\Tests\Rector\FuncCall\ArrayKeysAndInArrayToArrayKeyExistsRector\AbstractRunnable;

class SkipOver
{
public function run($values)
public function test($values)
{
$keys = array_keys($values);
}
Expand Down
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();
}
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;
}
}

0 comments on commit 7939a04

Please sign in to comment.