Skip to content

Commit 1a75cd4

Browse files
committed
test
1 parent e91742c commit 1a75cd4

File tree

1 file changed

+233
-0
lines changed

1 file changed

+233
-0
lines changed
Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\Core\Tests\Unit\Domain\Subscription\Service\Manager;
6+
7+
use Doctrine\ORM\EntityManagerInterface;
8+
use PhpList\Core\Domain\Identity\Model\Administrator;
9+
use PhpList\Core\Domain\Subscription\Model\SubscribePage;
10+
use PhpList\Core\Domain\Subscription\Model\SubscribePageData;
11+
use PhpList\Core\Domain\Subscription\Repository\SubscriberPageDataRepository;
12+
use PhpList\Core\Domain\Subscription\Repository\SubscriberPageRepository;
13+
use PhpList\Core\Domain\Subscription\Service\Manager\SubscribePageManager;
14+
use PHPUnit\Framework\MockObject\MockObject;
15+
use PHPUnit\Framework\TestCase;
16+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
17+
18+
class SubscribePageManagerTest extends TestCase
19+
{
20+
private SubscriberPageRepository|MockObject $pageRepository;
21+
private SubscriberPageDataRepository|MockObject $pageDataRepository;
22+
private EntityManagerInterface|MockObject $entityManager;
23+
private SubscribePageManager $manager;
24+
25+
protected function setUp(): void
26+
{
27+
$this->pageRepository = $this->createMock(SubscriberPageRepository::class);
28+
$this->pageDataRepository = $this->createMock(SubscriberPageDataRepository::class);
29+
$this->entityManager = $this->createMock(EntityManagerInterface::class);
30+
31+
$this->manager = new SubscribePageManager(
32+
pageRepository: $this->pageRepository,
33+
pageDataRepository: $this->pageDataRepository,
34+
entityManager: $this->entityManager,
35+
);
36+
}
37+
38+
public function testCreatePageCreatesAndSaves(): void
39+
{
40+
$owner = new Administrator();
41+
$this->pageRepository
42+
->expects($this->once())
43+
->method('save')
44+
->with($this->isInstanceOf(SubscribePage::class));
45+
46+
$page = $this->manager->createPage('My Page', true, $owner);
47+
48+
$this->assertInstanceOf(SubscribePage::class, $page);
49+
$this->assertSame('My Page', $page->getTitle());
50+
$this->assertTrue($page->isActive());
51+
$this->assertSame($owner, $page->getOwner());
52+
}
53+
54+
public function testGetPageReturnsPage(): void
55+
{
56+
$page = new SubscribePage();
57+
$this->pageRepository
58+
->expects($this->once())
59+
->method('find')
60+
->with(123)
61+
->willReturn($page);
62+
63+
$result = $this->manager->getPage(123);
64+
65+
$this->assertSame($page, $result);
66+
}
67+
68+
public function testGetPageThrowsWhenNotFound(): void
69+
{
70+
$this->pageRepository
71+
->expects($this->once())
72+
->method('find')
73+
->with(999)
74+
->willReturn(null);
75+
76+
$this->expectException(NotFoundHttpException::class);
77+
$this->expectExceptionMessage('Subscribe page not found');
78+
79+
$this->manager->getPage(999);
80+
}
81+
82+
public function testUpdatePageUpdatesProvidedFieldsAndFlushes(): void
83+
{
84+
$originalOwner = new Administrator();
85+
$newOwner = new Administrator();
86+
$page = (new SubscribePage())
87+
->setTitle('Old Title')
88+
->setActive(false)
89+
->setOwner($originalOwner);
90+
91+
$this->entityManager
92+
->expects($this->once())
93+
->method('flush');
94+
95+
$updated = $this->manager->updatePage($page, title: 'New Title', active: true, owner: $newOwner);
96+
97+
$this->assertSame($page, $updated);
98+
$this->assertSame('New Title', $updated->getTitle());
99+
$this->assertTrue($updated->isActive());
100+
$this->assertSame($newOwner, $updated->getOwner());
101+
}
102+
103+
public function testUpdatePageLeavesNullFieldsUntouched(): void
104+
{
105+
$owner = new Administrator();
106+
$page = (new SubscribePage())
107+
->setTitle('Keep Title')
108+
->setActive(true)
109+
->setOwner($owner);
110+
111+
$this->entityManager
112+
->expects($this->once())
113+
->method('flush');
114+
115+
$updated = $this->manager->updatePage(page: $page, title: null, active: null, owner: null);
116+
117+
$this->assertSame('Keep Title', $updated->getTitle());
118+
$this->assertTrue($updated->isActive());
119+
$this->assertSame($owner, $updated->getOwner());
120+
}
121+
122+
public function testSetActiveSetsFlagAndFlushes(): void
123+
{
124+
$page = (new SubscribePage())
125+
->setTitle('Any')
126+
->setActive(false);
127+
128+
$this->entityManager
129+
->expects($this->once())
130+
->method('flush');
131+
132+
$this->manager->setActive($page, true);
133+
$this->assertTrue($page->isActive());
134+
}
135+
136+
public function testDeletePageCallsRepositoryRemove(): void
137+
{
138+
$page = new SubscribePage();
139+
140+
$this->pageRepository
141+
->expects($this->once())
142+
->method('remove')
143+
->with($page);
144+
145+
$this->manager->deletePage($page);
146+
}
147+
148+
public function testGetPageDataReturnsStringWhenFound(): void
149+
{
150+
$page = new SubscribePage();
151+
$data = $this->createMock(SubscribePageData::class);
152+
$data->expects($this->once())->method('getData')->willReturn('value');
153+
154+
$this->pageDataRepository
155+
->expects($this->once())
156+
->method('findByPageAndName')
157+
->with($page, 'key')
158+
->willReturn($data);
159+
160+
$result = $this->manager->getPageData($page, 'key');
161+
$this->assertSame('value', $result);
162+
}
163+
164+
public function testGetPageDataReturnsNullWhenNotFound(): void
165+
{
166+
$page = new SubscribePage();
167+
168+
$this->pageDataRepository
169+
->expects($this->once())
170+
->method('findByPageAndName')
171+
->with($page, 'missing')
172+
->willReturn(null);
173+
174+
$result = $this->manager->getPageData($page, 'missing');
175+
$this->assertNull($result);
176+
}
177+
178+
public function testSetPageDataUpdatesExistingDataAndFlushes(): void
179+
{
180+
$page = new SubscribePage();
181+
$existing = new SubscribePageData();
182+
$existing->setId(5)->setName('color')->setData('red');
183+
184+
$this->pageDataRepository
185+
->expects($this->once())
186+
->method('findByPageAndName')
187+
->with($page, 'color')
188+
->willReturn($existing);
189+
190+
$this->entityManager
191+
->expects($this->never())
192+
->method('persist');
193+
194+
$this->entityManager
195+
->expects($this->once())
196+
->method('flush');
197+
198+
$result = $this->manager->setPageData($page, 'color', 'blue');
199+
200+
$this->assertSame($existing, $result);
201+
$this->assertSame('blue', $result->getData());
202+
}
203+
204+
public function testSetPageDataCreatesNewWhenMissingAndPersistsAndFlushes(): void
205+
{
206+
$page = $this->getMockBuilder(SubscribePage::class)
207+
->onlyMethods(['getId'])
208+
->getMock();
209+
$page->method('getId')->willReturn(123);
210+
211+
$this->pageDataRepository
212+
->expects($this->once())
213+
->method('findByPageAndName')
214+
->with($page, 'greeting')
215+
->willReturn(null);
216+
217+
$this->entityManager
218+
->expects($this->once())
219+
->method('persist')
220+
->with($this->isInstanceOf(SubscribePageData::class));
221+
222+
$this->entityManager
223+
->expects($this->once())
224+
->method('flush');
225+
226+
$result = $this->manager->setPageData($page, 'greeting', 'hello');
227+
228+
$this->assertInstanceOf(SubscribePageData::class, $result);
229+
$this->assertSame(123, $result->getId());
230+
$this->assertSame('greeting', $result->getName());
231+
$this->assertSame('hello', $result->getData());
232+
}
233+
}

0 commit comments

Comments
 (0)