Skip to content

UnderstudyPHPUnitIntegration

Rasuvaeff\Understudy\PhpUnit\UnderstudyPHPUnitIntegration

undefinedPackage: rasuvaeff/understudy-phpunitSourceVersion: v0.1.3

Ends every PHPUnit test with understudy's own bookkeeping done for it.

php
final class CheckoutTest extends TestCase
{
    use UnderstudyPHPUnitIntegration;

public function testChargesForTheCart(): void
    {
        $books = Understudy::for(BookRepositoryInterface::class);
        when(fn () => $books->find(7))->returns(new Book(7));

(new Checkout($books))->charge([7]);

expect(fn () => $books->find(7));   // verified for you
    }
}

On a test that reached PHPUnit\Framework\TestCase::assertPostConditions() — that is, passed its body — the whole context is verified: an expect() the code never fulfilled fails the test as an assertion failure. A test whose body threw keeps its own exception untouched; verification would only mask the error that actually happened. Either way the context is reset, so nothing leaks into the next test.

A base class may flip strict stubbing for a whole project by overriding PhpUnit\understudyStrictStubs().

If the class also overrides assertPostConditions() itself, PHP resolves the conflict silently in favour of the class — the trait's verification would stop running without any error. Compose explicitly instead:

php
use Rasuvaeff\Understudy\PhpUnit\UnderstudyPHPUnitIntegration {
    UnderstudyPHPUnitIntegration::assertPostConditions
        as understudyAssertPostConditions;
}

protected function assertPostConditions(): void
{
    $this->understudyAssertPostConditions();
    // your post-conditions ...
}

No public members beyond what is documented above.