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.
This commit is contained in:
Daniel Siepmann 2021-12-16 10:12:26 +01:00
parent 15dec49445
commit fb2eceb5e6
5 changed files with 117 additions and 177 deletions

View file

@ -2,8 +2,15 @@
namespace Wrm\Events\Tests\Functional\Import\DestinationDataTest; 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\CMS\Core\Localization\LanguageServiceFactory;
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase; use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
use Wrm\Events\Command\DestinationDataImportCommand;
use Wrm\Events\Tests\ClientFactory;
abstract class AbstractTest extends FunctionalTestCase abstract class AbstractTest extends FunctionalTestCase
{ {
@ -31,4 +38,46 @@ abstract class AbstractTest extends FunctionalTestCase
parent::tearDown(); 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;
}
} }

View file

@ -2,16 +2,9 @@
namespace Wrm\Events\Tests\Functional\Import\DestinationDataTest; namespace Wrm\Events\Tests\Functional\Import\DestinationDataTest;
use GuzzleHttp\ClientInterface as GuzzleClientInterface;
use GuzzleHttp\Psr7\Response; 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\Core\Environment;
use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Core\Utility\GeneralUtility;
use Wrm\Events\Command\DestinationDataImportCommand;
use Wrm\Events\Tests\ClientFactory;
/** /**
* @testdox DestinationData import * @testdox DestinationData import
@ -21,12 +14,10 @@ class ImportCleansTransientFilesTest extends AbstractTest
/** /**
* @test * @test
*/ */
public function importCleansTransientFiles(): void public function cleansTransientFiles(): void
{ {
$fileImportPath = 'staedte/beispielstadt/events/'; $fileImportPath = 'staedte/beispielstadt/events/';
$this->setUpFrontendRootPage(1, [], [ $this->setUpConfiguration([
'config' => implode(PHP_EOL, [
'module.tx_events_pi1.settings.destinationData {',
'restUrl = ' . $this->getInstancePath() . '/typo3conf/ext/events/Tests/Functional/Import/DestinationDataTest/Fixtures/Response.json', 'restUrl = ' . $this->getInstancePath() . '/typo3conf/ext/events/Tests/Functional/Import/DestinationDataTest/Fixtures/Response.json',
'license = example-license', 'license = example-license',
'restType = Event', 'restType = Event',
@ -35,37 +26,19 @@ class ImportCleansTransientFilesTest extends AbstractTest
'restTemplate = ET2014A.json', 'restTemplate = ET2014A.json',
'categoriesPid = ', 'categoriesPid = ',
'categoryParentUid = ', 'categoryParentUid = ',
'}',
]),
]); ]);
$requests = []; $requests = &$this->setUpResponses([
$client = ClientFactory::createClientWithHistory([
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') ?: ''), 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); $tester = $this->executeCommand([
self::assertInstanceOf(Command::class, $subject);
$tester = new CommandTester($subject);
$tester->execute(
[
'storage-pid' => 2, 'storage-pid' => 2,
'rest-experience' => 'beispielstadt', 'rest-experience' => 'beispielstadt',
'files-folder' => $fileImportPath, 'files-folder' => $fileImportPath,
], ]);
[
'capture_stderr_separately' => true,
]
);
self::assertSame(0, $tester->getStatusCode()); self::assertSame(0, $tester->getStatusCode());

View file

@ -2,16 +2,9 @@
namespace Wrm\Events\Tests\Functional\Import\DestinationDataTest; namespace Wrm\Events\Tests\Functional\Import\DestinationDataTest;
use GuzzleHttp\ClientInterface as GuzzleClientInterface;
use GuzzleHttp\Psr7\Response; 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\Core\Environment;
use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Core\Utility\GeneralUtility;
use Wrm\Events\Command\DestinationDataImportCommand;
use Wrm\Events\Tests\ClientFactory;
/** /**
* @testdox DestinationData import * @testdox DestinationData import
@ -21,12 +14,11 @@ class ImportDoesNotUserUploadsFolderTest extends AbstractTest
/** /**
* @test * @test
*/ */
public function importDoesNotUserUploadsFolder(): void public function doesNotUserUploadsFolder(): void
{ {
$fileImportPath = 'staedte/beispielstadt/events/'; $fileImportPath = 'staedte/beispielstadt/events/';
$this->setUpFrontendRootPage(1, [], [
'config' => implode(PHP_EOL, [ $this->setUpConfiguration([
'module.tx_events_pi1.settings.destinationData {',
'restUrl = ' . $this->getInstancePath() . '/typo3conf/ext/events/Tests/Functional/Import/DestinationDataTest/Fixtures/Response.json', 'restUrl = ' . $this->getInstancePath() . '/typo3conf/ext/events/Tests/Functional/Import/DestinationDataTest/Fixtures/Response.json',
'license = example-license', 'license = example-license',
'restType = Event', 'restType = Event',
@ -35,38 +27,19 @@ class ImportDoesNotUserUploadsFolderTest extends AbstractTest
'restTemplate = ET2014A.json', 'restTemplate = ET2014A.json',
'categoriesPid = ', 'categoriesPid = ',
'categoryParentUid = ', 'categoryParentUid = ',
'}',
]),
]); ]);
$requests = []; $requests = &$this->setUpResponses([
$client = ClientFactory::createClientWithHistory([
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') ?: ''), 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); $tester = $this->executeCommand([
self::assertInstanceOf(Command::class, $subject);
$tester = new CommandTester($subject);
$tester->execute(
[
'storage-pid' => 2, 'storage-pid' => 2,
'rest-experience' => 'beispielstadt', 'rest-experience' => 'beispielstadt',
'files-folder' => $fileImportPath, 'files-folder' => $fileImportPath,
], ]);
[
'capture_stderr_separately' => true,
]
);
self::assertSame(0, $tester->getStatusCode()); self::assertSame(0, $tester->getStatusCode());
self::assertCount(3, $requests, 'Unexpected number of requests were made.'); self::assertCount(3, $requests, 'Unexpected number of requests were made.');

View file

@ -2,15 +2,8 @@
namespace Wrm\Events\Tests\Functional\Import\DestinationDataTest; namespace Wrm\Events\Tests\Functional\Import\DestinationDataTest;
use GuzzleHttp\ClientInterface as GuzzleClientInterface;
use GuzzleHttp\Psr7\Response; 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 TYPO3\CMS\Core\Utility\GeneralUtility;
use Wrm\Events\Command\DestinationDataImportCommand;
use Wrm\Events\Tests\ClientFactory;
/** /**
* @testdox DestinationData import * @testdox DestinationData import
@ -25,9 +18,7 @@ class ImportsExampleAsExpectedTest extends AbstractTest
$fileImportPath = 'staedte/beispielstadt/events/'; $fileImportPath = 'staedte/beispielstadt/events/';
$this->importDataSet('EXT:events/Tests/Functional/Import/DestinationDataTest/Fixtures/SingleRegion.xml'); $this->importDataSet('EXT:events/Tests/Functional/Import/DestinationDataTest/Fixtures/SingleRegion.xml');
$this->setUpFrontendRootPage(1, [], [ $this->setUpConfiguration([
'config' => implode(PHP_EOL, [
'module.tx_events_pi1.settings.destinationData {',
'restUrl = ' . $this->getInstancePath() . '/typo3conf/ext/events/Tests/Functional/Import/DestinationDataTest/Fixtures/Response.json', 'restUrl = ' . $this->getInstancePath() . '/typo3conf/ext/events/Tests/Functional/Import/DestinationDataTest/Fixtures/Response.json',
'license = example-license', 'license = example-license',
'restType = Event', 'restType = Event',
@ -36,38 +27,20 @@ class ImportsExampleAsExpectedTest extends AbstractTest
'restTemplate = ET2014A.json', 'restTemplate = ET2014A.json',
'categoriesPid = ', 'categoriesPid = ',
'categoryParentUid = ', 'categoryParentUid = ',
'}',
]),
]); ]);
$requests = []; $requests = &$this->setUpResponses([
$client = ClientFactory::createClientWithHistory([
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') ?: ''), 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); $tester = $this->executeCommand([
self::assertInstanceOf(Command::class, $subject);
$tester = new CommandTester($subject);
$tester->execute(
[
'storage-pid' => 2, 'storage-pid' => 2,
'rest-experience' => 'beispielstadt', 'rest-experience' => 'beispielstadt',
'files-folder' => $fileImportPath, 'files-folder' => $fileImportPath,
'region-uid' => 1, 'region-uid' => 1,
], ]);
[
'capture_stderr_separately' => true,
]
);
self::assertSame(0, $tester->getStatusCode()); self::assertSame(0, $tester->getStatusCode());

View file

@ -2,15 +2,8 @@
namespace Wrm\Events\Tests\Functional\Import\DestinationDataTest; namespace Wrm\Events\Tests\Functional\Import\DestinationDataTest;
use GuzzleHttp\ClientInterface as GuzzleClientInterface;
use GuzzleHttp\Psr7\Response; 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 TYPO3\CMS\Core\Utility\GeneralUtility;
use Wrm\Events\Command\DestinationDataImportCommand;
use Wrm\Events\Tests\ClientFactory;
/** /**
* @testdox DestinationData import * @testdox DestinationData import
@ -23,9 +16,7 @@ class ImportsWithoutRegionIfNotProvided extends AbstractTest
public function importsWithoutRegionIfNotProvided(): void public function importsWithoutRegionIfNotProvided(): void
{ {
$fileImportPath = 'staedte/beispielstadt/events/'; $fileImportPath = 'staedte/beispielstadt/events/';
$this->setUpFrontendRootPage(1, [], [ $this->setUpConfiguration([
'config' => implode(PHP_EOL, [
'module.tx_events_pi1.settings.destinationData {',
'restUrl = ' . $this->getInstancePath() . '/typo3conf/ext/events/Tests/Functional/Import/DestinationDataTest/Fixtures/Response.json', 'restUrl = ' . $this->getInstancePath() . '/typo3conf/ext/events/Tests/Functional/Import/DestinationDataTest/Fixtures/Response.json',
'license = example-license', 'license = example-license',
'restType = Event', 'restType = Event',
@ -34,37 +25,18 @@ class ImportsWithoutRegionIfNotProvided extends AbstractTest
'restTemplate = ET2014A.json', 'restTemplate = ET2014A.json',
'categoriesPid = ', 'categoriesPid = ',
'categoryParentUid = ', 'categoryParentUid = ',
'}',
]),
]); ]);
$requests = []; $requests = &$this->setUpResponses([
$client = ClientFactory::createClientWithHistory([
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') ?: ''), 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(); $tester = $this->executeCommand([
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, 'storage-pid' => 2,
'rest-experience' => 'beispielstadt', 'rest-experience' => 'beispielstadt',
'files-folder' => $fileImportPath, 'files-folder' => $fileImportPath,
], ]);
[
'capture_stderr_separately' => true,
]
);
self::assertSame(0, $tester->getStatusCode()); self::assertSame(0, $tester->getStatusCode());