Skip to content

Commit bfec20d

Browse files
committed
add tests; fix cs
1 parent 765f435 commit bfec20d

File tree

6 files changed

+103
-25
lines changed

6 files changed

+103
-25
lines changed

src/Symfony/Extensions/PropertyNormalizerWrapper.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ protected function instantiateObject(
4040
\ReflectionClass $reflectionClass,
4141
$allowedAttributes,
4242
string $format = null
43-
): object
44-
{
43+
): object {
4544
return $reflectionClass->newInstanceWithoutConstructor();
4645
}
4746

@@ -52,11 +51,15 @@ protected function extractAttributes(object $object, string $format = null, arra
5251
{
5352
$class = \get_class($object);
5453

55-
if(false === isset($this->localStorage[$class]))
54+
if (false === isset($this->localStorage[$class]))
5655
{
5756
$this->localStorage[$class] = [];
5857

59-
foreach(\get_object_vars($object) as $key => $value)
58+
/**
59+
* @var string $key
60+
* @var mixed $value
61+
*/
62+
foreach (\get_object_vars($object) as $key => $value)
6063
{
6164
$this->localStorage[$class][] = $key;
6265
}
@@ -70,7 +73,7 @@ protected function extractAttributes(object $object, string $format = null, arra
7073
*/
7174
protected function getAttributeValue(object $object, string $attribute, string $format = null, array $context = [])
7275
{
73-
if(isset($object->{$attribute}) === true)
76+
if (true === isset($object->{$attribute}))
7477
{
7578
return $object->{$attribute};
7679
}
@@ -79,11 +82,13 @@ protected function getAttributeValue(object $object, string $attribute, string $
7982
}
8083

8184
/**
85+
* @psalm-param mixed $value
86+
*
8287
* {@inheritdoc}
8388
*/
84-
protected function setAttributeValue(object $object, string $attribute, $value, string $format = null, array $context = [])
89+
protected function setAttributeValue(object $object, string $attribute, $value, string $format = null, array $context = []): void
8590
{
86-
if(isset($object->{$attribute}) === true)
91+
if (true === isset($object->{$attribute}))
8792
{
8893
$object->{$attribute} = $value;
8994
}

src/Symfony/Extractor/FailOverExtractor.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
1616
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
1717
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
18-
use Symfony\Component\PropertyInfo\Type;
1918

2019
/**
2120
*/
@@ -24,26 +23,28 @@ final class FailOverExtractor implements PropertyTypeExtractorInterface
2423
/**
2524
* @var PropertyTypeExtractorInterface[]
2625
*/
27-
private array $extractors;
26+
private array
27+
28+
$extractors;
2829

2930
public function __construct()
3031
{
3132
$this->extractors = [
3233
new ReflectionExtractor(),
33-
new PhpDocExtractor()
34+
new PhpDocExtractor(),
3435
];
3536
}
3637

3738
/**
38-
* @inheritDoc
39+
* {@inheritdoc}
3940
*/
4041
public function getTypes(string $class, string $property, array $context = []): ?array
4142
{
42-
foreach($this->extractors as $extractor)
43+
foreach ($this->extractors as $extractor)
4344
{
4445
$types = $extractor->getTypes($class, $property, $context);
4546

46-
if(null !== $types)
47+
if (null !== $types)
4748
{
4849
return $types;
4950
}

src/Symfony/SymfonyMessageSerializer.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
use ServiceBus\MessageSerializer\SymfonyNormalizer\Extensions\EmptyDataNormalizer;
2525
use ServiceBus\MessageSerializer\SymfonyNormalizer\Extensions\PropertyNameConverter;
2626
use ServiceBus\MessageSerializer\SymfonyNormalizer\Extensions\PropertyNormalizerWrapper;
27-
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
2827
use Symfony\Component\Serializer as SymfonySerializer;
2928
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
3029

@@ -73,7 +72,7 @@ public function encode(object $message): string
7372

7473
return $this->serializer->serialize($data);
7574
}
76-
catch(\Throwable $throwable)
75+
catch (\Throwable $throwable)
7776
{
7877
throw new EncodeMessageFailed($throwable->getMessage(), (int) $throwable->getCode(), $throwable);
7978
}
@@ -96,7 +95,7 @@ public function decode(string $serializedMessage): object
9695

9796
return $object;
9897
}
99-
catch(\Throwable $throwable)
98+
catch (\Throwable $throwable)
10099
{
101100
throw new DecodeMessageFailed($throwable->getMessage(), (int) $throwable->getCode(), $throwable);
102101
}
@@ -117,7 +116,7 @@ public function denormalize(array $payload, string $class): object
117116

118117
return $object;
119118
}
120-
catch(\Throwable $throwable)
119+
catch (\Throwable $throwable)
121120
{
122121
throw new DenormalizeFailed($throwable->getMessage(), (int) $throwable->getCode(), $throwable);
123122
}
@@ -132,7 +131,7 @@ public function normalize(object $message): array
132131
{
133132
$data = $this->normalizer->normalize($message);
134133

135-
if(true === \is_array($data))
134+
if (true === \is_array($data))
136135
{
137136
/** @psalm-var array<string, mixed> $data */
138137

@@ -149,7 +148,7 @@ public function normalize(object $message): array
149148
);
150149
// @codeCoverageIgnoreEnd
151150
}
152-
catch(\Throwable $throwable)
151+
catch (\Throwable $throwable)
153152
{
154153
throw new NormalizationFailed($throwable->getMessage(), (int) $throwable->getCode(), $throwable);
155154
}
@@ -163,11 +162,10 @@ public function normalize(object $message): array
163162
private static function validateUnserializedData(array $data): void
164163
{
165164
/** Let's check if there are mandatory fields */
166-
if(
165+
if (
167166
false === isset($data['namespace']) ||
168167
false === isset($data['message'])
169-
)
170-
{
168+
) {
171169
throw new \UnexpectedValueException(
172170
'The serialized data must contains a "namespace" field (indicates the message class) and "message" (indicates the message parameters)'
173171
);
@@ -178,7 +176,7 @@ private static function validateUnserializedData(array $data): void
178176
*
179177
* @psalm-suppress DocblockTypeContradiction
180178
*/
181-
if('' === $data['namespace'] || false === \class_exists((string) $data['namespace']))
179+
if ('' === $data['namespace'] || false === \class_exists((string) $data['namespace']))
182180
{
183181
throw new \UnexpectedValueException(
184182
\sprintf('Class "%s" not found', $data['namespace'])

tests/Stubs/AuthorCollection.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,7 @@ final class AuthorCollection
1919
/**
2020
* @var Author[]
2121
*/
22-
public array $collection;
22+
public array
23+
24+
$collection;
2325
}

tests/Stubs/MixedWithLegacy.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/**
4+
* Messages serializer implementation.
5+
*
6+
* @author Maksim Masiukevich <[email protected]>
7+
* @license MIT
8+
* @license https://opensource.org/licenses/MIT
9+
*/
10+
11+
declare(strict_types = 1);
12+
13+
namespace ServiceBus\MessageSerializer\Tests\Stubs;
14+
15+
/**
16+
*/
17+
final class MixedWithLegacy
18+
{
19+
/**
20+
* @var string
21+
*/
22+
public $string;
23+
24+
/**
25+
* @var \DateTimeInterface
26+
*/
27+
public $dateTime;
28+
29+
/**
30+
* @var int
31+
*/
32+
public int $long;
33+
34+
/**
35+
* MixedWithLegacy constructor.
36+
*
37+
* @param string $string
38+
* @param \DateTimeInterface $dateTime
39+
* @param int $long
40+
*/
41+
public function __construct(string $string, \DateTimeInterface $dateTime, int $long)
42+
{
43+
$this->string = $string;
44+
$this->dateTime = $dateTime;
45+
$this->long = $long;
46+
}
47+
}

tests/Symfony/SymfonyMessageSerializerTest.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use ServiceBus\MessageSerializer\Tests\Stubs\AuthorCollection;
2222
use ServiceBus\MessageSerializer\Tests\Stubs\ClassWithPrivateConstructor;
2323
use ServiceBus\MessageSerializer\Tests\Stubs\EmptyClassWithPrivateConstructor;
24+
use ServiceBus\MessageSerializer\Tests\Stubs\MixedWithLegacy;
2425
use ServiceBus\MessageSerializer\Tests\Stubs\TestMessage;
2526
use ServiceBus\MessageSerializer\Tests\Stubs\WithDateTimeField;
2627
use ServiceBus\MessageSerializer\Tests\Stubs\WithNullableObjectArgument;
@@ -212,7 +213,8 @@ public function successCollection(): void
212213
{
213214
$serializer = new SymfonyMessageSerializer();
214215

215-
$object = new AuthorCollection();
216+
$object = new AuthorCollection();
217+
216218
$object->collection[] = Author::create('qwerty', 'root');
217219
$object->collection[] = Author::create('root', 'qwerty');
218220

@@ -224,4 +226,27 @@ public function successCollection(): void
224226
array_map(fn(Author $author): string => $author->firstName, $unserialized->collection),
225227
);
226228
}
229+
230+
/**
231+
* @test
232+
*
233+
* @throws \Throwable
234+
*/
235+
public function legacyPropertiesSupport(): void
236+
{
237+
$serializer = new SymfonyMessageSerializer();
238+
239+
$object = new MixedWithLegacy(
240+
'qwerty',
241+
new \DateTimeImmutable('2019-01-01', new \DateTimeZone('UTC')),
242+
100500
243+
);
244+
245+
/** @var MixedWithLegacy $unserialized */
246+
$unserialized = $serializer->decode($serializer->encode($object));
247+
248+
static::assertSame($object->string, $unserialized->string);
249+
static::assertSame($object->dateTime->getTimestamp(), $unserialized->dateTime->getTimestamp());
250+
static::assertSame($object->long, $unserialized->long);
251+
}
227252
}

0 commit comments

Comments
 (0)