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 Methods Trait Laravel Package

maks3w/phpunit-methods-trait

PHP trait exposing PHPUnit TestCase helper methods for use inside reusable test traits. Lets traits call mocking, expectations, and other TestCase APIs without extending TestCase directly; assertions are via PHPUnit\Framework\Assert.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require maks3w/phpunit-methods-trait
    

    Add the trait to your test trait or base test class:

    use Maks3w\PhpUnitMethodsTrait\Framework\TestCaseTrait;
    
  2. First Use Case: Use the trait in a test trait to access PHPUnit methods (e.g., mocking, assertions) without requiring TestCase inheritance:

    trait MockHelperTrait {
        use TestCaseTrait;
    
        public function createMock(string $className): object {
            return $this->getMockBuilder($className)->getMock();
        }
    }
    

Implementation Patterns

Common Workflows

  1. Test Traits for Reusable Logic: Extend the trait in a test trait to provide mocking/assertion helpers:

    trait DatabaseTestTrait {
        use TestCaseTrait;
    
        protected function mockDatabaseConnection(): void {
            $this->getMockBuilder(Connection::class)
                ->disableOriginalConstructor()
                ->getMock();
        }
    }
    
  2. Integration with Custom Test Cases: Use the trait in a base test class to avoid repeating method calls:

    abstract class BaseTestCase extends TestCase {
        use TestCaseTrait;
    }
    
  3. Static Assertions: For static assertions (e.g., Assert::assertTrue()), import PHPUnit\Framework\Assert directly:

    use PHPUnit\Framework\Assert;
    
    trait AssertionTrait {
        public function assertResponseIsValid(): void {
            Assert::assertTrue($this->response->isSuccessful());
        }
    }
    

IDE Autocomplete

  • The trait provides IDE hints for methods like:
    • getMockBuilder(), createMock(), assert*(), expects(), etc.
    • Works in PHPStorm, VSCode (with PHP Intelephense), and other IDEs supporting traits.

Gotchas and Tips

Pitfalls

  1. Static Assertions:

    • The trait does not include static Assert methods (e.g., Assert::assertTrue()).
    • Fix: Import PHPUnit\Framework\Assert manually in your trait/class.
  2. PHPUnit Version Mismatch:

    • The package supports PHPUnit 9.x (v9.0+). Using older versions may break autocompletion.
    • Fix: Update PHPUnit or use a compatible version of the package.
  3. Trait Conflicts:

    • If multiple traits use TestCaseTrait, method conflicts may arise.
    • Fix: Prefix methods or resolve conflicts explicitly.

Debugging

  • Missing Methods: If a method (e.g., assertArrayHasKey()) isn’t autocompleted:

    • Check if it’s a static Assert method (import manually).
    • Verify PHPUnit version compatibility.
  • IDE Not Recognizing Methods:

    • Trigger IDE index refresh (e.g., PHPStorm: File > Invalidate Caches).
    • Ensure the trait is used in the correct scope (not inside a class method).

Extension Points

  1. Customize Trait Methods: Override methods in your trait to add logic:

    trait CustomMockTrait {
        use TestCaseTrait;
    
        public function mockWithDefault(): object {
            return $this->getMockBuilder($this->className)
                ->setMethods(['defaultMethod'])
                ->getMock();
        }
    }
    
  2. Add New PHPUnit Methods: If a missing method is needed, fork the package and extend TestCaseTrait:

    trait ExtendedTestCaseTrait {
        use TestCaseTrait, \Maks3w\PhpUnitMethodsTrait\Framework\AssertTrait;
    }
    
  3. Combine with Other Traits: Use alongside Laravel’s RefreshDatabase or WithFaker traits:

    trait FeatureTestTrait {
        use TestCaseTrait, RefreshDatabase;
    }
    
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.
cocosmos/filament-sticky-save-bar
patrickbussmann/oauth2-apple
3brs/enterprise-security-bundle
anousss007/vigilance
supportpal/eloquent-model
ardenexal/fhir-models
laravel-at/laravel-image-sanitize
romalytar/yammi-audit-log-laravel
ardenexal/fhir-validation
arshaviras/weather-widget
laravel-chronicle/core
sunchayn/nimbus
daikazu/eloquent-salesforce-objects
unseen-codes/chat
romalytar/yammi-jobs-monitoring-laravel
kisame76/filament-db-table-state
nqxcode/laravel-lucene-search
dpfx/laravel-livewire-wizards
workos/workos-php-laravel
sofa/laravel-global-scope