PHP Classes

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

Recommend this page to a friend!
  Packages of Alberto Arena   Laravel Truss   tests/Unit/Doctor/Rules/ForeignKeyWithoutIndexTest.php   Download  
File: tests/Unit/Doctor/Rules/ForeignKeyWithoutIndexTest.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,888 bytes
 

Contents

Class file image Download
<?php

declare(strict_types=1);

use
AlbertoArena\Truss\Doctor\Rules\Index\ForeignKeyWithoutIndex;
use
AlbertoArena\Truss\Tests\Support\SchemaBuilder;

it('flags an unindexed foreign key as an error on postgresql', function () {
   
$snapshot = SchemaBuilder::make(driver: 'pgsql')
        ->
table('users', fn ($t) => $t->id())
        ->
table('posts', fn ($t) => $t->id()->foreignId('user_id')->on('users'))
        ->
build();

   
$findings = doctorCheck(new ForeignKeyWithoutIndex, $snapshot);

   
expect($findings)->toHaveFinding('TRUSS-IDX-001', table: 'posts', column: 'user_id')
        ->
and($findings[0]->severity->value)->toBe('error');
});

it('is only info on mysql, which indexes foreign keys automatically', function () {
   
$snapshot = SchemaBuilder::make(driver: 'mysql')
        ->
table('users', fn ($t) => $t->id())
        ->
table('posts', fn ($t) => $t->id()->foreignId('user_id')->on('users'))
        ->
build();

   
expect(doctorCheck(new ForeignKeyWithoutIndex, $snapshot)[0]->severity->value)->toBe('info');
});

it('is clean when the foreign key column is indexed', function () {
   
$snapshot = SchemaBuilder::make(driver: 'pgsql')
        ->
table('users', fn ($t) => $t->id())
        ->
table('posts', fn ($t) => $t->id()->foreignId('user_id')->on('users')->index('user_id'))
        ->
build();

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

it('accepts a composite index that leads with the foreign key column', function () {
   
$snapshot = SchemaBuilder::make(driver: 'pgsql')
        ->
table('users', fn ($t) => $t->id())
        ->
table('posts', fn ($t) => $t->id()
            ->
foreignId('user_id')->on('users')
            ->
column('created_at', 'timestamp', true)
            ->
index(['user_id', 'created_at']))
        ->
build();

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