wubbleyou/boundaries
Composer 安装命令:
composer require wubbleyou/boundaries
包简介
Automatic test generation for validating route middleware and policy boundaries
README 文档
README
Boundaries is a DX tool used to generate unit tests to automatically test your routes against developer-determined rules.
Two rules are provided with out of the box with Boundaries, a MiddlewareRule and a PolicyRule
Installation
Install the package normally via Composer
composer require wubbleyou/boundaries
Usage
To get started you'll need to run the following command to generate the base test file:
php/sail artisan wubbleyou:generate-test
Configuration
Now you've got your test generated and saved in tests\Feature\Boundaries\BoundaryTest.php you might notice a trait has been placed in app\Traits\BoundaryRouteTrait.php which contains 2 methods, please read the additional information at the bottom to understand why you need to supply these.
getWhitelist()
The getWhitelist() method allows you to return an array of all the routes you want to be ignored by Wubbleyou\Boundaries.
return [
'login',
'register',
'homepage',
'about',
];
getRoutes()
The getRoutes() method allows you to return an array to specify the exact assertions that should be ran on each route, here's an example:
$admin = User::factory()->create(['is_admin', true]);
$userOne = User::factory()->create();
$userTwo = User::factory()->create();
return [
'users.change-password' => [
new MiddlewareRule(['web', 'auth', 'incorrect']),
new PolicyRule('get', 403, $userOne, ['user' => $userTwo]),
]
];
MiddlewareRule
The middleware rule tests your route against a certain set of middleware, if it doesn't match all of these middleware the test will fail.
MiddlewareRule also has an optional second parameter for $strict (default: false). If strict is enabled the supplied array of expected middleware must match exactly to the middleware present on the route. If strict is not enabled the middleware present on the route must contain all the expected middleware but can also contain other middleware we're not testing for.
new MiddlewareRule(['web', 'auth'], true),
PolicyRule
The policy rule performs a test against a specific route to test the HTTP status code. You supply it with:
- The request type (get/post/etc)
- The expected HTTP status code response (200, 404, 403, etc)
- A user to test the route as (this can be set to NULL to test unauthenticated) (default: null)
- Parameters the route expects (optional) (default: [])
And it will test that route and return an error if the response code doesn't match the one you've supplied.
Custom BoundaryRules
You can also generate custom BoundaryRules using the following command:
php/sail artisan wubbleyou:generate-rule RuleName
An example BoundaryRule would look like:
<?php
namespace App\BoundaryRules;
use Wubbleyou\Boundaries\BoundaryRules\BoundaryRule;
use Illuminate\Routing\Route;
use Tests\TestCase;
class TestingRule extends BoundaryRule {
public function handle(Route $route, TestCase $test, string $routeName): array
{
return ['This test failed because this array is not empty'];
}
}
Closures
You can also supply a Closure as a BoundaryRule instead:
function(Route $route) {
if($route->getName() !== 'hello') {
return ['Route name is not hello'];
}
return [];
},
Additional Information
Boundaries testing will fail if every route is not accounted for in either getWhitelist() or getRoutes(), if you want to generate a list of routes you that aren't in either of those run the following command:
php/sail artisan wubbleyou:missing-routes
Please note this command requires you to have generated the BoundaryRouteTrait to function, if you need to generate just the trait without any of the tests you can run:
php/sail artisan wubbleyou:generate-route-trait
wubbleyou/boundaries 适用场景与选型建议
wubbleyou/boundaries 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.41k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 04 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 wubbleyou/boundaries 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 wubbleyou/boundaries 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.41k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-04-24