Skip to content

Commit 9284660

Browse files
committed
BounceManager
1 parent 9d0c1db commit 9284660

File tree

4 files changed

+178
-0
lines changed

4 files changed

+178
-0
lines changed

config/services/managers.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ services:
7272
autowire: true
7373
autoconfigure: true
7474

75+
PhpList\Core\Domain\Messaging\Service\Manager\BounceManager:
76+
autowire: true
77+
autoconfigure: true
78+
7579
PhpList\Core\Domain\Messaging\Service\Manager\ListMessageManager:
7680
autowire: true
7781
autoconfigure: true

config/services/repositories.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,8 @@ services:
135135
parent: PhpList\Core\Domain\Common\Repository\AbstractRepository
136136
arguments:
137137
- PhpList\Core\Domain\Messaging\Model\BounceRegex
138+
139+
PhpList\Core\Domain\Messaging\Repository\BounceRepository:
140+
parent: PhpList\Core\Domain\Common\Repository\AbstractRepository
141+
arguments:
142+
- PhpList\Core\Domain\Messaging\Model\Bounce
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\Core\Domain\Messaging\Service\Manager;
6+
7+
use DateTime;
8+
use PhpList\Core\Domain\Messaging\Model\Bounce;
9+
use PhpList\Core\Domain\Messaging\Repository\BounceRepository;
10+
11+
class BounceManager
12+
{
13+
private BounceRepository $bounceRepository;
14+
15+
public function __construct(BounceRepository $bounceRepository)
16+
{
17+
$this->bounceRepository = $bounceRepository;
18+
}
19+
20+
public function create(
21+
?DateTime $date = null,
22+
?string $header = null,
23+
?string $data = null,
24+
?string $status = null,
25+
?string $comment = null
26+
): Bounce {
27+
$bounce = new Bounce(
28+
date: $date,
29+
header: $header,
30+
data: $data,
31+
status: $status,
32+
comment: $comment
33+
);
34+
35+
$this->bounceRepository->save($bounce);
36+
37+
return $bounce;
38+
}
39+
40+
public function save(Bounce $bounce): void
41+
{
42+
$this->bounceRepository->save($bounce);
43+
}
44+
45+
public function delete(Bounce $bounce): void
46+
{
47+
$this->bounceRepository->remove($bounce);
48+
}
49+
50+
/** @return Bounce[] */
51+
public function getAll(): array
52+
{
53+
return $this->bounceRepository->findAll();
54+
}
55+
56+
public function getById(int $id): ?Bounce
57+
{
58+
/** @var Bounce|null $found */
59+
$found = $this->bounceRepository->find($id);
60+
return $found;
61+
}
62+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\Core\Tests\Unit\Domain\Messaging\Service\Manager;
6+
7+
use DateTime;
8+
use PhpList\Core\Domain\Messaging\Model\Bounce;
9+
use PhpList\Core\Domain\Messaging\Repository\BounceRepository;
10+
use PhpList\Core\Domain\Messaging\Service\Manager\BounceManager;
11+
use PHPUnit\Framework\MockObject\MockObject;
12+
use PHPUnit\Framework\TestCase;
13+
14+
class BounceManagerTest extends TestCase
15+
{
16+
private BounceRepository&MockObject $repository;
17+
private BounceManager $manager;
18+
19+
protected function setUp(): void
20+
{
21+
$this->repository = $this->createMock(BounceRepository::class);
22+
$this->manager = new BounceManager($this->repository);
23+
}
24+
25+
public function testCreatePersistsAndReturnsBounce(): void
26+
{
27+
$date = new DateTime('2020-01-01 00:00:00');
28+
$header = 'X-Test: Header';
29+
$data = 'raw bounce';
30+
$status = 'new';
31+
$comment = 'created by test';
32+
33+
$this->repository->expects($this->once())
34+
->method('save')
35+
->with($this->isInstanceOf(Bounce::class));
36+
37+
$bounce = $this->manager->create(
38+
date: $date,
39+
header: $header,
40+
data: $data,
41+
status: $status,
42+
comment: $comment
43+
);
44+
45+
$this->assertInstanceOf(Bounce::class, $bounce);
46+
$this->assertSame($date, $bounce->getDate());
47+
$this->assertSame($header, $bounce->getHeader());
48+
$this->assertSame($data, $bounce->getData());
49+
$this->assertSame($status, $bounce->getStatus());
50+
$this->assertSame($comment, $bounce->getComment());
51+
}
52+
53+
public function testSaveDelegatesToRepository(): void
54+
{
55+
$model = new Bounce();
56+
57+
$this->repository->expects($this->once())
58+
->method('save')
59+
->with($model);
60+
61+
$this->manager->save($model);
62+
}
63+
64+
public function testDeleteDelegatesToRepository(): void
65+
{
66+
$model = new Bounce();
67+
68+
$this->repository->expects($this->once())
69+
->method('remove')
70+
->with($model);
71+
72+
$this->manager->delete($model);
73+
}
74+
75+
public function testGetAllReturnsArray(): void
76+
{
77+
$expected = [new Bounce(), new Bounce()];
78+
79+
$this->repository->expects($this->once())
80+
->method('findAll')
81+
->willReturn($expected);
82+
83+
$this->assertSame($expected, $this->manager->getAll());
84+
}
85+
86+
public function testGetByIdReturnsBounce(): void
87+
{
88+
$expected = new Bounce();
89+
90+
$this->repository->expects($this->once())
91+
->method('find')
92+
->with(123)
93+
->willReturn($expected);
94+
95+
$this->assertSame($expected, $this->manager->getById(123));
96+
}
97+
98+
public function testGetByIdReturnsNullWhenNotFound(): void
99+
{
100+
$this->repository->expects($this->once())
101+
->method('find')
102+
->with(999)
103+
->willReturn(null);
104+
105+
$this->assertNull($this->manager->getById(999));
106+
}
107+
}

0 commit comments

Comments
 (0)