Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Strict Phpunit Laravel Package

webmozarts/strict-phpunit

Enforces strict PHPUnit configuration for PHP projects using the Webmozart standards. Helps catch risky tests, deprecated usage, and unintended behavior by turning on stricter PHPUnit settings and providing an easy way to apply and maintain them across projects.

View on GitHub
Deep Wiki
Context7

Getting Started

Begin by installing the package via Composer (composer require --dev webmozarts/strict-phpunit) and updating your test base class to extend Webmozart\Strict\PhpUnit\StrictTestCase instead of PHPUnit\Framework\TestCase. Your first use case: comparing value objects or DTOs in unit tests where accidental type coercion (e.g., "0" vs 0) would hide critical bugs. Replace assertEquals() with assertStrictEquals() for direct object equality checks—this enforces identical types and values, catching mismatches early in development.

Implementation Patterns

  • Layered strictness: Use StrictTestCase only in high-value test classes (e.g., domain model tests, API DTO contracts), leaving broader functional/integration tests on standard TestCase.
  • Complement static analysis: Integrate with PHPStan’s strict-rules to enforce typing at compile-time; this package then validates runtime behavior during tests.
  • Hybrid assertions: Mix assertStrictEquals() for core domain comparisons and assertEquals() for brittle edge cases (e.g., timezone-sensitive timestamps). Use helper methods like assertEqualsIgnoringOrder() to retain loose assertions where safe.
  • CI safety net: Configure PHPUnit to fail on any warning (via phpunit.xml’s beStrictAboutOutputDuringTests="true" and beStrictAboutTestsThatDoNotTestAnything="true"), which this package enforces by default—ensuring CI mirrors local behavior.

Gotchas and Tips

  • Object comparison pitfalls: PHP’s object identity rules mean assertStrictEquals() compares by spl_object_id—not structural equality. Ensure your value objects override __toString() or implement JsonSerializable/custom comparison methods, or compare scalar properties explicitly.
  • Migration risk: Legacy tests using assertEquals($expected, $actual) with objects may now fail (e.g., "123"123). Fix gradually: isolate failing tests, upgrade assertions one suite at a time, and leverage IDE refactoring tools.
  • Config overrides: Disable strict behavior per-test using @group non-strict and @requires strict annotations (if supported by your PHPUnit version), or override StrictTestCase methods to relax checks for known flaky scenarios (e.g., mock callbacks).
  • Obscure failures: If assertStrictEquals() throws RuntimeException, check for unserializable objects (e.g., closures, PDO connections) or circular references—extract only needed properties for comparison.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4