saritasa / laravel-testbed
此包已废弃,不再维护。未建议替代包。
Laravel Testbed
0.0.2
2020-12-01 11:42 UTC
Requires
- php: >=7.2
- saritasa/dingo-api-custom: ^2.2
Requires (Dev)
- laravel/framework: ^5.6 || ^6.0 || ^7.0 || ^8.0
- mockery/mockery: ^1.3
- phpunit/phpunit: ^7.0
- squizlabs/php_codesniffer: ^3.0
This package is auto-updated.
Last update: 2024-09-04 20:56:20 UTC
README
提供功能测试辅助,如API排序、身份验证、注册等。
用法
安装 saritasa/laravel-testbed
包
$ composer require saritasa/laravel-testbed
ApiListSortingCheck 特性
用于检查排序是否正确的工作特性。
使用特性
use Saritasa\LaravelTestbed\Traits\ApiListSortingCheck; /** * Vendor tests. */ class VendorTest extends TestCase { use ApiListSortingCheck; }
可用函数
/** * Check that API returns list sorted by specified field (order by single field - check for each of passed fields). * * @param string $url Api endpoint to check * @param int $count Count of created models * @param array|string[] $sortingFields Sorting fields to check * @param array|string[] $auth Auth * @param string|null $envelope Results envelope (like 'results', 'items', etc.) * * @return void */ public function assertSortingWorks(string $url, int $count, array $sortingFields, array $auth, ?string $envelope = null): void /** * Check that API returns list sorted by specified fields * (order by multiple fields - check for combinations of passed fields). * * @param string $url Api endpoint to check * @param int $count Count of created models * @param array|string[] $sortingFields Sorting fields to check * @param array|string[] $auth Auth * @param string|null $envelope Results envelope (like 'results', 'items', etc.) * * @return void */ public function assertMultiSortingWorks(string $url, int $count, array $sortingFields, array $auth, ?string $envelope = null): void
示例
/** Sorting options test */ public function testOrderBy() { $count = 15; $auth = Helpers::createAndAuthenticateUser(); factory(Vendor::class, $count)->create(); $envelope = 'results'; $this->assertSortingWorks("/api/vendors", $count, VendorListRequest::SORTING_FIELDS, $auth, $envelope); } /** Multi sorting options test */ public function testMultiOrderBy() { $count = 15; $auth = Helpers::createAndAuthenticateUser(); factory(Vendor::class, $count)->create(); $envelope = 'results'; $this->assertMultiSortingWorks("api/vendors", $count, VendorListRequest::SORTING_FIELDS, $auth, $envelope); }
注意:如果响应不包含封装(如“results”、“items”等),则不需要发送此参数。
按单个字段排序
要按升序排序一个字段,只需使用字段名。例如
- api/vendors?order_by=name
- api/vendors?order_by=contacts.name
要按降序排序,使用相同的方法,但前面加一个减号。例如
- api/vendors?order_by=-name
- api/vendors?order_by=-contacts.name
按多个字段排序
要按升序排序多个字段,列出字段名。例如
- api/vendors?order_by=id,name
- api/vendors?order_by=name,contacts.name
要按降序排序,使用相同的方法,但前面加一个减号。例如
- api/vendors?order_by=-id,name
- api/vendors?order_by=-id,-contacts.name
贡献
查看 CONTRIBUTING 和 行为准则,如果您想做出贡献(拉取请求)或只是在自己的环境中构建和测试项目。