Use new import class and move url building and fetching

This commit is contained in:
Daniel Siepmann 2022-01-26 14:16:12 +01:00
parent 295ad8645a
commit b5d095659a
16 changed files with 452 additions and 137 deletions

View file

@ -7,6 +7,7 @@ use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use TYPO3\CMS\Core\Core\Bootstrap; use TYPO3\CMS\Core\Core\Bootstrap;
use Wrm\Events\Domain\DestinationData\Import;
use Wrm\Events\Service\DestinationDataImportService; use Wrm\Events\Service\DestinationDataImportService;
class DestinationDataImportCommand extends Command class DestinationDataImportCommand extends Command
@ -61,11 +62,11 @@ class DestinationDataImportCommand extends Command
$regionUid = null; $regionUid = null;
} }
return $this->destinationDataImportService->import( return $this->destinationDataImportService->import(new Import(
$input->getArgument('rest-experience'), $input->getArgument('rest-experience'),
$input->getArgument('storage-pid'), $input->getArgument('storage-pid'),
$regionUid, $regionUid,
$input->getArgument('files-folder') $input->getArgument('files-folder')
); ));
} }
} }

View file

@ -0,0 +1,62 @@
<?php
namespace Wrm\Events\Domain\DestinationData;
/**
* Actual request to import.
* Includes all configuration specific to a concrete import.
*/
class Import
{
/**
* @var string
*/
private $restExperience;
/**
* @var int
*/
private $storagePid;
/**
* @var int|null
*/
private $regionUid;
/**
* @var string
*/
private $filesFolder;
public function __construct(
string $restExperience,
int $storagePid,
?int $regionUid,
string $filesFolder
) {
$this->restExperience = $restExperience;
$this->storagePid = $storagePid;
$this->regionUid = $regionUid;
$this->filesFolder = $filesFolder;
}
public function getRestExperience(): string
{
return $this->restExperience;
}
public function getStoragePid(): int
{
return $this->storagePid;
}
public function getRegionUid(): ?int
{
return $this->regionUid;
}
public function getFilesFolder(): string
{
return $this->filesFolder;
}
}

View file

@ -19,6 +19,7 @@ use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Extbase\Object\ObjectManager; use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager; use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;
use TYPO3\CMS\Extbase\Persistence\ObjectStorage; use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
use Wrm\Events\Domain\DestinationData\Import;
use Wrm\Events\Domain\Model\Category; use Wrm\Events\Domain\Model\Category;
use Wrm\Events\Domain\Model\Date; use Wrm\Events\Domain\Model\Date;
use Wrm\Events\Domain\Model\Event; use Wrm\Events\Domain\Model\Event;
@ -34,49 +35,9 @@ use Wrm\Events\Service\DestinationDataImportService\DataFetcher;
class DestinationDataImportService class DestinationDataImportService
{ {
/** /**
* @var string * @var Import
*/ */
private $restUrl; private $import;
/**
* @var string
*/
private $restLicenseKey;
/**
* @var string
*/
private $restType;
/**
* @var string
*/
private $restLimit;
/**
* @var string
*/
private $restMode;
/**
* @var string
*/
private $restTemplate;
/**
* @var string
*/
private $restExperience;
/**
* @var int
*/
private $storagePid;
/**
* @var ?int
*/
private $regionUid;
/** /**
* @var int * @var int
@ -88,16 +49,6 @@ class DestinationDataImportService
*/ */
private $categoryParentUid; private $categoryParentUid;
/**
* @var string
*/
private $filesFolder;
/**
* @var array
*/
private $settings = [];
/** /**
* @var Environment * @var Environment
*/ */
@ -210,34 +161,19 @@ class DestinationDataImportService
$this->environment = $environment; $this->environment = $environment;
$this->dataFetcher = $dataFetcher; $this->dataFetcher = $dataFetcher;
// Get Typoscript Settings $settings = $this->configurationManager->getConfiguration(
$this->settings = $this->configurationManager->getConfiguration(
ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS, ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS,
'Events', 'Events',
'Pi1' 'Pi1'
); )['destinationData'] ?? [];
$this->categoriesPid = (int) $settings['categoriesPid'];
// Set properties $this->categoryParentUid = (int) $settings['categoryParentUid'];
$this->restUrl = $this->settings['destinationData']['restUrl'];
$this->restLicenseKey = $this->settings['destinationData']['license'];
$this->restType = $this->settings['destinationData']['restType'];
$this->restLimit = $this->settings['destinationData']['restLimit'];
$this->restMode = $this->settings['destinationData']['restMode'];
$this->restTemplate = $this->settings['destinationData']['restTemplate'];
$this->categoriesPid = (int) $this->settings['destinationData']['categoriesPid'];
$this->categoryParentUid = (int) $this->settings['destinationData']['categoryParentUid'];
} }
public function import( public function import(
string $restExperience, Import $import
int $storagePid,
?int $regionUid,
string $filesFolder
): int { ): int {
$this->restExperience = $restExperience; $this->import = $import;
$this->storagePid = $storagePid;
$this->regionUid = $regionUid;
$this->filesFolder = $filesFolder;
// Get configuration // Get configuration
$frameworkConfiguration = $this->configurationManager->getConfiguration( $frameworkConfiguration = $this->configurationManager->getConfiguration(
@ -247,41 +183,23 @@ class DestinationDataImportService
// Set storage pid // Set storage pid
$persistenceConfiguration = [ $persistenceConfiguration = [
'persistence' => [ 'persistence' => [
'storagePid' => $this->storagePid, 'storagePid' => $this->import->getStoragePid(),
], ],
]; ];
// Set Configuration // Set Configuration
$this->configurationManager->setConfiguration(array_merge($frameworkConfiguration, $persistenceConfiguration)); $this->configurationManager->setConfiguration(array_merge($frameworkConfiguration, $persistenceConfiguration));
$this->logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__); $this->logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
$this->logger->info('Starting Destination Data Import Service'); $this->logger->info('Starting Destination Data Import Service');
$restUrl = $this->restUrl . '?experience=' . $this->restExperience . '&licensekey=' . $this->restLicenseKey . '&type=' . $this->restType . '&mode=' . $this->restMode . '&limit=' . $this->restLimit . '&template=' . $this->restTemplate;
$this->logger->info('Try to get data from ' . $restUrl);
try { try {
$fetchedData = $this->fetchData($restUrl); $data = $this->dataFetcher->fetchSearchResult($import);
} catch (\Exception $e) { } catch (\Exception $e) {
$this->logger->error('Could not receive data.'); $this->logger->error('Could not receive data.');
return 1; return 1;
} }
return $this->processData($fetchedData); return $this->processData($data);
}
private function fetchData(string $restUrl): array
{
$jsonContent = file_get_contents($restUrl);
if (is_string($jsonContent) === false) {
throw new \Exception('Could not receive data.', 1639495835);
}
$jsonResponse = json_decode($jsonContent, true);
if (is_array($jsonResponse) === false) {
throw new \Exception('Could not receive data.', 1639495835);
}
$this->logger->info('Received data with ' . count($jsonResponse['items']) . ' items');
return $jsonResponse;
} }
public function processData(array $data): int public function processData(array $data): int
@ -290,8 +208,8 @@ class DestinationDataImportService
// Get selected region // Get selected region
$selectedRegion = null; $selectedRegion = null;
if (is_int($this->regionUid)) { if (is_int($this->import->getRegionUid())) {
$selectedRegion = $this->regionRepository->findByUid($this->regionUid); $selectedRegion = $this->regionRepository->findByUid($this->import->getRegionUid());
} }
foreach ($data['items'] as $event) { foreach ($data['items'] as $event) {
@ -665,7 +583,7 @@ class DestinationDataImportService
$this->logger->info('File attached:' . $fileUrl); $this->logger->info('File attached:' . $fileUrl);
$this->logger->info('File attached sanitized:' . $orgFileNameSanitized); $this->logger->info('File attached sanitized:' . $orgFileNameSanitized);
$targetFilePath = $this->environment->getPublicPath() . '/fileadmin/' . $this->filesFolder $targetFilePath = $this->environment->getPublicPath() . '/fileadmin/' . $this->import->getFilesFolder()
. $orgFileNameSanitized; . $orgFileNameSanitized;
// Check if file already exists // Check if file already exists
@ -679,9 +597,9 @@ class DestinationDataImportService
$this->logger->info('Adding file ' . $filename); $this->logger->info('Adding file ' . $filename);
try { try {
$targetFolder = $storage->getFolder($this->filesFolder); $targetFolder = $storage->getFolder($this->import->getFilesFolder());
} catch (FolderDoesNotExistException $e) { } catch (FolderDoesNotExistException $e) {
$targetFolder = $storage->createFolder($this->filesFolder); $targetFolder = $storage->createFolder($this->import->getFilesFolder());
} }
$storage->addFile($filename, $targetFolder, basename($fileUrl)); $storage->addFile($filename, $targetFolder, basename($fileUrl));
@ -696,7 +614,7 @@ class DestinationDataImportService
// TODO: How to delete file references? // TODO: How to delete file references?
} else { } else {
$this->logger->info('No relation found'); $this->logger->info('No relation found');
$fileIdentifier = $this->filesFolder . $orgFileNameSanitized; $fileIdentifier = $this->import->getFilesFolder() . $orgFileNameSanitized;
$file = $storage->getFile($fileIdentifier); $file = $storage->getFile($fileIdentifier);
if (!$file instanceof File) { if (!$file instanceof File) {
$this->logger->warning('Could not find file.', [$fileIdentifier]); $this->logger->warning('Could not find file.', [$fileIdentifier]);
@ -715,7 +633,7 @@ class DestinationDataImportService
'tx_events_domain_model_event', 'tx_events_domain_model_event',
$this->tmpCurrentEvent->getUid(), $this->tmpCurrentEvent->getUid(),
'images', 'images',
$this->storagePid $this->import->getStoragePid()
); );
} }
} }

View file

@ -6,6 +6,9 @@ use GuzzleHttp\ClientInterface as GuzzleClientInterface;
use Psr\Http\Client\ClientInterface; use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface; use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use TYPO3\CMS\Core\Log\Logger;
use TYPO3\CMS\Core\Log\LogManager;
use Wrm\Events\Domain\DestinationData\Import;
/** /**
* Provides API to fetch data from remote. * Provides API to fetch data from remote.
@ -15,6 +18,11 @@ use Psr\Http\Message\ResponseInterface;
*/ */
class DataFetcher class DataFetcher
{ {
/**
* @var UrlFactory
*/
private $urlFactory;
/** /**
* @var RequestFactoryInterface * @var RequestFactoryInterface
*/ */
@ -25,12 +33,57 @@ class DataFetcher
*/ */
private $client; private $client;
/**
* @var Logger
*/
private $logger;
public function __construct( public function __construct(
UrlFactory $urlFactory,
LogManager $logManager,
RequestFactoryInterface $requestFactory, RequestFactoryInterface $requestFactory,
GuzzleClientInterface $client GuzzleClientInterface $client
) { ) {
$this->urlFactory = $urlFactory;
$this->requestFactory = $requestFactory; $this->requestFactory = $requestFactory;
$this->client = $client; $this->client = $client;
$this->logger = $logManager->getLogger(__CLASS__);
}
public function fetchSearchResult(Import $import): array
{
$url = $this->urlFactory->createSearchResultUrl($import);
$this->logger->info('Try to get data from ' . $url);
if ($this->client instanceof ClientInterface) {
// Keep after TYPO3 10 was dropped
$response = $this->client->sendRequest(
$this->requestFactory->createRequest(
'GET',
$url
)
);
} else {
// Drop once TYPO3 10 support was dropped
$response = $this->client->request(
'GET',
$url,
[]
);
}
$jsonContent = $response->getBody()->__toString();
$jsonResponse = json_decode($jsonContent, true);
if (is_array($jsonResponse) === false) {
throw new \Exception('No valid JSON fetched, got: "' . $jsonContent . '".', 1639495835);
}
$this->logger->info('Received data with ' . count($jsonResponse['items']) . ' items');
return $jsonResponse;
} }
public function fetchImage(string $url): ResponseInterface public function fetchImage(string $url): ResponseInterface

View file

@ -0,0 +1,51 @@
<?php
namespace Wrm\Events\Service\DestinationDataImportService;
use TYPO3\CMS\Core\Http\Uri;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use Wrm\Events\Domain\DestinationData\Import;
/**
* Factory to create URLs used during import of Destination Data.
*/
class UrlFactory
{
/**
* @var array
*/
private $settings = [];
public function __construct(
ConfigurationManager $configurationManager
) {
$this->settings = $configurationManager->getConfiguration(
ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS,
'Events',
'Pi1'
)['destinationData'] ?? [];
}
/**
* URL used to fetch initial set of data.
*/
public function createSearchResultUrl(
Import $import
): string {
$parameter = [
'experience' => $import->getRestExperience(),
'licensekey' => $this->settings['license'] ?? '',
'type' => $this->settings['restType'] ?? '',
'mode' => $this->settings['restMode'] ?? '',
'limit' => $this->settings['restLimit'] ?? '',
'template' => $this->settings['restTemplate'] ?? '',
];
$parameter = array_filter($parameter);
$url = new Uri($this->settings['restUrl']);
$url = $url->withQuery(http_build_query($parameter));
return (string) $url;
}
}

View file

@ -18,7 +18,7 @@ class ImportCleansTransientFilesTest extends AbstractTest
{ {
$fileImportPath = 'staedte/beispielstadt/events/'; $fileImportPath = 'staedte/beispielstadt/events/';
$this->setUpConfiguration([ $this->setUpConfiguration([
'restUrl = ' . $this->getInstancePath() . '/typo3conf/ext/events/Tests/Functional/Import/DestinationDataTest/Fixtures/Response.json', 'restUrl = https://example.com/some-path/',
'license = example-license', 'license = example-license',
'restType = Event', 'restType = Event',
'restLimit = 3', 'restLimit = 3',
@ -29,6 +29,7 @@ class ImportCleansTransientFilesTest extends AbstractTest
]); ]);
$requests = &$this->setUpResponses([ $requests = &$this->setUpResponses([
new Response(200, [], file_get_contents(__DIR__ . '/Fixtures/Response.json') ?: ''),
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') ?: ''),
@ -42,10 +43,11 @@ class ImportCleansTransientFilesTest extends AbstractTest
self::assertSame(0, $tester->getStatusCode()); self::assertSame(0, $tester->getStatusCode());
self::assertCount(3, $requests, 'Unexpected number of requests were made.'); self::assertCount(4, $requests, 'Unexpected number of requests were made.');
self::assertSame('https://dam.destination.one/849917/279ac45b3fc701a7197131f627164fffd9f8cc77bc75165e2fc2b864ed606920/theater-rudolstadt_johannes-gei-er_photo-by-lisa-stern_web_-jpg.jpg', (string)$requests[0]['request']->getUri()); self::assertSame('https://example.com/some-path/?experience=beispielstadt&licensekey=example-license&type=Event&mode=next_months%2C12&limit=3&template=ET2014A.json', (string)$requests[0]['request']->getUri());
self::assertSame('https://dam.destination.one/828118/f13bbf5602ffc406ebae2faa3527654dea84194666bce4925a1ca8bd3f50c5e9/tueftlerzeit-sfz-rudolstadt-jpg.jpg', (string)$requests[1]['request']->getUri()); self::assertSame('https://dam.destination.one/849917/279ac45b3fc701a7197131f627164fffd9f8cc77bc75165e2fc2b864ed606920/theater-rudolstadt_johannes-gei-er_photo-by-lisa-stern_web_-jpg.jpg', (string)$requests[1]['request']->getUri());
self::assertSame('https://dam.destination.one/853436/109ac1cf87913e21b5e2b0ef0cc63d223a14374364952a855746a8e7c3fcfc36/lutherkirche-jpg.jpg', (string)$requests[2]['request']->getUri()); self::assertSame('https://dam.destination.one/828118/f13bbf5602ffc406ebae2faa3527654dea84194666bce4925a1ca8bd3f50c5e9/tueftlerzeit-sfz-rudolstadt-jpg.jpg', (string)$requests[2]['request']->getUri());
self::assertSame('https://dam.destination.one/853436/109ac1cf87913e21b5e2b0ef0cc63d223a14374364952a855746a8e7c3fcfc36/lutherkirche-jpg.jpg', (string)$requests[3]['request']->getUri());
$importedFiles = GeneralUtility::getFilesInDir($this->getInstancePath() . '/fileadmin/' . $fileImportPath); $importedFiles = GeneralUtility::getFilesInDir($this->getInstancePath() . '/fileadmin/' . $fileImportPath);
self::assertIsArray($importedFiles, 'Failed to retrieve imported files from filesystem.'); self::assertIsArray($importedFiles, 'Failed to retrieve imported files from filesystem.');

View file

@ -19,7 +19,7 @@ class ImportDoesNotUseUploadsFolderTest extends AbstractTest
$fileImportPath = 'staedte/beispielstadt/events/'; $fileImportPath = 'staedte/beispielstadt/events/';
$this->setUpConfiguration([ $this->setUpConfiguration([
'restUrl = ' . $this->getInstancePath() . '/typo3conf/ext/events/Tests/Functional/Import/DestinationDataTest/Fixtures/Response.json', 'restUrl = https://example.com/some-path/',
'license = example-license', 'license = example-license',
'restType = Event', 'restType = Event',
'restLimit = 3', 'restLimit = 3',
@ -30,6 +30,7 @@ class ImportDoesNotUseUploadsFolderTest extends AbstractTest
]); ]);
$requests = &$this->setUpResponses([ $requests = &$this->setUpResponses([
new Response(200, [], file_get_contents(__DIR__ . '/Fixtures/Response.json') ?: ''),
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') ?: ''),
@ -42,10 +43,11 @@ class ImportDoesNotUseUploadsFolderTest extends AbstractTest
]); ]);
self::assertSame(0, $tester->getStatusCode()); self::assertSame(0, $tester->getStatusCode());
self::assertCount(3, $requests, 'Unexpected number of requests were made.'); self::assertCount(4, $requests, 'Unexpected number of requests were made.');
self::assertSame('https://dam.destination.one/849917/279ac45b3fc701a7197131f627164fffd9f8cc77bc75165e2fc2b864ed606920/theater-rudolstadt_johannes-gei-er_photo-by-lisa-stern_web_-jpg.jpg', (string)$requests[0]['request']->getUri()); self::assertSame('https://example.com/some-path/?experience=beispielstadt&licensekey=example-license&type=Event&mode=next_months%2C12&limit=3&template=ET2014A.json', (string)$requests[0]['request']->getUri());
self::assertSame('https://dam.destination.one/828118/f13bbf5602ffc406ebae2faa3527654dea84194666bce4925a1ca8bd3f50c5e9/tueftlerzeit-sfz-rudolstadt-jpg.jpg', (string)$requests[1]['request']->getUri()); self::assertSame('https://dam.destination.one/849917/279ac45b3fc701a7197131f627164fffd9f8cc77bc75165e2fc2b864ed606920/theater-rudolstadt_johannes-gei-er_photo-by-lisa-stern_web_-jpg.jpg', (string)$requests[1]['request']->getUri());
self::assertSame('https://dam.destination.one/853436/109ac1cf87913e21b5e2b0ef0cc63d223a14374364952a855746a8e7c3fcfc36/lutherkirche-jpg.jpg', (string)$requests[2]['request']->getUri()); self::assertSame('https://dam.destination.one/828118/f13bbf5602ffc406ebae2faa3527654dea84194666bce4925a1ca8bd3f50c5e9/tueftlerzeit-sfz-rudolstadt-jpg.jpg', (string)$requests[2]['request']->getUri());
self::assertSame('https://dam.destination.one/853436/109ac1cf87913e21b5e2b0ef0cc63d223a14374364952a855746a8e7c3fcfc36/lutherkirche-jpg.jpg', (string)$requests[3]['request']->getUri());
$importedFiles = GeneralUtility::getFilesInDir($this->getInstancePath() . '/fileadmin/' . $fileImportPath); $importedFiles = GeneralUtility::getFilesInDir($this->getInstancePath() . '/fileadmin/' . $fileImportPath);
self::assertIsArray($importedFiles, 'Failed to retrieve imported files from filesystem.'); self::assertIsArray($importedFiles, 'Failed to retrieve imported files from filesystem.');

View file

@ -20,7 +20,7 @@ class ImportsExampleAsExpectedTest extends AbstractTest
$this->importDataSet('EXT:events/Tests/Functional/Import/DestinationDataTest/Fixtures/SingleRegion.xml'); $this->importDataSet('EXT:events/Tests/Functional/Import/DestinationDataTest/Fixtures/SingleRegion.xml');
$this->importDataSet('EXT:events/Tests/Functional/Import/DestinationDataTest/Fixtures/SingleCategory.xml'); $this->importDataSet('EXT:events/Tests/Functional/Import/DestinationDataTest/Fixtures/SingleCategory.xml');
$this->setUpConfiguration([ $this->setUpConfiguration([
'restUrl = ' . $this->getInstancePath() . '/typo3conf/ext/events/Tests/Functional/Import/DestinationDataTest/Fixtures/Response.json', 'restUrl = https://example.com/some-path/',
'license = example-license', 'license = example-license',
'restType = Event', 'restType = Event',
'restLimit = 3', 'restLimit = 3',
@ -31,6 +31,7 @@ class ImportsExampleAsExpectedTest extends AbstractTest
]); ]);
$requests = &$this->setUpResponses([ $requests = &$this->setUpResponses([
new Response(200, [], file_get_contents(__DIR__ . '/Fixtures/Response.json') ?: ''),
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') ?: ''),
@ -45,10 +46,11 @@ class ImportsExampleAsExpectedTest extends AbstractTest
self::assertSame(0, $tester->getStatusCode()); self::assertSame(0, $tester->getStatusCode());
self::assertCount(3, $requests, 'Unexpected number of requests were made.'); self::assertCount(4, $requests, 'Unexpected number of requests were made.');
self::assertSame('https://dam.destination.one/849917/279ac45b3fc701a7197131f627164fffd9f8cc77bc75165e2fc2b864ed606920/theater-rudolstadt_johannes-gei-er_photo-by-lisa-stern_web_-jpg.jpg', (string)$requests[0]['request']->getUri()); self::assertSame('https://example.com/some-path/?experience=beispielstadt&licensekey=example-license&type=Event&mode=next_months%2C12&limit=3&template=ET2014A.json', (string)$requests[0]['request']->getUri());
self::assertSame('https://dam.destination.one/828118/f13bbf5602ffc406ebae2faa3527654dea84194666bce4925a1ca8bd3f50c5e9/tueftlerzeit-sfz-rudolstadt-jpg.jpg', (string)$requests[1]['request']->getUri()); self::assertSame('https://dam.destination.one/849917/279ac45b3fc701a7197131f627164fffd9f8cc77bc75165e2fc2b864ed606920/theater-rudolstadt_johannes-gei-er_photo-by-lisa-stern_web_-jpg.jpg', (string)$requests[1]['request']->getUri());
self::assertSame('https://dam.destination.one/853436/109ac1cf87913e21b5e2b0ef0cc63d223a14374364952a855746a8e7c3fcfc36/lutherkirche-jpg.jpg', (string)$requests[2]['request']->getUri()); self::assertSame('https://dam.destination.one/828118/f13bbf5602ffc406ebae2faa3527654dea84194666bce4925a1ca8bd3f50c5e9/tueftlerzeit-sfz-rudolstadt-jpg.jpg', (string)$requests[2]['request']->getUri());
self::assertSame('https://dam.destination.one/853436/109ac1cf87913e21b5e2b0ef0cc63d223a14374364952a855746a8e7c3fcfc36/lutherkirche-jpg.jpg', (string)$requests[3]['request']->getUri());
self::assertCount( self::assertCount(
0, 0,

View file

@ -19,7 +19,7 @@ class ImportsWithoutCategoryIfNotProvidedTest extends AbstractTest
$this->importDataSet('EXT:events/Tests/Functional/Import/DestinationDataTest/Fixtures/SingleRegion.xml'); $this->importDataSet('EXT:events/Tests/Functional/Import/DestinationDataTest/Fixtures/SingleRegion.xml');
$this->setUpConfiguration([ $this->setUpConfiguration([
'restUrl = ' . $this->getInstancePath() . '/typo3conf/ext/events/Tests/Functional/Import/DestinationDataTest/Fixtures/Response.json', 'restUrl = https://example.com/some-path/',
'license = example-license', 'license = example-license',
'restType = Event', 'restType = Event',
'restLimit = 3', 'restLimit = 3',
@ -30,6 +30,7 @@ class ImportsWithoutCategoryIfNotProvidedTest extends AbstractTest
]); ]);
$requests = &$this->setUpResponses([ $requests = &$this->setUpResponses([
new Response(200, [], file_get_contents(__DIR__ . '/Fixtures/Response.json') ?: ''),
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') ?: ''),
@ -44,10 +45,11 @@ class ImportsWithoutCategoryIfNotProvidedTest extends AbstractTest
self::assertSame(0, $tester->getStatusCode()); self::assertSame(0, $tester->getStatusCode());
self::assertCount(3, $requests, 'Unexpected number of requests were made.'); self::assertCount(4, $requests, 'Unexpected number of requests were made.');
self::assertSame('https://dam.destination.one/849917/279ac45b3fc701a7197131f627164fffd9f8cc77bc75165e2fc2b864ed606920/theater-rudolstadt_johannes-gei-er_photo-by-lisa-stern_web_-jpg.jpg', (string)$requests[0]['request']->getUri()); self::assertSame('https://example.com/some-path/?experience=beispielstadt&licensekey=example-license&type=Event&mode=next_months%2C12&limit=3&template=ET2014A.json', (string)$requests[0]['request']->getUri());
self::assertSame('https://dam.destination.one/828118/f13bbf5602ffc406ebae2faa3527654dea84194666bce4925a1ca8bd3f50c5e9/tueftlerzeit-sfz-rudolstadt-jpg.jpg', (string)$requests[1]['request']->getUri()); self::assertSame('https://dam.destination.one/849917/279ac45b3fc701a7197131f627164fffd9f8cc77bc75165e2fc2b864ed606920/theater-rudolstadt_johannes-gei-er_photo-by-lisa-stern_web_-jpg.jpg', (string)$requests[1]['request']->getUri());
self::assertSame('https://dam.destination.one/853436/109ac1cf87913e21b5e2b0ef0cc63d223a14374364952a855746a8e7c3fcfc36/lutherkirche-jpg.jpg', (string)$requests[2]['request']->getUri()); self::assertSame('https://dam.destination.one/828118/f13bbf5602ffc406ebae2faa3527654dea84194666bce4925a1ca8bd3f50c5e9/tueftlerzeit-sfz-rudolstadt-jpg.jpg', (string)$requests[2]['request']->getUri());
self::assertSame('https://dam.destination.one/853436/109ac1cf87913e21b5e2b0ef0cc63d223a14374364952a855746a8e7c3fcfc36/lutherkirche-jpg.jpg', (string)$requests[3]['request']->getUri());
self::assertCount( self::assertCount(
0, 0,

View file

@ -18,7 +18,7 @@ class ImportsWithoutRegionIfNotProvidedTest extends AbstractTest
$fileImportPath = 'staedte/beispielstadt/events/'; $fileImportPath = 'staedte/beispielstadt/events/';
$this->importDataSet('EXT:events/Tests/Functional/Import/DestinationDataTest/Fixtures/SingleCategory.xml'); $this->importDataSet('EXT:events/Tests/Functional/Import/DestinationDataTest/Fixtures/SingleCategory.xml');
$this->setUpConfiguration([ $this->setUpConfiguration([
'restUrl = ' . $this->getInstancePath() . '/typo3conf/ext/events/Tests/Functional/Import/DestinationDataTest/Fixtures/Response.json', 'restUrl = https://example.com/some-path/',
'license = example-license', 'license = example-license',
'restType = Event', 'restType = Event',
'restLimit = 3', 'restLimit = 3',
@ -29,6 +29,7 @@ class ImportsWithoutRegionIfNotProvidedTest extends AbstractTest
]); ]);
$requests = &$this->setUpResponses([ $requests = &$this->setUpResponses([
new Response(200, [], file_get_contents(__DIR__ . '/Fixtures/Response.json') ?: ''),
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') ?: ''),
@ -42,10 +43,11 @@ class ImportsWithoutRegionIfNotProvidedTest extends AbstractTest
self::assertSame(0, $tester->getStatusCode()); self::assertSame(0, $tester->getStatusCode());
self::assertCount(3, $requests, 'Unexpected number of requests were made.'); self::assertCount(4, $requests, 'Unexpected number of requests were made.');
self::assertSame('https://dam.destination.one/849917/279ac45b3fc701a7197131f627164fffd9f8cc77bc75165e2fc2b864ed606920/theater-rudolstadt_johannes-gei-er_photo-by-lisa-stern_web_-jpg.jpg', (string)$requests[0]['request']->getUri()); self::assertSame('https://example.com/some-path/?experience=beispielstadt&licensekey=example-license&type=Event&mode=next_months%2C12&limit=3&template=ET2014A.json', (string)$requests[0]['request']->getUri());
self::assertSame('https://dam.destination.one/828118/f13bbf5602ffc406ebae2faa3527654dea84194666bce4925a1ca8bd3f50c5e9/tueftlerzeit-sfz-rudolstadt-jpg.jpg', (string)$requests[1]['request']->getUri()); self::assertSame('https://dam.destination.one/849917/279ac45b3fc701a7197131f627164fffd9f8cc77bc75165e2fc2b864ed606920/theater-rudolstadt_johannes-gei-er_photo-by-lisa-stern_web_-jpg.jpg', (string)$requests[1]['request']->getUri());
self::assertSame('https://dam.destination.one/853436/109ac1cf87913e21b5e2b0ef0cc63d223a14374364952a855746a8e7c3fcfc36/lutherkirche-jpg.jpg', (string)$requests[2]['request']->getUri()); self::assertSame('https://dam.destination.one/828118/f13bbf5602ffc406ebae2faa3527654dea84194666bce4925a1ca8bd3f50c5e9/tueftlerzeit-sfz-rudolstadt-jpg.jpg', (string)$requests[2]['request']->getUri());
self::assertSame('https://dam.destination.one/853436/109ac1cf87913e21b5e2b0ef0cc63d223a14374364952a855746a8e7c3fcfc36/lutherkirche-jpg.jpg', (string)$requests[3]['request']->getUri());
self::assertCount( self::assertCount(
0, 0,

View file

@ -0,0 +1,102 @@
<?php
namespace Wrm\Events\Tests\Unit\Domain\DestinationData;
use Wrm\Events\Domain\DestinationData\Import;
use PHPUnit\Framework\TestCase;
/**
* @covers \Wrm\Events\Domain\DestinationData\Import
*/
class ImportTest extends TestCase
{
/**
* @test
*/
public function canBeCreated(): void
{
$subject = new Import(
'',
0,
null,
''
);
self::assertInstanceOf(
Import::class,
$subject
);
}
/**
* @test
*/
public function returnsRestExperience(): void
{
$subject = new Import(
'experience',
0,
null,
''
);
self::assertSame(
'experience',
$subject->getRestExperience()
);
}
/**
* @test
*/
public function returnsStoragePid(): void
{
$subject = new Import(
'',
20,
null,
''
);
self::assertSame(
20,
$subject->getStoragePid()
);
}
/**
* @test
*/
public function returnsRegionUid(): void
{
$subject = new Import(
'',
0,
30,
''
);
self::assertSame(
30,
$subject->getRegionUid()
);
}
/**
* @test
*/
public function returnsFilesFolder(): void
{
$subject = new Import(
'',
0,
null,
'test/folder'
);
self::assertSame(
'test/folder',
$subject->getFilesFolder()
);
}
}

View file

@ -0,0 +1,119 @@
<?php
namespace Wrm\Events\Tests\Unit\Service\DestinationDataImportService;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use Wrm\Events\Domain\DestinationData\Import;
use Wrm\Events\Service\DestinationDataImportService\UrlFactory;
use PHPUnit\Framework\TestCase;
/**
* @covers \Wrm\Events\Service\DestinationDataImportService\UrlFactory
*/
class UrlFactoryTest extends TestCase
{
use ProphecyTrait;
/**
* @test
*/
public function canBeCreated(): void
{
$configurationManager = $this->prophesize(ConfigurationManager::class);
$configurationManager->getConfiguration(
ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS,
'Events',
'Pi1'
)->willReturn([]);
$subject = new UrlFactory(
$configurationManager->reveal()
);
self::assertInstanceOf(
UrlFactory::class,
$subject
);
}
/**
* @test
* @dataProvider possibleImports
*/
public function createSearchResultUrl(
ObjectProphecy $import,
array $settings,
string $expectedResult
): void {
$configurationManager = $this->prophesize(ConfigurationManager::class);
$configurationManager->getConfiguration(
ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS,
'Events',
'Pi1'
)->willReturn(['destinationData' => $settings]);
$subject = new UrlFactory(
$configurationManager->reveal()
);
$result = $subject->createSearchResultUrl($import->reveal());
self::assertSame(
$result,
$expectedResult
);
}
public function possibleImports(): array
{
return [
'All provided' => [
'import' => (function () {
$import = $this->prophesize(Import::class);
$import->getRestExperience()->willReturn('experience');
return $import;
})(),
'settings' => [
'restUrl' => 'https://example.com/path',
'license' => 'licenseKey',
'restType' => 'restType',
'restMode' => 'restMode',
'restLimit' => 'restLimit',
'restTemplate' => 'restTemplate',
],
'expectedResult' => 'https://example.com/path?experience=experience&licensekey=licenseKey&type=restType&mode=restMode&limit=restLimit&template=restTemplate',
],
'All missing' => [
'import' => (function () {
$import = $this->prophesize(Import::class);
$import->getRestExperience()->willReturn('');
return $import;
})(),
'settings' => [
'restUrl' => 'https://example.com/path',
],
'expectedResult' => 'https://example.com/path',
],
'Some missing' => [
'import' => (function () {
$import = $this->prophesize(Import::class);
$import->getRestExperience()->willReturn('experience');
return $import;
})(),
'settings' => [
'restUrl' => 'https://example.com/path',
'license' => 'licenseKey',
'restLimit' => 'restLimit',
'restTemplate' => 'restTemplate',
],
'expectedResult' => 'https://example.com/path?experience=experience&licensekey=licenseKey&limit=restLimit&template=restTemplate',
],
];
}
}

View file

@ -54,7 +54,8 @@
"typo3/testing-framework": "^6.14", "typo3/testing-framework": "^6.14",
"jangregor/phpstan-prophecy": "^1.0", "jangregor/phpstan-prophecy": "^1.0",
"phpstan/phpstan-phpunit": "^1.0", "phpstan/phpstan-phpunit": "^1.0",
"guzzlehttp/guzzle": "^6.3 || ^7.3" "guzzlehttp/guzzle": "^6.3 || ^7.3",
"phpspec/prophecy-phpunit": "^1.0"
}, },
"config": { "config": {
"allow-plugins": { "allow-plugins": {

View file

@ -1,22 +1,17 @@
parameters: parameters:
ignoreErrors: ignoreErrors:
- -
message: "#^Parameter \\#1 \\$restExperience of method Wrm\\\\Events\\\\Service\\\\DestinationDataImportService\\:\\:import\\(\\) expects string, mixed given\\.$#" message: "#^Parameter \\#1 \\$restExperience of class Wrm\\\\Events\\\\Domain\\\\DestinationData\\\\Import constructor expects string, mixed given\\.$#"
count: 1 count: 1
path: Classes/Command/DestinationDataImportCommand.php path: Classes/Command/DestinationDataImportCommand.php
- -
message: "#^Parameter \\#2 \\$storagePid of method Wrm\\\\Events\\\\Service\\\\DestinationDataImportService\\:\\:import\\(\\) expects int, mixed given\\.$#" message: "#^Parameter \\#2 \\$storagePid of class Wrm\\\\Events\\\\Domain\\\\DestinationData\\\\Import constructor expects int, mixed given\\.$#"
count: 1 count: 1
path: Classes/Command/DestinationDataImportCommand.php path: Classes/Command/DestinationDataImportCommand.php
- -
message: "#^Parameter \\#3 \\$regionUid of method Wrm\\\\Events\\\\Service\\\\DestinationDataImportService\\:\\:import\\(\\) expects int\\|null, mixed given\\.$#" message: "#^Parameter \\#4 \\$filesFolder of class Wrm\\\\Events\\\\Domain\\\\DestinationData\\\\Import constructor expects string, mixed given\\.$#"
count: 1
path: Classes/Command/DestinationDataImportCommand.php
-
message: "#^Parameter \\#4 \\$filesFolder of method Wrm\\\\Events\\\\Service\\\\DestinationDataImportService\\:\\:import\\(\\) expects string, mixed given\\.$#"
count: 1 count: 1
path: Classes/Command/DestinationDataImportCommand.php path: Classes/Command/DestinationDataImportCommand.php

View file

@ -18,6 +18,9 @@
> >
<testsuites> <testsuites>
<testsuite name="unit">
<directory>Tests/Unit/</directory>
</testsuite>
<testsuite name="functional"> <testsuite name="functional">
<directory>Tests/Functional/</directory> <directory>Tests/Functional/</directory>
</testsuite> </testsuite>