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 Test Case Laravel Package

herrera-io/phpunit-test-case

PHPUnit TestCase class and Extras trait that add helpers for common test chores: create/delete temporary files and directories, call protected/private methods, and get/set protected/private properties. Use as a base class or mix into your own test case.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require herrera-io/phpunit-test-case=1.*
    

    (Note: Requires PHPUnit 3.7.x—ensure your project’s phpunit/phpunit dependency is pinned to ^3.7 in composer.json.)

  2. Basic Setup: Choose either the TestCase class or the Extras trait (not both):

    // Option 1: Extend TestCase
    use Herrera\PHPUnit\TestCase;
    
    class UserTest extends TestCase
    {
        // Tests...
    }
    
    // Option 2: Use Extras trait (with PHPUnit_Framework_TestCase)
    use Herrera\PHPUnit\Extras;
    
    class UserTest extends \PHPUnit_Framework_TestCase
    {
        use Extras;
    }
    
  3. First Use Case: Test a protected method in a Laravel model:

    public function testProtectedValidation()
    {
        $user = new User(['email' => 'test@example.com']);
        $result = $this->callProtectedMethod('validateOnly', ['email'], $user);
        $this->assertTrue($result);
    }
    

Implementation Patterns

Core Laravel Integration

  1. Service Container Testing: Use callPrivateMethod() to test private container bindings:

    public function testPrivateBinding()
    {
        $service = $this->callPrivateMethod('resolve', ['MyService'], $this->app);
        $this->assertInstanceOf(MyService::class, $service);
    }
    
  2. Artisan Command Logic: Access protected command methods:

    public function testCommandLogic()
    {
        $command = new MyCommand();
        $output = $this->callPrivateMethod('execute', [], $command);
        $this->assertContains('Success', $output);
    }
    
  3. File-Based Tests: Combine with Laravel’s Storage facade for temp files:

    public function testFileUpload()
    {
        $tempFile = $this->createTempFile('upload', 'txt');
        Storage::fake('local')->put('test.txt', 'content');
        $this->assertTrue(file_exists($tempFile));
    }
    

Workflow Tips

  • Trait Composition: Use alongside Laravel’s TestCase for hybrid testing:

    use Herrera\PHPUnit\Extras;
    use Illuminate\Foundation\Testing\TestCase as LaravelTestCase;
    
    class MyTest extends LaravelTestCase
    {
        use Extras;
    
        protected function tearDown(): void
        {
            parent::tearDown(); // Laravel's tearDown
            $this->cleanupExtras(); // Extras' cleanup
        }
    }
    
  • Database + Temp Files: Pair with refreshDatabase() for atomic tests:

    public function testFileUploadWithDB()
    {
        $this->refreshDatabase();
        $tempDir = $this->createTempDir();
        // Test logic...
        $this->assertDatabaseHas('uploads', ['path' => $tempDir]);
    }
    

Gotchas and Tips

Pitfalls

  1. PHPUnit 3.7 Lock-In:

    • Error: Class 'PHPUnit_Framework_TestCase' not found if using PHPUnit 8+.
    • Fix: Pin phpunit/phpunit:^3.7 in composer.json or fork the package.
  2. TearDown Hell:

    • Gotcha: Forgetting parent::tearDown() causes temp files to persist.
    • Fix: Override tearDown() and chain calls:
      protected function tearDown(): void
      {
          $this->cleanupExtras(); // Extras' cleanup
          parent::tearDown();    // Laravel/PHPUnit cleanup
      }
      
  3. Reflection Warnings:

    • Gotcha: callPrivateMethod() may trigger E_STRICT warnings in PHP 7+.
    • Fix: Suppress warnings or use a wrapper:
      @$this->callPrivateMethod('method', [$arg], $object);
      
  4. Symfony/Process Deprecations:

    • Gotcha: symfony/process:2.1 may fail on modern OSes (e.g., macOS).
    • Fix: Downgrade or replace with symfony/process:^6.0.

Debugging Tips

  • Temp File Leaks:

    • Check $this->getTempDir() to list remaining temp files.
    • Use dd($this->getTempFiles()) to debug cleanup.
  • Private Method Failures:

    • Verify method names exactly match (case-sensitive).
    • Use get_class_methods($object) to list available methods.

Extension Points

  1. Custom Cleanup: Override cleanupExtras() to add logic:

    protected function cleanupExtras(): void
    {
        parent::cleanupExtras();
        // Custom cleanup (e.g., delete logs)
    }
    
  2. Temp File Naming: Extend createTempFile()/createTempDir() for custom prefixes:

    $this->createTempFile('laravel_', 'log');
    
  3. Laravel-Specific Helpers: Add static methods to the trait for Laravel use cases:

    // In Extras.php (forked)
    public static function callPrivateServiceMethod(string $method, array $args, $service)
    {
        return static::callPrivateMethod($method, $args, app($service));
    }
    
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.
nasirkhan/laravel-sharekit
directorytree/privacy-filter-classifier
directorytree/privacy-filter
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata
splash/openapi
splash/scopes
splash/toolkit
testo/output-teamcity
testo/bridge-symfony