1616
1717class 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}
0 commit comments