From fb2eceb5e69dee38b20c2e1a02565dfae2c17669 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Thu, 16 Dec 2021 10:12:26 +0100 Subject: [PATCH] Refactor tests to move common code into parent class This keeps tests smaller and everyone can see the setup of the test and assertions, without boilerplate code in between. --- .../DestinationDataTest/AbstractTest.php | 49 +++++++++++++++ .../ImportCleansTransientFilesTest.php | 61 +++++------------- .../ImportDoesNotUseUploadsFolderTest.php | 63 ++++++------------- .../ImportsExampleAsExpectedTest.php | 61 +++++------------- .../ImportsWithoutRegionIfNotProvidedTest.php | 60 +++++------------- 5 files changed, 117 insertions(+), 177 deletions(-) diff --git a/Tests/Functional/Import/DestinationDataTest/AbstractTest.php b/Tests/Functional/Import/DestinationDataTest/AbstractTest.php index 38847bd..b1fdaa4 100644 --- a/Tests/Functional/Import/DestinationDataTest/AbstractTest.php +++ b/Tests/Functional/Import/DestinationDataTest/AbstractTest.php @@ -2,8 +2,15 @@ namespace Wrm\Events\Tests\Functional\Import\DestinationDataTest; +use GuzzleHttp\ClientInterface as GuzzleClientInterface; +use Psr\Http\Client\ClientInterface; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Tester\CommandTester; +use Symfony\Component\DependencyInjection\Container; use TYPO3\CMS\Core\Localization\LanguageServiceFactory; use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase; +use Wrm\Events\Command\DestinationDataImportCommand; +use Wrm\Events\Tests\ClientFactory; abstract class AbstractTest extends FunctionalTestCase { @@ -31,4 +38,46 @@ abstract class AbstractTest extends FunctionalTestCase parent::tearDown(); } + + protected function setUpConfiguration(array $configuration): void + { + $this->setUpFrontendRootPage(1, [], [ + 'config' => implode(PHP_EOL, [ + 'module.tx_events_pi1.settings.destinationData {', + implode(PHP_EOL, $configuration), + '}', + ]), + ]); + } + + protected function &setUpResponses(array $responses): array + { + $requests = []; + + $client = ClientFactory::createClientWithHistory($responses, $requests); + $container = $this->getContainer(); + if ($container instanceof Container) { + $container->set(ClientInterface::class, $client); + // For TYPO3 10 support + $container->set(GuzzleClientInterface::class, $client); + } + + return $requests; + } + + protected function executeCommand(array $argumentsAndOptions): CommandTester + { + $subject = $this->getContainer()->get(DestinationDataImportCommand::class); + self::assertInstanceOf(Command::class, $subject); + + $tester = new CommandTester($subject); + $tester->execute( + $argumentsAndOptions, + [ + 'capture_stderr_separately' => true, + ] + ); + + return $tester; + } } diff --git a/Tests/Functional/Import/DestinationDataTest/ImportCleansTransientFilesTest.php b/Tests/Functional/Import/DestinationDataTest/ImportCleansTransientFilesTest.php index a85cda3..24f73a2 100644 --- a/Tests/Functional/Import/DestinationDataTest/ImportCleansTransientFilesTest.php +++ b/Tests/Functional/Import/DestinationDataTest/ImportCleansTransientFilesTest.php @@ -2,16 +2,9 @@ namespace Wrm\Events\Tests\Functional\Import\DestinationDataTest; -use GuzzleHttp\ClientInterface as GuzzleClientInterface; use GuzzleHttp\Psr7\Response; -use Psr\Http\Client\ClientInterface; -use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Tester\CommandTester; -use Symfony\Component\DependencyInjection\Container; use TYPO3\CMS\Core\Core\Environment; use TYPO3\CMS\Core\Utility\GeneralUtility; -use Wrm\Events\Command\DestinationDataImportCommand; -use Wrm\Events\Tests\ClientFactory; /** * @testdox DestinationData import @@ -21,51 +14,31 @@ class ImportCleansTransientFilesTest extends AbstractTest /** * @test */ - public function importCleansTransientFiles(): void + public function cleansTransientFiles(): void { $fileImportPath = 'staedte/beispielstadt/events/'; - $this->setUpFrontendRootPage(1, [], [ - 'config' => implode(PHP_EOL, [ - 'module.tx_events_pi1.settings.destinationData {', - 'restUrl = ' . $this->getInstancePath() . '/typo3conf/ext/events/Tests/Functional/Import/DestinationDataTest/Fixtures/Response.json', - 'license = example-license', - 'restType = Event', - 'restLimit = 3', - 'restMode = next_months,12', - 'restTemplate = ET2014A.json', - 'categoriesPid = ', - 'categoryParentUid = ', - '}', - ]), + $this->setUpConfiguration([ + 'restUrl = ' . $this->getInstancePath() . '/typo3conf/ext/events/Tests/Functional/Import/DestinationDataTest/Fixtures/Response.json', + 'license = example-license', + 'restType = Event', + 'restLimit = 3', + 'restMode = next_months,12', + 'restTemplate = ET2014A.json', + 'categoriesPid = ', + 'categoryParentUid = ', ]); - $requests = []; - $client = ClientFactory::createClientWithHistory([ + $requests = &$this->setUpResponses([ new Response(200, [], file_get_contents(__DIR__ . '/Fixtures/ExampleImage.jpg') ?: ''), new Response(200, [], file_get_contents(__DIR__ . '/Fixtures/ExampleImage.jpg') ?: ''), new Response(200, [], file_get_contents(__DIR__ . '/Fixtures/ExampleImage.jpg') ?: ''), - ], $requests); - $container = $this->getContainer(); - if ($container instanceof Container) { - $container->set(ClientInterface::class, $client); - // For TYPO3 10 support - $container->set(GuzzleClientInterface::class, $client); - } + ]); - $subject = $this->getContainer()->get(DestinationDataImportCommand::class); - self::assertInstanceOf(Command::class, $subject); - - $tester = new CommandTester($subject); - $tester->execute( - [ - 'storage-pid' => 2, - 'rest-experience' => 'beispielstadt', - 'files-folder' => $fileImportPath, - ], - [ - 'capture_stderr_separately' => true, - ] - ); + $tester = $this->executeCommand([ + 'storage-pid' => 2, + 'rest-experience' => 'beispielstadt', + 'files-folder' => $fileImportPath, + ]); self::assertSame(0, $tester->getStatusCode()); diff --git a/Tests/Functional/Import/DestinationDataTest/ImportDoesNotUseUploadsFolderTest.php b/Tests/Functional/Import/DestinationDataTest/ImportDoesNotUseUploadsFolderTest.php index 87bb616..7665706 100644 --- a/Tests/Functional/Import/DestinationDataTest/ImportDoesNotUseUploadsFolderTest.php +++ b/Tests/Functional/Import/DestinationDataTest/ImportDoesNotUseUploadsFolderTest.php @@ -2,16 +2,9 @@ namespace Wrm\Events\Tests\Functional\Import\DestinationDataTest; -use GuzzleHttp\ClientInterface as GuzzleClientInterface; use GuzzleHttp\Psr7\Response; -use Psr\Http\Client\ClientInterface; -use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Tester\CommandTester; -use Symfony\Component\DependencyInjection\Container; use TYPO3\CMS\Core\Core\Environment; use TYPO3\CMS\Core\Utility\GeneralUtility; -use Wrm\Events\Command\DestinationDataImportCommand; -use Wrm\Events\Tests\ClientFactory; /** * @testdox DestinationData import @@ -21,52 +14,32 @@ class ImportDoesNotUserUploadsFolderTest extends AbstractTest /** * @test */ - public function importDoesNotUserUploadsFolder(): void + public function doesNotUserUploadsFolder(): void { $fileImportPath = 'staedte/beispielstadt/events/'; - $this->setUpFrontendRootPage(1, [], [ - 'config' => implode(PHP_EOL, [ - 'module.tx_events_pi1.settings.destinationData {', - 'restUrl = ' . $this->getInstancePath() . '/typo3conf/ext/events/Tests/Functional/Import/DestinationDataTest/Fixtures/Response.json', - 'license = example-license', - 'restType = Event', - 'restLimit = 3', - 'restMode = next_months,12', - 'restTemplate = ET2014A.json', - 'categoriesPid = ', - 'categoryParentUid = ', - '}', - ]), + + $this->setUpConfiguration([ + 'restUrl = ' . $this->getInstancePath() . '/typo3conf/ext/events/Tests/Functional/Import/DestinationDataTest/Fixtures/Response.json', + 'license = example-license', + 'restType = Event', + 'restLimit = 3', + 'restMode = next_months,12', + 'restTemplate = ET2014A.json', + 'categoriesPid = ', + 'categoryParentUid = ', ]); - $requests = []; - $client = ClientFactory::createClientWithHistory([ + $requests = &$this->setUpResponses([ new Response(200, [], file_get_contents(__DIR__ . '/Fixtures/ExampleImage.jpg') ?: ''), new Response(200, [], file_get_contents(__DIR__ . '/Fixtures/ExampleImage.jpg') ?: ''), new Response(200, [], file_get_contents(__DIR__ . '/Fixtures/ExampleImage.jpg') ?: ''), - ], $requests); - $container = $this->getContainer(); - if ($container instanceof Container) { - $container->set(ClientInterface::class, $client); - // For TYPO3 10 support - $container->set(GuzzleClientInterface::class, $client); - } - - $subject = $this->getContainer()->get(DestinationDataImportCommand::class); - self::assertInstanceOf(Command::class, $subject); - - $tester = new CommandTester($subject); - $tester->execute( - [ - 'storage-pid' => 2, - 'rest-experience' => 'beispielstadt', - 'files-folder' => $fileImportPath, - ], - [ - 'capture_stderr_separately' => true, - ] - ); + ]); + $tester = $this->executeCommand([ + 'storage-pid' => 2, + 'rest-experience' => 'beispielstadt', + 'files-folder' => $fileImportPath, + ]); self::assertSame(0, $tester->getStatusCode()); self::assertCount(3, $requests, 'Unexpected number of requests were made.'); diff --git a/Tests/Functional/Import/DestinationDataTest/ImportsExampleAsExpectedTest.php b/Tests/Functional/Import/DestinationDataTest/ImportsExampleAsExpectedTest.php index c7b466c..89cd6d7 100644 --- a/Tests/Functional/Import/DestinationDataTest/ImportsExampleAsExpectedTest.php +++ b/Tests/Functional/Import/DestinationDataTest/ImportsExampleAsExpectedTest.php @@ -2,15 +2,8 @@ namespace Wrm\Events\Tests\Functional\Import\DestinationDataTest; -use GuzzleHttp\ClientInterface as GuzzleClientInterface; use GuzzleHttp\Psr7\Response; -use Psr\Http\Client\ClientInterface; -use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Tester\CommandTester; -use Symfony\Component\DependencyInjection\Container; use TYPO3\CMS\Core\Utility\GeneralUtility; -use Wrm\Events\Command\DestinationDataImportCommand; -use Wrm\Events\Tests\ClientFactory; /** * @testdox DestinationData import @@ -25,49 +18,29 @@ class ImportsExampleAsExpectedTest extends AbstractTest $fileImportPath = 'staedte/beispielstadt/events/'; $this->importDataSet('EXT:events/Tests/Functional/Import/DestinationDataTest/Fixtures/SingleRegion.xml'); - $this->setUpFrontendRootPage(1, [], [ - 'config' => implode(PHP_EOL, [ - 'module.tx_events_pi1.settings.destinationData {', - 'restUrl = ' . $this->getInstancePath() . '/typo3conf/ext/events/Tests/Functional/Import/DestinationDataTest/Fixtures/Response.json', - 'license = example-license', - 'restType = Event', - 'restLimit = 3', - 'restMode = next_months,12', - 'restTemplate = ET2014A.json', - 'categoriesPid = ', - 'categoryParentUid = ', - '}', - ]), + $this->setUpConfiguration([ + 'restUrl = ' . $this->getInstancePath() . '/typo3conf/ext/events/Tests/Functional/Import/DestinationDataTest/Fixtures/Response.json', + 'license = example-license', + 'restType = Event', + 'restLimit = 3', + 'restMode = next_months,12', + 'restTemplate = ET2014A.json', + 'categoriesPid = ', + 'categoryParentUid = ', ]); - $requests = []; - $client = ClientFactory::createClientWithHistory([ + $requests = &$this->setUpResponses([ new Response(200, [], file_get_contents(__DIR__ . '/Fixtures/ExampleImage.jpg') ?: ''), new Response(200, [], file_get_contents(__DIR__ . '/Fixtures/ExampleImage.jpg') ?: ''), new Response(200, [], file_get_contents(__DIR__ . '/Fixtures/ExampleImage.jpg') ?: ''), - ], $requests); - $container = $this->getContainer(); - if ($container instanceof Container) { - $container->set(ClientInterface::class, $client); - // For TYPO3 10 support - $container->set(GuzzleClientInterface::class, $client); - } + ]); - $subject = $this->getContainer()->get(DestinationDataImportCommand::class); - self::assertInstanceOf(Command::class, $subject); - - $tester = new CommandTester($subject); - $tester->execute( - [ - 'storage-pid' => 2, - 'rest-experience' => 'beispielstadt', - 'files-folder' => $fileImportPath, - 'region-uid' => 1, - ], - [ - 'capture_stderr_separately' => true, - ] - ); + $tester = $this->executeCommand([ + 'storage-pid' => 2, + 'rest-experience' => 'beispielstadt', + 'files-folder' => $fileImportPath, + 'region-uid' => 1, + ]); self::assertSame(0, $tester->getStatusCode()); diff --git a/Tests/Functional/Import/DestinationDataTest/ImportsWithoutRegionIfNotProvidedTest.php b/Tests/Functional/Import/DestinationDataTest/ImportsWithoutRegionIfNotProvidedTest.php index c09e09f..e98b136 100644 --- a/Tests/Functional/Import/DestinationDataTest/ImportsWithoutRegionIfNotProvidedTest.php +++ b/Tests/Functional/Import/DestinationDataTest/ImportsWithoutRegionIfNotProvidedTest.php @@ -2,15 +2,8 @@ namespace Wrm\Events\Tests\Functional\Import\DestinationDataTest; -use GuzzleHttp\ClientInterface as GuzzleClientInterface; use GuzzleHttp\Psr7\Response; -use Psr\Http\Client\ClientInterface; -use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Tester\CommandTester; -use Symfony\Component\DependencyInjection\Container; use TYPO3\CMS\Core\Utility\GeneralUtility; -use Wrm\Events\Command\DestinationDataImportCommand; -use Wrm\Events\Tests\ClientFactory; /** * @testdox DestinationData import @@ -23,48 +16,27 @@ class ImportsWithoutRegionIfNotProvided extends AbstractTest public function importsWithoutRegionIfNotProvided(): void { $fileImportPath = 'staedte/beispielstadt/events/'; - $this->setUpFrontendRootPage(1, [], [ - 'config' => implode(PHP_EOL, [ - 'module.tx_events_pi1.settings.destinationData {', - 'restUrl = ' . $this->getInstancePath() . '/typo3conf/ext/events/Tests/Functional/Import/DestinationDataTest/Fixtures/Response.json', - 'license = example-license', - 'restType = Event', - 'restLimit = 3', - 'restMode = next_months,12', - 'restTemplate = ET2014A.json', - 'categoriesPid = ', - 'categoryParentUid = ', - '}', - ]), + $this->setUpConfiguration([ + 'restUrl = ' . $this->getInstancePath() . '/typo3conf/ext/events/Tests/Functional/Import/DestinationDataTest/Fixtures/Response.json', + 'license = example-license', + 'restType = Event', + 'restLimit = 3', + 'restMode = next_months,12', + 'restTemplate = ET2014A.json', + 'categoriesPid = ', + 'categoryParentUid = ', ]); - $requests = []; - $client = ClientFactory::createClientWithHistory([ + $requests = &$this->setUpResponses([ new Response(200, [], file_get_contents(__DIR__ . '/Fixtures/ExampleImage.jpg') ?: ''), new Response(200, [], file_get_contents(__DIR__ . '/Fixtures/ExampleImage.jpg') ?: ''), new Response(200, [], file_get_contents(__DIR__ . '/Fixtures/ExampleImage.jpg') ?: ''), - ], $requests); - $container = $this->getContainer(); - if ($container instanceof Container) { - $container->set(ClientInterface::class, $client); - // For TYPO3 10 support - $container->set(GuzzleClientInterface::class, $client); - } - - $subject = $this->getContainer()->get(DestinationDataImportCommand::class); - self::assertInstanceOf(Command::class, $subject); - - $tester = new CommandTester($subject); - $tester->execute( - [ - 'storage-pid' => 2, - 'rest-experience' => 'beispielstadt', - 'files-folder' => $fileImportPath, - ], - [ - 'capture_stderr_separately' => true, - ] - ); + ]); + $tester = $this->executeCommand([ + 'storage-pid' => 2, + 'rest-experience' => 'beispielstadt', + 'files-folder' => $fileImportPath, + ]); self::assertSame(0, $tester->getStatusCode());