Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,37 @@ public function testAddAttachmentFileToStep(
self::assertSame('.html', $attachment->getFileExtension());
}

/**
* A value that can't be serialize()'d (e.g. one containing a Closure) must not abort the test
* run: addAttachmentToCurrentStep falls back to a descriptive placeholder instead.
*/
public function testAddAttachmentToCurrentStepWithUnserializableDataDoesNotThrow(): void
{
$attachment = new AttachmentResult('a');
Allure::setLifecycleBuilder(
$this->createLifecycleBuilder($this->createResultFactoryWithAttachment($attachment)),
);

AllureHelper::addAttachmentToCurrentStep(['closure' => fn () => null], 'unserializableException');

self::assertSame('unserializableException', $attachment->getName());
}

/**
* Same guard on the addAttachmentToLastStep path.
*/
public function testAddAttachmentToLastStepWithUnserializableDataDoesNotThrow(): void
{
$attachment = new AttachmentResult('a');
Allure::setLifecycleBuilder(
$this->createLifecycleBuilder($this->createResultFactoryWithAttachment($attachment)),
);

AllureHelper::addAttachmentToLastStep(['closure' => fn () => null], 'unserializableException');

self::assertSame('unserializableException', $attachment->getName());
}

/**
* @return iterable<string, array{string, string|null, string|null}>
*/
Expand Down
29 changes: 23 additions & 6 deletions src/Magento/FunctionalTestingFramework/Allure/AllureHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@ class AllureHelper
public static function addAttachmentToCurrentStep($data, $caption): void
{
if (!is_string($data)) {
try {
$data = serialize($data);
} catch (\Exception $exception) {
throw new \Exception($data->getMessage());
}
$data = self::serializeAttachmentData($data);
}
if (@file_exists($data) && is_file($data)) {
Allure::attachmentFile($caption, $data);
Expand All @@ -49,7 +45,7 @@ public static function addAttachmentToCurrentStep($data, $caption): void
public static function addAttachmentToLastStep($data, $caption): void
{
if (!is_string($data)) {
$data = serialize($data);
$data = self::serializeAttachmentData($data);
}
if (@file_exists($data) && is_file($data)) {
Allure::attachmentFile($caption, $data);
Expand All @@ -58,6 +54,27 @@ public static function addAttachmentToLastStep($data, $caption): void
}
}

/**
* Serializes attachment data for the Allure report, falling back to a descriptive placeholder
* instead of letting an unserializable value (e.g. one containing a Closure) abort the whole
* test run: a failure to attach a debugging artifact must not be fatal to the test suite.
*
* @param mixed $data
* @return string
*/
private static function serializeAttachmentData($data): string
{
try {
return serialize($data);
} catch (\Exception $exception) {
return sprintf(
'Unable to serialize attachment data of type "%s": %s',
is_object($data) ? get_class($data) : gettype($data),
$exception->getMessage()
);
}
}

/**
* @param DataSourceInterface $dataSource
* @param string $name
Expand Down