mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-16 16:12:22 +00:00
Merge pull request #3089 from paslandau/run_fixtures_in_tests
Add RunnableTestCase to run fixed code in a test
This commit is contained in:
commit
d3e5960dc2
|
@ -6,9 +6,9 @@ namespace Rector\CodeQuality\Tests\Rector\FuncCall\ArrayKeysAndInArrayToArrayKey
|
|||
|
||||
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()
|
||||
|
@ -16,6 +16,7 @@ final class ArrayKeysAndInArrayToArrayKeyExistsRectorTest extends AbstractRector
|
|||
public function test(string $file): void
|
||||
{
|
||||
$this->doTestFile($file);
|
||||
$this->assertOriginalAndFixedFileResultEquals($file);
|
||||
}
|
||||
|
||||
public function provideData(): Iterator
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
@ -15,12 +20,16 @@ class SomeClass
|
|||
-----
|
||||
<?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"];
|
||||
return array_key_exists($packageName, $values);
|
||||
}
|
||||
}
|
||||
|
|
68
src/Testing/PHPUnit/AbstractRunnableRectorTestCase.php
Normal file
68
src/Testing/PHPUnit/AbstractRunnableRectorTestCase.php
Normal file
|
@ -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
src/Testing/PHPUnit/RunnableInterface.php
Normal file
10
src/Testing/PHPUnit/RunnableInterface.php
Normal file
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Core\Testing\PHPUnit;
|
||||
|
||||
interface RunnableInterface
|
||||
{
|
||||
public function run();
|
||||
}
|
Loading…
Reference in New Issue
Block a user