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

Tester Laravel Package

nette/tester

Nette Tester is a lightweight PHP testing framework for unit tests and test suites. It offers simple assertions, data-driven tests, informative output, and easy CLI integration. Ideal for fast, reliable testing in PHP projects and CI pipelines.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require nette/tester --dev
    

    Add to composer.json under require-dev:

    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    }
    
  2. First Test File: Create tests/MyTest.php:

    <?php
    use Tester\TestCase;
    
    class MyTest extends TestCase
    {
        public function testBasicAssertion()
        {
            $this->test(1 + 1 === 2);
        }
    }
    
  3. Run Tests:

    vendor/bin/tester tests/MyTest.php
    

First Use Case: Assertions

public function testLaravelSpecific()
{
    $response = new \Illuminate\Http\Response('Hello');
    $this->test($response->getContent() === 'Hello');
    $this->test($response->getStatusCode() === 200);
    $this->testException(function () {
        throw new \RuntimeException('Expected error');
    }, 'RuntimeException');
}

Implementation Patterns

1. Test Structure

Laravel Integration Pattern:

class UserTest extends TestCase
{
    protected function setUp(): void
    {
        parent::setUp();
        $this->app = require __DIR__.'/../bootstrap/app.php';
        $this->app->make(\Illuminate\Contracts\Http\Kernel::class)->bootstrap();
    }

    public function testUserCreation()
    {
        $user = \App\Models\User::factory()->create();
        $this->test($user->exists);
        $this->test($user->email !== null);
    }
}

2. Data Providers

public function providerUserRoles()
{
    return [
        ['admin', 'Administrator'],
        ['user', 'Regular User'],
    ];
}

public function testUserRoles($role, $description)
{
    $user = \App\Models\User::factory()->create(['role' => $role]);
    $this->test($user->role === $role);
    $this->test($user->description === $description);
}

3. Mocking HTTP Requests

public function testGetUser()
{
    $response = $this->get('/api/user/1');
    $this->test($response->getStatusCode() === 200);
    $this->test($response->json()['id'] === 1);
}

4. Database Transactions

protected function setUp(): void
{
    parent::setUp();
    $this->beginDatabaseTransaction();
}

protected function tearDown(): void
{
    $this->rollbackDatabaseTransaction();
    parent::tearDown();
}

5. Parallel Test Execution

vendor/bin/tester tests/ --parallel

Gotchas and Tips

Common Pitfalls

  1. System php.ini Usage:

    • Since v2.6.0, Tester uses system php.ini by default. Use -C flag to override:
      vendor/bin/tester tests/ -C /path/to/custom/php.ini
      
  2. Test Lifecycle:

    • tearDown() runs even when tests fail (since v2.6.0). Avoid side effects in assertions.
  3. Assertion Order:

    • Assert::equal() with $matchOrder = true can cause false positives if array order matters.
  4. Parallel Execution:

    • Windows users should use PHP 8.5+ for true parallelism (not just process forking).

Debugging Tips

  1. Verbose Output:

    vendor/bin/tester tests/ -v
    
  2. Console Lines Mode:

    vendor/bin/tester tests/ --console-lines
    
  3. Dumper Integration:

    $this->dump($this->app->make(\App\Models\User::class));
    
  4. Error Output Capture:

    $this->testException(function () {
        // Code that might fail
    }, 'RuntimeException', 'Expected error message');
    

Extension Points

  1. Custom Assertions:

    $this->assertDatabaseHas('users', ['email' => 'test@example.com']);
    
  2. Test Templates: Extend TestCase for shared setup:

    abstract class LaravelTestCase extends TestCase
    {
        protected $app;
    
        protected function setUp(): void
        {
            $this->app = require __DIR__.'/../../bootstrap/app.php';
            $this->app->make(\Illuminate\Contracts\Http\Kernel::class)->bootstrap();
        }
    }
    
  3. Code Coverage:

    vendor/bin/tester tests/ --coverage
    
  4. Custom Output Formats:

    vendor/bin/tester tests/ --format=junit
    

Laravel-Specific Quirks

  1. Service Container:

    • Use app() helper or $this->app->make() instead of direct instantiation.
  2. Artisan Commands:

    $this->artisan('migrate:fresh')
        ->expectsQuestion('confirm', 'yes')
        ->assertExitCode(0);
    
  3. Event Testing:

    $this->app->make(\Illuminate\Contracts\Events\Dispatcher::class)
        ->listen('user.created', function () {
            // Assert event was fired
        });
    
  4. Queue Testing:

    $this->app->make(\Illuminate\Contracts\Queue\Factory::class)
        ->fake()
        ->assertPushed(\App\Jobs\SendEmail::class);
    
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.
codraw/framework-extra-bundle
codraw/messenger
codraw/security
codraw/mailer
codraw/contracts
codraw/profiling
codraw/dependency-injection
codraw/tester
codraw/core
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony