Weave 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

Test Case Laravel Package

antimattr/test-case

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require --dev antimattr/test-case
    

    Add the trait to your test class:

    use Antimattr\TestCase\TestCase;
    
    class ExampleTest extends TestCase
    
  2. First Use Case Replace assertTrue() with assertIsTrue() or leverage built-in assertions like:

    $this->assertIsTrue(true);
    $this->assertIsFalse(false);
    $this->assertIsNull(null);
    $this->assertIsNotNull("string");
    
  3. Key Features to Explore

    • Data Providers: Use @dataProvider with dataProvider() method.
    • Mocking: Built-in mock() helper for cleaner mocking.
    • Assertions: Check src/TestCase.php for all available assertions (e.g., assertIsArray(), assertContains()).

Implementation Patterns

Workflows

  1. Test Data Management Use dataProvider() for reusable test data:

    public function dataProvider()
    {
        return [
            'valid case' => [true, true],
            'invalid case' => [false, false],
        ];
    }
    
    public function testBooleanValues(bool $input, bool $expected)
    {
        $this->assertIsTrue($input === $expected);
    }
    
  2. Mocking Objects Replace PHPUnit’s createMock() with:

    $mock = $this->mock(MyClass::class);
    $mock->method('doSomething')->willReturn('result');
    
  3. Assertion Chaining Combine assertions for cleaner tests:

    $this->assertIsArray($data)
          ->assertArrayHasKey('id')
          ->assertIsNotEmpty();
    

Integration Tips

  • Laravel-Specific: Extend Illuminate\Foundation\Testing\TestCase and include TestCase trait:
    use Antimattr\TestCase\TestCase;
    use Illuminate\Foundation\Testing\TestCase as LaravelTestCase;
    
    class FeatureTest extends LaravelTestCase
    {
        use TestCase;
    }
    
  • Custom Assertions: Override assertions in your test class:
    protected function assertIsCustom($value, $message = '')
    {
        $this->assertIsTrue($value === 'custom', $message);
    }
    

Gotchas and Tips

Pitfalls

  1. Assertion Method Conflicts Avoid naming custom methods assert*—they may override package assertions. Fix: Prefix with customAssert*.

  2. Mocking Quirks The mock() helper uses PHPUnit’s createMock() under the hood. Ensure your mocks extend the correct class:

    // ❌ Fails if MyClass doesn’t extend ArrayAccess
    $mock = $this->mock(MyClass::class);
    
    // ✅ Works
    $mock = $this->mock(MyClass::class)->makePartial();
    
  3. Data Provider Caching @dataProvider runs once per test method. For dynamic data, use yield:

    public function dataProvider()
    {
        yield 'dynamic' => [rand(), rand()];
    }
    

Debugging Tips

  • Assertion Failures: Use $this->fail() for explicit test failures with messages:
    $this->fail('Expected failure: ' . $this->getLastAssertionMessage());
    
  • Inspect Assertions: Chain ->debug() to dump variables:
    $this->assertIsArray($data)->debug($data)->assertNotEmpty();
    

Extension Points

  1. Custom Assertions Add assertions to your test class:
    protected function assertIsJson($value, $message = '')
    {
        $this->assertIsTrue(
            is_string($value) && json_validate($value),
            $message
        );
    }
    
  2. Global Setup/Teardown Override setUp()/tearDown() for shared test logic:
    protected function setUp(): void
    {
        parent::setUp();
        $this->app->singleton(MyService::class, fn() => new MyService());
    }
    
  3. Configuration Extend via TestCase constructor:
    public function __construct()
    {
        parent::__construct();
        $this->useShortAssertions = false; // Disable short assertion syntax
    }
    
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.
3mdikram/worldmoneywords
snowsoft/config
itmosoft/laravel-subscriptions
larva/laravel-ai-qwen
diephp/sequences
users-au/socialite-provider
twidpay/twid-logger
mrnewport/laravel-plaid
mrnewport/laravel-repo
mrnewport/laravel-docsign
mrnewport/laravel-stow
mrnewport/laravel-flow
mrnewport/laravel-priceable
mrnewport/laravel-nestedterms
fredmeng/laravel-apidoc-generator
itstructure/laravel-multilingual-tools
pdustdar/elasticsearch
markupai/php-sdk
rlacerda83/lumem-microservice-shipment
uni-method/symfony-lib