Skip to content

Commit

Permalink
[PSR4] Fix deleting file that matches class name (#163)
Browse files Browse the repository at this point in the history
* Add failing test fixture for MultipleClassFileToPsr4ClassesRector

* Fix deleting file that matches class name
  • Loading branch information
zingimmick committed Jun 7, 2021
1 parent 84c80e4 commit 0c9a1f8
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Rector\Tests\PSR4\Rector\Namespace_\MultipleClassFileToPsr4ClassesRector\Fixture;

class ClassMatchesFilenameException
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Rector\Tests\PSR4\Rector\Namespace_\MultipleClassFileToPsr4ClassesRector\Fixture;

class ClassMatchesFilename
{
}

class ClassMatchesFilenameException
{
}

?>
-----
<?php

namespace Rector\Tests\PSR4\Rector\Namespace_\MultipleClassFileToPsr4ClassesRector\Fixture;

class ClassMatchesFilename
{
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Iterator;
use Rector\FileSystemRector\ValueObject\AddedFileWithContent;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\EasyTesting\StaticFixtureSplitter;
use Symplify\SmartFileSystem\SmartFileInfo;
use Symplify\SmartFileSystem\SmartFileSystem;

Expand All @@ -16,13 +17,24 @@ final class MultipleClassFileToPsr4ClassesRectorTest extends AbstractRectorTestC
* @param AddedFileWithContent[] $expectedFilePathsWithContents
* @dataProvider provideData()
*/
public function test(SmartFileInfo $originalFileInfo, array $expectedFilePathsWithContents): void
{
public function test(
SmartFileInfo $originalFileInfo,
array $expectedFilePathsWithContents,
bool $expectedOriginalFileWasRemoved = true
): void {
$this->doTestFileInfo($originalFileInfo);

$this->assertCount($this->removedAndAddedFilesCollector->getAddedFileCount(), $expectedFilePathsWithContents);

$this->assertFilesWereAdded($expectedFilePathsWithContents);

$inputFileInfoAndExpectedFileInfo = StaticFixtureSplitter::splitFileInfoToLocalInputAndExpectedFileInfos(
$originalFileInfo
);
self::assertSame(
$expectedOriginalFileWasRemoved,
$this->removedAndAddedFilesCollector->isFileRemoved($inputFileInfoAndExpectedFileInfo->getInputFileInfo())
);
}

/**
Expand Down Expand Up @@ -74,6 +86,15 @@ public function provideData(): Iterator
];

yield [new SmartFileInfo(__DIR__ . '/Fixture/class_trait_and_interface.php.inc'), $filePathsWithContents];

$filePathsWithContents = [
new AddedFileWithContent(
$this->getFixtureTempDirectory() . '/ClassMatchesFilenameException.php',
$smartFileSystem->readFile(__DIR__ . '/Expected/ClassMatchesFilenameException.php')
),
];

yield [new SmartFileInfo(__DIR__ . '/Fixture/ClassMatchesFilename.php.inc'), $filePathsWithContents, false];
}

public function provideConfigFilePath(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,6 @@ private function printNewNodes(ClassLike $classLike, Node $mainNode): void

$fileDestination = $this->createClassLikeFileDestination($classLike, $smartFileInfo);

$this->removedAndAddedFilesCollector->removeFile($smartFileInfo);

$addedFileWithNodes = new AddedFileWithNodes($fileDestination, $nodesToPrint);
$this->removedAndAddedFilesCollector->addAddedFile($addedFileWithNodes);
}
Expand Down

0 comments on commit 0c9a1f8

Please sign in to comment.