Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

11565 Improve error logging main #124

Merged
merged 4 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions Classes/Domain/Import/Typo3Converter/GeneralConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ private function buildDataArrayFromEntity(

'parking_facility_near_by' => $entity instanceof Base ? implode(',', $this->getParkingFacilitiesNearByUids($entity)) : '',

'opening_hours' => $entity instanceof Place ? $this->getOpeningHours($entity->getOpeningHoursSpecification()) : '',
'special_opening_hours' => $entity instanceof Place ? $this->getOpeningHours($entity->getSpecialOpeningHoursSpecification()) : '',
'opening_hours' => $entity instanceof Place ? $this->getOpeningHours($entity->getOpeningHoursSpecification(), $entity) : '',
'special_opening_hours' => $entity instanceof Place ? $this->getOpeningHours($entity->getSpecialOpeningHoursSpecification(), $entity) : '',
'address' => $entity instanceof Place ? $this->getAddress($entity) : '',
'url' => $entity->getUrls()[0] ?? '',
'offers' => $entity instanceof Place ? $this->getOffers($entity) : '',
Expand Down Expand Up @@ -374,7 +374,7 @@ private function getSingleMedia(
/**
* @param OpeningHour[] $openingHours
*/
private function getOpeningHours(array $openingHours): string
private function getOpeningHours(array $openingHours, Minimum $entity): string
{
$data = [];

Expand All @@ -388,8 +388,9 @@ private function getOpeningHours(array $openingHours): string
'daysOfWeek' => $openingHour->getDaysOfWeek(),
]);
} catch (InvalidDataException $e) {
$this->logger->error('Could not import opening hour due to type error: {errorMessage}', [
$this->logger->error('Could not import opening hour of entity "{entityId}" due to type error: {errorMessage}', [
'errorMessage' => $e->getMessage(),
'entityId' => $entity->getId(),
'openingHour' => var_export($openingHour, true),
]);
continue;
Expand Down
19 changes: 19 additions & 0 deletions Tests/Functional/AbstractImportTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
namespace WerkraumMedia\ThueCat\Tests\Functional;

use Codappix\Typo3PhpDatasets\TestingFramework;
use DateTimeImmutable;
use TypeError;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Context\DateTimeAspect;
use TYPO3\CMS\Core\Localization\LanguageServiceFactory;

abstract class AbstractImportTestCase extends \TYPO3\TestingFramework\Core\Functional\FunctionalTestCase
Expand Down Expand Up @@ -73,6 +77,7 @@ protected function setUp(): void

GuzzleClientFaker::registerClient();
$this->importPHPDataSet(__DIR__ . '/Fixtures/Import/BackendUser.php');
$this->setDateAspect(new DateTimeImmutable('2024-09-19T00:00:00+00:00'));
$this->setUpBackendUser(1);
$GLOBALS['LANG'] = $this->getContainer()->get(LanguageServiceFactory::class)->create('en_US');
foreach ($this->getLogFiles() as $logFile) {
Expand Down Expand Up @@ -116,4 +121,18 @@ protected function getErrorLogFile(): string
{
return self::getInstancePath() . '/typo3temp/var/log/typo3_error_0493d91d8e.log';
}

/**
* @api Actual tests can use this method to define the actual date of "now".
*/
protected function setDateAspect(DateTimeImmutable $dateTime): void
{
$context = $this->getContainer()->get(Context::class);
if (!$context instanceof Context) {
throw new TypeError('Retrieved context was of unexpected type.', 1638182021);
}

$aspect = new DateTimeAspect($dateTime);
$context->setAspect('date', $aspect);
}
}
2 changes: 1 addition & 1 deletion Tests/Functional/ImportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ public function importsWithBrokenOpeningHour(): void
$loggedErrors = file_get_contents($this->getErrorLogFile());
self::assertIsString($loggedErrors);
self::assertStringContainsString(
'Could not import opening hour due to type error: Opens was empty for opening hour.',
'Could not import opening hour of entity "https://thuecat.org/resources/attraction-with-broken-opening-hour" due to type error: Opens was empty for opening hour.',
$loggedErrors
);
self::assertStringContainsString('\'closes\' => NULL,', $loggedErrors);
Expand Down
4 changes: 2 additions & 2 deletions Tests/Unit/Domain/Import/ImportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public function nestedImportMergesLog(): void

$configuration2 = new ImportConfiguration();
$subject->start($configuration2);
$importLogEntry = $this->createStub(ImportLogEntry::class);
$importLogEntry = self::createStub(ImportLogEntry::class);
$subject->getLog()->addEntry($importLogEntry);
$subject->end();

Expand All @@ -180,7 +180,7 @@ public function nestedImportReturnsHandledForRemoteId(): void

$configuration2 = new ImportConfiguration();
$subject->start($configuration2);
$importLogEntry = $this->createStub(ImportLogEntry::class);
$importLogEntry = self::createStub(ImportLogEntry::class);
$importLogEntry->method('getRemoteId')->willReturn('https://example.com/remote-id');
$subject->getLog()->addEntry($importLogEntry);
$subject->end();
Expand Down
60 changes: 30 additions & 30 deletions Tests/Unit/Domain/Import/Importer/FetchDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ class FetchDataTest extends TestCase
#[Test]
public function canBeCreated(): void
{
$requestFactory = $this->createStub(RequestFactoryInterface::class);
$httpClient = $this->createStub(ClientInterface::class);
$cache = $this->createStub(FrontendInterface::class);
$requestFactory = self::createStub(RequestFactoryInterface::class);
$httpClient = self::createStub(ClientInterface::class);
$cache = self::createStub(FrontendInterface::class);

$subject = new FetchData(
$requestFactory,
Expand All @@ -56,17 +56,17 @@ public function canBeCreated(): void
#[Test]
public function returnsParsedJsonLdBasedOnUrl(): void
{
$requestFactory = $this->createStub(RequestFactoryInterface::class);
$httpClient = $this->createStub(ClientInterface::class);
$cache = $this->createStub(FrontendInterface::class);
$requestFactory = self::createStub(RequestFactoryInterface::class);
$httpClient = self::createStub(ClientInterface::class);
$cache = self::createStub(FrontendInterface::class);

$request = $this->createStub(RequestInterface::class);
$response = $this->createStub(ResponseInterface::class);
$request = self::createStub(RequestInterface::class);
$response = self::createStub(ResponseInterface::class);

$requestFactory->method('createRequest')->willReturn($request);
$httpClient->method('sendRequest')->willReturn($response);

$body = $this->createStub(StreamInterface::class);
$body = self::createStub(StreamInterface::class);
$body->method('__toString')->willReturn('{"@graph":[{"@id":"https://example.com/resources/018132452787-ngbe"}]}');

$response->method('getStatusCode')->willReturn(200);
Expand All @@ -91,18 +91,18 @@ public function returnsParsedJsonLdBasedOnUrl(): void
#[Test]
public function returnsEmptyArrayInCaseOfError(): void
{
$requestFactory = $this->createStub(RequestFactoryInterface::class);
$httpClient = $this->createStub(ClientInterface::class);
$cache = $this->createStub(FrontendInterface::class);
$requestFactory = self::createStub(RequestFactoryInterface::class);
$httpClient = self::createStub(ClientInterface::class);
$cache = self::createStub(FrontendInterface::class);

$request = $this->createStub(RequestInterface::class);
$response = $this->createStub(ResponseInterface::class);
$request = self::createStub(RequestInterface::class);
$response = self::createStub(ResponseInterface::class);

$requestFactory->method('createRequest')->willReturn($request);

$httpClient->method('sendRequest')->willReturn($response);

$body = $this->createStub(StreamInterface::class);
$body = self::createStub(StreamInterface::class);
$body->method('__toString')->willReturn('[]');

$response->method('getStatusCode')->willReturn(200);
Expand All @@ -121,9 +121,9 @@ public function returnsEmptyArrayInCaseOfError(): void
#[Test]
public function returnsResultFromCacheIfAvailable(): void
{
$requestFactory = $this->createStub(RequestFactoryInterface::class);
$httpClient = $this->createStub(ClientInterface::class);
$cache = $this->createStub(FrontendInterface::class);
$requestFactory = self::createStub(RequestFactoryInterface::class);
$httpClient = self::createStub(ClientInterface::class);
$cache = self::createStub(FrontendInterface::class);

$cache->method('get')->willReturn([
'@graph' => [
Expand Down Expand Up @@ -152,22 +152,22 @@ public function returnsResultFromCacheIfAvailable(): void
#[Test]
public function throwsExceptionOn404(): void
{
$requestFactory = $this->createStub(RequestFactoryInterface::class);
$httpClient = $this->createStub(ClientInterface::class);
$cache = $this->createStub(FrontendInterface::class);
$requestFactory = self::createStub(RequestFactoryInterface::class);
$httpClient = self::createStub(ClientInterface::class);
$cache = self::createStub(FrontendInterface::class);

$request = $this->createStub(RequestInterface::class);
$response = $this->createStub(ResponseInterface::class);
$request = self::createStub(RequestInterface::class);
$response = self::createStub(ResponseInterface::class);

$uri = $this->createStub(UriInterface::class);
$uri = self::createStub(UriInterface::class);
$uri->method('__toString')->willReturn('https://example.com/resources/018132452787-ngbe');
$request->method('getUri')->willReturn($uri);

$requestFactory->method('createRequest')->willReturn($request);

$httpClient->method('sendRequest')->willReturn($response);

$body = $this->createStub(StreamInterface::class);
$body = self::createStub(StreamInterface::class);
$body->method('__toString')->willReturn('{"error":"404"}');

$response->method('getStatusCode')->willReturn(404);
Expand All @@ -189,12 +189,12 @@ public function throwsExceptionOn404(): void
#[Test]
public function throwsExceptionOn401(): void
{
$requestFactory = $this->createStub(RequestFactoryInterface::class);
$httpClient = $this->createStub(ClientInterface::class);
$cache = $this->createStub(FrontendInterface::class);
$requestFactory = self::createStub(RequestFactoryInterface::class);
$httpClient = self::createStub(ClientInterface::class);
$cache = self::createStub(FrontendInterface::class);

$request = $this->createStub(RequestInterface::class);
$response = $this->createStub(ResponseInterface::class);
$request = self::createStub(RequestInterface::class);
$response = self::createStub(ResponseInterface::class);

$requestFactory->method('createRequest')->willReturn($request);

Expand Down
10 changes: 5 additions & 5 deletions Tests/Unit/Domain/Import/Model/EntityCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public function returnsEmptyArrayAsDefaultEntities(): void
#[Test]
public function returnsFirstEntityForDefaultLanguage(): void
{
$entityWithTranslation = $this->createStub(Entity::class);
$entityWithTranslation = self::createStub(Entity::class);
$entityWithTranslation->method('isForDefaultLanguage')->willReturn(false);

$entityWithDefaultLanguage = $this->createStub(Entity::class);
$entityWithDefaultLanguage = self::createStub(Entity::class);
$entityWithDefaultLanguage->method('isForDefaultLanguage')->willReturn(true);

$subject = new EntityCollection();
Expand All @@ -68,7 +68,7 @@ public function returnsFirstEntityForDefaultLanguage(): void
#[Test]
public function returnsNullIfNoEntityForDefaultLanguageExists(): void
{
$entityWithTranslation = $this->createStub(Entity::class);
$entityWithTranslation = self::createStub(Entity::class);
$entityWithTranslation->method('isForDefaultLanguage')->willReturn(false);

$subject = new EntityCollection();
Expand All @@ -82,7 +82,7 @@ public function returnsNullIfNoEntityForDefaultLanguageExists(): void
#[Test]
public function returnsEntitiesToTranslate(): void
{
$entityWithTranslation = $this->createStub(Entity::class);
$entityWithTranslation = self::createStub(Entity::class);
$entityWithTranslation->method('isTranslation')->willReturn(true);
$entityWithTranslation->method('exists')->willReturn(false);

Expand All @@ -100,7 +100,7 @@ public function returnsEntitiesToTranslate(): void
#[Test]
public function returnsExistingEntities(): void
{
$entityWithTranslation = $this->createStub(Entity::class);
$entityWithTranslation = self::createStub(Entity::class);
$entityWithTranslation->method('exists')->willReturn(true);

$subject = new EntityCollection();
Expand Down
16 changes: 8 additions & 8 deletions Tests/Unit/Domain/Import/RequestFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class RequestFactoryTest extends TestCase
#[Test]
public function canBeCreated(): void
{
$extensionConfiguration = $this->createStub(ExtensionConfiguration::class);
$requestFactory = $this->createStub(Typo3RequestFactory::class);
$extensionConfiguration = self::createStub(ExtensionConfiguration::class);
$requestFactory = self::createStub(Typo3RequestFactory::class);
$uriFactory = new UriFactory();

$subject = new RequestFactory(
Expand All @@ -53,8 +53,8 @@ public function canBeCreated(): void
#[Test]
public function returnsRequestWithJsonIdFormat(): void
{
$extensionConfiguration = $this->createStub(ExtensionConfiguration::class);
$requestFactory = new Typo3RequestFactory($this->createStub(GuzzleClientFactory::class));
$extensionConfiguration = self::createStub(ExtensionConfiguration::class);
$requestFactory = new Typo3RequestFactory(self::createStub(GuzzleClientFactory::class));
$uriFactory = new UriFactory();

$subject = new RequestFactory(
Expand All @@ -71,9 +71,9 @@ public function returnsRequestWithJsonIdFormat(): void
#[Test]
public function returnsRequestWithApiKeyWhenConfigured(): void
{
$extensionConfiguration = $this->createStub(ExtensionConfiguration::class);
$extensionConfiguration = self::createStub(ExtensionConfiguration::class);
$extensionConfiguration->method('get')->willReturn('some-api-key');
$requestFactory = new Typo3RequestFactory($this->createStub(GuzzleClientFactory::class));
$requestFactory = new Typo3RequestFactory(self::createStub(GuzzleClientFactory::class));
$uriFactory = new UriFactory();

$subject = new RequestFactory(
Expand All @@ -90,9 +90,9 @@ public function returnsRequestWithApiKeyWhenConfigured(): void
#[Test]
public function returnsRequestWithoutApiKeyWhenUnkown(): void
{
$extensionConfiguration = $this->createStub(ExtensionConfiguration::class);
$extensionConfiguration = self::createStub(ExtensionConfiguration::class);
$extensionConfiguration->method('get')->willThrowException(new ExtensionConfigurationExtensionNotConfiguredException());
$requestFactory = new Typo3RequestFactory($this->createStub(GuzzleClientFactory::class));
$requestFactory = new Typo3RequestFactory(self::createStub(GuzzleClientFactory::class));
$uriFactory = new UriFactory();

$subject = new RequestFactory(
Expand Down
36 changes: 18 additions & 18 deletions Tests/Unit/Domain/Import/Typo3Converter/GeneralConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ class GeneralConverterTest extends TestCase
#[Test]
public function canBeCreated(): void
{
$resolveForeignReference = $this->createStub(ResolveForeignReference::class);
$importer = $this->createStub(Importer::class);
$languageHandling = $this->createStub(LanguageHandling::class);
$organisationRepository = $this->createStub(OrganisationRepository::class);
$townRepository = $this->createStub(TownRepository::class);
$parkingFacilityRepository = $this->createStub(ParkingFacilityRepository::class);
$nameExtractor = $this->createStub(NameExtractor::class);
$logManager = $this->createStub(LogManager::class);
$logManager->method('getLogger')->willReturn($this->createStub(Logger::class));
$resolveForeignReference = self::createStub(ResolveForeignReference::class);
$importer = self::createStub(Importer::class);
$languageHandling = self::createStub(LanguageHandling::class);
$organisationRepository = self::createStub(OrganisationRepository::class);
$townRepository = self::createStub(TownRepository::class);
$parkingFacilityRepository = self::createStub(ParkingFacilityRepository::class);
$nameExtractor = self::createStub(NameExtractor::class);
$logManager = self::createStub(LogManager::class);
$logManager->method('getLogger')->willReturn(self::createStub(Logger::class));

$subject = new GeneralConverter(
$resolveForeignReference,
Expand All @@ -75,17 +75,17 @@ public function canBeCreated(): void
#[Test]
public function skipsWithoutManager(): void
{
$resolveForeignReference = $this->createStub(ResolveForeignReference::class);
$importer = $this->createStub(Importer::class);
$resolveForeignReference = self::createStub(ResolveForeignReference::class);
$importer = self::createStub(Importer::class);
$importer->method('importConfiguration')->willReturn(new ImportLog());
$languageHandling = $this->createStub(LanguageHandling::class);
$languageHandling = self::createStub(LanguageHandling::class);
$languageHandling->method('getLanguageUidForString')->willReturn(0);
$organisationRepository = $this->createStub(OrganisationRepository::class);
$townRepository = $this->createStub(TownRepository::class);
$parkingFacilityRepository = $this->createStub(ParkingFacilityRepository::class);
$nameExtractor = $this->createStub(NameExtractor::class);
$logManager = $this->createStub(LogManager::class);
$logManager->method('getLogger')->willReturn($this->createStub(Logger::class));
$organisationRepository = self::createStub(OrganisationRepository::class);
$townRepository = self::createStub(TownRepository::class);
$parkingFacilityRepository = self::createStub(ParkingFacilityRepository::class);
$nameExtractor = self::createStub(NameExtractor::class);
$logManager = self::createStub(LogManager::class);
$logManager->method('getLogger')->willReturn(self::createStub(Logger::class));

$subject = new GeneralConverter(
$resolveForeignReference,
Expand Down
Loading
Loading