webmozarts / strict-phpunit
Enables type-safe comparisons of objects in PHPUnit
Installs: 96 707
Dependents: 4
Suggesters: 0
Security: 0
Stars: 29
Watchers: 6
Forks: 1
Open Issues: 0
Requires
- php: ^8.1
- phpunit/phpunit: ^9.4.3 || ^10.3
Requires (Dev)
- ergebnis/composer-normalize: ^2.28
- infection/infection: ^0.26.6
- dev-main
- 7.13.1
- 7.13.0
- 7.12.1
- 7.12.0
- 7.11.19
- 7.11.18
- 7.11.17
- 7.11.16
- 7.11.15
- 7.11.14
- 7.11.13
- 7.11.12
- 7.11.11
- 7.11.10
- 7.11.9
- 7.11.8
- 7.11.7
- 7.11.6
- 7.11.5
- 7.11.4
- 7.11.3
- 7.11.1
- 7.11.0
- 7.10.1
- 7.10.0
- 7.9.x-dev
- 7.9.3
- 7.9.2
- 7.9.1
- 7.9.0
- 7.8.x-dev
- 7.8.0
- 7.7.x-dev
- 7.7.7
- 7.7.6
- 7.7.5
- 7.7.4
- 7.7.3
- 7.7.2
- 7.7.1
- 7.7.0
- 7.6.x-dev
- 7.6.6
- 7.6.5
- 7.6.4
- 7.6.3
- 7.6.2
- 7.6.1
- 7.6.0
- 7.5.x-dev
- 7.5.4
- 7.5.3
- 7.5.2
- 7.5.1
- 7.5.0
- 7.4.x-dev
- 7.4.1
- 7.4.0
- 7.3.x-dev
- 7.3.3
- 7.3.2
- 7.3.1
- 7.3.0
- 7.2.10
- 7.2.9
- 7.2.8
- 7.2.7
- 7.2.6
- 7.2.5
- 7.2.4
- 7.2.3
- 7.2.2
- 7.2.1
- 7.2.0
- 7.1.1
- 7.1.0
- 7.0.5
- 7.0.4
- 7.0.3
- 7.0.2
- 7.0.1
- 7.0.0
- 7.0.0-beta.5
- 7.0.0-beta.4
- 7.0.0-beta.3
- 7.0.0-beta.2
- 7.0.0-beta.1
- dev-build/fix-enum-deploy
- dev-bugfix/version
- dev-feature/dependencies
This package is auto-updated.
Last update: 2024-11-16 12:48:11 UTC
README
Enables type-safe comparisons of objects in PHPUnit.
Problem
PHPUnit has a very powerful comparison system that helps you comparing objects with expected values:
class ValueObject { public ?string $property; public function __construct(?string $property) { $this->property = $property; } } $actual = new ValueObject('foo!'); self::assertEquals(new ValueObject('foo'), $actual); // => fails with a very helpful error message
This comparison system will give you a meaningful exception that guides you precisely to the problem that caused the assertion to fail. Strings are furthermore diffed so that you see exactly which character of the string causes a mismatch.
PHPUnit compares each scalar property of an object with relaxed types. It is a
little more intelligent than using just ==
under the hood, but still that
will not always provide the results you want:
var_dump('Hi' == true); // => true self::assertEquals(new ValueObject('Hi'), new ValueObject(true)); // => fails var_dump('' == null); // => true self::assertEquals(new ValueObject(''), new ValueObject(null)); // => succeeds
Solution
This extension enables a comparator for scalar values that fights this problem.
With this extension, whenever PHPUnit finds a scalar value during
assertEquals()
(even recursively within objects or arrays), it will compare
the value with ===
.
Objects are still not checked for identity, hence you can still construct example objects to compare against.
Error messages stay meaningful.
self::assertEquals(new ValueObject(''), new ValueObject(null)); // => fails with a meaningful error self::assertEquals(new ValueObject('foo!'), new ValueObject('foo')); // => fails with a meaningful error self::assertEquals(new ValueObject('foo!'), new ValueObject('foo!')); // => succeeds
Installation
The extension can be installed with Composer:
$ composer require --dev webmozarts/strict-phpunit
Add the extension to your phpunit.xml.dist
file to enable it:
<?xml version="1.0" encoding="UTF-8"?> <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"> <!-- ... --> <extensions> <extension class="Webmozarts\StrictPHPUnit\StrictPHPUnitExtension"/> </extensions> <!-- ... --> </phpunit>
Authors
Contribute
Contributions to the package are always welcome!
- Report any bugs or issues you find on the issue tracker.
- You can grab the source code at the package's Git repository.
Note that this repository is a subtree-split of a monorepo and hence read only. PRs will be ported to the (internal) monorepo.
License
All contents of this package are licensed under the MIT license.