tests: fix conflicting validating by autoloading only Wrong files in tests

This commit is contained in:
Tomas Votruba 2018-10-04 00:50:07 +08:00
parent 2812c30ee9
commit 5b65bb52fa
32 changed files with 234 additions and 37 deletions

View File

@ -85,8 +85,102 @@
"packages/Symfony/tests",
"packages/Twig/tests",
"packages/Utils/tests",
"tests/Issues",
"tests/Rector"
"tests/Source",
"tests/Rector/Architecture/DependencyInjection/ActionInjectionToConstructorInjectionRector/Wrong",
"tests/Rector/Architecture/DependencyInjection/AnnotatedPropertyInjectToConstructorInjectionRector/Wrong",
"tests/Issues/Issue594/Wrong",
"tests/Rector/Assign/PropertyAssignToMethodCallRector/Wrong",
"tests/Rector/RectorOrder/Wrong",
"tests/Rector/DomainDrivenDesign/ValueObjectRemoverRector/Wrong",
"tests/Rector/DomainDrivenDesign/ValueObjectRemoverDocBlockRector/Wrong",
"tests/Rector/StaticCall/StaticCallToFunctionRector/Wrong",
"tests/Rector/MethodCall/MethodCallToAnotherMethodCallWithArgumentsRector/Wrong",
"tests/Rector/MethodCall/StaticMethodNameReplacerRector/Wrong",
"tests/Rector/MethodCall/MethodNameReplacerRector/Wrong",
"tests/Rector/MethodBody/ReturnThisRemoveRector/Wrong",
"tests/Rector/MethodBody/NormalToFluentRector/Wrong",
"tests/Rector/MethodBody/FluentReplaceRector/Wrong",
"tests/Rector/MagicDisclosure/GetAndSetToMethodCallRector/Wrong",
"tests/Rector/MagicDisclosure/UnsetAndIssetToMethodCallRector/Wrong",
"tests/Rector/MagicDisclosure/ToStringToMethodCallRector/Wrong",
"tests/Rector/Typehint/ReturnTypehintRector/Wrong",
"tests/Rector/Typehint/ParentTypehintedArgumentRector/Wrong",
"tests/Rector/Argument/ArgumentRemoverRector/Wrong",
"tests/Rector/Argument/ArgumentAdderRector/Wrong",
"tests/Rector/Argument/ArgumentDefaultValueReplacerRector/Wrong",
"tests/Rector/Namespace_/NamespaceReplacerRector/Wrong",
"tests/Rector/Namespace_/PseudoNamespaceToNamespaceRector/Wrong",
"tests/Rector/Namespace_/PseudoNamespaceToNamespaceRector/Source",
"tests/Rector/Function_/FunctionToStaticCallRector/Wrong",
"tests/Rector/Function_/FunctionReplaceRector/Wrong",
"tests/Rector/Function_/FunctionToMethodCallRector/Wrong",
"tests/Rector/Property/PropertyNameReplacerRector/Wrong",
"tests/Rector/Property/PropertyToMethodRector/Wrong",
"tests/Rector/Architecture/DependencyInjection/AnnotatedPropertyInjectToConstructorInjectionRector/Wrong",
"tests/Rector/Architecture/DependencyInjection/ActionInjectionToConstructorInjectionRector/Wrong",
"tests/Rector/Architecture/DoctrineRepositoryAsService/Wrong",
"tests/Rector/Interface_/MergeInterfacesRector/Wrong",
"tests/Rector/Visibility/ChangeMethodVisibilityRector/Wrong",
"tests/Rector/Visibility/ChangePropertyVisibilityRector/Wrong",
"tests/Rector/Visibility/ChangeConstantVisibilityRector/Wrong",
"tests/Rector/Annotation/AnnotationReplacerRector/Wrong",
"tests/Rector/CodeQuality/UnnecessaryTernaryExpressionRector/Wrong",
"tests/Rector/CodeQuality/InArrayAndArrayKeysToArrayKeyExistsRector/Wrong",
"tests/Rector/Constant/ClassConstantReplacerRector/Wrong",
"tests/Rector/Constant/RenameClassConstantsUseToStringsRector/Wrong",
"tests/Rector/Class_/ParentClassToTraitsRector/Wrong",
"tests/Rector/Class_/ClassReplacerRector/Wrong",
"packages/PHPUnit/tests/Rector/DelegateExceptionArgumentsRector/Wrong",
"packages/PHPUnit/tests/Rector/ArrayToYieldDataProviderRector/Wrong",
"packages/PHPUnit/tests/Rector/ExceptionAnnotationRector/Wrong",
"packages/PHPUnit/tests/Rector/TryCatchToExpectExceptionRector/Wrong",
"packages/PHPUnit/tests/Rector/SpecificMethod/AssertPropertyExistsRector/Wrong",
"packages/PHPUnit/tests/Rector/SpecificMethod/AssertIssetToSpecificMethodRector/Wrong",
"packages/PHPUnit/tests/Rector/SpecificMethod/AssertRegExpRector/Wrong",
"packages/PHPUnit/tests/Rector/SpecificMethod/AssertComparisonToSpecificMethodRector/Wrong",
"packages/PHPUnit/tests/Rector/SpecificMethod/AssertCompareToSpecificMethodRector/Wrong",
"packages/PHPUnit/tests/Rector/SpecificMethod/AssertSameBoolNullToSpecificMethodRector/Wrong",
"packages/PHPUnit/tests/Rector/SpecificMethod/AssertTrueFalseToSpecificMethodRector/Wrong",
"packages/PHPUnit/tests/Rector/SpecificMethod/AssertTrueFalseInternalTypeToSpecificMethodRector/Wrong",
"packages/PHPUnit/tests/Rector/SpecificMethod/AssertNotOperatorRector/Wrong",
"packages/PHPUnit/tests/Rector/SpecificMethod/AssertInstanceOfComparisonRector/Wrong",
"packages/PHPUnit/tests/Rector/SpecificMethod/AssertFalseStrposToContainsRector/Wrong",
"packages/PHPUnit/tests/Rector/GetMockRector/Wrong",
"packages/Sylius/tests/Rector/Review/Wrong",
"packages/Silverstripe/tests/Rector/DefineConstantToStaticCallRector/Wrong",
"packages/Silverstripe/tests/Rector/ConstantToStaticCallRector/Wrong",
"packages/Symfony/tests/Rector/HttpKernel/GetRequestRector/Wrong",
"packages/Symfony/tests/Rector/Controller/RedirectToRouteRector/Wrong",
"packages/Symfony/tests/Rector/Controller/AddFlashRector/Wrong",
"packages/Symfony/tests/Rector/Controller/ActionSuffixRemoverRector/Wrong",
"packages/Symfony/tests/Rector/Validator/ConstraintUrlOptionRector/Wrong",
"packages/Symfony/tests/Rector/Console/ConsoleExceptionToErrorEventConstantRector/Wrong",
"packages/Symfony/tests/Rector/Yaml/SessionStrictTrueByDefaultYamlRector/Wrong",
"packages/Symfony/tests/Rector/Yaml/SpaceBetweenKeyAndValueYamlRector/Wrong",
"packages/Symfony/tests/Rector/Yaml/ParseFileRector/Wrong",
"packages/Symfony/tests/Rector/DependencyInjection/ContainerBuilderCompileEnvArgumentRector/Wrong",
"packages/Symfony/tests/Rector/VarDumper/VarDumperTestTraitMethodArgsRector/Wrong",
"packages/Symfony/tests/Rector/FrameworkBundle/ContainerGetToConstructorInjectionRector/Wrong",
"packages/Symfony/tests/Rector/FrameworkBundle/GetParameterToConstructorInjectionRector/Wrong",
"packages/Symfony/tests/Rector/FrameworkBundle/GetToConstructorInjectionRector/Wrong",
"packages/Symfony/tests/Rector/Form/OptionNameRector/Wrong",
"packages/Symfony/tests/Rector/Form/FormIsValidRector/Wrong",
"packages/Symfony/tests/Rector/Form/StringFormTypeToClassRector/Wrong",
"packages/Symfony/tests/Rector/Form/FormTypeGetParentRector/Wrong",
"packages/Symfony/tests/Rector/Process/ProcessBuilderGetProcessRector/Wrong",
"packages/Symfony/tests/Rector/Process/ProcessBuilderInstanceRector/Wrong",
"packages/Twig/tests/Rector/SimpleFunctionAndFilterRector/Wrong",
"packages/Doctrine/tests/Rector/AliasToClassRector/Wrong",
"packages/PhpParser/tests/Rector/RemoveNodeRector/Wrong",
"packages/PhpParser/tests/Rector/IdentifierRector/Wrong",
"packages/PhpParser/tests/Rector/ParamAndStaticVarNameRector/Wrong",
"packages/PhpParser/tests/Rector/UseWithAliasRector/Wrong",
"packages/PhpParser/tests/Rector/CatchAndClosureUseNameRector/Wrong",
"packages/PhpParser/tests/Rector/SetLineRector/Wrong",
"packages/CakePHP/tests/Rector/MethodCall/ModalToGetSetRector/Wrong",
"packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/Wrong",
"packages/Php/tests/Rector/TypedPropertyRector/Wrong",
"packages/Php/tests/Rector/ExceptionHandlerTypehintRector/Wrong"
],
"files": [
"tests/Rector/Psr4/MultipleClassFileToPsr4ClassesRector/Source/exceptions.php",

View File

@ -1,9 +1,23 @@
<?php declare(strict_types=1);
use PhpParser\Node;
class SomeVisitor implements \PhpParser\NodeVisitor
{
public function leaveNode(\PhpParser\Node $node)
{
return \PhpParser\NodeTraverser::REMOVE_NODE;
}
public function beforeTraverse(array $nodes)
{
}
public function enterNode(Node $node)
{
}
public function afterTraverse(array $nodes)
{
}
}

View File

@ -1,9 +1,23 @@
<?php declare(strict_types=1);
use PhpParser\Node;
class SomeVisitor implements \PhpParser\NodeVisitor
{
public function leaveNode(\PhpParser\Node $node)
{
return false;
}
public function beforeTraverse(array $nodes)
{
}
public function enterNode(Node $node)
{
}
public function afterTraverse(array $nodes)
{
}
}

View File

@ -2,6 +2,7 @@
namespace Rector\Testing\PHPUnit;
use PHPStan\AnalysedCodeException;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Rector\Application\FileProcessor;
@ -68,7 +69,14 @@ abstract class AbstractRectorTestCase extends TestCase
$this->parameterProvider->changeParameter('source', [$file]);
$reconstructedFileContent = $this->fileProcessor->processFileToString(new SmartFileInfo($file));
try {
$reconstructedFileContent = $this->fileProcessor->processFileToString(new SmartFileInfo($file));
} catch (AnalysedCodeException $analysedCodeException) {
// change message to include responsible file
$message = sprintf('Analyze error in "%s" file:%s%s', $file, PHP_EOL, $analysedCodeException->getMessage());
$exceptionClass = get_class($analysedCodeException);
throw new $exceptionClass($message);
}
$this->assertStringEqualsFile(
$reconstructedFile,

View File

@ -6,6 +6,7 @@ use Iterator;
use Rector\FileSystem\FilesFinder;
use Rector\Tests\AbstractContainerAwareTestCase;
use Symfony\Component\Finder\SplFileInfo;
use function Safe\sort;
final class FilesFinderTest extends AbstractContainerAwareTestCase
{
@ -20,25 +21,39 @@ final class FilesFinderTest extends AbstractContainerAwareTestCase
}
/**
* @param string[] $suffixes
* @dataProvider provideData()
*/
public function test(array $suffixes, int $count, string $fileName): void
public function testSingleSuffix(string $suffix, int $count, string $expectedFileName): void
{
$foundFiles = $this->filesFinder->findInDirectoriesAndFiles([__DIR__ . '/FilesFinderSource'], $suffixes);
$foundFiles = $this->filesFinder->findInDirectoriesAndFiles([__DIR__ . '/FilesFinderSource'], [$suffix]);
$this->assertCount($count, $foundFiles);
/** @var SplFileInfo $foundFile */
$foundFile = array_pop($foundFiles);
$this->assertSame($fileName, $foundFile->getBasename());
$this->assertSame($expectedFileName, $foundFile->getBasename());
}
public function provideData(): Iterator
{
yield [['php'], 1, 'SomeFile.php'];
yield [['yml'], 1, 'some_config.yml'];
yield [['yaml'], 1, 'other_config.yaml'];
yield [['php'], 1, 'SomeFile.php'];
yield [['yaml', 'yml'], 2, 'some_config.yml'];
yield ['php', 1, 'SomeFile.php'];
yield ['yml', 1, 'some_config.yml'];
yield ['yaml', 1, 'other_config.yaml'];
yield ['php', 1, 'SomeFile.php'];
}
public function testMultipleSuffixes(): void
{
$foundFiles = $this->filesFinder->findInDirectoriesAndFiles([__DIR__ . '/FilesFinderSource'], ['yaml', 'yml']);
$this->assertCount(2, $foundFiles);
$foundFileNames = [];
foreach ($foundFiles as $foundFile) {
$foundFileNames[] = $foundFile->getFilename();
}
$expectedFoundFileNames = ['some_config.yml', 'other_config.yaml'];
sort($foundFileNames);
sort($expectedFoundFileNames);
$this->assertSame($expectedFoundFileNames, $foundFileNames);
}
}

View File

@ -1,6 +1,6 @@
<?php declare(strict_types=1);
namespace AppBundle\Controller;
namespace Rector\Tests\Rector\Architecture\DoctrineRepositoryAsService\Wrong;
use AppBundle\Entity\Post;
use Rector\Tests\Rector\Architecture\DoctrineRepositoryAsService\Source\SymfonyController;

View File

@ -23,7 +23,7 @@ final class DoctrineRepositoryAsServiceTest extends AbstractRectorTestCase
public function provideWrongToFixedFiles(): Iterator
{
yield [__DIR__ . '/Wrong/wrong.php.inc', __DIR__ . '/Correct/correct.php.inc'];
yield [__DIR__ . '/Wrong/wrong2.php.inc', __DIR__ . '/Correct/correct2.php.inc'];
yield [__DIR__ . '/Wrong/PostController.php', __DIR__ . '/Correct/correct2.php.inc'];
}
protected function provideConfig(): string

View File

@ -1,6 +1,6 @@
<?php declare(strict_types=1);
namespace AppBundle\Controller;
namespace Rector\Tests\Rector\Architecture\DoctrineRepositoryAsService\Wrong;
use AppBundle\Entity\Post;
use Rector\Tests\Rector\Architecture\DoctrineRepositoryAsService\Source\SymfonyController;

View File

@ -1,5 +1,8 @@
<?php declare (strict_types=1);
namespace Rector\Tests\Rector\Class_\ParentClassToTraitsRector\Wrong;
use Rector\Tests\Rector\Class_\ParentClassToTraitsRector\Source\AnotherTrait;
use Rector\Tests\Rector\Class_\ParentClassToTraitsRector\Source\ParentObject;
class ClassWithExternalConstant

View File

@ -0,0 +1,8 @@
<?php declare(strict_types=1);
namespace Rector\Tests\Rector\Class_\ParentClassToTraitsRector\Source;
trait AnotherTrait
{
}

View File

@ -1,5 +1,8 @@
<?php declare (strict_types=1);
namespace Rector\Tests\Rector\Class_\ParentClassToTraitsRector\Wrong;
use Rector\Tests\Rector\Class_\ParentClassToTraitsRector\Source\AnotherTrait;
use Rector\Tests\Rector\Class_\ParentClassToTraitsRector\Source\ParentObject;
class ClassWithExternalConstant extends ParentObject

View File

@ -1,6 +1,6 @@
<?php declare(strict_types=1);
namespace Rector\Tests\Rector\Function_\FunctionReplacerRector;
namespace Rector\Tests\Rector\Function_\FunctionReplaceRector;
use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
@ -8,7 +8,7 @@ use Rector\Testing\PHPUnit\AbstractRectorTestCase;
/**
* @see \Rector\Rector\Function_\FunctionReplaceRector
*/
final class FunctionReplacerRectorTest extends AbstractRectorTestCase
final class FunctionReplaceRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideWrongToFixedFiles()

View File

@ -4,7 +4,7 @@ namespace OldNamespace\SubNamespace;
class SomeTest extends \PHPUnit\Framework\TestCase
{
public function create(\PHPUnit\Something $argument): \PHPUnit\Something
public function create(\PHPUnit\Something $argument): \PHPUnit\Framework\Something
{
return new \PHPUnit\Framework\Something;
}

View File

@ -1,5 +1,7 @@
<?php declare(strict_types=1);
namespace Rector\Tests\Rector\Namespace_\PseudoNamespaceToNamespaceRector\Wrong;
use PHPUnit\SpecialClass;
class SomeTest

View File

@ -0,0 +1,6 @@
<?php declare(strict_types=1);
final class PHPUnit_Framework_Something
{
}

View File

@ -0,0 +1,6 @@
<?php declare(strict_types=1);
abstract class PHPUnit_Framework_TestCase
{
}

View File

@ -0,0 +1,6 @@
<?php declare(strict_types=1);
final class PHPUnit_Something
{
}

View File

@ -0,0 +1,6 @@
<?php declare(strict_types=1);
final class PHPUnit_SpecialClass
{
}

View File

@ -4,7 +4,7 @@ namespace OldNamespace\SubNamespace;
class SomeTest extends \PHPUnit_Framework_TestCase
{
public function create(\PHPUnit_Something $argument): \PHPUnit_Something
public function create(\PHPUnit_Something $argument): \PHPUnit_Framework_Something
{
return new \PHPUnit_Framework_Something;
}

View File

@ -1,5 +1,7 @@
<?php declare(strict_types=1);
namespace Rector\Tests\Rector\Namespace_\PseudoNamespaceToNamespaceRector\Wrong;
use PHPUnit_SpecialClass;
class SomeTest

View File

@ -1,6 +1,6 @@
<?php declare(strict_types=1);
namespace SomeNamespace;
namespace Rector\Tests\Rector\Typehint\ParentTypehintedArgumentRector\Wrong;
use Rector\Tests\Rector\Typehint\ParentTypehintedArgumentRector\Source\ParserInterface;

View File

@ -1,6 +1,6 @@
<?php declare(strict_types=1);
namespace SomeNamespace;
namespace Rector\Tests\Rector\Typehint\ParentTypehintedArgumentRector\Wrong;
use Rector\Tests\Rector\Typehint\ParentTypehintedArgumentRector\Source\ClassMetadataFactory;

View File

@ -20,8 +20,8 @@ final class ParentTypehintedArgumentRectorTest extends AbstractRectorTestCase
public function provideWrongToFixedFiles(): Iterator
{
yield [__DIR__ . '/Wrong/wrong.php.inc', __DIR__ . '/Correct/correct.php.inc'];
yield [__DIR__ . '/Wrong/wrong2.php.inc', __DIR__ . '/Correct/correct2.php.inc'];
yield [__DIR__ . '/Wrong/SomeClassImplementingParserInterface.php', __DIR__ . '/Correct/correct.php.inc'];
yield [__DIR__ . '/Wrong/MyMetadataFactory.php', __DIR__ . '/Correct/correct2.php.inc'];
}
protected function provideConfig(): string

View File

@ -4,5 +4,5 @@ namespace Rector\Tests\Rector\Typehint\ParentTypehintedArgumentRector\Source;
interface ParserInterface
{
public function parse(string $code, \PhpParser\ErrorHandler $errorHandler = null);
public function parse($code, \PhpParser\ErrorHandler $errorHandler = null);
}

View File

@ -1,6 +1,6 @@
<?php declare(strict_types=1);
namespace SomeNamespace;
namespace Rector\Tests\Rector\Typehint\ParentTypehintedArgumentRector\Wrong;
use Rector\Tests\Rector\Typehint\ParentTypehintedArgumentRector\Source\ClassMetadataFactory;

View File

@ -1,6 +1,6 @@
<?php declare(strict_types=1);
namespace SomeNamespace;
namespace Rector\Tests\Rector\Typehint\ParentTypehintedArgumentRector\Wrong;
use Rector\Tests\Rector\Typehint\ParentTypehintedArgumentRector\Source\ParserInterface;

View File

@ -1,9 +1,9 @@
services:
Rector\Rector\Typehint\ParentTypehintedArgumentRector:
$typehintForArgumentByMethodAndClass:
'Rector\Tests\Rector\Typehint\ParentTypehintedArgumentRector\Source\ParserInterface':
'parse':
'code': 'string'
'Rector\Tests\Rector\Typehint\ParentTypehintedArgumentRector\Source\ClassMetadataFactory':
'setEntityManager':
'em': 'Doctrine\ORM\EntityManagerInterface'
Rector\Tests\Rector\Typehint\ParentTypehintedArgumentRector\Source\ParserInterface:
parse:
code: 'string'
Rector\Tests\Rector\Typehint\ParentTypehintedArgumentRector\Source\ClassMetadataFactory:
setEntityManager:
em: 'Doctrine\ORM\EntityManagerInterface'

View File

@ -6,7 +6,7 @@ use Rector\Tests\Rector\Visibility\ChangeConstantVisibilityRector\Source\ParentO
class ClassWithInvalidConstants extends ParentObject
{
private const TO_BE_PUBLIC_CONSTANT = 1;
public const TO_BE_PUBLIC_CONSTANT = 1;
public const TO_BE_PROTECTED_CONSTANT = 2;
protected const TO_BE_PRIVATE_CONSTANT = 3;
}

View File

@ -8,7 +8,7 @@ use Rector\Testing\PHPUnit\AbstractRectorTestCase;
/**
* @covers \Rector\Rector\Visibility\ChangeMethodVisibilityRector
*/
final class ChangePropertyVisibilityRectorTest extends AbstractRectorTestCase
final class ChangeMethodVisibilityRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideWrongToFixedFiles()

View File

@ -4,11 +4,11 @@ namespace Rector\Tests\Rector\Visibility\ChangeMethodVisibilityRector\Source;
class ParentObject
{
public function toBePublicMethod() {
private function toBePublicMethod() {
}
public function toBePublicStaticMethod() {
static function toBePublicStaticMethod() {
}

View File

@ -4,7 +4,7 @@ use Rector\Tests\Rector\Visibility\ChangeMethodVisibilityRector\Source\ParentObj
class ClassThatInherits extends ParentObject
{
private function toBePublicStaticMethod() {
static function toBePublicStaticMethod() {
}
}

View File

@ -0,0 +1,10 @@
<?php declare(strict_types=1);
// class that is used all over the code
// autoloaded manually via composer to keep the rest of code clean
namespace SomeNamespace;
final class SomeClass
{
}