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

Yoast / PHPUnit-Polyfills / 16579531037

28 Jul 2025 08:25PM UTC coverage: 63.368% (-32.5%) from 95.846%
16579531037

push

github

jrfnl
Merge branch '1.x' into 2.x

365 of 576 relevant lines covered (63.37%)

17.41 hits per line

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

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

3
namespace Yoast\PHPUnitPolyfills\Polyfills;
4

5
use PHPUnit\Framework\Assert;
6
use ReflectionObject;
7
use TypeError;
8
use Yoast\PHPUnitPolyfills\Autoload;
9

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

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

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

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

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

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

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

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

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

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