ohffs/counts-database-queries-trait

Laravel testing trait to count and track database queries

1.0.2 2022-02-23 17:24 UTC

This package is auto-updated.

Last update: 2024-04-23 22:11:17 UTC


README

Trait for Laravel testing to count/assert about database queries

Installing

composer require ohffs/counts-database-queries-trait

Usage

<?php

namespace Tests\Feature;

use Ohffs\CountsDatabaseQueries\CountsDatabaseQueries;

class MyAmazingTest extends TestCase
{
    use CountsDatabaseQueries;

    /** @test */
    public function we_can_see_the_webpage()
    {
        $user = User::factory()->create();
        $stuff = Stuff::factory()->count(3)->create();
 
        // you'd usually want to start counting only after all your setup calls
        $this->countDatabaseQueries();

        $response = $this->actingAs($user)->get('/stuff');

        $response->assertOk();
        $response->assertSee('Stuff');
        $this->assertQueryCountEquals(6);
        $this->assertQueryCountBetween(5, 7);
        $this->assertQueryCountGreaterThan(5);
        $this->assertQueryCountLessThan(7);
        dd($this->getDatabaseQueries());
    }
}