Installation Add the package via Composer:
composer require codeheures-fr/scrutineer
Publish the configuration (if needed):
php artisan vendor:publish --provider="Scrutineer\ScrutineerServiceProvider"
Basic Usage Register a test case in your Laravel routes or controller:
use Scrutineer\Scrutineer;
Route::get('/test-route', function () {
return Scrutineer::test('Test Route', function () {
// Your manual test steps here
return view('test-view');
});
});
First Test Case
Create a dedicated route (e.g., /scrutineer) to access the console:
Route::get('/scrutineer', [ScrutineerController::class, 'index']);
Define a simple test in ScrutineerController:
public function index()
{
return Scrutineer::run(function () {
Scrutineer::step('Verify Homepage Loads', function () {
$response = $this->get('/');
$response->assertStatus(200);
});
});
}
Define Test Contracts
Use Scrutineer::contract() to outline expected behavior before implementation:
Scrutineer::contract('User Login', [
'step1' => 'Navigate to login page',
'step2' => 'Enter valid credentials',
'step3' => 'Verify dashboard redirect',
]);
Integrate with Laravel Features
Route::middleware(['auth'])->group(function () {
Scrutineer::test('Admin Dashboard', function () {
// Test admin-only functionality
});
});
use Illuminate\Console\Command;
class RunScrutineerTests extends Command
{
protected $signature = 'scrutineer:run';
public function handle()
{
Scrutineer::runAll();
}
}
Step-Based Testing Break tests into reusable steps:
Scrutineer::step('Add to Cart', function () {
$this->post('/cart/add', ['product_id' => 1]);
$this->assertSessionHas('cart_item');
});
Data-Driven Tests
Use Laravel’s DatabaseSeeder or factories to populate test data:
Scrutineer::step('Test User Creation', function () {
$user = User::factory()->create();
$this->actingAs($user);
$this->get('/profile')->assertSee($user->email);
});
State Management
Scrutineer::withState() to persist data:
Scrutineer::withState(function () {
Auth::loginUsingId(1); // Preserve auth state
});
Configuration Quirks
SCRUTINEER_ENABLED is set to true in .env for production-like testing.config/scrutineer.php:
'theme' => 'dark',
'steps_per_page' => 5,
Debugging
Scrutineer::debug() to log intermediate state:
Scrutineer::step('Debug Response', function () {
$response = $this->get('/api/data');
Scrutineer::debug($response->json());
});
storage/logs/laravel.log) for failed assertions.Performance
Scrutineer::test('Slow Test', function () { ... })->tag('slow');
php artisan scrutineer:run --tag=smoke
Custom Assertions Extend the test client with helper methods:
use Scrutineer\ScrutineerTestCase;
class CustomScrutineerTestCase extends ScrutineerTestCase
{
public function assertCustomCondition($condition)
{
if (!$condition) {
$this->fail('Custom condition failed');
}
}
}
Plugin System
Hook into the scrutineer.running event to add pre/post-test logic:
Scrutineer::listen('scrutineer.running', function ($test) {
// Setup: e.g., mock external APIs
});
API Testing
Use Laravel’s HTTP tests within Scrutineer::step():
Scrutineer::step('API Validation', function () {
$response = $this->json('POST', '/api/login', ['email' => '[email protected]']);
$response->assertJsonStructure(['data' => ['token']]);
});
How can I help you explore Laravel packages today?