eliashaeussler/deep-closure-comparator
PHPUnit comparator/assertion that deeply compares closures inside objects/arrays by serializing them via opis/closure. Use DeepClosureAssert::assertEquals() as a drop-in replacement for PHPUnit’s assertEquals when closures should be treated as equal.
Install via Composer:
composer require --dev eliashaeussler/deep-closure-comparator
Then register the comparator in your PHPUnit configuration (phpunit.xml):
<phpunit>
<extensions>
<extension class="EliasHaeussler\DeepClosureComparator\PHPUnit\ComparatorExtension"/>
</extensions>
</phpunit>
First use case: comparing two closures for structural equality (same parameters, same body, same static bindings) in unit tests:
$this->assertEquals(
fn ($x) => $x * 2,
$someVariableThatShouldBeTheSameClosure,
);
map, filter, or custom wrappers return expected closures:
$expected = fn ($item) => $item->isActive();
$actual = $this->service->createFilter();
$this->assertEquals($expected, $actual);
Closure::fromCallable(): Validate when converting callables to closures:
$callable = [$this, 'handler'];
$closure = Closure::fromCallable($callable);
$this->assertEquals(Closure::fromCallable([$this, 'handler']), $closure);
bindTo()) are not equal, even with identical code:
$a = fn () => 42;
$b = $a->bindTo(new StdClass());
$this->assertNotEquals($a, $b); // ✅ Correctly different
ReflectionFunction internally—avoid closures that rely on runtime-mutated state or anonymous classes referencing non-serializable resources.strlen::class) cannot be compared meaningfully—expect EqualsComparisonException.How can I help you explore Laravel packages today?