11<?php
22
3+ final class ProbeFailureException extends Exception
4+ {
5+ public function __construct (string |Throwable $ failure , ?Throwable $ previous = null )
6+ {
7+ if ($ failure instanceof Throwable) {
8+ $ previous = $ failure ;
9+ $ failure = $ failure ->getMessage ();
10+ }
11+
12+ parent ::__construct ($ failure , 0 , $ previous );
13+ }
14+ }
15+
316final class ProbeCache
417{
518 private const FAILURE_PREFIX = 'failure: ' ;
619
720 /**
8- * Runs a probe once per configuration for the test run , caching only a
9- * failure. The callback returns its skip reason, or null on success .
21+ * Runs a probe once per configuration during SKIPIF , caching only failures.
22+ * Outside SKIPIF, the cache is bypassed and wrapped exceptions are rethrown .
1023 */
11- public static function getFailure (string $ namespace , array $ configuration , callable $ probe ): ? string
24+ public static function getFailure (string $ namespace , array $ configuration , callable $ probe ): mixed
1225 {
26+ if (getenv ('TEST_PHP_EVALUATING_SKIPIF ' ) !== '1 ' ) {
27+ try {
28+ return $ probe ();
29+ } catch (ProbeFailureException $ e ) {
30+ throw $ e ->getPrevious () ?? $ e ;
31+ }
32+ }
33+
1334 $ directory = getenv ('TEST_PHP_SHARED_CACHE_DIR ' );
1435 if (!is_string ($ directory ) || !is_dir ($ directory )) {
1536 return $ probe ();
@@ -32,18 +53,18 @@ final class ProbeCache
3253 try {
3354 $ cached = stream_get_contents ($ cache );
3455 if (is_string ($ cached ) && str_starts_with ($ cached , self ::FAILURE_PREFIX )) {
35- return substr ($ cached , strlen (self ::FAILURE_PREFIX ));
56+ throw new ProbeFailureException ( substr ($ cached , strlen (self ::FAILURE_PREFIX ) ));
3657 }
3758
38- $ failure = $ probe ();
39- if (is_string ($ failure )) {
59+ try {
60+ return $ probe ();
61+ } catch (ProbeFailureException $ e ) {
4062 rewind ($ cache );
4163 ftruncate ($ cache , 0 );
42- fwrite ($ cache , self ::FAILURE_PREFIX . $ failure );
64+ fwrite ($ cache , self ::FAILURE_PREFIX . $ e -> getMessage () );
4365 fflush ($ cache );
66+ throw $ e ;
4467 }
45-
46- return $ failure ;
4768 } finally {
4869 flock ($ cache , LOCK_UN );
4970 fclose ($ cache );
0 commit comments