77use Dot \Mail \Email ;
88use Dot \Mail \Result \MailResult ;
99use Dot \Mail \Service \MailServiceInterface ;
10+ use Fig \Http \Message \StatusCodeInterface ;
1011use Laminas \Diactoros \Response \HtmlResponse ;
1112use Laminas \Diactoros \Response \RedirectResponse ;
13+ use Light \App \Enum \ContactTopicEnum ;
1214use Light \App \Handler \PostContactCreateHandler ;
1315use Light \App \Service \ContactService ;
16+ use Mezzio \Helper \UrlHelper ;
1417use Mezzio \Template \TemplateRendererInterface ;
1518use PHPUnit \Framework \MockObject \Exception ;
1619use PHPUnit \Framework \TestCase ;
@@ -63,11 +66,18 @@ public function testValidSubmissionRedirectsOnSuccess(): void
6366 $ template = $ this ->createMock (TemplateRendererInterface::class);
6467 $ template ->expects ($ this ->never ())->method ('render ' );
6568
66- $ handler = new PostContactCreateHandler ($ template , $ this ->service (new MailResult (true )));
69+ $ urlHelper = $ this ->createMock (UrlHelper::class);
70+ $ urlHelper
71+ ->expects ($ this ->once ())
72+ ->method ('generate ' )
73+ ->with ('page::contact ' , [], ['contact ' => 'sent ' ], 'contact-form ' )
74+ ->willReturn ('/contact/?contact=sent#contact-form ' );
75+
76+ $ handler = new PostContactCreateHandler ($ template , $ urlHelper , $ this ->service (new MailResult (true )));
6777 $ response = $ handler ->handle ($ request );
6878
6979 $ this ->assertInstanceOf (RedirectResponse::class, $ response );
70- $ this ->assertSame (303 , $ response ->getStatusCode ());
80+ $ this ->assertSame (StatusCodeInterface:: STATUS_SEE_OTHER , $ response ->getStatusCode ());
7181 $ this ->assertSame ('/contact/?contact=sent#contact-form ' , $ response ->getHeaderLine ('Location ' ));
7282 }
7383
@@ -90,15 +100,20 @@ public function testMissingRequiredFieldsRerendersWithErrors(): void
90100 ->with (
91101 'page::contact ' ,
92102 $ this ->callback (function (array $ params ): bool {
93- return $ params ['contact_mail_failed ' ] === false
103+ return $ params ['topics ' ] === ContactTopicEnum ::cases ()
104+ && $ params ['contact_mail_failed ' ] === false
94105 && array_key_exists ('name ' , $ params ['contact_errors ' ])
95106 && array_key_exists ('message ' , $ params ['contact_errors ' ])
96107 && $ params ['contact_values ' ]['email ' ] === 'jane@example.com ' ;
97108 })
98109 )
99110 ->willReturn ('<html></html> ' );
100111
101- $ handler = new PostContactCreateHandler ($ template , $ this ->service (new MailResult (true )));
112+ $ handler = new PostContactCreateHandler (
113+ $ template ,
114+ $ this ->createStub (UrlHelper::class),
115+ $ this ->service (new MailResult (true ))
116+ );
102117 $ response = $ handler ->handle ($ request );
103118
104119 $ this ->assertInstanceOf (HtmlResponse::class, $ response );
@@ -131,7 +146,11 @@ public function testRerenderKeepsAnyExtraQueryParamsAsDynamicHiddenFields(): voi
131146 )
132147 ->willReturn ('<html></html> ' );
133148
134- $ handler = new PostContactCreateHandler ($ template , $ this ->service (new MailResult (true )));
149+ $ handler = new PostContactCreateHandler (
150+ $ template ,
151+ $ this ->createStub (UrlHelper::class),
152+ $ this ->service (new MailResult (true ))
153+ );
135154 $ handler ->handle ($ request );
136155 }
137156
@@ -158,6 +177,7 @@ public function testMailTransportFailureRerendersWithMailFailedFlag(): void
158177
159178 $ handler = new PostContactCreateHandler (
160179 $ template ,
180+ $ this ->createStub (UrlHelper::class),
161181 $ this ->service (new RuntimeException ('SMTP connection refused ' ))
162182 );
163183 $ response = $ handler ->handle ($ request );
0 commit comments