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
@@ -27,6 +29,7 @@ public function testAnonymousUserCanAccessPublicSubscribeAssetRoutes(string $pat
2729 'browser ' => static ::CHROME ,
2830 'connection_timeout_in_ms ' => 10000 ,
2931 ]);
32+ $ client ->getCookieJar ()->clear ();
3033 $ client ->request ('GET ' , $ path );
3134
3235 $ currentPath = (string ) parse_url ($ client ->getCurrentURL (), PHP_URL_PATH );
@@ -56,6 +59,7 @@ public function testAnonymousUserCanAccessPublicSubscribeAndUnsubscribePages(str
5659 'browser ' => static ::CHROME ,
5760 'connection_timeout_in_ms ' => 10000 ,
5861 ]);
62+ $ client ->getCookieJar ()->clear ();
5963 $ client ->request ('GET ' , $ path );
6064
6165 $ currentPath = (string ) parse_url ($ client ->getCurrentURL (), PHP_URL_PATH );
@@ -70,10 +74,18 @@ public function testSubscribePageDisplaysEmailFieldAndSubmitButton(): void
7074 'browser ' => static ::CHROME ,
7175 'connection_timeout_in_ms ' => 10000 ,
7276 ]);
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 );
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,80 @@ 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 test log: ' . "\n" . $ this ->readRecentLogLines ('test.log ' ),
129+ 'Recent dev log: ' . "\n" . $ this ->readRecentLogLines ('dev.log ' ),
130+ ])
131+ );
132+
133+ fwrite (STDERR , "Public subscribe diagnostics written to var/screenshots/public-subscribe-error.* \n" );
134+ }
135+
136+ private function readPageSource (Client $ client ): string
137+ {
138+ try {
139+ return $ client ->getPageSource ();
140+ } catch (Throwable $ throwable ) {
141+ return 'Unable to read page source: ' . $ throwable ->getMessage ();
142+ }
143+ }
144+
145+ private function readCurrentUrl (Client $ client ): string
146+ {
147+ try {
148+ return $ client ->getCurrentURL ();
149+ } catch (Throwable $ throwable ) {
150+ return 'Unable to read current URL: ' . $ throwable ->getMessage ();
151+ }
152+ }
153+
154+ private function readPageTitle (Client $ client ): string
155+ {
156+ try {
157+ return $ client ->getTitle ();
158+ } catch (Throwable $ throwable ) {
159+ return 'Unable to read page title: ' . $ throwable ->getMessage ();
160+ }
161+ }
162+
163+ private function readBodyText (Client $ client ): string
164+ {
165+ try {
166+ return $ client ->getCrawler ()->filter ('body ' )->text ('' , true );
167+ } catch (Throwable $ throwable ) {
168+ return 'Unable to read body text: ' . $ throwable ->getMessage ();
169+ }
170+ }
171+
172+ private function readRecentLogLines (string $ fileName ): string
173+ {
174+ $ path = __DIR__ . '/../../../var/logs/ ' . $ fileName ;
175+ if (!is_file ($ path )) {
176+ return sprintf ('Log file %s does not exist. ' , $ path );
177+ }
178+
179+ $ lines = file ($ path , FILE_IGNORE_NEW_LINES );
180+ if ($ lines === false ) {
181+ return sprintf ('Unable to read log file %s. ' , $ path );
182+ }
183+
184+ return implode ("\n" , array_slice ($ lines , -80 ));
185+ }
98186}
0 commit comments