Skip to content

Commit 62b2aea

Browse files
committed
ISSUE-345: update normalizers
1 parent 2b47c39 commit 62b2aea

File tree

8 files changed

+100
-26
lines changed

8 files changed

+100
-26
lines changed

config/services/normalizers.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ services:
1515
tags: [ 'serializer.normalizer' ]
1616
autowire: true
1717

18+
PhpList\RestBundle\Subscription\Serializer\SubscriberOnlyNormalizer:
19+
tags: [ 'serializer.normalizer' ]
20+
autowire: true
21+
1822
PhpList\RestBundle\Identity\Serializer\AdministratorTokenNormalizer:
1923
tags: [ 'serializer.normalizer' ]
2024
autowire: true

src/Subscription/OpenApi/SwaggerSchemasResponse.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
format: 'date-time',
1919
example: '2022-12-01T10:00:00Z'
2020
),
21+
new OA\Property(property: 'list_position', type: 'integer', example: 1),
22+
new OA\Property(property: 'subject_prefix', type: 'string', example: 'Newsletter: '),
2123
new OA\Property(property: 'public', type: 'boolean', example: true),
24+
new OA\Property(property: 'category', type: 'string', example: 'News'),
2225
],
2326
type: 'object'
2427
)]
@@ -47,10 +50,30 @@
4750
],
4851
type: 'object'
4952
)]
53+
#[OA\Schema(
54+
schema: 'SubscriberOnly',
55+
properties: [
56+
new OA\Property(property: 'id', type: 'integer', example: 1),
57+
new OA\Property(property: 'email', type: 'string', example: '[email protected]'),
58+
new OA\Property(
59+
property: 'created_at',
60+
type: 'string',
61+
format: 'date-time',
62+
example: '2023-01-01T12:00:00Z',
63+
),
64+
new OA\Property(property: 'confirmed', type: 'boolean', example: true),
65+
new OA\Property(property: 'blacklisted', type: 'boolean', example: false),
66+
new OA\Property(property: 'bounce_count', type: 'integer', example: 0),
67+
new OA\Property(property: 'unique_id', type: 'string', example: '69f4e92cf50eafca9627f35704f030f4'),
68+
new OA\Property(property: 'html_email', type: 'boolean', example: true),
69+
new OA\Property(property: 'disabled', type: 'boolean', example: false),
70+
],
71+
type: 'object'
72+
)]
5073
#[OA\Schema(
5174
schema: 'Subscription',
5275
properties: [
53-
new OA\Property(property: 'subscriber', ref: '#/components/schemas/Subscriber'),
76+
new OA\Property(property: 'subscriber', ref: '#/components/schemas/SubscriberOnly'),
5477
new OA\Property(property: 'subscriber_list', ref: '#/components/schemas/SubscriberList'),
5578
new OA\Property(
5679
property: 'subscription_date',

src/Subscription/Serializer/SubscriberNormalizer.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010

1111
class SubscriberNormalizer implements NormalizerInterface
1212
{
13+
public function __construct(private readonly SubscriberListNormalizer $subscriberListNormalizer)
14+
{
15+
}
16+
1317
/**
1418
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
1519
*/
@@ -30,14 +34,7 @@ public function normalize($object, string $format = null, array $context = []):
3034
'html_email' => $object->hasHtmlEmail(),
3135
'disabled' => $object->isDisabled(),
3236
'subscribed_lists' => array_map(function (Subscription $subscription) {
33-
return [
34-
'id' => $subscription->getSubscriberList()->getId(),
35-
'name' => $subscription->getSubscriberList()->getName(),
36-
'description' => $subscription->getSubscriberList()->getDescription(),
37-
'created_at' => $subscription->getSubscriberList()->getCreatedAt()->format('Y-m-d\TH:i:sP'),
38-
'public' => $subscription->getSubscriberList()->isPublic(),
39-
'subscription_date' => $subscription->getCreatedAt()->format('Y-m-d\TH:i:sP'),
40-
];
37+
return $this->subscriberListNormalizer->normalize($subscription->getSubscriberList());
4138
}, $object->getSubscriptions()->toArray()),
4239
];
4340
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\RestBundle\Subscription\Serializer;
6+
7+
use PhpList\Core\Domain\Subscription\Model\Subscriber;
8+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
9+
10+
class SubscriberOnlyNormalizer implements NormalizerInterface
11+
{
12+
/**
13+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
14+
*/
15+
public function normalize($object, string $format = null, array $context = []): array
16+
{
17+
if (!$object instanceof Subscriber) {
18+
return [];
19+
}
20+
21+
return [
22+
'id' => $object->getId(),
23+
'email' => $object->getEmail(),
24+
'created_at' => $object->getCreatedAt()->format('Y-m-d\TH:i:sP'),
25+
'confirmed' => $object->isConfirmed(),
26+
'blacklisted' => $object->isBlacklisted(),
27+
'bounce_count' => $object->getBounceCount(),
28+
'unique_id' => $object->getUniqueId(),
29+
'html_email' => $object->hasHtmlEmail(),
30+
'disabled' => $object->isDisabled(),
31+
];
32+
}
33+
34+
/**
35+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
36+
*/
37+
public function supportsNormalization($data, string $format = null): bool
38+
{
39+
return $data instanceof Subscriber;
40+
}
41+
}

src/Subscription/Serializer/SubscriptionNormalizer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99

1010
class SubscriptionNormalizer implements NormalizerInterface
1111
{
12-
private SubscriberNormalizer $subscriberNormalizer;
12+
private SubscriberOnlyNormalizer $subscriberNormalizer;
1313
private SubscriberListNormalizer $subscriberListNormalizer;
1414

1515
public function __construct(
16-
SubscriberNormalizer $subscriberNormalizer,
16+
SubscriberOnlyNormalizer $subscriberNormalizer,
1717
SubscriberListNormalizer $subscriberListNormalizer
1818
) {
1919
$this->subscriberNormalizer = $subscriberNormalizer;

tests/Integration/Subscription/Controller/SubscriberListControllerTest.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,12 @@ public function testGetListMembersWithCurrentSessionKeyForExistingListWithSubscr
270270
[
271271
'id' => 2,
272272
'name' => 'More news',
273-
'description' => '',
274273
'created_at' => '2016-06-22T15:01:17+00:00',
274+
'description' => '',
275+
'list_position' => 12,
276+
'subject_prefix' => '',
275277
'public' => true,
276-
'subscription_date' => '2016-07-22T15:01:17+00:00',
278+
'category' => '',
277279
],
278280
],
279281
], [
@@ -290,18 +292,22 @@ public function testGetListMembersWithCurrentSessionKeyForExistingListWithSubscr
290292
[
291293
'id' => 2,
292294
'name' => 'More news',
293-
'description' => '',
294295
'created_at' => '2016-06-22T15:01:17+00:00',
296+
'description' => '',
297+
'list_position' => 12,
298+
'subject_prefix' => '',
295299
'public' => true,
296-
'subscription_date' => '2016-08-22T15:01:17+00:00',
300+
'category' => '',
297301
],
298302
[
299303
'id' => 1,
300304
'name' => 'News',
301-
'description' => 'News (and some fun stuff)',
302305
'created_at' => '2016-06-22T15:01:17+00:00',
306+
'description' => 'News (and some fun stuff)',
307+
'list_position' => 12,
308+
'subject_prefix' => 'phpList',
303309
'public' => true,
304-
'subscription_date' => '2016-09-22T15:01:17+00:00',
310+
'category' => 'news',
305311
],
306312
],
307313
],

tests/Unit/Subscription/Serializer/SubscriberNormalizerTest.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PhpList\Core\Domain\Subscription\Model\Subscriber;
1010
use PhpList\Core\Domain\Subscription\Model\SubscriberList;
1111
use PhpList\Core\Domain\Subscription\Model\Subscription;
12+
use PhpList\RestBundle\Subscription\Serializer\SubscriberListNormalizer;
1213
use PhpList\RestBundle\Subscription\Serializer\SubscriberNormalizer;
1314
use PHPUnit\Framework\TestCase;
1415
use stdClass;
@@ -17,7 +18,7 @@ class SubscriberNormalizerTest extends TestCase
1718
{
1819
public function testSupportsNormalization(): void
1920
{
20-
$normalizer = new SubscriberNormalizer();
21+
$normalizer = new SubscriberNormalizer(new SubscriberListNormalizer());
2122
$subscriber = $this->createMock(Subscriber::class);
2223

2324
$this->assertTrue($normalizer->supportsNormalization($subscriber));
@@ -49,7 +50,7 @@ public function testNormalize(): void
4950
$subscriber->method('isDisabled')->willReturn(false);
5051
$subscriber->method('getSubscriptions')->willReturn(new ArrayCollection([$subscription]));
5152

52-
$normalizer = new SubscriberNormalizer();
53+
$normalizer = new SubscriberNormalizer(new SubscriberListNormalizer());
5354

5455
$expected = [
5556
'id' => 101,
@@ -65,10 +66,12 @@ public function testNormalize(): void
6566
[
6667
'id' => 1,
6768
'name' => 'News',
68-
'description' => 'Latest news',
6969
'created_at' => '2025-01-01T00:00:00+00:00',
70+
'description' => 'Latest news',
71+
'list_position' => null,
72+
'subject_prefix' => null,
7073
'public' => true,
71-
'subscription_date' => '2025-01-10T00:00:00+00:00'
74+
'category' => '',
7275
]
7376
]
7477
];
@@ -78,7 +81,7 @@ public function testNormalize(): void
7881

7982
public function testNormalizeWithInvalidObject(): void
8083
{
81-
$normalizer = new SubscriberNormalizer();
84+
$normalizer = new SubscriberNormalizer(new SubscriberListNormalizer());
8285
$this->assertSame([], $normalizer->normalize(new stdClass()));
8386
}
8487
}

tests/Unit/Subscription/Serializer/SubscriptionNormalizerTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use PhpList\Core\Domain\Subscription\Model\SubscriberList;
1010
use PhpList\Core\Domain\Subscription\Model\Subscription;
1111
use PhpList\RestBundle\Subscription\Serializer\SubscriberListNormalizer;
12-
use PhpList\RestBundle\Subscription\Serializer\SubscriberNormalizer;
12+
use PhpList\RestBundle\Subscription\Serializer\SubscriberOnlyNormalizer;
1313
use PhpList\RestBundle\Subscription\Serializer\SubscriptionNormalizer;
1414
use PHPUnit\Framework\TestCase;
1515

@@ -18,7 +18,7 @@ class SubscriptionNormalizerTest extends TestCase
1818
public function testSupportsNormalization(): void
1919
{
2020
$normalizer = new SubscriptionNormalizer(
21-
$this->createMock(SubscriberNormalizer::class),
21+
$this->createMock(SubscriberOnlyNormalizer::class),
2222
$this->createMock(SubscriberListNormalizer::class)
2323
);
2424

@@ -38,7 +38,7 @@ public function testNormalize(): void
3838
$subscription->method('getSubscriberList')->willReturn($subscriberList);
3939
$subscription->method('getCreatedAt')->willReturn($subscriptionDate);
4040

41-
$subscriberNormalizer = $this->createMock(SubscriberNormalizer::class);
41+
$subscriberNormalizer = $this->createMock(SubscriberOnlyNormalizer::class);
4242
$subscriberListNormalizer = $this->createMock(SubscriberListNormalizer::class);
4343

4444
$subscriberNormalizer->method('normalize')->with($subscriber)->willReturn(['subscriber_data']);
@@ -58,7 +58,7 @@ public function testNormalize(): void
5858
public function testNormalizeWithInvalidObjectReturnsEmptyArray(): void
5959
{
6060
$normalizer = new SubscriptionNormalizer(
61-
$this->createMock(SubscriberNormalizer::class),
61+
$this->createMock(SubscriberOnlyNormalizer::class),
6262
$this->createMock(SubscriberListNormalizer::class)
6363
);
6464

0 commit comments

Comments
 (0)