Skip to content

Commit

Permalink
Merge pull request #38 from mteu/task/increase-test-coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
mteu authored Sep 12, 2024
2 parents d0cad5d + bd33dda commit 6e6b802
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 13 deletions.
27 changes: 18 additions & 9 deletions Classes/Log/Writer/StreamWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,31 @@ public function __construct(array $options = ['outputStream' => StandardStream::
/**
* @param mixed[] $options
* @return class-string[]
* @throws InvalidLogWriterConfigurationException
*/
private function getIgnoredComponentsOption(array $options): array
{
if (array_key_exists('ignoreComponents', $options)) {
return $options['ignoreComponents'];

if (
$options['ignoreComponents'] === '' ||
$options['ignoreComponents'] === null
) {
$this->throwConfigurationException('Missing', 'ignoreComponents', 1722422118);
}
}

return [];
}

private function throwConfigurationException(string $type, string $option, int $code): never
{
throw new InvalidLogWriterConfigurationException(
$type . ' LogWriter configuration option "' . $option . '" for log writer of type "' . __CLASS__ . '"',
$code,
);
}

/**
* @param mixed[] $options
* @throws InvalidLogWriterConfigurationException
Expand All @@ -91,17 +106,11 @@ private function getOutputStreamOption(array $options): StandardStream
$options['outputStream'] === '' ||
$options['outputStream'] === null
) {
throw new InvalidLogWriterConfigurationException(
'Missing LogWriter configuration option "outputStream" for log writer of type "' . __CLASS__ . '"',
1722422118,
);
$this->throwConfigurationException('Missing', 'outputStream', 1722422117);
}

if (!$options['outputStream'] instanceof StandardStream) {
throw new InvalidLogWriterConfigurationException(
'Invalid LogWriter configuration option "' . $options['outputStream'] . '" for log writer of type "' . __CLASS__ . '"',
1722422119,
);
$this->throwConfigurationException('Invalid', 'outputStream', 1722422116);
}

return $options['outputStream'];
Expand Down
104 changes: 100 additions & 4 deletions Tests/Unit/Log/Writer/StreamWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@ private function generatePhpScriptForLogWriting(
$classFileName = dirname(__DIR__, 4) . '/Classes/Log/Writer/StreamWriter.php';
$coverageFile = dirname(__DIR__, 4) . '/.build/coverage/sub-process_' . uniqid() . '.cov';

// data will most likely be set for ExceptionHandlers only
$data = [
'mode' => $record->getData()['mode'] ?? '',
'application_mode' => $record->getData()['application_mode'] ?? '',
'exception_class' => $record->getData()['exception_class'] ?? '',
'exception_code' => $record->getData()['exception_code'] ?? 0,
'file' => $record->getData()['file'] ?? '',
'line' => $record->getData()['line'] ?? 0,
'message' => $record->getData()['message'] ?? '',
];

return <<<PHP
<?php
Expand Down Expand Up @@ -149,7 +160,15 @@ private function generatePhpScriptForLogWriting(
'{$record->getComponent()}',
'{$record->getLevel()}',
'{$record->getMessage()}',
[],
[
'mode' => '{$data['mode']}',
'application_mode' => '{$data['application_mode']}',
'exception_class' => '{$data['exception_class']}',
'exception_code' => '{$data['exception_code']}',
'file' => '{$data['file']}',
'line' => '{$data['line']}',
'message' => '{$data['message']}',
],
'{$record->getRequestId()}',
),
);
Expand Down Expand Up @@ -198,8 +217,8 @@ public function writeLogCreationSucceedsWithProperlyConfiguredOutputStream(): vo
public function writeLogCreationThrowsExceptionForInvalidConfiguration(): void
{
self::expectException(InvalidLogWriterConfigurationException::class);
self::expectExceptionMessage('Missing LogWriter configuration option "outputStream" for log writer of type "mteu\StreamWriter\Log\Writer\StreamWriter');
$this->createWriter(['foo']);
self::expectExceptionMessage('Invalid LogWriter configuration option "outputStream" for log writer of type "mteu\StreamWriter\Log\Writer\StreamWriter');
$this->createWriter(['outputStream' => 'foo']);
}

#[Framework\Attributes\Test]
Expand All @@ -211,11 +230,88 @@ public function writeLogCreationThrowsExceptionForUnsetOutputStreamValue(): void
}

#[Framework\Attributes\Test]
public function writeLogCreationThrowsExceptionForEmptyOutputStreamValue(): void
public function writeLogCreationThrowsExceptionForEmptyValues(): void
{
self::expectException(InvalidLogWriterConfigurationException::class);
self::expectExceptionMessage('Missing LogWriter configuration option "outputStream" for log writer of type "mteu\StreamWriter\Log\Writer\StreamWriter"');
$this->createWriter(['outputStream' => '']);

self::expectException(InvalidLogWriterConfigurationException::class);
self::expectExceptionMessage('Missing LogWriter configuration option "ignoreComponents" for log writer of type "mteu\StreamWriter\Log\Writer\StreamWriter"');
$this->createWriter([
'outputStream' => StandardStream::Out,
'ignoreComponents' => '',
]);
}

#[Framework\Attributes\Test]
public function writeLogCreationThrowsExceptionForUnspecifiedIgnoreComponentsValue(): void
{
self::expectException(InvalidLogWriterConfigurationException::class);
self::expectExceptionMessage('Missing LogWriter configuration option "ignoreComponents" for log writer of type "mteu\StreamWriter\Log\Writer\StreamWriter"');
$this->createWriter([
'outputStream' => StandardStream::Out,
'ignoreComponents' => null,
]);
}

#[Framework\Attributes\Test]
public function writeLogCreationExtractsStackDateFromExceptionHandler(): void
{
$data = [
'mode' => 'BE',
'application_mode' => 'WEB',
'exception_class' => 'ExceptionClass',
'exception_code' => 'ExceptionCode',
'file' => '',
'line' => 1,
'message' => 'Message',
];

$record = new LogRecord(
'TYPO3.CMS.Core.Error.ExceptionHandlerInterface',
Src\LogLevel::ERROR->value,
'Foo',
$data,
'Bar',
);

/**
* @var array{
* mode: string,
* application_mode: string,
* exception_class: string,
* exception_code: int,
* file: string,
* line: int,
* message: string,
* } $data
*/
$data = $record->getData();

$expected = sprintf(
'[%s] %s: (%s: %s) %s, code %d, file %s, line %d: %s',
strtoupper($record->getLevel()),
$record->getComponent(),
$data['mode'],
$data['application_mode'],
$data['exception_class'],
$data['exception_code'],
$data['file'],
$data['line'],
$data['message'],
);

self::assertSame(
$expected,
$this->writeToStreamInSeparateProcess(
StandardStream::Out,
$record,
Src\LogLevel::EMERGENCY,
__METHOD__,
),
);

}

/**
Expand Down

0 comments on commit 6e6b802

Please sign in to comment.