PHP Classes

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

Recommend this page to a friend!
  Packages of Alberto Arena   Laravel Truss   tests/Unit/Doctor/Rules/PolymorphicWithoutIndexTest.php   Download  
File: tests/Unit/Doctor/Rules/PolymorphicWithoutIndexTest.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: 8 days ago
Size: 1,358 bytes
 

Contents

Class file image Download
<?php

declare(strict_types=1);

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

it('flags a *_type/*_id pair with no composite index', function () {
   
$snapshot = SchemaBuilder::make()
        ->
table('comments', fn ($t) => $t->id()
            ->
string('commentable_type')
            ->
column('commentable_id', 'bigint unsigned'))
        ->
build();

   
expect(doctorCheck(new PolymorphicWithoutIndex, $snapshot))
        ->
toHaveFinding('TRUSS-INT-009', table: 'comments', column: 'commentable_type');
});

it('is clean when the morph pair has its composite index', function () {
   
$snapshot = SchemaBuilder::make()
        ->
table('comments', fn ($t) => $t->id()->morphs('commentable'))
        ->
build();

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

it('does not treat a lone *_type column as a morph pair', function () {
   
// A "type" column with no matching *_id is not a polymorphic relation.
   
$snapshot = SchemaBuilder::make()
        ->
table('products', fn ($t) => $t->id()->string('product_type'))
        ->
build();

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

it('is heuristic', function () {
   
expect((new PolymorphicWithoutIndex)->confidence()->value)->toBe('heuristic');
});