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

Yoast / PHPUnit-Polyfills / 16579231444

28 Jul 2025 08:10PM UTC coverage: 90.774% (-5.6%) from 96.408%
16579231444

push

github

jrfnl
GH Actions: remove GH Token set via `env`

As of the release of `shivammathur/setup-php` v`2.35.0`, this should no longer be needed as the `setup-php` action runner will automatically use the `secrets.GITHUB_TOKEN` token.

Ref:
* https://github.com/shivammathur/setup-php/releases/tag/2.35.0
* https://github.com/shivammathur/setup-php/commit/55463ffe4

610 of 672 relevant lines covered (90.77%)

28.87 hits per line

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

71.43
/src/Polyfills/AssertObjectProperty.php
1
<?php
2

3
namespace Yoast\PHPUnitPolyfills\Polyfills;
4

5
use ReflectionObject;
6
use TypeError;
7
use Yoast\PHPUnitPolyfills\Autoload;
8

9
/**
10
 * Polyfill the Assert::assertObjectHasProperty() and Assert::assertObjectNotHasProperty() methods,
11
 * which replace the Assert::assertObjectHasAttribute() and Assert::assertObjectNotHasAttribute() methods.
12
 *
13
 * Introduced in PHPUnit 10.1.0 and PHPUnit 9.6.11.
14
 *
15
 * The Assert::assertObjectHasAttribute() and Assert::assertObjectNotHasAttribute() methods
16
 * were deprecated in PHPUnit 9.6.1 and removed in PHPUnit 10.0.0.
17
 *
18
 * @link https://github.com/sebastianbergmann/phpunit/pull/5231
19
 * @link https://github.com/sebastianbergmann/phpunit/issues/5478
20
 *
21
 * @since 1.1.0
22
 */
23
trait AssertObjectProperty {
24

25
        /**
26
         * Asserts that an object has a specified property.
27
         *
28
         * @param string $propertyName The name of the property.
29
         * @param object $object       The object on which to check whether the property exists.
30
         * @param string $message      Optional failure message to display.
31
         *
32
         * @return void
33
         *
34
         * @throws TypeError When any of the passed arguments do not meet the required type.
35
         */
36
        final public static function assertObjectHasProperty( $propertyName, $object, $message = '' ) {
156✔
37
                /*
38
                 * Parameter input validation.
39
                 * In PHPUnit this is done via PHP native type declarations. Emulating this for the polyfill,
40
                 * including for those PHPUnit versions where we hand off to a native PHPUnit alternative, as
41
                 * otherwise the method referenced in the error message would get very confusing and inconsistent.
42
                 */
43
                if ( \is_string( $propertyName ) === false ) {
156✔
44
                        throw new TypeError(
42✔
45
                                \sprintf(
42✔
46
                                        'Argument 1 passed to assertObjectHasProperty() must be of type string, %s given',
42✔
47
                                        \gettype( $propertyName )
42✔
48
                                )
14✔
49
                        );
14✔
50
                }
51
                if ( \is_object( $object ) === false ) {
114✔
52
                        throw new TypeError(
42✔
53
                                \sprintf(
42✔
54
                                        'Argument 2 passed to assertObjectHasProperty() must be of type object, %s given',
42✔
55
                                        \gettype( $object )
42✔
56
                                )
14✔
57
                        );
14✔
58
                }
59

60
                if ( \method_exists( '\PHPUnit\Framework\Assert', 'assertObjectHasAttribute' )
72✔
61
                        && \version_compare( Autoload::getPHPUnitVersion(), '9.6.0', '<=' )
72✔
62
                ) {
22✔
63
                        // PHPUnit <= 9.6.0.
64
                        static::assertObjectHasAttribute( $propertyName, $object, $message );
72✔
65
                        return;
60✔
66
                }
67

68
                /*
69
                 * PHPUnit 9.6.1+.
70
                 * Note: letting this polyfill code kick in for PHPUnit 9.6.1+ as well
71
                 * to prevent the PHPUnit deprecation notice showing.
72
                 */
73
                $msg  = self::assertObjectHasPropertyFailureDescription( $object );
×
74
                $msg .= \sprintf( ' has property "%s".', $propertyName );
×
75
                if ( $message !== '' ) {
×
76
                        $msg = $message . \PHP_EOL . $msg;
×
77
                }
78

79
                $hasProperty = ( new ReflectionObject( $object ) )->hasProperty( $propertyName );
×
80
                static::assertTrue( $hasProperty, $msg );
×
81
        }
82

83
        /**
84
         * Asserts that an object does not have a specified property.
85
         *
86
         * @param string $propertyName The name of the property.
87
         * @param object $object       The object on which to check whether the property exists.
88
         * @param string $message      Optional failure message to display.
89
         *
90
         * @return void
91
         *
92
         * @throws TypeError When any of the passed arguments do not meet the required type.
93
         */
94
        final public static function assertObjectNotHasProperty( $propertyName, $object, $message = '' ) {
150✔
95
                /*
96
                 * Parameter input validation.
97
                 * In PHPUnit this is done via PHP native type declarations. Emulating this for the polyfill,
98
                 * including for those PHPUnit versions where we hand off to a native PHPUnit alternative, as
99
                 * otherwise the method referenced in the error message would get very confusing and inconsistent.
100
                 */
101
                if ( \is_string( $propertyName ) === false ) {
150✔
102
                        throw new TypeError(
42✔
103
                                \sprintf(
42✔
104
                                        'Argument 1 passed to assertObjectNotHasProperty() must be of type string, %s given',
42✔
105
                                        \gettype( $propertyName )
42✔
106
                                )
14✔
107
                        );
14✔
108
                }
109
                if ( \is_object( $object ) === false ) {
108✔
110
                        throw new TypeError(
42✔
111
                                \sprintf(
42✔
112
                                        'Argument 2 passed to assertObjectNotHasProperty() must be of type object, %s given',
42✔
113
                                        \gettype( $object )
42✔
114
                                )
14✔
115
                        );
14✔
116
                }
117

118
                if ( \method_exists( '\PHPUnit\Framework\Assert', 'assertObjectNotHasAttribute' )
66✔
119
                        && \version_compare( Autoload::getPHPUnitVersion(), '9.6.0', '<=' )
66✔
120
                ) {
22✔
121
                        // PHPUnit <= 9.6.0.
122
                        static::assertObjectNotHasAttribute( $propertyName, $object, $message );
66✔
123
                        return;
6✔
124
                }
125

126
                /*
127
                 * PHPUnit 9.6.1+.
128
                 * Note: letting this polyfill code kick in for PHPUnit 9.6.1+ as well
129
                 * to prevent the PHPUnit deprecation notice showing.
130
                 */
131
                $msg  = self::assertObjectHasPropertyFailureDescription( $object );
×
132
                $msg .= \sprintf( ' does not have property "%s".', $propertyName );
×
133
                if ( $message !== '' ) {
×
134
                        $msg = $message . \PHP_EOL . $msg;
×
135
                }
136

137
                $hasProperty = ( new ReflectionObject( $object ) )->hasProperty( $propertyName );
×
138
                static::assertFalse( $hasProperty, $msg );
×
139
        }
140

141
        /**
142
         * Returns the description of the failure.
143
         *
144
         * @param object $object The object under test.
145
         *
146
         * @return string
147
         */
148
        private static function assertObjectHasPropertyFailureDescription( $object ) {
×
149
                return \sprintf(
×
150
                        'Failed asserting that object of class "%s"',
×
151
                        \get_class( $object )
×
152
                );
153
        }
154
}
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