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

Phony Phpunit Laravel Package

eloquent/phony-phpunit

Integration of the Phony mocking/stubbing library with PHPUnit, providing helpers to use Phony in your test suite. Note: this package is no longer maintained; see the linked statement and consider alternatives or the main Phony repo.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Pros:

    • Laravel/PHPUnit Synergy: Seamlessly integrates with Laravel’s PHPUnit-driven test ecosystem, offering a cleaner syntax for mocking/stubbing compared to PHPUnit’s native createMock() or getMockBuilder(). Ideal for testing Laravel services, repositories, or Eloquent models where dependency isolation is critical.
    • Expressive API: Phony’s fluent interface (e.g., when()->thenReturn(), anInstanceOf()) reduces test verbosity, improving readability and maintainability—especially for complex interactions (e.g., mocking API clients, event dispatchers, or database transactions).
    • Lightweight: Adds no runtime overhead (dev-only dependency) and avoids the complexity of full-fledged frameworks like Mockery.
    • Type Safety: Supports PHP 7.2+ features (e.g., object type hints), aligning with Laravel’s modern PHP versions.
  • Cons:

    • Archived Status: No guarantees for compatibility with future Laravel/PHPUnit versions (e.g., PHPUnit 10, Laravel 11). Risk of technical debt if the package diverges from upstream Phony.
    • Indirect Phony Dependency: Introduces an additional layer (Phony) that must be maintained alongside PHPUnit, increasing test stack complexity.
    • Limited Laravel-Specific Features: Lacks built-in Laravel integrations (e.g., mocking Illuminate\Contracts or service container bindings). Requires manual setup for Laravel-specific use cases.
    • No Active Community: Absence of dependents or open issues suggests niche adoption; may lack long-term viability.

Technical Risk

  • Compatibility Risks:
    • PHPUnit Version Lock: Last supported version was PHPUnit 9.x. Upgrading to PHPUnit 10+ may require manual patches or forks.
    • PHP Version Lock: Minimum PHP 7.3+; older Laravel versions (e.g., 8.x) may need polyfills or upgrades.
    • Phony Under the Hood: Breaking changes in upstream Phony (e.g., API deprecations) could require test suite updates without package maintenance.
  • Testing Risks:
    • False Positives/Negatives: Phony’s mocking behavior might differ subtly from PHPUnit’s, leading to undetected test failures (e.g., in edge cases like partial mocks or dynamic method calls).
    • Debugging Complexity: Stack traces or error messages may reference Phony internals, complicating debugging for non-experts.
  • Migration Risks:
    • Lock-in: Tests written with this package may be harder to migrate to alternatives (e.g., Pest, Mockery) due to syntax differences.
    • Deprecation Path: No clear roadmap for sunsetting this package, risking stranded technical debt.

Key Questions for TPM

  1. Strategic Alignment:

    • Does this align with the team’s long-term testing strategy (e.g., modernizing to Pest or PHPUnit 10+)? If not, is the short-term gain in test readability worth the risk?
    • How does this fit with Laravel’s testing roadmap (e.g., built-in test helpers, Pest integration)?
  2. Technical Debt:

    • What’s the cost of maintaining this package alongside PHPUnit? Are there internal resources to monitor Phony/PHPUnit compatibility?
    • How would we handle a breaking change in Phony or PHPUnit (e.g., forking, manual patches)?
  3. Adoption Scope:

    • Should this be adopted project-wide (riskier due to maintenance) or piloted in specific areas (e.g., legacy test suites, complex services)?
    • Are there Laravel-specific pain points (e.g., mocking Illuminate\Contracts) that this package doesn’t address?
  4. Alternatives:

    • Would Mockery (actively maintained, Laravel-friendly) or Pest (modern, Phony-compatible) be better long-term investments?
    • Could PHPUnit’s native mocks (improved in v9+) suffice with minor syntax tweaks?
  5. Ramp-Up:

    • How would the team onboard to Phony’s syntax? Are there training materials or internal docs needed?
    • What’s the effort to migrate existing PHPUnit mocks to Phony syntax?

Integration Approach

Stack Fit

  • Laravel/PHPUnit Ecosystem:

    • Fits Well With: Laravel 9+/10+ using PHPUnit 9.x, PHP 7.3+/8.x. Works alongside:
      • Eloquent testing (mocking repositories, models).
      • HTTP testing (mocking API clients like Guzzle).
      • Service layer testing (mocking Illuminate\Contracts or custom services).
    • Conflicts With:
      • PHPUnit 7.x/8.x (last supported version was 9.x).
      • Mockery (if already used; Phony offers a different API).
      • Pest (which bundles Phony natively; redundant dependency).
  • Non-Laravel PHP Projects:

    • Useful for any PHPUnit 9.x project needing expressive mocking, but lacks Laravel-specific integrations.

Migration Path

  1. Assessment Phase:

    • Audit existing PHPUnit tests to identify mocking-heavy suites (prioritize for migration).
    • Verify compatibility with the team’s PHP/PHPUnit/Laravel versions (e.g., run composer require --dev eloquent/phony-phpunit in a staging environment).
  2. Pilot Phase:

    • Scope: Migrate 1–2 test classes or modules (e.g., a service layer or API client tests).
    • Process:
      • Replace createMock()/getMockBuilder() with Phony’s mock()/stub().
      • Example migration:
        // Before (PHPUnit)
        $mock = $this->createMock(SomeService::class);
        $mock->method('fetchData')->willReturn([]);
        
        // After (Phony)
        $mock = Phony::mock(SomeService::class);
        $mock->when('fetchData')->thenReturn([]);
        
      • Update assertions to use Phony’s syntax (e.g., verify() instead of expects()).
    • Validation: Ensure test outcomes match pre-migration results; fix discrepancies.
  3. Gradual Rollout:

    • Phased Adoption: Roll out to additional test suites, monitoring for:
      • Test failures due to mocking behavior differences.
      • Developer feedback on readability/ergonomics.
    • Documentation: Create internal docs for Phony syntax, common patterns (e.g., mocking closures, exceptions), and migration tips.
  4. Long-Term Strategy:

    • Option 1: Sunset Plan: If adopting Pest or Mockery, document a migration path to replace Phony (e.g., use Pest’s Phony integration).
    • Option 2: Fork: If critical, fork the package to maintain compatibility with newer PHPUnit/Laravel versions.
    • Option 3: Deprecate: Gradually phase out Phony in favor of PHPUnit’s native mocks (if improved sufficiently) or alternatives.

Compatibility

  • Laravel-Specific Considerations:

    • Service Container: Phony doesn’t mock Laravel’s DI container directly. Workarounds:
      • Mock concrete service classes instead of interfaces (e.g., App\Services\AnalyticsService instead of AnalyticsServiceInterface).
      • Use Laravel’s partialMock() (if available) or manual stubbing for container bindings.
    • Eloquent: Works for mocking repositories/models, but may not handle Laravel’s dynamic property access (e.g., with() clauses) natively.
    • Artisan/Console: Limited support for mocking Artisan commands or console inputs.
  • PHPUnit Plugins:

    • Conflicts with other PHPUnit extensions (e.g., phpunit-db-testcase). Test in isolation first.
  • CI/CD:

    • Ensure CI environments (e.g., GitHub Actions, GitLab CI) use compatible PHP/PHPUnit versions. Example:
      # GitHub Actions snippet
      services:
        php:
          image: php:8.1
      steps:
        - run: composer require --dev phpunit/phpunit:^9 eloquent/phony-phpunit
      

Sequencing

  1. Prerequisites:
    • Upgrade PHPUnit to 9.x if not already done (critical for compatibility).
    • Standardize on PHP 7.3+ (or 8.x) across the codebase.
  2. Dependencies:
    • Install as a dev dependency:
      composer require --dev eloquent/phony-phpunit
      
    • Add to composer.json under require-dev:
      "eloquent/phony-phpunit": "^7.1"
      
  3. Testing:
    • Run tests in a containerized environment to catch version conflicts early.
    • Use --filter to test migrated suites in isolation:
      phpunit --filter TestSuiteWithPhony
      
  4. Tooling:
    • Update IDE plugins (e.g., PHPStorm) to recognize Phony’s syntax for autocompletion.
    • Add Phony to static analysis tools (e.g., Psalm) if used
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.
cadot.eu/make
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