Fixed PHPunit tests
Some checks are pending
Build assets artifact / Build assets artifact (push) Waiting to run
Docker Image Build / docker (push) Waiting to run
Docker Image Build (FrankenPHP) / docker (push) Waiting to run
Static analysis / Static analysis (push) Waiting to run
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.1, mysql) (push) Waiting to run
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.2, mysql) (push) Waiting to run
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.3, mysql) (push) Waiting to run
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.4, mysql) (push) Waiting to run
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.1, postgres) (push) Waiting to run
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.2, postgres) (push) Waiting to run
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.3, postgres) (push) Waiting to run
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.4, postgres) (push) Waiting to run
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.1, sqlite) (push) Waiting to run
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.2, sqlite) (push) Waiting to run
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.3, sqlite) (push) Waiting to run
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.4, sqlite) (push) Waiting to run

This commit is contained in:
Jan Böhmer 2025-07-13 20:06:38 +02:00
parent db810445fb
commit dc25397469
5 changed files with 26 additions and 9 deletions

View file

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace App\Serializer;
use App\Entity\Base\AbstractStructuralDBElement;
use App\Serializer\APIPlatform\SkippableItemNormalizer;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
@ -36,6 +37,8 @@ class StructuralElementNormalizer implements NormalizerInterface, NormalizerAwar
{
use NormalizerAwareTrait;
public const ALREADY_CALLED = 'STRUCTURAL_ELEMENT_NORMALIZER_ALREADY_CALLED';
public function supportsNormalization($data, ?string $format = null, array $context = []): bool
{
//Only normalize if we are doing a file export operation
@ -43,15 +46,25 @@ class StructuralElementNormalizer implements NormalizerInterface, NormalizerAwar
return false;
}
if (isset($context[self::ALREADY_CALLED]) && in_array($data, $context[self::ALREADY_CALLED], true)) {
//If we already handled this object, skip it
return false;
}
return $data instanceof AbstractStructuralDBElement;
}
public function normalize($object, ?string $format = null, array $context = []): \ArrayObject|bool|float|int|string
public function normalize($object, ?string $format = null, array $context = []): \ArrayObject|bool|float|int|string|array
{
if (!$object instanceof AbstractStructuralDBElement) {
throw new \InvalidArgumentException('This normalizer only supports AbstractStructural objects!');
}
//Avoid infinite recursion by checking if we already handled this object
$context[self::ALREADY_CALLED] = $context[self::ALREADY_CALLED] ?? [];
$context[SkippableItemNormalizer::DISABLE_ITEM_NORMALIZER] = true;
$context[self::ALREADY_CALLED][] = $object;
$data = $this->normalizer->normalize($object, $format, $context);
//If the data is not an array, we can't do anything with it
@ -75,7 +88,8 @@ class StructuralElementNormalizer implements NormalizerInterface, NormalizerAwar
public function getSupportedTypes(?string $format): array
{
return [
AbstractStructuralDBElement::class => true,
//We cannot cache the result, as it depends on the context
AbstractStructuralDBElement::class => false,
];
}
}