Skip to content

Commit e2fb0d4

Browse files
committed
ISSUE-345: add unit tests
1 parent 1173da8 commit e2fb0d4

16 files changed

+902
-4
lines changed

tests/Integration/Controller/SessionControllerTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,39 @@ public function testDeleteSessionWithCurrentSessionAndOwnSessionKeyKeepsSession(
193193

194194
self::assertNotNull($this->administratorTokenRepository->find(3));
195195
}
196+
197+
public function testPostSessionWithExtraFieldsIsIgnored(): void
198+
{
199+
$this->loadFixtures([AdministratorFixture::class]);
200+
201+
$jsonData = json_encode([
202+
'loginName' => 'john.doe',
203+
'password' => 'Bazinga!',
204+
'extraField' => 'ignore_me'
205+
]);
206+
207+
$this->jsonRequest('post', '/api/v2/sessions', [], [], [], $jsonData);
208+
209+
$this->assertHttpCreated();
210+
$response = $this->getDecodedJsonResponseContent();
211+
self::assertArrayNotHasKey('extraField', $response);
212+
}
213+
214+
public function testDeleteSessionWithInvalidFormatIdReturns404(): void
215+
{
216+
$this->authenticatedJsonRequest('DELETE', '/api/v2/sessions/not-an-id');
217+
$this->assertHttpNotFound();
218+
}
219+
220+
public function testPostSessionWithWrongHttpMethodReturns405(): void
221+
{
222+
self::getClient()->request('PUT', '/api/v2/sessions');
223+
$this->assertHttpMethodNotAllowed();
224+
}
225+
226+
public function testDeleteSessionWithNoSuchSessionReturns404(): void
227+
{
228+
$this->authenticatedJsonRequest('DELETE', '/api/v2/sessions/999999');
229+
$this->assertHttpNotFound();
230+
}
196231
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\RestBundle\Tests\Unit\Serializer;
6+
7+
use DateTime;
8+
use PhpList\Core\Domain\Model\Identity\AdministratorToken;
9+
use PhpList\RestBundle\Serializer\AdministratorTokenNormalizer;
10+
use PHPUnit\Framework\TestCase;
11+
12+
class AdministratorTokenNormalizerTest extends TestCase
13+
{
14+
public function testSupportsNormalization(): void
15+
{
16+
$normalizer = new AdministratorTokenNormalizer();
17+
$token = $this->createMock(AdministratorToken::class);
18+
19+
$this->assertTrue($normalizer->supportsNormalization($token));
20+
$this->assertFalse($normalizer->supportsNormalization(AdministratorToken::class));
21+
}
22+
23+
public function testNormalize(): void
24+
{
25+
$expiry = new DateTime('2025-01-01T12:00:00+00:00');
26+
27+
$token = $this->createMock(AdministratorToken::class);
28+
$token->method('getId')->willReturn(42);
29+
$token->method('getKey')->willReturn('abcdef123456');
30+
$token->method('getExpiry')->willReturn($expiry);
31+
32+
$normalizer = new AdministratorTokenNormalizer();
33+
34+
$expected = [
35+
'id' => 42,
36+
'key' => 'abcdef123456',
37+
'expiry_date' => '2025-01-01T12:00:00+00:00'
38+
];
39+
40+
$this->assertSame($expected, $normalizer->normalize($token));
41+
}
42+
43+
public function testNormalizeWithInvalidObjectReturnsEmptyArray(): void
44+
{
45+
$normalizer = new AdministratorTokenNormalizer();
46+
$this->assertSame([], $normalizer->normalize(AdministratorToken::class));
47+
}
48+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\RestBundle\Tests\Unit\Serializer;
6+
7+
use DateTime;
8+
use PhpList\Core\Domain\Model\Messaging\Message;
9+
use PhpList\Core\Domain\Model\Messaging\Message\MessageContent;
10+
use PhpList\Core\Domain\Model\Messaging\Message\MessageFormat;
11+
use PhpList\Core\Domain\Model\Messaging\Message\MessageMetadata;
12+
use PhpList\Core\Domain\Model\Messaging\Message\MessageOptions;
13+
use PhpList\Core\Domain\Model\Messaging\Message\MessageSchedule;
14+
use PhpList\Core\Domain\Model\Messaging\Template;
15+
use PhpList\RestBundle\Serializer\MessageNormalizer;
16+
use PHPUnit\Framework\TestCase;
17+
18+
class MessageNormalizerTest extends TestCase
19+
{
20+
public function testSupportsNormalization(): void
21+
{
22+
$normalizer = new MessageNormalizer();
23+
24+
$message = $this->createMock(Message::class);
25+
$this->assertTrue($normalizer->supportsNormalization($message));
26+
$this->assertFalse($normalizer->supportsNormalization(new \stdClass()));
27+
}
28+
29+
public function testNormalizeReturnsExpectedArray(): void
30+
{
31+
$template = $this->createConfiguredMock(Template::class, [
32+
'getId' => 5,
33+
'getTitle' => 'Test Template',
34+
'getTemplate' => '<html>Hello</html>',
35+
'getTemplateText' => 'Hello',
36+
'getListOrder' => 1,
37+
]);
38+
39+
$content = new MessageContent('Subject', 'Text', 'TextMsg', 'Footer');
40+
$format = new MessageFormat(true, 'html');
41+
$format->setFormatOptions(['text', 'html']);
42+
43+
$entered = new DateTime('2025-01-01T10:00:00+00:00');
44+
$sent = new DateTime('2025-01-02T10:00:00+00:00');
45+
46+
$metadata = new MessageMetadata('draft');
47+
$metadata->setProcessed(true);
48+
$metadata->setViews(10);
49+
$metadata->setBounceCount(3);
50+
$metadata->setEntered($entered);
51+
$metadata->setSent($sent);
52+
53+
$schedule = new MessageSchedule(
54+
24,
55+
new DateTime('2025-01-10T00:00:00+00:00'),
56+
12,
57+
new DateTime('2025-01-05T00:00:00+00:00'),
58+
new DateTime('2025-01-01T00:00:00+00:00')
59+
);
60+
61+
$options = new MessageOptions('[email protected]', '[email protected]', '[email protected]', 'group');
62+
63+
$message = $this->createMock(Message::class);
64+
$message->method('getId')->willReturn(1);
65+
$message->method('getUuid')->willReturn('uuid-123');
66+
$message->method('getTemplate')->willReturn($template);
67+
$message->method('getContent')->willReturn($content);
68+
$message->method('getFormat')->willReturn($format);
69+
$message->method('getMetadata')->willReturn($metadata);
70+
$message->method('getSchedule')->willReturn($schedule);
71+
$message->method('getOptions')->willReturn($options);
72+
73+
$normalizer = new MessageNormalizer();
74+
$result = $normalizer->normalize($message);
75+
76+
$this->assertSame(1, $result['id']);
77+
$this->assertSame('uuid-123', $result['unique_id']);
78+
$this->assertSame('Test Template', $result['template']['title']);
79+
$this->assertSame('Subject', $result['message_content']['subject']);
80+
$this->assertSame(['text', 'html'], $result['message_format']['format_options']);
81+
$this->assertSame('draft', $result['message_metadata']['status']);
82+
$this->assertSame('[email protected]', $result['message_options']['from_field']);
83+
}
84+
85+
public function testNormalizeWithInvalidObjectReturnsEmptyArray(): void
86+
{
87+
$normalizer = new MessageNormalizer();
88+
$this->assertSame([], $normalizer->normalize(new \stdClass()));
89+
}
90+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\RestBundle\Tests\Unit\Serializer;
6+
7+
use DateTime;
8+
use PhpList\Core\Domain\Model\Subscription\SubscriberList;
9+
use PhpList\RestBundle\Serializer\SubscriberListNormalizer;
10+
use PHPUnit\Framework\TestCase;
11+
12+
class SubscriberListNormalizerTest extends TestCase
13+
{
14+
public function testSupportsNormalization(): void
15+
{
16+
$normalizer = new SubscriberListNormalizer();
17+
18+
$subscriberList = $this->createMock(SubscriberList::class);
19+
$this->assertTrue($normalizer->supportsNormalization($subscriberList));
20+
$this->assertFalse($normalizer->supportsNormalization(new \stdClass()));
21+
}
22+
23+
public function testNormalize(): void
24+
{
25+
$mock = $this->createMock(SubscriberList::class);
26+
$mock->method('getId')->willReturn(101);
27+
$mock->method('getName')->willReturn('Tech News');
28+
$mock->method('getCreationDate')->willReturn(new DateTime('2025-04-01T10:00:00+00:00'));
29+
$mock->method('getDescription')->willReturn('All tech updates');
30+
$mock->method('getListPosition')->willReturn(2);
31+
$mock->method('getSubjectPrefix')->willReturn('tech');
32+
$mock->method('isPublic')->willReturn(true);
33+
$mock->method('getCategory')->willReturn('technology');
34+
35+
$normalizer = new SubscriberListNormalizer();
36+
$result = $normalizer->normalize($mock);
37+
38+
$this->assertSame([
39+
'id' => 101,
40+
'name' => 'Tech News',
41+
'creation_date' => '2025-04-01T10:00:00+00:00',
42+
'description' => 'All tech updates',
43+
'list_position' => 2,
44+
'subject_prefix' => 'tech',
45+
'public' => true,
46+
'category' => 'technology',
47+
], $result);
48+
}
49+
50+
public function testNormalizeWithInvalidObject(): void
51+
{
52+
$normalizer = new SubscriberListNormalizer();
53+
$this->assertSame([], $normalizer->normalize(new \stdClass()));
54+
}
55+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\RestBundle\Tests\Unit\Serializer;
6+
7+
use DateTime;
8+
use PhpList\Core\Domain\Model\Subscription\Subscriber;
9+
use PhpList\Core\Domain\Model\Subscription\SubscriberList;
10+
use PhpList\Core\Domain\Model\Subscription\Subscription;
11+
use PhpList\RestBundle\Serializer\SubscriberNormalizer;
12+
use PHPUnit\Framework\TestCase;
13+
use Doctrine\Common\Collections\ArrayCollection;
14+
use stdClass;
15+
16+
class SubscriberNormalizerTest extends TestCase
17+
{
18+
public function testSupportsNormalization(): void
19+
{
20+
$normalizer = new SubscriberNormalizer();
21+
$subscriber = $this->createMock(Subscriber::class);
22+
23+
$this->assertTrue($normalizer->supportsNormalization($subscriber));
24+
$this->assertFalse($normalizer->supportsNormalization(new stdClass()));
25+
}
26+
27+
public function testNormalize(): void
28+
{
29+
$subscriberList = $this->createMock(SubscriberList::class);
30+
$subscriberList->method('getId')->willReturn(1);
31+
$subscriberList->method('getName')->willReturn('News');
32+
$subscriberList->method('getDescription')->willReturn('Latest news');
33+
$subscriberList->method('getCreationDate')->willReturn(new DateTime('2025-01-01T00:00:00+00:00'));
34+
$subscriberList->method('isPublic')->willReturn(true);
35+
36+
$subscription = $this->createMock(Subscription::class);
37+
$subscription->method('getSubscriberList')->willReturn($subscriberList);
38+
$subscription->method('getCreationDate')->willReturn(new DateTime('2025-01-10T00:00:00+00:00'));
39+
40+
$subscriber = $this->createMock(Subscriber::class);
41+
$subscriber->method('getId')->willReturn(101);
42+
$subscriber->method('getEmail')->willReturn('[email protected]');
43+
$subscriber->method('getCreationDate')->willReturn(new DateTime('2024-12-31T12:00:00+00:00'));
44+
$subscriber->method('isConfirmed')->willReturn(true);
45+
$subscriber->method('isBlacklisted')->willReturn(false);
46+
$subscriber->method('getBounceCount')->willReturn(0);
47+
$subscriber->method('getUniqueId')->willReturn('abc123');
48+
$subscriber->method('hasHtmlEmail')->willReturn(true);
49+
$subscriber->method('isDisabled')->willReturn(false);
50+
$subscriber->method('getSubscriptions')->willReturn(new ArrayCollection([$subscription]));
51+
52+
$normalizer = new SubscriberNormalizer();
53+
54+
$expected = [
55+
'id' => 101,
56+
'email' => '[email protected]',
57+
'creation_date' => '2024-12-31T12:00:00+00:00',
58+
'confirmed' => true,
59+
'blacklisted' => false,
60+
'bounce_count' => 0,
61+
'unique_id' => 'abc123',
62+
'html_email' => true,
63+
'disabled' => false,
64+
'subscribed_lists' => [
65+
[
66+
'id' => 1,
67+
'name' => 'News',
68+
'description' => 'Latest news',
69+
'creation_date' => '2025-01-01T00:00:00+00:00',
70+
'public' => true,
71+
'subscription_date' => '2025-01-10T00:00:00+00:00'
72+
]
73+
]
74+
];
75+
76+
$this->assertSame($expected, $normalizer->normalize($subscriber));
77+
}
78+
79+
public function testNormalizeWithInvalidObject(): void
80+
{
81+
$normalizer = new SubscriberNormalizer();
82+
$this->assertSame([], $normalizer->normalize(new stdClass()));
83+
}
84+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\RestBundle\Tests\Unit\Serializer;
6+
7+
use DateTime;
8+
use PhpList\Core\Domain\Model\Subscription\Subscriber;
9+
use PhpList\Core\Domain\Model\Subscription\SubscriberList;
10+
use PhpList\Core\Domain\Model\Subscription\Subscription;
11+
use PhpList\RestBundle\Serializer\SubscriberListNormalizer;
12+
use PhpList\RestBundle\Serializer\SubscriberNormalizer;
13+
use PhpList\RestBundle\Serializer\SubscriptionNormalizer;
14+
use PHPUnit\Framework\TestCase;
15+
16+
class SubscriptionNormalizerTest extends TestCase
17+
{
18+
public function testSupportsNormalization(): void
19+
{
20+
$normalizer = new SubscriptionNormalizer(
21+
$this->createMock(SubscriberNormalizer::class),
22+
$this->createMock(SubscriberListNormalizer::class)
23+
);
24+
25+
$subscription = $this->createMock(Subscription::class);
26+
$this->assertTrue($normalizer->supportsNormalization($subscription));
27+
$this->assertFalse($normalizer->supportsNormalization(new \stdClass()));
28+
}
29+
30+
public function testNormalize(): void
31+
{
32+
$subscriber = $this->createMock(Subscriber::class);
33+
$subscriberList = $this->createMock(SubscriberList::class);
34+
$subscriptionDate = new DateTime('2025-01-01T12:00:00+00:00');
35+
36+
$subscription = $this->createMock(Subscription::class);
37+
$subscription->method('getSubscriber')->willReturn($subscriber);
38+
$subscription->method('getSubscriberList')->willReturn($subscriberList);
39+
$subscription->method('getCreationDate')->willReturn($subscriptionDate);
40+
41+
$subscriberNormalizer = $this->createMock(SubscriberNormalizer::class);
42+
$subscriberListNormalizer = $this->createMock(SubscriberListNormalizer::class);
43+
44+
$subscriberNormalizer->method('normalize')->with($subscriber)->willReturn(['subscriber_data']);
45+
$subscriberListNormalizer->method('normalize')->with($subscriberList)->willReturn(['list_data']);
46+
47+
$normalizer = new SubscriptionNormalizer($subscriberNormalizer, $subscriberListNormalizer);
48+
49+
$result = $normalizer->normalize($subscription);
50+
51+
$this->assertSame([
52+
'subscriber' => ['subscriber_data'],
53+
'subscriber_list' => ['list_data'],
54+
'subscription_date' => '2025-01-01T12:00:00+00:00',
55+
], $result);
56+
}
57+
58+
public function testNormalizeWithInvalidObjectReturnsEmptyArray(): void
59+
{
60+
$normalizer = new SubscriptionNormalizer(
61+
$this->createMock(SubscriberNormalizer::class),
62+
$this->createMock(SubscriberListNormalizer::class)
63+
);
64+
65+
$this->assertSame([], $normalizer->normalize(new \stdClass()));
66+
}
67+
}

0 commit comments

Comments
 (0)