PHP Classes

File: tests/Unit/Doctor/Rules/MissingPrimaryKeyTest.php

Recommend this page to a friend!
  Packages of Alberto Arena   Laravel Truss   tests/Unit/Doctor/Rules/MissingPrimaryKeyTest.php   Download  
File: tests/Unit/Doctor/Rules/MissingPrimaryKeyTest.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Laravel Truss
View a diagram of a Laravel application models
Author: By
Last change:
Date: 9 days ago
Size: 1,393 bytes
 

Contents

Class file image Download
<?php

declare(strict_types=1);

use
AlbertoArena\Truss\Doctor\Rules\Integrity\MissingPrimaryKey;
use
AlbertoArena\Truss\Tests\Support\SchemaBuilder;

it('flags a table with no primary key', function () {
   
$snapshot = SchemaBuilder::make()
        ->
table('logs', fn ($t) => $t->string('message'))
        ->
build();

   
expect(doctorCheck(new MissingPrimaryKey, $snapshot))
        ->
toHaveFinding('TRUSS-INT-001', table: 'logs');
});

it('is clean when the table has a primary key', function () {
   
$snapshot = SchemaBuilder::make()
        ->
table('users', fn ($t) => $t->id()->string('email'))
        ->
build();

   
expect(doctorCheck(new MissingPrimaryKey, $snapshot))->toBeClean();
});

it('accepts a composite primary key (pivot table)', function () {
   
$snapshot = SchemaBuilder::make()
        ->
table('role_user', fn ($t) => $t->integer('role_id')->integer('user_id')->primary(['role_id', 'user_id']))
        ->
build();

   
expect(doctorCheck(new MissingPrimaryKey, $snapshot))->toBeClean();
});

it('reports the connection on the finding', function () {
   
$snapshot = SchemaBuilder::make('tenant')
        ->
table('logs', fn ($t) => $t->string('message'))
        ->
build();

   
$findings = doctorCheck(new MissingPrimaryKey, $snapshot, 'tenant');

   
expect($findings[0]->connection)->toBe('tenant')
        ->
and($findings[0]->severity->value)->toBe('error');
});