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;
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;
}
}

View file

@ -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());

View file

@ -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.');

View file

@ -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());

View file

@ -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());