Skip to content

Commit

Permalink
Merge pull request #170 from Yoast/feature/improve-php-8.4-workaround…
Browse files Browse the repository at this point in the history
…-implicitly-nullable

Tests: improve PHP 8.4 deprecation work-around
  • Loading branch information
jrfnl authored Jul 12, 2024
2 parents 8c2f833 + 5c58570 commit 92b81d8
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 25 deletions.
3 changes: 2 additions & 1 deletion .phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,15 @@
<rule ref="PHPCompatibility.FunctionDeclarations.NewReturnTypeDeclarations">
<exclude-pattern>/tests/Polyfills/Fixtures/ChildValueObject\.php$</exclude-pattern>
<exclude-pattern>/tests/Polyfills/Fixtures/ValueObject\.php$</exclude-pattern>
<exclude-pattern>/tests/Polyfills/Fixtures/ValueObjectParamNotRequired\.php$</exclude-pattern>
<exclude-pattern>/tests/Polyfills/Fixtures/ValueObjectUnion\.php$</exclude-pattern>
</rule>
<rule ref="PHPCompatibility.FunctionDeclarations.NewParamTypeDeclarations.UnionTypeFound">
<exclude-pattern>/tests/Polyfills/Fixtures/ValueObjectUnion\.php$</exclude-pattern>
<exclude-pattern>/tests/Polyfills/Fixtures/ValueObjectUnionNoReturnType\.php$</exclude-pattern>
</rule>
<rule ref="PHPCompatibility.FunctionDeclarations.NewNullableTypes.typeDeclarationFound">
<exclude-pattern>/tests/Polyfills/Fixtures/ValueObject\.php$</exclude-pattern>
<exclude-pattern>/tests/Polyfills/Fixtures/ValueObjectParamNotRequired\.php$</exclude-pattern>
</rule>
<rule ref="PHPCompatibility.Operators.NewOperators.t_spaceshipFound">
<exclude-pattern>/tests/Polyfills/Fixtures/ValueObject\.php$</exclude-pattern>
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@
"@php ./vendor/php-parallel-lint/php-parallel-lint/parallel-lint . -e php --show-deprecated --exclude vendor --exclude .git --exclude src/Exceptions/Error.php --exclude src/Exceptions/TypeError.php --exclude tests/Polyfills/Fixtures/ValueObjectUnion.php --exclude tests/Polyfills/Fixtures/ValueObjectUnionNoReturnType.php"
],
"lint70": [
"@php ./vendor/php-parallel-lint/php-parallel-lint/parallel-lint . -e php --show-deprecated --exclude vendor --exclude .git --exclude src/Exceptions/Error.php --exclude src/Exceptions/TypeError.php --exclude tests/Polyfills/Fixtures/ValueObject.php --exclude tests/Polyfills/Fixtures/ValueObjectUnion.php --exclude tests/Polyfills/Fixtures/ValueObjectUnionNoReturnType.php"
"@php ./vendor/php-parallel-lint/php-parallel-lint/parallel-lint . -e php --show-deprecated --exclude vendor --exclude .git --exclude src/Exceptions/Error.php --exclude src/Exceptions/TypeError.php --exclude tests/Polyfills/Fixtures/ValueObjectParamNotRequired.php --exclude tests/Polyfills/Fixtures/ValueObjectUnion.php --exclude tests/Polyfills/Fixtures/ValueObjectUnionNoReturnType.php"
],
"lint-lt70": [
"@php ./vendor/php-parallel-lint/php-parallel-lint/parallel-lint . -e php --show-deprecated --exclude vendor --exclude .git --exclude src/TestCases/TestCasePHPUnitGte8.php --exclude src/TestListeners/TestListenerDefaultImplementationPHPUnitGte7.php --exclude tests/Polyfills/Fixtures/ChildValueObject.php --exclude tests/Polyfills/Fixtures/ValueObject.php --exclude tests/Polyfills/Fixtures/ValueObjectUnion.php --exclude tests/Polyfills/Fixtures/ValueObjectUnionNoReturnType.php"
"@php ./vendor/php-parallel-lint/php-parallel-lint/parallel-lint . -e php --show-deprecated --exclude vendor --exclude .git --exclude src/TestCases/TestCasePHPUnitGte8.php --exclude src/TestListeners/TestListenerDefaultImplementationPHPUnitGte7.php --exclude tests/Polyfills/Fixtures/ChildValueObject.php --exclude tests/Polyfills/Fixtures/ValueObject.php --exclude tests/Polyfills/Fixtures/ValueObjectParamNotRequired.php --exclude tests/Polyfills/Fixtures/ValueObjectUnion.php --exclude tests/Polyfills/Fixtures/ValueObjectUnionNoReturnType.php"
],
"lint-gte80": [
"@php ./vendor/php-parallel-lint/php-parallel-lint/parallel-lint . -e php --show-deprecated --exclude vendor --exclude .git"
Expand Down
18 changes: 8 additions & 10 deletions tests/Polyfills/AssertObjectEqualsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionMessageMatches;
use Yoast\PHPUnitPolyfills\Tests\Polyfills\Fixtures\ChildValueObject;
use Yoast\PHPUnitPolyfills\Tests\Polyfills\Fixtures\ValueObject;
use Yoast\PHPUnitPolyfills\Tests\Polyfills\Fixtures\ValueObjectParamNotRequired;
use Yoast\PHPUnitPolyfills\Tests\Polyfills\Fixtures\ValueObjectUnion;

/**
Expand All @@ -26,16 +27,11 @@
* Due to the use of return types in the classes under test (fixtures), these
* tests can only run on PHP 7.0 and higher.
*
* And due to the implicitly nullable parameter deprecation in PHP 8.4 requiring a
* nullable type for one of the tests, the minimum PHP version for running this
* version of the tests has been set to PHP 7.1.
*
* The `AssertObjectEqualsPHPUnitLt940Test` class mirrors this test class
* and tests the polyfill method for PHP < 7.1.
* The `AssertObjectEqualsPHPUnitLt940Test` class mirrors this test class.
*
* @covers \Yoast\PHPUnitPolyfills\Polyfills\AssertObjectEquals
*
* @requires PHP 7.1
* @requires PHP 7.0
*/
final class AssertObjectEqualsTest extends TestCase {

Expand Down Expand Up @@ -216,10 +212,12 @@ public function testAssertObjectEqualsFailsOnMethodAllowsForMoreParams() {
/**
* Verify that the assertObjectEquals() method throws an error when the $method is not a required parameter.
*
* @requires PHP 7.1
*
* @return void
*/
public function testAssertObjectEqualsFailsOnMethodParamNotRequired() {
$msg = 'Comparison method Yoast\PHPUnitPolyfills\Tests\Polyfills\Fixtures\ValueObject::equalsParamNotRequired() does not declare exactly one parameter.';
$msg = 'Comparison method Yoast\PHPUnitPolyfills\Tests\Polyfills\Fixtures\ValueObjectParamNotRequired::equalsParamNotRequired() does not declare exactly one parameter.';

$exception = self::COMPARATOR_EXCEPTION;
if ( \class_exists( ComparisonMethodDoesNotDeclareExactlyOneParameterException::class ) ) {
Expand All @@ -230,8 +228,8 @@ public function testAssertObjectEqualsFailsOnMethodParamNotRequired() {
$this->expectException( $exception );
$this->expectExceptionMessage( $msg );

$expected = new ValueObject( 'test' );
$actual = new ValueObject( 'test' );
$expected = new ValueObjectParamNotRequired( 'test' );
$actual = new ValueObjectParamNotRequired( 'test' );
$this->assertObjectEquals( $expected, $actual, 'equalsParamNotRequired' );
}

Expand Down
11 changes: 0 additions & 11 deletions tests/Polyfills/Fixtures/ValueObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,6 @@ public function equalsTwoParams( $other, $param ): bool {
return ( $param && $this->value === $other->value );
}

/**
* Comparator method: incorrectly declared - parameter is not required.
*
* @param self|null $other Object to compare.
*
* @return bool
*/
public function equalsParamNotRequired( ?self $other = null ): bool {
return ( $this->value === $other->value );
}

/**
* Comparator method: incorrectly declared - parameter is not typed.
*
Expand Down
40 changes: 40 additions & 0 deletions tests/Polyfills/Fixtures/ValueObjectParamNotRequired.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Yoast\PHPUnitPolyfills\Tests\Polyfills\Fixtures;

/**
* Fixture to test the AssertObjectEquals trait.
*
* The `equalsParamNotRequired()` method needs to be in this separate fixture as it needs
* a nullable type declaration (PHP 7.1+) to prevent a deprecation about an implicitely nullable
* type on PHP 8.4.
*/
class ValueObjectParamNotRequired {

/**
* The value.
*
* @var mixed
*/
protected $value;

/**
* Constructor.
*
* @param mixed $value The value.
*/
public function __construct( $value ) {
$this->value = $value;
}

/**
* Comparator method: incorrectly declared - parameter is not required.
*
* @param self|null $other Object to compare.
*
* @return bool
*/
public function equalsParamNotRequired( ?self $other = null ): bool {
return ( $this->value === $other->value );
}
}
2 changes: 1 addition & 1 deletion tests/TestCases/TestCaseTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ final public function testAvailabilityEqualToSpecializations() {
/**
* Verify availability of trait polyfilled PHPUnit methods [14].
*
* @requires PHP 7.1
* @requires PHP 7.0
*
* @return void
*/
Expand Down

0 comments on commit 92b81d8

Please sign in to comment.