66
77use PHPUnitRetry \RetryAnnotationTrait ;
88use PHPUnitRetry \RetryTrait ;
9+ use Symfony \Component \Panther \Client ;
910use Symfony \Component \Panther \PantherTestCase ;
11+ use Throwable ;
1012
1113/**
1214 * @retryAttempts 1
@@ -18,15 +20,22 @@ class PublicSubscribeControllerPantherTest extends PantherTestCase
1820 use RetryAnnotationTrait;
1921 use RetryTrait;
2022
23+ private const PANTHER_OPTIONS = [
24+ 'browser ' => self ::CHROME ,
25+ 'connection_timeout_in_ms ' => 10000 ,
26+ 'env ' => [
27+ 'APP_ENV ' => 'test ' ,
28+ 'APP_DEBUG ' => '1 ' ,
29+ ],
30+ ];
31+
2132 /**
2233 * @dataProvider publicSubscribeAssetRoutesProvider
2334 */
2435 public function testAnonymousUserCanAccessPublicSubscribeAssetRoutes (string $ path ): void
2536 {
26- $ client = static ::createPantherClient ([
27- 'browser ' => static ::CHROME ,
28- 'connection_timeout_in_ms ' => 10000 ,
29- ]);
37+ $ client = static ::createPantherClient (self ::PANTHER_OPTIONS );
38+ $ client ->getCookieJar ()->clear ();
3039 $ client ->request ('GET ' , $ path );
3140
3241 $ currentPath = (string ) parse_url ($ client ->getCurrentURL (), PHP_URL_PATH );
@@ -52,10 +61,8 @@ public function publicSubscribeAssetRoutesProvider(): array
5261 */
5362 public function testAnonymousUserCanAccessPublicSubscribeAndUnsubscribePages (string $ path ): void
5463 {
55- $ client = static ::createPantherClient ([
56- 'browser ' => static ::CHROME ,
57- 'connection_timeout_in_ms ' => 10000 ,
58- ]);
64+ $ client = static ::createPantherClient (self ::PANTHER_OPTIONS );
65+ $ client ->getCookieJar ()->clear ();
5966 $ client ->request ('GET ' , $ path );
6067
6168 $ currentPath = (string ) parse_url ($ client ->getCurrentURL (), PHP_URL_PATH );
@@ -66,14 +73,19 @@ public function testAnonymousUserCanAccessPublicSubscribeAndUnsubscribePages(str
6673
6774 public function testSubscribePageDisplaysEmailFieldAndSubmitButton (): void
6875 {
69- $ client = static ::createPantherClient ([
70- 'browser ' => static ::CHROME ,
71- 'connection_timeout_in_ms ' => 10000 ,
72- ]);
73- $ client ->request ('GET ' , '/index.php/subscribe/1 ' );
74-
75- $ client ->takeScreenshot ('var/screenshots/public-subscribe-0.png ' );
76- $ client ->waitFor ('form.legacy-form ' , 10 );
76+ $ client = static ::createPantherClient (self ::PANTHER_OPTIONS );
77+ try {
78+ $ client ->getCookieJar ()->clear ();
79+ $ client ->request ('GET ' , '/index.php/subscribe/1 ' );
80+
81+ $ client ->takeScreenshot ('var/screenshots/public-subscribe-0.png ' );
82+ $ client ->waitFor ('form.legacy-form ' , 10 );
83+ } catch (Throwable $ throwable ) {
84+ $ client ->takeScreenshot ('var/screenshots/public-subscribe-error.png ' );
85+ $ this ->writePublicSubscribeDiagnostics ($ client , $ throwable );
86+
87+ throw $ throwable ;
88+ }
7789 $ client ->takeScreenshot ('var/screenshots/public-subscribe-1.png ' );
7890
7991 $ currentPath = (string ) parse_url ($ client ->getCurrentURL (), PHP_URL_PATH );
@@ -95,4 +107,98 @@ public function publicSubscribePageRoutesProvider(): array
95107 'unsubscribe page route ' => ['/index.php/unsubscribe/1 ' ],
96108 ];
97109 }
110+
111+ private function writePublicSubscribeDiagnostics (Client $ client , Throwable $ throwable ): void
112+ {
113+ $ diagnosticsDir = __DIR__ . '/../../../var/screenshots ' ;
114+ if (!is_dir ($ diagnosticsDir )) {
115+ mkdir ($ diagnosticsDir , 0777 , true );
116+ }
117+
118+ $ pageSource = $ this ->readPageSource ($ client );
119+ file_put_contents ($ diagnosticsDir . '/public-subscribe-error.html ' , $ pageSource );
120+ file_put_contents (
121+ $ diagnosticsDir . '/public-subscribe-error.txt ' ,
122+ implode ("\n\n" , [
123+ 'Exception: ' . $ throwable ::class,
124+ 'Message: ' . $ throwable ->getMessage (),
125+ 'Current URL: ' . $ this ->readCurrentUrl ($ client ),
126+ 'Page title: ' . $ this ->readPageTitle ($ client ),
127+ 'Body text: ' . $ this ->readBodyText ($ client ),
128+ 'Recent Symfony logs: ' . "\n" . $ this ->readRecentSymfonyLogs (),
129+ ])
130+ );
131+
132+ fwrite (STDERR , "Public subscribe diagnostics written to var/screenshots/public-subscribe-error.* \n" );
133+ }
134+
135+ private function readPageSource (Client $ client ): string
136+ {
137+ try {
138+ return $ client ->getPageSource ();
139+ } catch (Throwable $ throwable ) {
140+ return 'Unable to read page source: ' . $ throwable ->getMessage ();
141+ }
142+ }
143+
144+ private function readCurrentUrl (Client $ client ): string
145+ {
146+ try {
147+ return $ client ->getCurrentURL ();
148+ } catch (Throwable $ throwable ) {
149+ return 'Unable to read current URL: ' . $ throwable ->getMessage ();
150+ }
151+ }
152+
153+ private function readPageTitle (Client $ client ): string
154+ {
155+ try {
156+ return $ client ->getTitle ();
157+ } catch (Throwable $ throwable ) {
158+ return 'Unable to read page title: ' . $ throwable ->getMessage ();
159+ }
160+ }
161+
162+ private function readBodyText (Client $ client ): string
163+ {
164+ try {
165+ return $ client ->getCrawler ()->filter ('body ' )->text ('' , true );
166+ } catch (Throwable $ throwable ) {
167+ return 'Unable to read body text: ' . $ throwable ->getMessage ();
168+ }
169+ }
170+
171+ private function readRecentSymfonyLogs (): string
172+ {
173+ $ projectDir = dirname (__DIR__ , 3 );
174+ $ paths = array_merge (
175+ glob ($ projectDir . '/var/logs/*.log ' ) ?: [],
176+ glob ($ projectDir . '/var/log/*.log ' ) ?: []
177+ );
178+
179+ if ($ paths === []) {
180+ return sprintf ('No log files found under %s/var/logs or %s/var/log. ' , $ projectDir , $ projectDir );
181+ }
182+
183+ $ logs = [];
184+ foreach (array_unique ($ paths ) as $ path ) {
185+ $ logs [] = sprintf ("==> %s <== \n%s " , $ path , $ this ->readRecentLogLines ($ path ));
186+ }
187+
188+ return implode ("\n\n" , $ logs );
189+ }
190+
191+ private function readRecentLogLines (string $ path ): string
192+ {
193+ if (!is_file ($ path )) {
194+ return sprintf ('Log file %s does not exist. ' , $ path );
195+ }
196+
197+ $ lines = file ($ path , FILE_IGNORE_NEW_LINES );
198+ if ($ lines === false ) {
199+ return sprintf ('Unable to read log file %s. ' , $ path );
200+ }
201+
202+ return implode ("\n" , array_slice ($ lines , -80 ));
203+ }
98204}
0 commit comments