Skip to content

Commit ddfcf18

Browse files
author
thisismeonmounteverest
committed
Update tests to not create PHPUnit notices.
1 parent bcc96a5 commit ddfcf18

File tree

2 files changed

+23
-45
lines changed

2 files changed

+23
-45
lines changed

tests/Model/AboutModelTest.php

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,9 @@
1616

1717
class AboutModelTest extends TestCase
1818
{
19-
private $entityManager;
20-
private $mailer;
21-
private $aboutModel;
22-
23-
protected function setUp(): void
24-
{
25-
$this->entityManager = $this->createMock(EntityManagerInterface::class);
26-
$this->mailer = $this->createMock(Mailer::class);
27-
$this->aboutModel = new AboutModel($this->entityManager, $this->mailer);
28-
}
29-
3019
public function testGetFeedbackCategoriesReturnsResult(): void
3120
{
3221
// Stubbing the chain to return a specific result.
33-
// We don't care about the exact method calls on the query builder, mostly just that it returns the expected data.
3422
$expectedCategories = [new FeedbackCategory(), new FeedbackCategory()];
3523

3624
$query = $this->createStub(Query::class);
@@ -44,17 +32,22 @@ public function testGetFeedbackCategoriesReturnsResult(): void
4432
$qb->method('indexBy')->willReturnSelf();
4533
$qb->method('getQuery')->willReturn($query);
4634

47-
$this->entityManager->method('createQueryBuilder')->willReturn($qb);
35+
$mailer = $this->createStub(Mailer::class);
36+
$entityManager = $this->createStub(EntityManagerInterface::class);
37+
$entityManager->method('createQueryBuilder')->willReturn($qb);
4838

49-
$result = $this->aboutModel->getFeedbackCategories();
39+
$aboutModel = new AboutModel($entityManager, $mailer);
40+
$result = $aboutModel->getFeedbackCategories();
5041

5142
$this->assertSame($expectedCategories, $result);
5243
}
5344

5445
public function testSendFeedbackEmailTriggersMailer(): void
5546
{
5647
// Side-effect test: verify mailer is called.
57-
$this->mailer->expects($this->once())->method('sendFeedbackEmail');
48+
$mailer = $this->createMock(Mailer::class);
49+
$mailer->expects($this->once())->method('sendFeedbackEmail');
50+
$entityManager = $this->createStub(EntityManagerInterface::class);
5851

5952
$category = new FeedbackCategory();
6053
$category->setEmailtonotify('admin@example.com');
@@ -65,26 +58,30 @@ public function testSendFeedbackEmailTriggersMailer(): void
6558
'message' => 'hello',
6659
];
6760

68-
$this->aboutModel->sendFeedbackEmail($data);
61+
$aboutModel = new AboutModel($entityManager, $mailer);
62+
$aboutModel->sendFeedbackEmail($data);
6963
}
7064

7165
public function testAddFeedbackPersistsData(): void
7266
{
7367
// Side-effect test: verify persistence.
74-
$this->entityManager->expects($this->once())->method('persist')->with($this->isInstanceOf(Feedback::class));
75-
$this->entityManager->expects($this->once())->method('flush');
68+
$mailer = $this->createStub(Mailer::class);
69+
$entityManager = $this->createMock(EntityManagerInterface::class);
70+
$entityManager->expects($this->once())->method('persist')->with($this->isInstanceOf(Feedback::class));
71+
$entityManager->expects($this->once())->method('flush');
7672

7773
// Stub repository to return a dummy language
78-
$repo = $this->createStub(EntityRepository::class);
79-
$repo->method('find')->willReturn(new Language());
80-
$this->entityManager->method('getRepository')->willReturn($repo);
74+
$repository = $this->createStub(EntityRepository::class);
75+
$repository->method('find')->willReturn(new Language());
76+
$entityManager->method('getRepository')->willReturn($repository);
8177

8278
$data = [
8379
'member' => new Member(),
8480
'FeedbackQuestion' => 'Question',
8581
'IdCategory' => new FeedbackCategory(),
8682
];
8783

88-
$this->aboutModel->addFeedback($data);
84+
$aboutModel = new AboutModel($entityManager, $mailer);
85+
$aboutModel->addFeedback($data);
8986
}
9087
}

tests/Model/ConversationModelTest.php

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,11 @@
2020

2121
class ConversationModelTest extends TestCase
2222
{
23-
private $entityManager;
24-
private $mailer;
25-
private $translator;
26-
private $model;
27-
2823
protected function setUp(): void
2924
{
30-
$this->entityManager = $this->createMock(EntityManagerInterface::class);
31-
$this->mailer = $this->createMock(Mailer::class);
32-
$this->translator = $this->createMock(TranslatorInterface::class);
25+
$this->entityManager = $this->createStub(EntityManagerInterface::class);
26+
$this->mailer = $this->createStub(Mailer::class);
27+
$this->translator = $this->createStub(TranslatorInterface::class);
3328

3429
$this->model = new ConversationModel(
3530
$this->mailer,
@@ -79,18 +74,13 @@ public function testMarkConversationDeletedUpdatesMessages(): void
7974
public function testMarkConversationAsSpamUpdatesStatusAndInfo(): void
8075
{
8176
$receiver = new Member();
82-
$sender = new Member();
8377
$message = new Message();
8478
$message->setReceiver($receiver);
8579
$message->setFolder(InFolderType::NORMAL);
8680
$message->setStatus(MessageStatusType::SENT);
8781

8882
$conversation = [$message];
8983

90-
// Expect persistence
91-
$this->entityManager->expects($this->once())->method('persist')->with($message);
92-
$this->entityManager->expects($this->once())->method('flush');
93-
9484
$this->model->markConversationAsSpam($receiver, $conversation, 'Spam comment');
9585

9686
$this->assertEquals(InFolderType::SPAM, $message->getFolder());
@@ -138,9 +128,6 @@ public function testUnmarkConversationDeletedRestoresMessages(): void
138128

139129
$conversation = [$message];
140130

141-
$this->entityManager->expects($this->once())->method('persist')->with($message);
142-
$this->entityManager->expects($this->once())->method('flush');
143-
144131
$this->model->unmarkConversationDeleted($receiver, $conversation);
145132

146133
// Should replace RECEIVER_DELETED relative to empty string or existing state.
@@ -159,9 +146,6 @@ public function testUnmarkConversationAsSpamRestoresNormalState(): void
159146

160147
$conversation = [$message];
161148

162-
$this->entityManager->expects($this->once())->method('persist')->with($message);
163-
$this->entityManager->expects($this->once())->method('flush');
164-
165149
$this->model->unmarkConversationAsSpam($receiver, $conversation);
166150

167151
$this->assertEquals(InFolderType::NORMAL, $message->getFolder());
@@ -178,9 +162,6 @@ public function testMarkConversationAsReadSetsTime(): void
178162

179163
$conversation = [$message];
180164

181-
$this->entityManager->expects($this->once())->method('persist')->with($message);
182-
$this->entityManager->expects($this->once())->method('flush');
183-
184165
$this->model->markConversationAsRead($receiver, $conversation);
185166

186167
$this->assertNotNull($message->getFirstRead());
@@ -195,7 +176,7 @@ public function testGetLastMessageInConversationReturnsLatest(): void
195176
$msg1 = new Message();
196177
$msg2 = new Message();
197178

198-
$repo = $this->createMock(\Doctrine\ORM\EntityRepository::class);
179+
$repo = $this->createStub(\Doctrine\ORM\EntityRepository::class);
199180
$repo->method('findBy')->with(['subject' => $subject])->willReturn([$msg1, $msg2]);
200181

201182
$this->entityManager->method('getRepository')->with(Message::class)->willReturn($repo);

0 commit comments

Comments
 (0)