PHP code example of structuraphp / structura-phpunit

1. Go to this page and download the library: Download structuraphp/structura-phpunit library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

structuraphp / structura-phpunit example snippets




declare(strict_types=1);

namespace Tests\Architecture;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Resources\Json\JsonResource;
use PHPUnit\Framework\TestCase;
use StructuraPhp\Structura\Expr;
use StructuraPhp\Structura\ExprScript;
use StructuraPhp\StructuraPhpunit\ArchitectureAsserts;

final class ArchitectureHttpTest extends TestCase
{
    use ArchitectureAsserts;
    
    private static string $dir;
    
    public static function setUpBeforeClass(): void
    {
        self::$dir = dirname(__DIR__, 2).'/app/Http';
    }

    public function testHttpRequestsArchitecture(): void
    {
        $rules = $this
            ->allClasses()
            ->fromDir(self::$dir.'/Requests')
            ->should(
                static fn(Expr $assert): Expr => $assert
                    ->toExtend(FormRequest::class)
                    ->toHaveSuffix('Request')
                    ->or(static fn(Expr $assert) => $assert
                        ->toHaveMethod('attributes')
                        ->toHaveMethod('messages')
                    ),
            );

        self::assertRules($rules);
    }

    public function testHttpControllersArchitecture(): void
    {
        $rules = $this
            ->allClasses()
            ->fromDir(self::$dir.'/Controllers')
            ->should(
                static fn(Expr $assert): Expr => $assert
                    ->toExtend(Controller::class)
                    ->toBeFinal()
                    ->toHaveSuffix('Controller'),
            );

        self::assertRules($rules);
    }

    public function testHttpResourcesArchitecture(): void
    {
        $rules = $this
            ->allClasses()
            ->fromDir(self::$dir.'/Resources')
            ->should(
                static fn(Expr $assert): Expr => $assert
                    ->toExtend(JsonResource::class)
                    ->toBeFinal()
                    ->toHaveSuffix('Resources')
                    ->toHaveMethod('toArray'),
            );

        self::assertRules($rules);
    }
}