#PHPUnit Query Count Assertions for
#Laravel #PHP #testing #opensource
🧪 Count & assert SQL queries in your tests - catch N+1 problems, full table scans, duplicate queries & slow queries before production
⚡ Assert exact query counts, upper/lower bounds or ranges with closures like assertQueryCountMatches()
🔍 Detect N+1 problems using
#Laravel's built-in lazy loading prevention with assertNoLazyLoading()
📊 EXPLAIN-based index analysis detects full table scans, missing indexes, filesort & temporary tables on
#MySQL,
#MariaDB &
#SQLite
🔄 Duplicate query detection finds identical queries executed multiple times with location tracking
⏱️ Query timing assertions for individual query time & total execution time thresholds
🛡️ Combined assertQueriesAreEfficient() checks N+1, duplicates & missing indexes in one call
📋 Detailed failure messages show actual SQL, bindings & source locations
🔧 Configurable thresholds for scan warnings, query cost limits & small table optimization
🧩 Custom analyser support via QueryAnalyser interface for PostgreSQL or other databases
🎯 Works with
#PHPUnit setUp() &
#Pest beforeEach() including "paranoid mode"
📦 composer require --dev mattiasgeniar/phpunit-query-count-assertions
Requires PHP 8.2+, Laravel 11/12, PHPUnit 11
github.com/mattiasgeniar/…