Skip to content

Commit dc25397

Browse files
committed
Fixed PHPunit tests
1 parent db81044 commit dc25397

File tree

5 files changed

+26
-9
lines changed

5 files changed

+26
-9
lines changed

src/Serializer/StructuralElementNormalizer.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
namespace App\Serializer;
2424

2525
use App\Entity\Base\AbstractStructuralDBElement;
26+
use App\Serializer\APIPlatform\SkippableItemNormalizer;
2627
use Symfony\Component\DependencyInjection\Attribute\Autowire;
2728
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
2829
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
@@ -36,22 +37,34 @@ class StructuralElementNormalizer implements NormalizerInterface, NormalizerAwar
3637
{
3738
use NormalizerAwareTrait;
3839

40+
public const ALREADY_CALLED = 'STRUCTURAL_ELEMENT_NORMALIZER_ALREADY_CALLED';
41+
3942
public function supportsNormalization($data, ?string $format = null, array $context = []): bool
4043
{
4144
//Only normalize if we are doing a file export operation
4245
if (!($context['partdb_export'] ?? false)) {
4346
return false;
4447
}
4548

49+
if (isset($context[self::ALREADY_CALLED]) && in_array($data, $context[self::ALREADY_CALLED], true)) {
50+
//If we already handled this object, skip it
51+
return false;
52+
}
53+
4654
return $data instanceof AbstractStructuralDBElement;
4755
}
4856

49-
public function normalize($object, ?string $format = null, array $context = []): \ArrayObject|bool|float|int|string
57+
public function normalize($object, ?string $format = null, array $context = []): \ArrayObject|bool|float|int|string|array
5058
{
5159
if (!$object instanceof AbstractStructuralDBElement) {
5260
throw new \InvalidArgumentException('This normalizer only supports AbstractStructural objects!');
5361
}
5462

63+
//Avoid infinite recursion by checking if we already handled this object
64+
$context[self::ALREADY_CALLED] = $context[self::ALREADY_CALLED] ?? [];
65+
$context[SkippableItemNormalizer::DISABLE_ITEM_NORMALIZER] = true;
66+
$context[self::ALREADY_CALLED][] = $object;
67+
5568
$data = $this->normalizer->normalize($object, $format, $context);
5669

5770
//If the data is not an array, we can't do anything with it
@@ -75,7 +88,8 @@ public function normalize($object, ?string $format = null, array $context = []):
7588
public function getSupportedTypes(?string $format): array
7689
{
7790
return [
78-
AbstractStructuralDBElement::class => true,
91+
//We cannot cache the result, as it depends on the context
92+
AbstractStructuralDBElement::class => false,
7993
];
8094
}
8195
}

src/Services/InfoProviderSystem/Providers/Element14Provider.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class Element14Provider implements InfoProviderInterface
4646
'rohsPhthalatesCompliant', 'SVHC', 'tariffCode', 'usEccn', 'hazardCode'];
4747

4848
private readonly HttpClientInterface $element14Client;
49-
49+
5050
public function __construct(HttpClientInterface $element14Client, private readonly Element14Settings $settings)
5151
{
5252
/* We use the mozilla CA from the composer ca bundle directly, as some debian systems seems to have problems
@@ -77,7 +77,7 @@ public function getProviderKey(): string
7777

7878
public function isActive(): bool
7979
{
80-
return trim($this->settings->apiKey) !== '';
80+
return $this->settings->apiKey !== null && trim($this->settings->apiKey) !== '';
8181
}
8282

8383
/**
@@ -308,4 +308,4 @@ public function getCapabilities(): array
308308
ProviderCapabilities::DATASHEET,
309309
];
310310
}
311-
}
311+
}

src/Services/InfoProviderSystem/Providers/TMEClient.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function makeRequest(string $action, array $parameters): ResponseInterfac
4848

4949
public function isUsable(): bool
5050
{
51-
return !($this->settings->apiToken === '' || $this->settings->apiSecret === '');
51+
return !($this->settings->apiToken === null || $this->settings->apiSecret === null);
5252
}
5353

5454
/**
@@ -59,7 +59,7 @@ public function isUsable(): bool
5959
public function isUsingPrivateToken(): bool
6060
{
6161
//Private tokens are longer than anonymous ones (50 instead of 45 characters)
62-
return strlen($this->settings->apiToken) > 45;
62+
return strlen($this->settings->apiToken ?? '') > 45;
6363
}
6464

6565
/**
@@ -94,4 +94,4 @@ private function sortSignatureParams(array $params): array
9494

9595
return $params;
9696
}
97-
}
97+
}

tests/Serializer/StructuralElementNormalizerTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ protected function setUp(): void
4141
//Get an service instance.
4242
self::bootKernel();
4343
$this->service = self::getContainer()->get(StructuralElementNormalizer::class);
44+
//Inject the serializer, as the normalizer as this is not handled by the DI container
45+
$this->service->setNormalizer(self::getContainer()->get('serializer'));
46+
4447
}
4548

4649
public function testNormalize(): void

tests/Validator/Constraints/ValidGoogleAuthCodeValidatorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function getRoles(): array
8787
return [];
8888
}
8989

90-
public function eraseCredentials()
90+
public function eraseCredentials(): void
9191
{
9292
}
9393

0 commit comments

Comments
 (0)