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

Phpunit Double Colon Syntax Laravel Package

sanmai/phpunit-double-colon-syntax

Run individual PHPUnit test methods with the familiar file::method syntax (like pytest). This dev package rewrites args via Composer autoload so vendor/bin/phpunit tests/FooTest.php::testBar becomes file + --filter automatically. Supports multiple methods; PHPUnit 6+.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require --dev sanmai/phpunit-double-colon-syntax
    

    Add it to your composer.json under require-dev.

  2. First Use Case: Run a single test method using the familiar pytest-style syntax:

    vendor/bin/phpunit tests/Feature/UserTest.php::testLogin
    

    This replaces the need for --filter and reduces ambiguity.

Where to Look First

  • README.md: For quick installation and basic usage.
  • docs/index.md: For detailed examples and troubleshooting.
  • GitHub Issues: Check for edge cases or compatibility notes (though minimal).

Implementation Patterns

Usage Patterns

  1. Single Test Execution:

    vendor/bin/phpunit tests/Unit/ExampleTest.php::testExampleMethod
    

    Equivalent to:

    vendor/bin/phpunit --filter testExampleMethod tests/Unit/ExampleTest.php
    
  2. Multiple Tests in One Command:

    vendor/bin/phpunit tests/Unit/UserTest.php::testCreate tests/Unit/UserTest.php::testUpdate
    
  3. Integration with Laravel’s Test Structure:

    vendor/bin/phpunit tests/Feature/Auth/LoginTest.php::testGuestCannotLogin
    vendor/bin/phpunit tests/Unit/Http/Controllers/UserControllerTest.php::testIndex
    
  4. CI/CD Pipelines: Use the syntax in GitHub Actions, Laravel Forge, or Envoyer for explicit test targeting:

    # GitHub Actions example
    - run: vendor/bin/phpunit tests/Unit/UserTest.php::testCreate
    

Workflows

  • Debugging Workflow: Replace ambiguous --filter calls with explicit file::method syntax to avoid typos or partial matches.

    # Before (risk of typos or partial matches)
    vendor/bin/phpunit --filter testCreate
    
    # After (explicit and unambiguous)
    vendor/bin/phpunit tests/Unit/UserTest.php::testCreate
    
  • Team Onboarding: Standardize test commands across teams familiar with pytest/Jest, reducing cognitive load for hybrid teams.

  • Test Isolation: Use the syntax to run tests in isolation during development, especially in large Laravel applications with modular test suites.

Integration Tips

  1. Laravel Artisan Aliases: Add a custom alias in ~/.bashrc or ~/.zshrc for convenience:

    alias phpunit="vendor/bin/phpunit"
    

    Now you can use:

    phpunit tests/Feature/UserTest.php::testLogin
    
  2. IDE Support: Configure your IDE (PHPStorm, VSCode) to recognize the file::method syntax in run configurations for PHPUnit.

  3. Custom Scripts: Wrap the command in a script (e.g., run-test) to add pre/post-processing logic while maintaining the double-colon syntax.

  4. Laravel Packages: Document the syntax in your package’s README.md to help users run tests easily:

    ## Running Tests
    ```bash
    composer test
    vendor/bin/phpunit tests/Feature/PackageTest.php::testFeatureX
    
    
    

Gotchas and Tips

Pitfalls

  1. Incompatibility with --filter: The package does not work when combined with --filter in the same command. Choose one syntax or the other:

    # Works (double-colon)
    vendor/bin/phpunit tests/UserTest.php::testLogin
    
    # Works (native PHPUnit)
    vendor/bin/phpunit --filter testLogin tests/UserTest.php
    
    # Fails (mixed syntax)
    vendor/bin/phpunit --filter testLogin tests/UserTest.php::testLogin
    
  2. Static Analysis Tools: Tools like Psalm or PHPStan may flag the file::method syntax as invalid since it’s not native to PHPUnit. Add an ignore rule or configure the tool to skip the transformed arguments.

  3. Non-vendor/bin/phpunit Binaries: The package only intercepts vendor/bin/phpunit. If you use a global phpunit binary or a custom alias, the syntax won’t work. Ensure you’re using the local vendor binary:

    ./vendor/bin/phpunit tests/UserTest.php::testLogin
    
  4. Windows Paths: On Windows, ensure paths use forward slashes or escape backslashes:

    vendor/bin/phpunit tests\Unit\UserTest.php::testLogin
    

    Or:

    vendor/bin/phpunit tests/Unit/UserTest.php::testLogin
    
  5. Test Method Naming: The package relies on exact method names. If your test method uses spaces or special characters (e.g., test user login), ensure the syntax matches exactly:

    # Fails (spaces in method name)
    vendor/bin/phpunit tests/UserTest.php::test user login
    
    # Works (use underscores or camelCase)
    vendor/bin/phpunit tests/UserTest.php::testUserLogin
    

Debugging

  1. Verify Installation: Check if the package is loaded by running:

    composer show sanmai/phpunit-double-colon-syntax
    

    Ensure it appears in require-dev.

  2. Check Autoloader: The package hooks into Composer’s autoloader. If it’s not working, clear Composer’s cache:

    composer clear-cache
    
  3. Test with a Simple Command: Run a basic test to confirm the syntax works:

    vendor/bin/phpunit tests/Unit/ExampleTest.php::testTrue
    

    If this fails, check for typos or path issues.

  4. Enable PHPUnit Debugging: Use --debug to inspect how arguments are processed:

    vendor/bin/phpunit --debug tests/UserTest.php::testLogin
    

Tips

  1. Leverage for CI/CD: Use the syntax in CI pipelines to run specific tests for faster feedback:

    # GitHub Actions example
    - name: Run specific test
      run: vendor/bin/phpunit tests/Unit/UserTest.php::testCreate
    
  2. Combine with Laravel’s Test Helpers: Use the syntax alongside Laravel’s test helpers (e.g., actingAs, refreshDatabase) in your test methods:

    public function testUserCanLogin()
    {
        // Test logic here
    }
    

    Run it with:

    vendor/bin/phpunit tests/Feature/Auth/LoginTest.php::testUserCanLogin
    
  3. Document the Syntax: Add a section to your project’s CONTRIBUTING.md or README.md to educate contributors:

    ## Running Tests
    Use the `file::method` syntax for clarity:
    ```bash
    vendor/bin/phpunit tests/Feature/UserTest.php::testCreate
    
    
    
  4. Alias for PestPHP Users: If your team uses PestPHP, consider migrating to Pest for native double-colon support and a more expressive testing syntax. This package is a good stopgap for PHPUnit users.

  5. Performance Note: The package adds minimal overhead since it only transforms arguments before PHPUnit starts. No runtime performance impact is expected.

  6. Future-Proofing: The package supports PHPUnit 6–13 and PHP 7.1–8.5+. If you’re using Laravel (PHP 8.0+), you’re covered for the foreseeable future. Monitor the GitHub repo for updates.

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.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
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