From 9883e928f22f44eccc5c5c69c70ffc2b0f166096 Mon Sep 17 00:00:00 2001 From: Sebastian Schreiber Date: Wed, 17 Apr 2024 15:06:02 +0200 Subject: [PATCH] [TASK] Simplify testing of assertions --- .../src/Testing/PHPUnit/AbstractFractorTestCase.php | 12 ++++++++++++ fractor/src/ValueObject/File.php | 10 ++++++++++ .../Fractor/FractorRunner/Fixture/my_text_file.txt | 1 - .../Fractor/FractorRunner/Fixtures/my_text_file.txt | 3 +++ .../Fractor/FractorRunner/FractorRunnerTest.php | 3 --- .../Fractor/FractorRunner/config/application.php | 3 +-- .../FlexForm/AddRenderTypeToFlexFormFractorTest.php | 10 ---------- .../SelectWithoutRenderType.xml | 0 .../SelectWithoutRenderTypeNotInFlexForm.xml | 0 .../tests/Rules/FlexForm/config/application.php | 2 +- 10 files changed, 27 insertions(+), 17 deletions(-) delete mode 100644 fractor/tests/Fractor/FractorRunner/Fixture/my_text_file.txt create mode 100644 fractor/tests/Fractor/FractorRunner/Fixtures/my_text_file.txt rename typo3-fractor/tests/Rules/FlexForm/{Fixture => Fixtures}/SelectWithoutRenderType.xml (100%) rename typo3-fractor/tests/Rules/FlexForm/{Fixture => Fixtures}/SelectWithoutRenderTypeNotInFlexForm.xml (100%) diff --git a/fractor/src/Testing/PHPUnit/AbstractFractorTestCase.php b/fractor/src/Testing/PHPUnit/AbstractFractorTestCase.php index 84aedffc..9ff19741 100644 --- a/fractor/src/Testing/PHPUnit/AbstractFractorTestCase.php +++ b/fractor/src/Testing/PHPUnit/AbstractFractorTestCase.php @@ -41,6 +41,18 @@ protected function bootFromConfigFile(): void protected function doTest(): void { $this->fractorRunner->run(new NullOutput(), true); + + foreach ($this->fileCollector->getFiles() as $file) { + if ($file) { + $assertionFile = $file->getDirectoryName() . '/../Assertions/' . $file->getFileName(); + + if (file_exists($assertionFile)) { + self::assertStringEqualsFile($assertionFile, $file->getContent()); + } else { + self::assertFalse($file->hasChanged()); + } + } + } } /** diff --git a/fractor/src/ValueObject/File.php b/fractor/src/ValueObject/File.php index 02fab1b8..c5543554 100644 --- a/fractor/src/ValueObject/File.php +++ b/fractor/src/ValueObject/File.php @@ -19,6 +19,16 @@ public function getFilePath(): string return $this->filePath; } + public function getDirectoryName(): string + { + return dirname($this->filePath); + } + + public function getFileName(): string + { + return basename($this->filePath); + } + public function getContent(): string { return $this->content; diff --git a/fractor/tests/Fractor/FractorRunner/Fixture/my_text_file.txt b/fractor/tests/Fractor/FractorRunner/Fixture/my_text_file.txt deleted file mode 100644 index 7d6c0e0b..00000000 --- a/fractor/tests/Fractor/FractorRunner/Fixture/my_text_file.txt +++ /dev/null @@ -1 +0,0 @@ -The text XXX will be replaced by YYY \ No newline at end of file diff --git a/fractor/tests/Fractor/FractorRunner/Fixtures/my_text_file.txt b/fractor/tests/Fractor/FractorRunner/Fixtures/my_text_file.txt new file mode 100644 index 00000000..6bfc3631 --- /dev/null +++ b/fractor/tests/Fractor/FractorRunner/Fixtures/my_text_file.txt @@ -0,0 +1,3 @@ +The text XXX will be replaced by YYY +----- +The text YYY will be replaced by YYY \ No newline at end of file diff --git a/fractor/tests/Fractor/FractorRunner/FractorRunnerTest.php b/fractor/tests/Fractor/FractorRunner/FractorRunnerTest.php index b94e06c7..6c9f51e9 100644 --- a/fractor/tests/Fractor/FractorRunner/FractorRunnerTest.php +++ b/fractor/tests/Fractor/FractorRunner/FractorRunnerTest.php @@ -11,9 +11,6 @@ final class FractorRunnerTest extends AbstractFractorTestCase public function test(): void { $this->doTest(); - $file = $this->fileCollector->getFileByPath(__DIR__ . '/Fixture/my_text_file.txt'); - self::assertNotNull($file); - self::assertStringEqualsFile(__DIR__ . '/Assertions/my_text_file.txt', $file->getContent()); } protected function additionalConfigurationFiles(): array diff --git a/fractor/tests/Fractor/FractorRunner/config/application.php b/fractor/tests/Fractor/FractorRunner/config/application.php index ef7f203b..d0474cc9 100644 --- a/fractor/tests/Fractor/FractorRunner/config/application.php +++ b/fractor/tests/Fractor/FractorRunner/config/application.php @@ -10,14 +10,13 @@ return static function (ContainerConfigurator $containerConfigurator, ContainerBuilder $containerBuilder): void { $parameters = $containerConfigurator->parameters(); - $parameters->set(Option::PATHS, [__DIR__ . '/../Fixture/']); + $parameters->set(Option::PATHS, [__DIR__ . '/../Fixtures/']); $services = $containerConfigurator->services(); $services->defaults() ->autowire() ->autoconfigure(); $services->set(ReplaceXXXTextRule::class); - $services->set(TextFileProcessor::class)->arg('$rules', tagged_iterator('fractor.text_rules')); $containerBuilder->registerForAutoconfiguration(TextRule::class)->addTag('fractor.text_rules'); }; diff --git a/typo3-fractor/tests/Rules/FlexForm/AddRenderTypeToFlexFormFractorTest.php b/typo3-fractor/tests/Rules/FlexForm/AddRenderTypeToFlexFormFractorTest.php index 04838430..a80c2762 100644 --- a/typo3-fractor/tests/Rules/FlexForm/AddRenderTypeToFlexFormFractorTest.php +++ b/typo3-fractor/tests/Rules/FlexForm/AddRenderTypeToFlexFormFractorTest.php @@ -8,17 +8,7 @@ final class AddRenderTypeToFlexFormFractorTest extends AbstractFractorTestCase { public function test(): void { - // Act $this->doTest(); - - // Arrange - $file = $this->fileCollector->getFileByPath(__DIR__ . '/Fixture/SelectWithoutRenderTypeNotInFlexForm.xml'); - self::assertNotNull($file); - self::assertStringEqualsFile(__DIR__ . '/Fixture/SelectWithoutRenderTypeNotInFlexForm.xml', $file->getContent()); - - $file = $this->fileCollector->getFileByPath(__DIR__ . '/Fixture/SelectWithoutRenderType.xml'); - self::assertNotNull($file); - self::assertStringEqualsFile(__DIR__ . '/Assertions/SelectWithoutRenderType.xml', $file->getContent()); } protected function additionalConfigurationFiles(): array diff --git a/typo3-fractor/tests/Rules/FlexForm/Fixture/SelectWithoutRenderType.xml b/typo3-fractor/tests/Rules/FlexForm/Fixtures/SelectWithoutRenderType.xml similarity index 100% rename from typo3-fractor/tests/Rules/FlexForm/Fixture/SelectWithoutRenderType.xml rename to typo3-fractor/tests/Rules/FlexForm/Fixtures/SelectWithoutRenderType.xml diff --git a/typo3-fractor/tests/Rules/FlexForm/Fixture/SelectWithoutRenderTypeNotInFlexForm.xml b/typo3-fractor/tests/Rules/FlexForm/Fixtures/SelectWithoutRenderTypeNotInFlexForm.xml similarity index 100% rename from typo3-fractor/tests/Rules/FlexForm/Fixture/SelectWithoutRenderTypeNotInFlexForm.xml rename to typo3-fractor/tests/Rules/FlexForm/Fixtures/SelectWithoutRenderTypeNotInFlexForm.xml diff --git a/typo3-fractor/tests/Rules/FlexForm/config/application.php b/typo3-fractor/tests/Rules/FlexForm/config/application.php index f3d8d155..dd99f3e4 100644 --- a/typo3-fractor/tests/Rules/FlexForm/config/application.php +++ b/typo3-fractor/tests/Rules/FlexForm/config/application.php @@ -8,5 +8,5 @@ $containerConfigurator->import(__DIR__ . '/../../../../../fractor-xml/config/application.php'); $parameters = $containerConfigurator->parameters(); - $parameters->set(Option::PATHS, [__DIR__ . '/../Fixture/']); + $parameters->set(Option::PATHS, [__DIR__ . '/../Fixtures/']); };