PHP Classes

File: tests/Unit/Doctor/Formatters/JsonFormatterTest.php

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

Contents

Class file image Download
<?php

declare(strict_types=1);

use
AlbertoArena\Truss\Doctor\Finding;
use
AlbertoArena\Truss\Doctor\FindingCollection;
use
AlbertoArena\Truss\Doctor\Formatters\JsonFormatter;
use
AlbertoArena\Truss\Doctor\Severity;

it('renders findings and a summary as json', function () {
   
$findings = (new FindingCollection(
        new
Finding('TRUSS-INT-001', Severity::Error, 'mysql', 'logs', null, 'no pk', 'hint'),
        new
Finding('TRUSS-IDX-006', Severity::Warning, 'mysql', 'users', 'email', 'no unique', 'hint', 'alter table ...'),
    ))->
sorted();

   
$data = json_decode((new JsonFormatter)->format($findings), true, flags: JSON_THROW_ON_ERROR);

   
expect($data['summary'])->toBe(['total' => 2, 'error' => 1, 'warning' => 1, 'info' => 0]);
   
expect($data['findings'][0]['code'])->toBe('TRUSS-INT-001');
   
expect($data['findings'][0])->toHaveKeys(['code', 'severity', 'connection', 'table', 'column', 'message', 'hint', 'suggestion', 'fingerprint']);
   
expect($data['findings'][1]['column'])->toBe('email');
   
expect($data['findings'][1]['suggestion'])->toBe('alter table ...');
});

it('renders an empty finding list for a clean schema', function () {
   
$data = json_decode((new JsonFormatter)->format(new FindingCollection), true, flags: JSON_THROW_ON_ERROR);

   
expect($data['findings'])->toBe([]);
   
expect($data['summary']['total'])->toBe(0);
});