Skip to content

Commit 915df7e

Browse files
committed
ISSUE-345: message repository
1 parent 58b548e commit 915df7e

File tree

9 files changed

+185
-22
lines changed

9 files changed

+185
-22
lines changed

config/repositories.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,8 @@ services:
2525
parent: PhpList\Core\Domain\Repository
2626
arguments:
2727
- PhpList\Core\Domain\Model\Subscription\Subscription
28+
29+
PhpList\Core\Domain\Repository\Messaging\MessageRepository:
30+
parent: PhpList\Core\Domain\Repository
31+
arguments:
32+
- PPhpList\Core\Domain\Model\Messaging\Message

src/Domain/Model/Messaging/Message/Message.php renamed to src/Domain/Model/Messaging/Message.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@
22

33
declare(strict_types=1);
44

5-
namespace PhpList\Core\Domain\Model\Messaging\Message;
5+
namespace PhpList\Core\Domain\Model\Messaging;
66

7-
use DateTime;
87
use Doctrine\ORM\Mapping as ORM;
8+
use PhpList\Core\Domain\Model\Identity\Administrator;
99
use PhpList\Core\Domain\Model\Interfaces\DomainModel;
1010
use PhpList\Core\Domain\Model\Interfaces\Identity;
1111
use PhpList\Core\Domain\Model\Interfaces\ModificationDate;
12+
use PhpList\Core\Domain\Model\Messaging\Message\MessageContent;
13+
use PhpList\Core\Domain\Model\Messaging\Message\MessageFormat;
14+
use PhpList\Core\Domain\Model\Messaging\Message\MessageMetadata;
15+
use PhpList\Core\Domain\Model\Messaging\Message\MessageOptions;
16+
use PhpList\Core\Domain\Model\Messaging\Message\MessageSchedule;
1217
use PhpList\Core\Domain\Model\Traits\IdentityTrait;
1318
use PhpList\Core\Domain\Model\Traits\ModificationDateTrait;
1419
use PhpList\Core\Domain\Repository\Messaging\MessageRepository;
@@ -22,37 +27,43 @@ class Message implements DomainModel, Identity, ModificationDate
2227
use IdentityTrait;
2328
use ModificationDateTrait;
2429

25-
#[ORM\Embedded(class: 'MessageFormat')]
30+
#[ORM\Embedded(class: MessageFormat::class)]
2631
private MessageFormat $format;
2732

28-
#[ORM\Embedded(class: 'MessageSchedule')]
33+
#[ORM\Embedded(class: MessageSchedule::class)]
2934
private MessageSchedule $schedule;
3035

31-
#[ORM\Embedded(class: 'MessageMetadata')]
36+
#[ORM\Embedded(class: MessageMetadata::class)]
3237
private MessageMetadata $metadata;
3338

34-
#[ORM\Embedded(class: 'MessageContent')]
39+
#[ORM\Embedded(class: MessageContent::class)]
3540
private MessageContent $content;
3641

37-
#[ORM\Embedded(class: 'MessageOptions')]
42+
#[ORM\Embedded(class: MessageOptions::class)]
3843
private MessageOptions $options;
3944

4045
#[ORM\Column(type: 'string', length: 36, nullable: true, options: ['default' => ''])]
4146
private ?string $uuid = '';
4247

48+
#[ORM\ManyToOne(targetEntity: Administrator::class)]
49+
#[ORM\JoinColumn(name: 'owner', referencedColumnName: 'id', nullable: true)]
50+
private ?Administrator $owner;
51+
4352
public function __construct(
4453
MessageFormat $format,
4554
MessageSchedule $schedule,
4655
MessageMetadata $metadata,
4756
MessageContent $content,
48-
MessageOptions $options
57+
MessageOptions $options,
58+
?Administrator $owner = null
4959
) {
5060
$this->format = $format;
5161
$this->schedule = $schedule;
5262
$this->metadata = $metadata;
5363
$this->content = $content;
5464
$this->options = $options;
5565
$this->uuid = bin2hex(random_bytes(18));
66+
$this->owner = $owner;
5667
}
5768

5869
public function getFormat(): MessageFormat
@@ -84,4 +95,9 @@ public function getUuid(): ?string
8495
{
8596
return $this->uuid;
8697
}
98+
99+
public function getOwner(): ?Administrator
100+
{
101+
return $this->owner;
102+
}
87103
}

src/Domain/Model/Messaging/Message/MessageFormat.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class MessageFormat
3030
#[ORM\Column(name: 'astextandpdf', type: 'integer', options: ['default' => 0])]
3131
private int $asTextAndPdf = 0;
3232

33-
public function __construct(bool $htmlFormatted, ?string $sendFormat)
33+
public function __construct(bool $htmlFormatted, ?string $sendFormat = null)
3434
{
3535
$this->htmlFormatted = $htmlFormatted;
3636
$this->sendFormat = $sendFormat;

src/Domain/Model/Messaging/Message/MessageOptions.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ class MessageOptions
3434
#[ORM\Column(name: 'rsstemplate', type: 'string', length: 100, nullable: true)]
3535
private ?string $rssTemplate;
3636

37-
#[ORM\Column(type: 'integer', nullable: true)]
38-
private ?int $owner;
39-
4037
public function __construct(
4138
string $fromField = '',
4239
string $toField = '',
@@ -45,8 +42,7 @@ public function __construct(
4542
?string $userSelection = null,
4643
?int $template = null,
4744
?DateTime $sendStart = null,
48-
?string $rssTemplate = null,
49-
?int $owner = null
45+
?string $rssTemplate = null
5046
) {
5147
$this->fromField = $fromField;
5248
$this->toField = $toField;
@@ -56,7 +52,6 @@ public function __construct(
5652
$this->template = $template;
5753
$this->sendStart = $sendStart;
5854
$this->rssTemplate = $rssTemplate;
59-
$this->owner = $owner;
6055
}
6156

6257
public function getFromField(): string
@@ -98,9 +93,4 @@ public function getRssTemplate(): ?string
9893
{
9994
return $this->rssTemplate;
10095
}
101-
102-
public function getOwner(): ?int
103-
{
104-
return $this->owner;
105-
}
10696
}

src/Domain/Model/Messaging/UserMessage.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use DateTime;
88
use Doctrine\ORM\Mapping as ORM;
99
use PhpList\Core\Domain\Model\Interfaces\DomainModel;
10-
use PhpList\Core\Domain\Model\Messaging\Message\Message;
1110
use PhpList\Core\Domain\Model\Subscription\Subscriber;
1211

1312
#[ORM\Entity]

src/Domain/Repository/Messaging/MessageRepository.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,18 @@
44

55
namespace PhpList\Core\Domain\Repository\Messaging;
66

7+
use PhpList\Core\Domain\Model\Messaging\Message;
78
use PhpList\Core\Domain\Repository\AbstractRepository;
89

910
class MessageRepository extends AbstractRepository
1011
{
12+
/** @return Message[] */
13+
public function getByOwnerId(int $ownerId): array
14+
{
15+
return $this->createQueryBuilder('m')
16+
->where('IDENTITY(m.owner) = :ownerId')
17+
->setParameter('ownerId', $ownerId)
18+
->getQuery()
19+
->getResult();
20+
}
1121
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\Core\Tests\Unit\Domain\Model\Messaging;
6+
7+
use DateTime;
8+
use PhpList\Core\Domain\Model\Identity\Administrator;
9+
use PhpList\Core\Domain\Model\Messaging\Message;
10+
use PhpList\Core\Domain\Model\Messaging\Message\MessageContent;
11+
use PhpList\Core\Domain\Model\Messaging\Message\MessageFormat;
12+
use PhpList\Core\Domain\Model\Messaging\Message\MessageMetadata;
13+
use PhpList\Core\Domain\Model\Messaging\Message\MessageOptions;
14+
use PhpList\Core\Domain\Model\Messaging\Message\MessageSchedule;
15+
use PhpList\Core\Domain\Model\Interfaces\DomainModel;
16+
use PhpList\Core\Domain\Model\Interfaces\Identity;
17+
use PhpList\Core\Domain\Model\Interfaces\ModificationDate;
18+
use PHPUnit\Framework\TestCase;
19+
20+
class MessageTest extends TestCase
21+
{
22+
private Message $message;
23+
private MessageFormat $format;
24+
private MessageSchedule $schedule;
25+
private MessageMetadata $metadata;
26+
private MessageContent $content;
27+
private MessageOptions $options;
28+
private Administrator $owner;
29+
30+
protected function setUp(): void
31+
{
32+
$this->format = new MessageFormat(true);
33+
$this->schedule = new MessageSchedule(1, new DateTime(), 2, new DateTime());
34+
$this->metadata = new MessageMetadata();
35+
$this->content = new MessageContent('This is the body');
36+
$this->options = new MessageOptions();
37+
$this->owner = new Administrator();
38+
39+
$this->message = new Message(
40+
$this->format,
41+
$this->schedule,
42+
$this->metadata,
43+
$this->content,
44+
$this->options,
45+
$this->owner
46+
);
47+
}
48+
49+
public function testIsDomainModel(): void
50+
{
51+
self::assertInstanceOf(DomainModel::class, $this->message);
52+
self::assertInstanceOf(Identity::class, $this->message);
53+
self::assertInstanceOf(ModificationDate::class, $this->message);
54+
}
55+
56+
public function testUuidIsGenerated(): void
57+
{
58+
$uuid = $this->message->getUuid();
59+
self::assertNotEmpty($uuid);
60+
self::assertMatchesRegularExpression('/^[a-f0-9]{36}$/', $uuid);
61+
}
62+
63+
public function testGetFormat(): void
64+
{
65+
self::assertSame($this->format, $this->message->getFormat());
66+
}
67+
68+
public function testGetSchedule(): void
69+
{
70+
self::assertSame($this->schedule, $this->message->getSchedule());
71+
}
72+
73+
public function testGetMetadata(): void
74+
{
75+
self::assertSame($this->metadata, $this->message->getMetadata());
76+
}
77+
78+
public function testGetContent(): void
79+
{
80+
self::assertSame($this->content, $this->message->getContent());
81+
}
82+
83+
public function testGetOptions(): void
84+
{
85+
self::assertSame($this->options, $this->message->getOptions());
86+
}
87+
88+
public function testGetOwner(): void
89+
{
90+
self::assertSame($this->owner, $this->message->getOwner());
91+
}
92+
93+
public function testGetOwnerInitiallyNull(): void
94+
{
95+
$message = new Message(
96+
$this->format,
97+
$this->schedule,
98+
$this->metadata,
99+
$this->content,
100+
$this->options,
101+
null
102+
);
103+
104+
self::assertNull($message->getOwner());
105+
}
106+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\Core\Tests\Unit\Domain\Repository\Messaging;
6+
7+
use Doctrine\ORM\EntityManager;
8+
use Doctrine\ORM\EntityRepository;
9+
use Doctrine\ORM\Mapping\ClassMetadata;
10+
use PhpList\Core\Domain\Repository\Messaging\MessageRepository;
11+
use PHPUnit\Framework\TestCase;
12+
13+
/**
14+
* Testcase.
15+
*
16+
* @author Tatevik Grigoryan <[email protected]>
17+
*/
18+
class MessageRepositoryTest extends TestCase
19+
{
20+
private MessageRepository $subject;
21+
22+
protected function setUp(): void
23+
{
24+
$entityManager = $this->createMock(EntityManager::class);
25+
26+
$classMetadata = $this->createMock(ClassMetadata::class);
27+
$classMetadata->name = MessageRepository::class;
28+
29+
$this->subject = new MessageRepository($entityManager, $classMetadata);
30+
}
31+
32+
public function testClassIsEntityRepository(): void
33+
{
34+
self::assertInstanceOf(EntityRepository::class, $this->subject);
35+
}
36+
}

tests/Unit/Domain/Repository/Messaging/SubscriberListRepositoryTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Doctrine\ORM\EntityManager;
88
use Doctrine\ORM\EntityRepository;
99
use Doctrine\ORM\Mapping\ClassMetadata;
10+
use PhpList\Core\Domain\Model\Subscription\SubscriberList;
1011
use PhpList\Core\Domain\Repository\Subscription\SubscriberListRepository;
1112
use PHPUnit\Framework\TestCase;
1213

@@ -24,7 +25,7 @@ protected function setUp(): void
2425
$entityManager = $this->createMock(EntityManager::class);
2526

2627
$classMetadata = $this->createMock(ClassMetadata::class);
27-
$classMetadata->name = 'PhpList\Core\Domain\Model\Subscription\SubscriberList';
28+
$classMetadata->name = SubscriberList::class;
2829

2930
$this->subject = new SubscriberListRepository($entityManager, $classMetadata);
3031
}

0 commit comments

Comments
 (0)