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,51 +14,31 @@ 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, [ 'restUrl = ' . $this->getInstancePath() . '/typo3conf/ext/events/Tests/Functional/Import/DestinationDataTest/Fixtures/Response.json',
'module.tx_events_pi1.settings.destinationData {', 'license = example-license',
'restUrl = ' . $this->getInstancePath() . '/typo3conf/ext/events/Tests/Functional/Import/DestinationDataTest/Fixtures/Response.json', 'restType = Event',
'license = example-license', 'restLimit = 3',
'restType = Event', 'restMode = next_months,12',
'restLimit = 3', 'restTemplate = ET2014A.json',
'restMode = next_months,12', 'categoriesPid = ',
'restTemplate = ET2014A.json', 'categoryParentUid = ',
'categoriesPid = ',
'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); 'storage-pid' => 2,
'rest-experience' => 'beispielstadt',
$tester = new CommandTester($subject); 'files-folder' => $fileImportPath,
$tester->execute( ]);
[
'storage-pid' => 2,
'rest-experience' => 'beispielstadt',
'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,52 +14,32 @@ 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', 'restLimit = 3',
'restLimit = 3', 'restMode = next_months,12',
'restMode = next_months,12', '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);
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::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,49 +18,29 @@ 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, [ 'restUrl = ' . $this->getInstancePath() . '/typo3conf/ext/events/Tests/Functional/Import/DestinationDataTest/Fixtures/Response.json',
'module.tx_events_pi1.settings.destinationData {', 'license = example-license',
'restUrl = ' . $this->getInstancePath() . '/typo3conf/ext/events/Tests/Functional/Import/DestinationDataTest/Fixtures/Response.json', 'restType = Event',
'license = example-license', 'restLimit = 3',
'restType = Event', 'restMode = next_months,12',
'restLimit = 3', 'restTemplate = ET2014A.json',
'restMode = next_months,12', 'categoriesPid = ',
'restTemplate = ET2014A.json', 'categoryParentUid = ',
'categoriesPid = ',
'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); 'storage-pid' => 2,
'rest-experience' => 'beispielstadt',
$tester = new CommandTester($subject); 'files-folder' => $fileImportPath,
$tester->execute( 'region-uid' => 1,
[ ]);
'storage-pid' => 2,
'rest-experience' => 'beispielstadt',
'files-folder' => $fileImportPath,
'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,48 +16,27 @@ 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, [ 'restUrl = ' . $this->getInstancePath() . '/typo3conf/ext/events/Tests/Functional/Import/DestinationDataTest/Fixtures/Response.json',
'module.tx_events_pi1.settings.destinationData {', 'license = example-license',
'restUrl = ' . $this->getInstancePath() . '/typo3conf/ext/events/Tests/Functional/Import/DestinationDataTest/Fixtures/Response.json', 'restType = Event',
'license = example-license', 'restLimit = 3',
'restType = Event', 'restMode = next_months,12',
'restLimit = 3', 'restTemplate = ET2014A.json',
'restMode = next_months,12', 'categoriesPid = ',
'restTemplate = ET2014A.json', 'categoryParentUid = ',
'categoriesPid = ',
'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) { 'storage-pid' => 2,
$container->set(ClientInterface::class, $client); 'rest-experience' => 'beispielstadt',
// For TYPO3 10 support 'files-folder' => $fileImportPath,
$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,
]
);
self::assertSame(0, $tester->getStatusCode()); self::assertSame(0, $tester->getStatusCode());