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

Plugin Phpunit Laravel Package

psalm/plugin-phpunit

Psalm plugin that teaches Psalm about PHPUnit tests. Adds better type inference and assertions for PHPUnit APIs so your test suite is analyzed more accurately. Requires Psalm v4+. Install via Composer and enable with psalm-plugin.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Seamless Integration with Laravel/Psalm Ecosystem: Designed as a Psalm plugin, it leverages Psalm’s static analysis engine to augment PHPUnit test validation. Aligns perfectly with Laravel’s adoption of Psalm for type safety and PHPUnit for testing.
  • Attribute-First Design: Supports PHPUnit 9+ attributes (#[Test], #[DataProvider], #[Before]), critical for modern Laravel testing (e.g., Laravel’s #[Test] in phpunit:10).
  • Type Inference for Tests: Reduces false positives in test analysis by teaching Psalm about PHPUnit assertions (e.g., assertSame(), assertInstanceOf()), improving accuracy for Laravel’s Eloquent, API responses, and service container tests.
  • Plugin-Based Isolation: Encapsulates PHPUnit-specific logic, avoiding contamination of core Psalm or Laravel systems.

Integration Feasibility

  • Low Friction: Requires two commands post-install:
    composer require --dev psalm/plugin-phpunit
    vendor/bin/psalm-plugin enable psalm/plugin-phpunit
    
  • Config-Driven: Works with existing psalm.xml (add <dir>tests/</dir> to <file-list>). No Laravel-specific overrides needed.
  • Dependency Alignment:
    • Psalm v4+: Laravel’s official Psalm config targets v5+, so compatibility is high.
    • PHPUnit 9+: Laravel’s phpunit/phpunit package (v10+) is fully supported.
  • CI/CD Ready: Zero runtime overhead; runs as a pre-test static check (e.g., in GitHub Actions via ./vendor/bin/psalm).

Technical Risk

Risk Area Assessment Mitigation
Psalm Version Lock Plugin drops support for Psalm 4 (requires v5+). Laravel’s ecosystem is migrating to v5+, but legacy projects may lag. Audit composer.json for Psalm version; upgrade if <v5. Use --strict flag to enforce compatibility.
False Positives/Negatives Early adopters report reduced false positives for assertions, but complex Laravel patterns (e.g., dynamic data providers) may trigger edge cases. Start with --no-cache in CI to surface issues early. Use psalm.xml to exclude problematic files temporarily (e.g., <exclude-files>vendor/</exclude-files>).
Attribute Support Gaps While #[Test], #[DataProvider] are covered, Laravel-specific attributes (e.g., #[Laravel\Tests\CreatesApplication]) may not be recognized. Extend plugin via Psalm’s plugin API if needed (low priority; most Laravel tests use standard PHPUnit attributes).
Performance Overhead Static analysis adds ~10–30% slower test suite initialization (Psalm’s baseline). Cache results in CI (--cache-globals) and run in parallel with PHPUnit. Exclude large test files (e.g., Feature/*Test.php) if performance is critical.
Breaking Changes Plugin drops PHPUnit 7.5 support (irrelevant for Laravel) but may introduce Psalm v7-specific behaviors. Monitor changelog for Psalm version bumps; test against psalm/psalm:dev for early warnings.

Key Questions

  1. Psalm Adoption Maturity:
    • Is Psalm already integrated into the Laravel project’s CI/CD pipeline (e.g., ./vendor/bin/psalm --init)?
    • If not, what’s the blocker (e.g., warning fatigue, legacy code)? Prioritize fixing critical issues first.
  2. Test Suite Complexity:
    • What percentage of tests use data providers, attributes, or complex assertions (e.g., assertDatabaseHas)? Higher complexity = greater plugin value.
    • Are there custom PHPUnit extensions (e.g., Laravel’s RefreshDatabase) that may conflict?
  3. CI/CD Pipeline Impact:
    • How will this integrate with existing test stages? Suggest running Psalm before PHPUnit to fail fast.
    • What’s the acceptable failure threshold for static analysis warnings? (e.g., "Block builds on test-related errors only.")
  4. Developer Ramp-Up:
    • How will the team interpret Psalm warnings in tests? Provide examples:
      • Warning: Argument 1 of assertSame() must be of type int, string given (e.g., $user->id is string but asserted as int).
      • Warning: Data provider must return iterable, array returned (e.g., missing yield in provider).
  5. Long-Term Maintenance:
    • Who will triage Psalm warnings in tests? Assign a "Psalm Champion" to resolve false positives.
    • How will the plugin be updated as Psalm/PHPUnit evolve? Use composer require --dev with version constraints (e.g., ^0.20).

Integration Approach

Stack Fit

  • Laravel + Psalm + PHPUnit 10:
    • Psalm: Static analysis for production code and tests.
    • PHPUnit 10: Modern attribute syntax (#[Test]) fully supported.
    • Composer: Dev dependency with zero runtime impact.
  • Toolchain Synergy:
    • Pint: Laravel’s PHP formatter (no conflict).
    • PHPStan: If used, Psalm’s stricter type system may reduce overlap (but no direct conflict).
    • GitHub Actions: Add as a pre-test step:
      - name: Psalm (Tests)
        run: ./vendor/bin/psalm --init --no-cache --output-format=github
      

Migration Path

Phase Action Tools/Commands
Assessment Audit Psalm/PHPUnit versions and test complexity. composer show psalm/phpunit grep -r "#[Test]" tests/
Installation Add plugin to composer.json and enable it. composer require --dev psalm/plugin-phpunit vendor/bin/psalm-plugin enable psalm/plugin-phpunit
Configuration Update psalm.xml to include test files and configure strictness. ```xml
  <file-list>
    <dir>app/</dir>
    <dir>tests/</dir> <!-- Add test directory -->
  </file-list>
  <plugins>
    <plugin-class>PsalmPlugin\PHPUnitPlugin</plugin-class>
  </plugins>
  ```                                                                                                                                                                                                 |

| Validation | Run Psalm on a subset of tests (e.g., tests/Unit/) to validate warnings. | ./vendor/bin/psalm --init --no-cache --tests-only | | CI Integration| Add Psalm to CI pipeline before PHPUnit. | GitHub Actions example: psalm-ci-template | | Iteration | Fix critical warnings (e.g., assertion type mismatches) and refine psalm.xml exclusions. | ./vendor/bin/psalm --init --fix (experimental) Adjust <exclude-files> as needed. |

Compatibility

  • Laravel-Specific Considerations:
    • Eloquent Tests: Plugin improves type checks for assertDatabaseHas(), assertModelExists(), etc.
    • API Testing: Validates assertJson()/assertJsonStructure() against actual response types.
    • Service Container: Catches mismatches in bind()/singleton() assertions.
  • Edge Cases:
    • Dynamic Data Providers: May require @psalm-suppress for complex generators (e.g., yield from).
    • Legacy PHPUnit 8: Unsupported; migrate to PHPUnit 10 for full attribute support.
    • Custom Assertions: Extend plugin via Psalm’s API if needed.

Sequencing

  1. Start with Unit Tests:
    • Focus on tests/Unit/ first (lower complexity, higher ROI).
    • Example: Validate assertSame($user->id, 1) where $user->id is string.
  2. **
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.
besmartand-pro/php-quality-config
sentix/ai-chatbot
codifyo/ts-generator-bundle
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor