Skip to content

Commit a70ce16

Browse files
committed
AssertObjectEquals: improve "not boolean return value" error message
... and add tests covering this exception.
1 parent 9970f5f commit a70ce16

File tree

6 files changed

+76
-2
lines changed

6 files changed

+76
-2
lines changed

.phpcs.xml.dist

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@
146146
</rule>
147147

148148
<!-- These fixtures for the assertEqualObject() tests will only be loaded on PHP 7+/8+ respectively. -->
149-
<rule ref="PHPCompatibility.FunctionDeclarations.NewReturnTypeDeclarations.boolFound">
149+
<rule ref="PHPCompatibility.FunctionDeclarations.NewReturnTypeDeclarations">
150150
<exclude-pattern>/tests/Polyfills/Fixtures/ChildValueObject\.php$</exclude-pattern>
151151
<exclude-pattern>/tests/Polyfills/Fixtures/ValueObject\.php$</exclude-pattern>
152152
<exclude-pattern>/tests/Polyfills/Fixtures/ValueObjectUnion\.php$</exclude-pattern>
@@ -155,5 +155,8 @@
155155
<exclude-pattern>/tests/Polyfills/Fixtures/ValueObjectUnion\.php$</exclude-pattern>
156156
<exclude-pattern>/tests/Polyfills/Fixtures/ValueObjectUnionNoReturnType\.php$</exclude-pattern>
157157
</rule>
158+
<rule ref="PHPCompatibility.Operators.NewOperators.t_spaceshipFound">
159+
<exclude-pattern>/tests/Polyfills/Fixtures/ValueObject\.php$</exclude-pattern>
160+
</rule>
158161

159162
</ruleset>

src/Polyfills/AssertObjectEquals.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public static function assertObjectEquals( $expected, $actual, $method = 'equals
212212
if ( \is_bool( $result ) === false ) {
213213
throw new InvalidComparisonMethodException(
214214
\sprintf(
215-
'%s::%s() does not return a boolean value.',
215+
'Comparison method %s::%s() does not return a boolean value.',
216216
\get_class( $actual ),
217217
$method
218218
)

tests/Polyfills/AssertObjectEqualsPHPUnitLt940Test.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,23 @@ public function testAssertObjectEqualsFailsOnMethodParamTypeMismatch() {
263263
$this->assertObjectEquals( new stdClass(), $actual );
264264
}
265265

266+
/**
267+
* Verify that the assertObjectEquals() method throws an error when the declared return type/
268+
* the return value is not boolean.
269+
*
270+
* @return void
271+
*/
272+
public function testAssertObjectEqualsFailsOnNonBooleanReturnValue() {
273+
$msg = 'Comparison method Yoast\PHPUnitPolyfills\Tests\Polyfills\Fixtures\ValueObjectNoReturnType::equalsNonBooleanReturnType() does not return a boolean value.';
274+
275+
$this->expectException( self::COMPARATOR_EXCEPTION );
276+
$this->expectExceptionMessage( $msg );
277+
278+
$expected = new ValueObjectNoReturnType( 100 );
279+
$actual = new ValueObjectNoReturnType( 100 );
280+
$this->assertObjectEquals( $expected, $actual, 'equalsNonBooleanReturnType' );
281+
}
282+
266283
/**
267284
* Verify that the assertObjectEquals() method fails a test when a call to method
268285
* determines that the objects are not equal.

tests/Polyfills/AssertObjectEqualsTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,30 @@ public function testAssertObjectEqualsFailsOnMethodParamTypeMismatch() {
340340
$this->assertObjectEquals( new stdClass(), $actual );
341341
}
342342

343+
/**
344+
* Verify that the assertObjectEquals() method throws an error when the declared return type/
345+
* the return value is not boolean.
346+
*
347+
* @return void
348+
*/
349+
public function testAssertObjectEqualsFailsOnNonBooleanReturnValue() {
350+
$msg = 'Comparison method Yoast\PHPUnitPolyfills\Tests\Polyfills\Fixtures\ValueObject::equalsNonBooleanReturnType() does not return a boolean value.';
351+
352+
$exception = self::COMPARATOR_EXCEPTION;
353+
if ( \class_exists( 'PHPUnit\Framework\ComparisonMethodDoesNotDeclareBoolReturnTypeException' ) ) {
354+
// PHPUnit > 9.4.0.
355+
$msg = 'Comparison method Yoast\PHPUnitPolyfills\Tests\Polyfills\Fixtures\ValueObject::equalsNonBooleanReturnType() does not declare bool return type.';
356+
$exception = 'PHPUnit\Framework\ComparisonMethodDoesNotDeclareBoolReturnTypeException';
357+
}
358+
359+
$this->expectException( $exception );
360+
$this->expectExceptionMessage( $msg );
361+
362+
$expected = new ValueObject( 100 );
363+
$actual = new ValueObject( 100 );
364+
$this->assertObjectEquals( $expected, $actual, 'equalsNonBooleanReturnType' );
365+
}
366+
343367
/**
344368
* Verify that the assertObjectEquals() method fails a test when a call to method
345369
* determines that the objects are not equal.

tests/Polyfills/Fixtures/ValueObject.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,15 @@ public function equalsParamNonClassType( array $other ): bool {
102102
public function equalsParamNonExistentClassType( ClassWhichDoesntExist $other ): bool {
103103
return ( $this->value === $other->value );
104104
}
105+
106+
/**
107+
* Comparator method: incorrectly declared - non-boolean return type/value.
108+
*
109+
* @param self $other Object to compare.
110+
*
111+
* @return bool
112+
*/
113+
public function equalsNonBooleanReturnType( self $other ): int {
114+
return ( $this->value <=> $other->value );
115+
}
105116
}

tests/Polyfills/Fixtures/ValueObjectNoReturnType.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,23 @@ public function equalsParamNonClassType( array $other ) {
102102
public function equalsParamNonExistentClassType( ClassWhichDoesntExist $other ) {
103103
return ( $this->value === $other->value );
104104
}
105+
106+
/**
107+
* Comparator method: incorrectly declared - non-boolean return type/value.
108+
*
109+
* @param self $other Object to compare.
110+
*
111+
* @return int
112+
*/
113+
public function equalsNonBooleanReturnType( self $other ) {
114+
if ( $this->value === $other->value ) {
115+
return 0;
116+
}
117+
118+
if ( $this->value > $other->value ) {
119+
return 1;
120+
}
121+
122+
return -1;
123+
}
105124
}

0 commit comments

Comments
 (0)