Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RunnableTestCase to run fixed code in a test #3089

Merged
merged 1 commit into from
Mar 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@

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

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

public function provideData(): Iterator
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
<?php

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

class SomeClass
use Rector\Core\Testing\PHPUnit\RunnableInterface;

class SomeClass implements RunnableInterface
{
public function run($packageName, $values)
public function run()
{
$packageName = "foo";
$values = ["foo" => "bar"];

$keys = array_keys($values);
return in_array($packageName, $keys, true);
}
Expand All @@ -15,12 +20,16 @@ class SomeClass
-----
<?php

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

use Rector\Core\Testing\PHPUnit\RunnableInterface;

class SomeClass
class SomeClass implements RunnableInterface
{
public function run($packageName, $values)
public function run()
{
$packageName = "foo";
$values = ["foo" => "bar"];
return array_key_exists($packageName, $values);
}
}
Expand Down
68 changes: 68 additions & 0 deletions src/Testing/PHPUnit/AbstractRunnableRectorTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Testing\PHPUnit;

use Nette\Utils\Strings;
use Rector\Core\Testing\ValueObject\SplitLine;
use ReflectionClass;
use Symplify\SmartFileSystem\SmartFileInfo;

abstract class AbstractRunnableRectorTestCase extends AbstractRectorTestCase
{
protected function assertOriginalAndFixedFileResultEquals(string $file): void
{
/**
* 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) {
$actual = $originalInstance->run();
$expected = $expectedInstance->run();

$this->assertSame($actual, $expected);
}
}
}

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 Strings::replace($className, '#[^0-9a-zA-Z]#', '_');
}

protected function loadClass(string $classContent): ?RunnableInterface
{
$className = $this->getTemporaryClassName();
$loadable = Strings::replace($classContent, '#\\s*<\\?php#', '');
$loadable = Strings::replace($loadable, '#\\s*namespace.*;#', '');
$loadable = Strings::replace($loadable, '#class\\s+(\\S*)\\s+#', sprintf('class %s ', $className));
eval($loadable);
if (is_a($className, RunnableInterface::class, true)) {
/**
* @var RunnableInterface
*/
return new $className();
}
return null;
}
}
10 changes: 10 additions & 0 deletions src/Testing/PHPUnit/RunnableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Testing\PHPUnit;

interface RunnableInterface
{
public function run();
}