Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHPUnit 11.5.0 | AssertContainsOnly trait: polyfill the Assert::assertContains[Not]Only*() methods #225

Merged
merged 2 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,59 @@ The `assertObjectNotEquals()` assertion was introduced in PHPUnit 11.2.0.

[`Assert::assertObjectNotEquals()`]: https://docs.phpunit.de/en/11.5/assertions.html#assertobjectequals

#### PHPUnit < 11.5.0: `Yoast\PHPUnitPolyfills\Polyfills\AssertContainsOnly`

Polyfills the following methods:

| | |
| ------------------------------------------------ | --------------------------------------------------- |
| [`Assert::assertContainsOnlyArray()`] | [`Assert::assertContainsNotOnlyArray()`] |
| [`Assert::assertContainsOnlyBool()`] | [`Assert::assertContainsNotOnlyBool()`] |
| [`Assert::assertContainsOnlyCallable()`] | [`Assert::assertContainsNotOnlyCallable()`] |
| [`Assert::assertContainsOnlyFloat()`] | [`Assert::assertContainsNotOnlyFloat()`] |
| [`Assert::assertContainsOnlyInt()`] | [`Assert::assertContainsNotOnlyInt()`] |
| [`Assert::assertContainsOnlyIterable()`] | [`Assert::assertContainsNotOnlyIterable()`] |
| [`Assert::assertContainsOnlyNull()`] | [`Assert::assertContainsNotOnlyNull()`] |
| [`Assert::assertContainsOnlyNumeric()`] | [`Assert::assertContainsNotOnlyNumeric()`] |
| [`Assert::assertContainsOnlyObject()`] | [`Assert::assertContainsNotOnlyObject()`] |
| [`Assert::assertContainsOnlyResource()`] | [`Assert::assertContainsNotOnlyResource()`] |
| [`Assert::assertContainsOnlyClosedResource()`] * | [`Assert::assertContainsNotOnlyClosedResource()`] * |
| [`Assert::assertContainsOnlyScalar()`] | [`Assert::assertContainsNotOnlyScalar()`] |
| [`Assert::assertContainsOnlyString()`] | [`Assert::assertContainsNotOnlyString()`] |
| [`Assert::assertContainsNotOnlyInstancesOf()`] | |
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though there's only the "Not" version, should this assert be listed in the "Not" (inverse) column for consistency?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point.

Makes me think I should probably reformat the table for the AssertIsType polyfills too.

Copy link
Collaborator Author

@jrfnl jrfnl Jan 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done - see: #226 #227


These methods were introduced in PHPUnit 11.5.0 as alternatives to the `Assert::assertContainsOnly()` and `Assert::assertNotContainsOnly()` methods, which were soft deprecated in PHPUnit 11.5.0, hard deprecated (warning) in PHPUnit 12.0.0 and will be removed in PHPUnit 13.0.0.

* The `assertContains[Not]OnlyClosedResource()` methods are affected by issues in older PHP versions. Please read the [warning about checking for closed resources and how to optional skip such tests](https://github.com/Yoast/PHPUnit-Polyfills/tree/1.x?tab=readme-ov-file#phpunit--930-yoastphpunitpolyfillspolyfillsassertclosedresource).

[`Assert::assertContainsOnlyArray()`]: https://docs.phpunit.de/en/11.5/assertions.html#assertcontainsonlyarray
[`Assert::assertContainsNotOnlyArray()`]: https://docs.phpunit.de/en/11.5/assertions.html#assertcontainsonlyarray
[`Assert::assertContainsOnlyBool()`]: https://docs.phpunit.de/en/11.5/assertions.html#assertcontainsonlybool
[`Assert::assertContainsNotOnlyBool()`]: https://docs.phpunit.de/en/11.5/assertions.html#assertcontainsonlybool
[`Assert::assertContainsOnlyCallable()`]: https://docs.phpunit.de/en/11.5/assertions.html#assertcontainsonlycallable
[`Assert::assertContainsNotOnlyCallable()`]: https://docs.phpunit.de/en/11.5/assertions.html#assertcontainsonlycallable
[`Assert::assertContainsOnlyFloat()`]: https://docs.phpunit.de/en/11.5/assertions.html#assertcontainsonlyfloat
[`Assert::assertContainsNotOnlyFloat()`]: https://docs.phpunit.de/en/11.5/assertions.html#assertcontainsonlyfloat
[`Assert::assertContainsOnlyInt()`]: https://docs.phpunit.de/en/11.5/assertions.html#assertcontainsonlyint
[`Assert::assertContainsNotOnlyInt()`]: https://docs.phpunit.de/en/11.5/assertions.html#assertcontainsonlyint
[`Assert::assertContainsOnlyIterable()`]: https://docs.phpunit.de/en/11.5/assertions.html#assertcontainsonlyiterable
[`Assert::assertContainsNotOnlyIterable()`]: https://docs.phpunit.de/en/11.5/assertions.html#assertcontainsonlyiterable
[`Assert::assertContainsOnlyNull()`]: https://docs.phpunit.de/en/11.5/assertions.html#assertcontainsonlynull
[`Assert::assertContainsNotOnlyNull()`]: https://docs.phpunit.de/en/11.5/assertions.html#assertcontainsonlynull
[`Assert::assertContainsOnlyNumeric()`]: https://docs.phpunit.de/en/11.5/assertions.html#assertcontainsonlynumeric
[`Assert::assertContainsNotOnlyNumeric()`]: https://docs.phpunit.de/en/11.5/assertions.html#assertcontainsonlynumeric
[`Assert::assertContainsOnlyObject()`]: https://docs.phpunit.de/en/11.5/assertions.html#assertcontainsonlyobject
[`Assert::assertContainsNotOnlyObject()`]: https://docs.phpunit.de/en/11.5/assertions.html#assertcontainsonlyobject
[`Assert::assertContainsOnlyResource()`]: https://docs.phpunit.de/en/11.5/assertions.html#assertcontainsonlyresource
[`Assert::assertContainsNotOnlyResource()`]: https://docs.phpunit.de/en/11.5/assertions.html#assertcontainsonlyresource
[`Assert::assertContainsOnlyClosedResource()`]: https://docs.phpunit.de/en/11.5/assertions.html#assertcontainsonlyclosedresource
[`Assert::assertContainsNotOnlyClosedResource()`]: https://docs.phpunit.de/en/11.5/assertions.html#assertcontainsonlyclosedresource
[`Assert::assertContainsOnlyScalar()`]: https://docs.phpunit.de/en/11.5/assertions.html#assertcontainsonlyscalar
[`Assert::assertContainsNotOnlyScalar()`]: https://docs.phpunit.de/en/11.5/assertions.html#assertcontainsonlyscalar
[`Assert::assertContainsOnlyString()`]: https://docs.phpunit.de/en/11.5/assertions.html#assertcontainsonlystring
[`Assert::assertContainsNotOnlyString()`]: https://docs.phpunit.de/en/11.5/assertions.html#assertcontainsonlystring
[`Assert::assertContainsNotOnlyInstancesOf()`]: https://docs.phpunit.de/en/11.5/assertions.html#assertcontainsonlyinstancesof


### TestCases

Expand Down
1 change: 1 addition & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ parameters:
- src/TestCases/TestCasePHPUnitLte7.php
# Triggers "Referencing prefixed PHPUnit class: PHPUnitPHAR\SebastianBergmann\Exporter\Exporter." notices, which cannot be ignored.
- src/Polyfills/AssertClosedResource.php
- src/Polyfills/AssertContainsOnly.php
- src/Polyfills/AssertIgnoringLineEndings.php
treatPhpDocTypesAsCertain: false

Expand Down
21 changes: 21 additions & 0 deletions phpunitpolyfills-autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ public static function load( $className ) {
self::loadAssertObjectNotEquals();
return true;

case 'Yoast\PHPUnitPolyfills\Polyfills\AssertContainsOnly':
self::loadAssertContainsOnly();
return true;

case 'Yoast\PHPUnitPolyfills\TestCases\TestCase':
self::loadTestCase();
return true;
Expand Down Expand Up @@ -381,6 +385,23 @@ public static function loadAssertObjectNotEquals() {
require_once __DIR__ . '/src/Polyfills/AssertObjectNotEquals_Empty.php';
}

/**
* Load the AssertContainsOnly polyfill or an empty trait with the same name
* if a PHPUnit version is used which already contains this functionality.
*
* @return void
*/
public static function loadAssertContainsOnly() {
if ( \method_exists( Assert::class, 'assertContainsOnlyIterable' ) === false ) {
// PHPUnit < 11.5.0.
require_once __DIR__ . '/src/Polyfills/AssertContainsOnly.php';
return;
}

// PHPUnit >= 11.5.0.
require_once __DIR__ . '/src/Polyfills/AssertContainsOnly_Empty.php';
}

/**
* Load the appropriate TestCase class based on the PHPUnit version being used.
*
Expand Down
Loading
Loading