• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / PHPUnit-Polyfills / 19485817145

19 Nov 2025 12:54AM UTC coverage: 95.833% (-0.6%) from 96.408%
19485817145

push

github

web-flow
Merge pull request #269 from Yoast/dependabot/github_actions/1.x/DavidAnson/markdownlint-cli2-action-21

GH Actions: Bump DavidAnson/markdownlint-cli2-action from 20 to 21

667 of 696 relevant lines covered (95.83%)

63.18 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.29
/src/Polyfills/AssertClosedResource.php
1
<?php
2

3
namespace Yoast\PHPUnitPolyfills\Polyfills;
4

5
use PHPUnit\SebastianBergmann\Exporter\Exporter as Exporter_In_Phar_Old;
6
use PHPUnitPHAR\SebastianBergmann\Exporter\Exporter as Exporter_In_Phar;
7
use SebastianBergmann\Exporter\Exporter;
8
use Yoast\PHPUnitPolyfills\Helpers\ResourceHelper;
9

10
/**
11
 * Polyfill the Assert::assertIsClosedResource() and Assert::assertIsNotClosedResource() methods.
12
 *
13
 * Introduced in PHPUnit 9.3.0.
14
 *
15
 * @link https://github.com/sebastianbergmann/phpunit/issues/4276
16
 * @link https://github.com/sebastianbergmann/phpunit/pull/4365
17
 *
18
 * @since 1.0.0
19
 */
20
trait AssertClosedResource {
21

22
        /**
23
         * Asserts that a variable is of type resource and is closed.
24
         *
25
         * @param mixed  $actual  The variable to test.
26
         * @param string $message Optional failure message to display.
27
         *
28
         * @return void
29
         */
30
        public static function assertIsClosedResource( $actual, $message = '' ) {
366✔
31
                $exporter = self::getPHPUnitExporterObject();
366✔
32
                $msg      = \sprintf( 'Failed asserting that %s is of type "resource (closed)"', $exporter->export( $actual ) );
366✔
33

34
                if ( $message !== '' ) {
366✔
35
                        $msg = $message . \PHP_EOL . $msg;
12✔
36
                }
5✔
37

38
                static::assertTrue( ResourceHelper::isClosedResource( $actual ), $msg );
366✔
39
        }
123✔
40

41
        /**
42
         * Asserts that a variable is not of type resource or is an open resource.
43
         *
44
         * @param mixed  $actual  The variable to test.
45
         * @param string $message Optional failure message to display.
46
         *
47
         * @return void
48
         */
49
        public static function assertIsNotClosedResource( $actual, $message = '' ) {
362✔
50
                $exporter = self::getPHPUnitExporterObject();
362✔
51
                $type     = $exporter->export( $actual );
362✔
52
                if ( $type === 'NULL' ) {
362✔
53
                        $type = 'resource (closed)';
97✔
54
                }
55✔
55
                $msg = \sprintf( 'Failed asserting that %s is not of type "resource (closed)"', $type );
362✔
56

57
                if ( $message !== '' ) {
362✔
58
                        $msg = $message . \PHP_EOL . $msg;
12✔
59
                }
5✔
60

61
                static::assertFalse( ResourceHelper::isClosedResource( $actual ), $msg );
362✔
62
        }
231✔
63

64
        /**
65
         * Helper function to determine whether an assertion regarding a resource's state should be skipped.
66
         *
67
         * Due to some bugs in PHP itself, the "is closed resource" determination
68
         * cannot always be done reliably.
69
         *
70
         * This method can determine whether or not the current value in combination with
71
         * the current PHP version on which the test is being run is affected by this.
72
         *
73
         * Use this function to skip running a test using `assertIs[Not]ClosedResource()` or
74
         * to skip running just that particular assertion.
75
         *
76
         * @param mixed $actual The variable to test.
77
         *
78
         * @return bool
79
         */
80
        public static function shouldClosedResourceAssertionBeSkipped( $actual ) {
164✔
81
                return ( ResourceHelper::isResourceStateReliable( $actual ) === false );
164✔
82
        }
83

84
        /**
85
         * Helper function to obtain an instance of the Exporter class.
86
         *
87
         * @return Exporter|Exporter_In_Phar|Exporter_In_Phar_Old
88
         */
89
        private static function getPHPUnitExporterObject() {
728✔
90
                if ( \class_exists( 'SebastianBergmann\Exporter\Exporter' ) ) {
728✔
91
                        // Composer install or really old PHAR files.
92
                        return new Exporter();
728✔
93
                }
94
                elseif ( \class_exists( 'PHPUnitPHAR\SebastianBergmann\Exporter\Exporter' ) ) {
×
95
                        // PHPUnit PHAR file for 8.5.38+, 9.6.19+, 10.5.17+ and 11.0.10+.
96
                        return new Exporter_In_Phar();
×
97
                }
98

99
                // PHPUnit PHAR file for < 8.5.38, < 9.6.19, < 10.5.17 and < 11.0.10.
100
                return new Exporter_In_Phar_Old();
×
101
        }
102
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc