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 unit testing framework with simple assertions, clear output, and easy CLI running. It supports writing isolated tests, reporting failures nicely, and integrates well into CI pipelines for fast, reliable test suites.

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. Basic Test File: Create tests/MyTest.php:

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

    vendor/bin/tester tests/
    

First Use Case: Assertions

public function testAssertions()
{
    $this->assert($value === 'expected');
    $this->assertNull($nullValue);
    $this->assertException(function () {
        throw new RuntimeException('Expected');
    });
}

Implementation Patterns

Test Workflows

  1. Data-Driven Testing:

    #[DataProvider('dataProvider')]
    public function testWithData($input, $expected)
    {
        $this->assert($this->transform($input) === $expected);
    }
    
    public function dataProvider()
    {
        return [
            ['input1', 'output1'],
            ['input2', 'output2'],
        ];
    }
    
  2. Mocking & Isolation:

    public function testWithMock()
    {
        $mock = $this->getMockBuilder(Service::class)
            ->disableOriginalConstructor()
            ->getMock();
        $mock->method('doWork')->willReturn('mocked');
    
        $this->assert($mock->doWork() === 'mocked');
    }
    
  3. HTTP Testing (via HttpAssert):

    public function testHttpResponse()
    {
        $response = $this->get('/api/users');
        $this->assert($response->code === 200);
        $this->assert($response->json['id'] === 1);
    }
    

Integration Tips

  • Parallel Execution:
    vendor/bin/tester --parallel tests/
    
  • Code Coverage:
    vendor/bin/tester --coverage tests/
    
  • Watch Mode (auto-run on file changes):
    vendor/bin/tester --watch tests/
    

Laravel-Specific Patterns

  1. Service Container Testing:

    public function testServiceBinding()
    {
        $this->assertTrue(app()->bound('MyService'));
        $this->assertInstanceOf(MyService::class, app('MyService'));
    }
    
  2. Route Testing:

    public function testRoute()
    {
        $response = $this->get('/dashboard');
        $this->assert($response->code === 302);
        $this->assert($response->headers['location'] === '/login');
    }
    
  3. Database Transactions:

    public function testDatabaseOperation()
    {
        $this->beginTransaction();
        User::create(['name' => 'Test']);
        $this->assert(User::count() === 1);
        $this->rollBack();
    }
    

Gotchas and Tips

Pitfalls

  1. System php.ini:

    • Since v2.6.0, Tester uses the system php.ini by default. Use --config or -C to override:
      vendor/bin/tester --config=php.ini tests/
      
  2. Parallel Execution:

    • Windows users: Ensure PHP 8.5+ for true parallelism (not just sequential with threads).
    • Linux: Avoid paths/args with spaces/special chars (passed as arrays to proc_open()).
  3. Assertion Errors:

    • getResult() throws LogicException if called before test completion. Use Test::getResult() only after the test finishes.
  4. Data Providers:

    • Empty data sets now fail/skip tests (v2.4.1+). Validate providers:
      public function dataProvider()
      {
          return [
              ['valid', 'data'],
              // Avoid: []
          ];
      }
      
  5. IDE Hints:

    • Use @var or PHPDoc for complex types (e.g., #[Assert\Type('array<int, string>')]).

Debugging Tips

  1. Dumper:

    • Use dump() for debugging:
      dump($complexObject, 'object_name');
      
    • Outputs to PhpStorm terminal with clickable links (requires Awesome Console).
  2. Error Output:

    • Capture stderr via temp files (v2.5.0+):
      $this->assertNoError(function () {
          // Code that may trigger errors
      }, 'Custom error message');
      
  3. Slow Tests:

    • Measure runtime:
      $this->benchmark(function () {
          // Slow operation
      }, 'Operation took {time}ms');
      

Extension Points

  1. Custom Assertions:

    use Tester\Assert;
    
    Assert::add('isEven', function ($value) {
        return $value % 2 === 0;
    });
    
    $this->assertIsEven(4);
    
  2. Test Lifecycle Hooks:

    public static function before()
    {
        // Runs before all tests
    }
    
    public static function after()
    {
        // Runs after all tests
    }
    
  3. Environment Setup:

    public static function setup()
    {
        putenv('APP_ENV=testing');
    }
    
  4. Custom Templates:

    • Override coverage.html in tests/_templates/ for custom reports.

Laravel-Specific Quirks

  1. Artisan Commands:

    • Use Artisan::call() in tests:
      $this->assertArtisanSuccess('migrate:fresh');
      
  2. Queue Testing:

    public function testQueueJob()
    {
        $job = new ProcessPodcast();
        $this->assertQueueJob($job);
    }
    
  3. Session/Authentication:

    public function testAuthenticatedRoute()
    {
        $this->actingAs(User::first());
        $this->get('/profile')->assertOk();
    }
    
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.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope