定制 structuraphp/structura-phpunit 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

structuraphp/structura-phpunit

最新稳定版本:0.0.1

Composer 安装命令:

composer require structuraphp/structura-phpunit

包简介

Architectural testing for PHP with phpunit

README 文档

README

License PHP from Packagist

PHPUnit extension for Structura.

Full documentation is available at structuraphp.github.io/structura.

Requirements

PHP version

PHPUnit Version PHP Version Structura ^0.7
<= 10.x <= 8.1 ✗ Unsupported
11.x / 12.x / 13.x 8.2 / 8.3 / 8.4 / 8.5 ✓ Supported

Installation

Using Composer

composer required --dev structuraphp/structura-phpunit

Usage

Here's an example of an architecture test for Laravel's HTTP scope:

<?php

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);
    }
}

Then run your PHPUnit tests with the following command:

vendor/bin/phpunit --filter=Architecture

统计信息

  • 总下载量: 2
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-04-12

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固