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

[TASK] Use fixture files #251

Merged
merged 1 commit into from
Nov 26, 2024
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 @@ -15,12 +15,12 @@ final class AddPackageToRequireComposerJsonFractorTest extends AbstractFractorTe
public function test(string $filePath): void
{
$this->doTestFile($filePath);
$this->assertThatRuleIsApplied($filePath, AddPackageToRequireComposerJsonFractor::class);
$this->assertThatRuleIsApplied(AddPackageToRequireComposerJsonFractor::class);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixtures', '*.json');
return self::yieldFilesFromDirectory(__DIR__ . '/Fixtures', '*.json.fixture');
}

public function provideConfigFilePath(): ?string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ final class AddPackageToRequireDevComposerJsonFractorTest extends AbstractFracto
public function test(string $filePath): void
{
$this->doTestFile($filePath);
$this->assertThatRuleIsApplied($filePath, AddPackageToRequireDevComposerJsonFractor::class);
$this->assertThatRuleIsApplied(AddPackageToRequireDevComposerJsonFractor::class);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixtures', '*.json');
return self::yieldFilesFromDirectory(__DIR__ . '/Fixtures', '*.json.fixture');
}

public function provideConfigFilePath(): ?string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ final class ChangePackageVersionComposerJsonFractorTest extends AbstractFractorT
public function test(string $filePath): void
{
$this->doTestFile($filePath);
$this->assertThatRuleIsApplied($filePath, ChangePackageVersionComposerJsonFractor::class);
$this->assertThatRuleIsApplied(ChangePackageVersionComposerJsonFractor::class);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixtures', '*.json');
return self::yieldFilesFromDirectory(__DIR__ . '/Fixtures', '*.json.fixture');
}

public function provideConfigFilePath(): ?string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ final class RemovePackageComposerJsonFractorTest extends AbstractFractorTestCase
public function test(string $filePath): void
{
$this->doTestFile($filePath);
$this->assertThatRuleIsApplied($filePath, RemovePackageComposerJsonFractor::class);
$this->assertThatRuleIsApplied(RemovePackageComposerJsonFractor::class);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixtures', '*.json');
return self::yieldFilesFromDirectory(__DIR__ . '/Fixtures', '*.json.fixture');
}

public function provideConfigFilePath(): ?string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ final class RenamePackageComposerJsonFractorTest extends AbstractFractorTestCase
public function test(string $filePath): void
{
$this->doTestFile($filePath);
$this->assertThatRuleIsApplied($filePath, RenamePackageComposerJsonFractor::class);
$this->assertThatRuleIsApplied(RenamePackageComposerJsonFractor::class);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixtures', '*.json');
return self::yieldFilesFromDirectory(__DIR__ . '/Fixtures', '*.json.fixture');
}

public function provideConfigFilePath(): ?string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ final class ReplacePackageAndVersionComposerJsonFractorTest extends AbstractFrac
public function test(string $filePath): void
{
$this->doTestFile($filePath);
$this->assertThatRuleIsApplied($filePath, ReplacePackageAndVersionComposerJsonFractor::class);
$this->assertThatRuleIsApplied(ReplacePackageAndVersionComposerJsonFractor::class);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixtures', '*.json');
return self::yieldFilesFromDirectory(__DIR__ . '/Fixtures', '*.json.fixture');
}

public function provideConfigFilePath(): ?string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ final class FluidFileProcessorTest extends AbstractFractorTestCase
public function test(string $filePath): void
{
$this->doTestFile($filePath);
$this->assertThatRuleIsApplied($filePath, DummyFluidFractorRule::class);
$this->assertThatRuleIsApplied(DummyFluidFractorRule::class);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixtures', '*.html');
return self::yieldFilesFromDirectory(__DIR__ . '/Fixtures', '*.html.fixture');
}

public function provideConfigFilePath(): ?string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private function addRuleAndTestCase(string $fixtureFileExtension): array
return [
__DIR__ . '/../../templates/rules/TYPO3__MajorPrefixed__/__Type__/__Name__.php',
__DIR__ . '/../../templates/rules-tests/TYPO3__MajorPrefixed__/__Type__/__Test_Directory__/__Name__Test.php.inc',
__DIR__ . '/../../templates/rules-tests/TYPO3__MajorPrefixed__/__Type__/__Test_Directory__/Fixtures/fixture.' . $fixtureFileExtension,
__DIR__ . '/../../templates/rules-tests/TYPO3__MajorPrefixed__/__Type__/__Test_Directory__/Fixtures/fixture.' . $fixtureFileExtension . '.fixture',
__DIR__ . '/../../templates/rules-tests/TYPO3__MajorPrefixed__/__Type__/__Test_Directory__/config/fractor.php.inc',
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ public function testCreateRuleForFlexFormWithoutChangelog(): void
self::assertFileExists($basePathConfig . '/typo3-9999.php');
self::assertFileExists($basePathRules . '/TYPO3v9999/FlexForm/MigrateFlexFormFractor.php');
self::assertFileExists($basePathRuleTests . '/TYPO3v9999/FlexForm/MigrateFlexFormFractor/config/fractor.php');
self::assertFileExists($basePathRuleTests . '/TYPO3v9999/FlexForm/MigrateFlexFormFractor/Fixtures/fixture.xml');
self::assertFileExists(
$basePathRuleTests . '/TYPO3v9999/FlexForm/MigrateFlexFormFractor/Fixtures/fixture.xml.fixture'
);
self::assertFileExists(
$basePathRuleTests . '/TYPO3v9999/FlexForm/MigrateFlexFormFractor/MigrateFlexFormFractorTest.php.inc'
);
Expand Down Expand Up @@ -141,7 +143,9 @@ public function testCreateRuleForFlexFormWithChangelog(): void
self::assertFileExists($basePathConfig . '/typo3-9999.php');
self::assertFileExists($basePathRules . '/TYPO3v9999/FlexForm/MigrateFlexFormFractor.php');
self::assertFileExists($basePathRuleTests . '/TYPO3v9999/FlexForm/MigrateFlexFormFractor/config/fractor.php');
self::assertFileExists($basePathRuleTests . '/TYPO3v9999/FlexForm/MigrateFlexFormFractor/Fixtures/fixture.xml');
self::assertFileExists(
$basePathRuleTests . '/TYPO3v9999/FlexForm/MigrateFlexFormFractor/Fixtures/fixture.xml.fixture'
);
self::assertFileExists(
$basePathRuleTests . '/TYPO3v9999/FlexForm/MigrateFlexFormFractor/MigrateFlexFormFractorTest.php.inc'
);
Expand Down Expand Up @@ -176,7 +180,9 @@ public function testCreateRuleForFluidWithoutChangelog(): void
self::assertFileExists($basePathConfig . '/typo3-9999.php');
self::assertFileExists($basePathRules . '/TYPO3v9999/Fluid/MigrateFluidFractor.php');
self::assertFileExists($basePathRuleTests . '/TYPO3v9999/Fluid/MigrateFluidFractor/config/fractor.php');
self::assertFileExists($basePathRuleTests . '/TYPO3v9999/Fluid/MigrateFluidFractor/Fixtures/fixture.html');
self::assertFileExists(
$basePathRuleTests . '/TYPO3v9999/Fluid/MigrateFluidFractor/Fixtures/fixture.html.fixture'
);
self::assertFileExists(
$basePathRuleTests . '/TYPO3v9999/Fluid/MigrateFluidFractor/MigrateFluidFractorTest.php.inc'
);
Expand Down Expand Up @@ -216,7 +222,9 @@ public function testCreateRuleForFluidWithChangelog(): void
self::assertFileExists($basePathConfig . '/typo3-9999.php');
self::assertFileExists($basePathRules . '/TYPO3v9999/Fluid/MigrateFluidFractor.php');
self::assertFileExists($basePathRuleTests . '/TYPO3v9999/Fluid/MigrateFluidFractor/config/fractor.php');
self::assertFileExists($basePathRuleTests . '/TYPO3v9999/Fluid/MigrateFluidFractor/Fixtures/fixture.html');
self::assertFileExists(
$basePathRuleTests . '/TYPO3v9999/Fluid/MigrateFluidFractor/Fixtures/fixture.html.fixture'
);
self::assertFileExists(
$basePathRuleTests . '/TYPO3v9999/Fluid/MigrateFluidFractor/MigrateFluidFractorTest.php.inc'
);
Expand Down Expand Up @@ -253,7 +261,7 @@ public function testCreateRuleForTypoScriptWithoutChangelog(): void
$basePathRuleTests . '/TYPO3v9999/TypoScript/MigrateTypoScriptFractor/config/fractor.php'
);
self::assertFileExists(
$basePathRuleTests . '/TYPO3v9999/TypoScript/MigrateTypoScriptFractor/Fixtures/fixture.typoscript'
$basePathRuleTests . '/TYPO3v9999/TypoScript/MigrateTypoScriptFractor/Fixtures/fixture.typoscript.fixture'
);
self::assertFileExists(
$basePathRuleTests . '/TYPO3v9999/TypoScript/MigrateTypoScriptFractor/MigrateTypoScriptFractorTest.php.inc'
Expand Down Expand Up @@ -300,7 +308,7 @@ public function testCreateRuleForTypoScriptWithChangelog(): void
$basePathRuleTests . '/TYPO3v9999/TypoScript/MigrateTypoScriptFractor/config/fractor.php'
);
self::assertFileExists(
$basePathRuleTests . '/TYPO3v9999/TypoScript/MigrateTypoScriptFractor/Fixtures/fixture.typoscript'
$basePathRuleTests . '/TYPO3v9999/TypoScript/MigrateTypoScriptFractor/Fixtures/fixture.typoscript.fixture'
);
self::assertFileExists(
$basePathRuleTests . '/TYPO3v9999/TypoScript/MigrateTypoScriptFractor/MigrateTypoScriptFractorTest.php.inc'
Expand Down Expand Up @@ -335,7 +343,9 @@ public function testCreateRuleForYamlWithoutChangelog(): void
self::assertFileExists($basePathConfig . '/typo3-9999.php');
self::assertFileExists($basePathRules . '/TYPO3v9999/Yaml/MigrateYamlFractor.php');
self::assertFileExists($basePathRuleTests . '/TYPO3v9999/Yaml/MigrateYamlFractor/config/fractor.php');
self::assertFileExists($basePathRuleTests . '/TYPO3v9999/Yaml/MigrateYamlFractor/Fixtures/fixture.yaml');
self::assertFileExists(
$basePathRuleTests . '/TYPO3v9999/Yaml/MigrateYamlFractor/Fixtures/fixture.yaml.fixture'
);
self::assertFileExists(
$basePathRuleTests . '/TYPO3v9999/Yaml/MigrateYamlFractor/MigrateYamlFractorTest.php.inc'
);
Expand Down Expand Up @@ -375,7 +385,9 @@ public function testCreateRuleForYamlWithChangelog(): void
self::assertFileExists($basePathConfig . '/typo3-9999.php');
self::assertFileExists($basePathRules . '/TYPO3v9999/Yaml/MigrateYamlFractor.php');
self::assertFileExists($basePathRuleTests . '/TYPO3v9999/Yaml/MigrateYamlFractor/config/fractor.php');
self::assertFileExists($basePathRuleTests . '/TYPO3v9999/Yaml/MigrateYamlFractor/Fixtures/fixture.yaml');
self::assertFileExists(
$basePathRuleTests . '/TYPO3v9999/Yaml/MigrateYamlFractor/Fixtures/fixture.yaml.fixture'
);
self::assertFileExists(
$basePathRuleTests . '/TYPO3v9999/Yaml/MigrateYamlFractor/MigrateYamlFractorTest.php.inc'
);
Expand Down Expand Up @@ -410,7 +422,7 @@ public function testCreateRuleForComposerWithoutChangelog(): void
self::assertFileExists($basePathRules . '/TYPO3v9999/Composer/MigrateComposerFractor.php');
self::assertFileExists($basePathRuleTests . '/TYPO3v9999/Composer/MigrateComposerFractor/config/fractor.php');
self::assertFileExists(
$basePathRuleTests . '/TYPO3v9999/Composer/MigrateComposerFractor/Fixtures/fixture.json'
$basePathRuleTests . '/TYPO3v9999/Composer/MigrateComposerFractor/Fixtures/fixture.json.fixture'
);
self::assertFileExists(
$basePathRuleTests . '/TYPO3v9999/Composer/MigrateComposerFractor/MigrateComposerFractorTest.php.inc'
Expand Down Expand Up @@ -455,7 +467,7 @@ public function testCreateRuleForComposerWithChangelog(): void
self::assertFileExists($basePathRules . '/TYPO3v9999/Composer/MigrateComposerFractor.php');
self::assertFileExists($basePathRuleTests . '/TYPO3v9999/Composer/MigrateComposerFractor/config/fractor.php');
self::assertFileExists(
$basePathRuleTests . '/TYPO3v9999/Composer/MigrateComposerFractor/Fixtures/fixture.json'
$basePathRuleTests . '/TYPO3v9999/Composer/MigrateComposerFractor/Fixtures/fixture.json.fixture'
);
self::assertFileExists(
$basePathRuleTests . '/TYPO3v9999/Composer/MigrateComposerFractor/MigrateComposerFractorTest.php.inc'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ class TypoScriptFileProcessorTest extends AbstractFractorTestCase
public function test(string $filePath): void
{
$this->doTestFile($filePath);
$this->assertThatRuleIsApplied($filePath, DummyTypoScriptFractorRule::class);
$this->assertThatRuleIsApplied(DummyTypoScriptFractorRule::class);
}

public static function provideData(): \Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixtures', '*.typoscript');
return self::yieldFilesFromDirectory(__DIR__ . '/Fixtures', '*.typoscript.fixture');
}

public function provideConfigFilePath(): ?string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ final class YamlFileProcessorTest extends AbstractFractorTestCase
public function test(string $filePath): void
{
$this->doTestFile($filePath);
$this->assertThatRuleIsApplied($filePath, DummyYamlFractorRule::class);
$this->assertThatRuleIsApplied(DummyYamlFractorRule::class);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixtures', '*.yaml');
return self::yieldFilesFromDirectory(__DIR__ . '/Fixtures', '*.yaml.fixture');
}

public function provideConfigFilePath(): ?string
Expand Down
55 changes: 33 additions & 22 deletions packages/fractor/src/Testing/PHPUnit/AbstractFractorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
use a9f\Fractor\Testing\Contract\FractorTestInterface;
use a9f\Fractor\Testing\Fixture\FixtureFileFinder;
use a9f\Fractor\Testing\Fixture\FixtureSplitter;
use a9f\Fractor\Testing\PHPUnit\ValueObject\FractorTestResult;
use Nette\Utils\FileSystem;
use Nette\Utils\Strings;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;

Expand All @@ -30,8 +32,6 @@ abstract class AbstractFractorTestCase extends TestCase implements FractorTestIn

private FractorRunner $fractorRunner;

private ?string $copiedFile = null;

protected function setUp(): void
{
$this->bootContainer();
Expand All @@ -42,10 +42,8 @@ protected function setUp(): void
protected function tearDown(): void
{
// clear temporary file
if (is_string($this->inputFilePath) && is_string($this->copiedFile)) {
if (\is_string($this->inputFilePath)) {
FileSystem::delete($this->inputFilePath);
// restore copied file
FileSystem::rename($this->copiedFile, $this->inputFilePath);
}

unset($this->currentContainer);
Expand Down Expand Up @@ -93,9 +91,6 @@ protected static function yieldFilesFromDirectory(string $directory, string $suf

protected function doTestFile(string $fixtureFilePath): void
{
$this->copiedFile = $fixtureFilePath . '.tmp';

FileSystem::copy($fixtureFilePath, $this->copiedFile);
// prepare input file contents and expected file output contents
$fixtureFileContents = FileSystem::read($fixtureFilePath);

Expand All @@ -113,9 +108,8 @@ protected function doTestFile(string $fixtureFilePath): void
$inputFilePath = $this->createInputFilePath($fixtureFilePath);
// to remove later in tearDown()
$this->inputFilePath = $inputFilePath;

if ($fixtureFilePath === $this->copiedFile) {
throw new ShouldNotHappenException('Fixture file and copied file cannot be the same: ' . $fixtureFilePath);
if ($fixtureFilePath === $inputFilePath) {
throw new ShouldNotHappenException('Fixture file and input file cannot be the same: ' . $fixtureFilePath);
}

// write temp file
Expand All @@ -127,9 +121,12 @@ protected function doTestFile(string $fixtureFilePath): void
/**
* @param class-string<FractorRule> $rule
*/
protected function assertThatRuleIsApplied(string $filePath, string $rule): void
protected function assertThatRuleIsApplied(string $rule): void
{
$file = $this->fileCollector->getFileByPath($filePath);
if (! \is_string($this->inputFilePath)) {
self::fail('inputFilePath is not a string');
}
$file = $this->fileCollector->getFileByPath($this->inputFilePath);
self::assertInstanceOf(File::class, $file);
self::assertEquals([AppliedRule::fromClassString($rule)], $file->getAppliedRules());
}
Expand All @@ -148,32 +145,46 @@ private function doTestFileMatchesExpectedContent(
string $fixtureFilePath
): void {
// the file is now changed (if any rule matches)
$changedContents = $this->processFilePath($originalFilePath);
$fractorTestResult = $this->processFilePath($originalFilePath);
$changedContents = $fractorTestResult->getChangedContents();

$fixtureFilename = basename($fixtureFilePath);
$failureMessage = sprintf('Failed on fixture file "%s"', $fixtureFilename);
// give more context about used rules in case of set testing
if (\count($fractorTestResult->getAppliedFractorRules()) > 0) {
$failureMessage .= \PHP_EOL . \PHP_EOL;
$failureMessage .= 'Applied Fractor rules:' . \PHP_EOL;
foreach ($fractorTestResult->getAppliedFractorRules() as $appliedFractorRule) {
$failureMessage .= ' * ' . $appliedFractorRule . \PHP_EOL;
}
}

self::assertSame(trim($expectedFileContents), trim($changedContents), $failureMessage);
}

private function processFilePath(string $filePath): string
private function processFilePath(string $filePath): FractorTestResult
{
$configurationFactory = $this->getService(ConfigurationFactory::class);
$configuration = $configurationFactory->createForTests([$filePath]);

$this->fractorRunner->run(new NullOutput(), $configuration);
$processResult = $this->fractorRunner->run(new NullOutput(), $configuration);

// return changed file contents
return FileSystem::read($filePath);
$changedFileContents = FileSystem::read($filePath);

return new FractorTestResult($changedFileContents, $processResult);
}

private function createInputFilePath(string $fixtureFilePath): string
{
$inputFileDirectory = dirname($fixtureFilePath);

$trimmedFixtureFilePath = $fixtureFilePath;

$fixtureBasename = pathinfo($trimmedFixtureFilePath, PATHINFO_BASENAME);
$inputFileDirectory = \dirname($fixtureFilePath);
// remove ".fixture" suffix
if (str_ends_with($fixtureFilePath, '.fixture')) {
$trimmedFixtureFilePath = Strings::substring($fixtureFilePath, 0, -8);
} else {
$trimmedFixtureFilePath = $fixtureFilePath;
}
$fixtureBasename = \pathinfo($trimmedFixtureFilePath, \PATHINFO_BASENAME);
return $inputFileDirectory . '/' . $fixtureBasename;
}
}
Loading