PHP Classes

File: tests/Unit/Doctor/FindingTest.php

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

Contents

Class file image Download
<?php

declare(strict_types=1);

use
AlbertoArena\Truss\Doctor\Finding;
use
AlbertoArena\Truss\Doctor\Severity;

function
doctorFinding(
   
string $code = 'TRUSS-IDX-001',
   
string $table = 'posts',
    ?
string $column = 'user_id',
   
string $message = 'a message',
   
string $connection = 'mysql',
):
Finding {
    return new
Finding(
       
code: $code,
       
severity: Severity::Error,
       
connection: $connection,
       
table: $table,
       
column: $column,
       
message: $message,
       
hint: 'why it matters',
    );
}

it('fingerprints on code, connection, table and column, not the message', function () {
   
// A reworded message must not change the fingerprint, or a suppression breaks.
   
expect(doctorFinding(message: 'wording one')->fingerprint())
        ->
toBe(doctorFinding(message: 'totally different wording')->fingerprint());
});

it('changes the fingerprint when the column changes', function () {
   
expect(doctorFinding(column: 'user_id')->fingerprint())
        ->
not->toBe(doctorFinding(column: 'author_id')->fingerprint());
});

it('distinguishes a column-less finding from a column one', function () {
   
expect(doctorFinding(column: null)->fingerprint())
        ->
not->toBe(doctorFinding(column: 'user_id')->fingerprint());
});

it('changes the fingerprint when the connection or code changes', function () {
   
expect(doctorFinding(connection: 'mysql')->fingerprint())
        ->
not->toBe(doctorFinding(connection: 'tenant')->fingerprint());
   
expect(doctorFinding(code: 'TRUSS-IDX-001')->fingerprint())
        ->
not->toBe(doctorFinding(code: 'TRUSS-IDX-002')->fingerprint());
});

it('is stable across calls', function () {
   
$f = doctorFinding();
   
expect($f->fingerprint())->toBe($f->fingerprint());
});

it('copies itself with a new severity, every other field intact', function () {
   
$original = doctorFinding();
   
$changed = $original->withSeverity(Severity::Warning);

   
expect($changed->severity)->toBe(Severity::Warning)
        ->
and($changed->code)->toBe($original->code)
        ->
and($changed->table)->toBe($original->table)
        ->
and($changed->column)->toBe($original->column)
        ->
and($changed->message)->toBe($original->message)
       
// Identity is unchanged, so an override does not move a suppression.
       
->and($changed->fingerprint())->toBe($original->fingerprint());
});