eliashaeussler/deep-closure-comparator
Provides a PHPUnit comparator to assert equality of closures
Maintainers
Package info
github.com/eliashaeussler/deep-closure-comparator
pkg:composer/eliashaeussler/deep-closure-comparator
Requires
- php: ~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0
- opis/closure: ^4.4
- phpunit/phpunit: ^10.5 || ^11.5 || ^12.3 || ^13.0
- sebastian/comparator: ^5.0 || ^6.3 || ^7.0 || ^8.0
Requires (Dev)
Suggests
None
Provides
None
Conflicts
None
Replaces
None
GPL-3.0-or-later 7c1897e338c2b72dbe0c8245de84ad311ff4a702
- Elias HΓ€uΓler <elias.woop@haeussler.dev>
README
Deep Closure Comparator
π Coverage
π CGL
π Tests
π Supported PHP Versions
Important
Abandoned! This library was created in response to a
change
within the sebastian/comparator library. Since the underlying comparison
mechanism was updated
in the meanwhile, this library is no longer necessary and is therefore
abandoned. Please switch back to sebastian/comparator (versions 7.1.8,
8.2.1 and onwards).
A Composer library that provides a PHPUnit comparator to assert equality
of closures. It can be used to perform deep evaluation of closures, e.g.
as part of objects. Closures are compared using their serialized value,
which is calculated by the opis/closure
library.
π₯ Installation
π Packagist
π Packagist Downloads
composer require --dev eliashaeussler/deep-closure-comparator
β‘ Usage
Instead of using the self::assertEquals() method of a PHPUnit test case, use the assertEquals
method that is shipped within the DeepClosureAssert class:
use EliasHaeussler\DeepClosureComparator\DeepClosureAssert; use PHPUnit\Framework\TestCase; final class Foo { public function __construct( public ?\Closure $bar = null, ) {} } final class FooTest extends TestCase { public function testSomething(): void { $expected = new Foo(); $expected->bar = static fn() => 'foo'; $actual = new Foo(); $actual->bar = static fn() => 'foo'; DeepClosureAssert::assertEquals($expected, $actual); } }
Or, in other words:
-self::assertEquals($expected, $actual); +DeepClosureAssert::assertEquals($expected, $actual);
Note
Closures are compared using their serialized representation. This is done by the
opis/closure library, which provides mechanisms
to deep inspect and serialize given closures. More information can be found in the
official documentation of this library.
π§βπ» Contributing
Please have a look at CONTRIBUTING.md.
π Credits
This project developed from a hardened implementation detail in the
sebastian/comparator library,
as part of PHPUnit's supply chain. With the introduction of a new ClosureComparator, comparing
closures got a lot more difficult. Finally, @tstarling suggested
parts of the actual implementation of this deep-closure-comparator library. Thank you very much
for your support!
β License
This project is licensed under GNU General Public License 3.0 (or later).
