bit-mx / saloon-response-factories
轻松测试saloon请求
1.0.2
2024-04-15 22:33 UTC
Requires
- php: ^8.1 || ^8.2 || ^8.3
- fakerphp/faker: ^1.23
- illuminate/console: ^10.0 || ^11.0
- illuminate/support: ^10.0 || ^11.0
- saloonphp/saloon: ^3.0
Requires (Dev)
- laravel/pint: ^1.15
- orchestra/testbench: ^8.0|| ^9.0
- pestphp/pest: ^2.34
- pestphp/pest-plugin-laravel: ^2.3
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^1.10
- tomasvotruba/type-coverage: ^0.2.5
This package is auto-updated.
Last update: 2024-09-15 23:21:33 UTC
README
目录
安装
您可以通过composer安装此包
composer require bitmx/saloon-response-factories
需求
此包需要Laravel 10.0或更高版本以及PHP 8.1或更高版本。
使用
您可以使用工厂创建用于Saloon测试的假数据
要创建一个工厂,您应该扩展SaloonResponseFactory类并实现定义方法。
namespace Tests\SaloonResponseFactories; use BitMx\SaloonResponseFactories\Factories\SaloonResponseFactory; class PostResponseFactoryFactory extends SaloonResponseFactory { /** * {@inheritDoc} */ public function definition(): array { return [ 'id' => $this->faker->unique()->randomNumber(), 'title' => $this->faker->sentence(), 'content' => $this->faker->paragraph(), ]; } }
您可以使用faker属性来生成假数据。
use Tests\SaloonResponseFactories\PostResponseFactoryFactory; it('should get the post', function () { Saloon::fake([ GetPostsRequest::class => PostResponseFactoryFactory::new()->create(), ]); });
包装响应
您可以使用wrap方法将响应包装在自定义结构中。
namespace Tests\SaloonResponseFactories; use BitMx\SaloonResponseFactories\Factories\SaloonResponseFactory; class PostResponseFactoryFactory extends SaloonResponseFactory { /** * {@inheritDoc} */ public function definition(): array { return [ 'id' => $this->faker->unique()->randomNumber(), 'title' => $this->faker->sentence(), 'content' => $this->faker->paragraph(), ]; } public function wrap(): string { return 'data'; } }
这将创建一个类似于这样的响应
\Saloon\Http\Faking\MockResponse::make([ 'data' => [ 'id' => 1, 'title' => 'Title 1', 'content' => 'Content 1', ], ]);
元数据
您可以使用metadata方法向响应添加元数据。
namespace Tests\SaloonResponseFactories; use BitMx\SaloonResponseFactories\Factories\SaloonResponseFactory; class PostResponseFactoryFactory extends SaloonResponseFactory { /** * {@inheritDoc} */ public function definition(): array { return [ 'id' => $this->faker->unique()->randomNumber(), 'title' => $this->faker->sentence(), 'content' => $this->faker->paragraph(), ]; } public function wrap(): string{ return 'data'; } public function metadata(): array { return [ 'total' => 10, ]; } }
这将创建一个类似于这样的响应
\Saloon\Http\Faking\MockResponse::make([ 'data' => [ 'id' => 1, 'title' => 'Title 1', 'content' => 'Content 1', ], 'metadata' => [ 'total' => 10, ], ]);
计数
您还可以使用count方法创建一个假数据数组。
use Tests\SaloonResponseFactories\PostResponseFactoryFactory; it('should get the post', function () { Saloon::fake([ GetPostsRequest::class => PostResponseFactoryFactory::new()->count(5)->create(), ]); });
此代码创建了一个类似于这样的MockResponse
\Saloon\Http\Faking\MockResponse::make([ [ 'id' => 1, 'title' => 'Title 1', 'content' => 'Content 1', ], [ 'id' => 2, 'title' => 'Title 2', 'content' => 'Content 2', ], [ 'id' => 3, 'title' => 'Title 3', 'content' => 'Content 3', ], [ 'id' => 4, 'title' => 'Title 4', 'content' => 'Content 4', ], [ 'id' => 5, 'title' => 'Title 5', 'content' => 'Content 5', ], ]);
您可以使用state方法更改工厂的默认值。
it('should get the post', function () { Saloon::fake([ GetPostsRequest::class => PostResponseFactoryFactory::new()->state([ 'title' => 'Custom Title', ])->create(), ]); });
或者,在工厂中创建一个新方法来更改默认值。
namespace Tests\SaloonResponseFactories; use BitMx\SaloonResponseFactories\Factories\SaloonResponseFactory; class PostResponseFactoryFactory extends SaloonResponseFactory { /** * {@inheritDoc} */ public function definition(): array { return [ 'id' => $this->faker->unique()->randomNumber(), 'title' => $this->faker->sentence(), 'content' => $this->faker->paragraph(), ]; } public function withCustomTitle(): self { return $this->state([ 'title' => 'Custom Title', ]); } }
se Tests\SaloonResponseFactories\PostResponseFactoryFactory; it('should get the post', function () { Saloon::fake([ GetPostsRequest::class => PostResponseFactoryFactory::new()->withCustomTitle()->create(), ]); });
头部
您可以使用headers方法向响应添加头部。
namespace Tests\SaloonResponseFactories; use BitMx\SaloonResponseFactories\Factories\SaloonResponseFactory; class PostResponseFactoryFactory extends SaloonResponseFactory { /** * {@inheritDoc} */ public function definition(): array { return [ 'id' => $this->faker->unique()->randomNumber(), 'title' => $this->faker->sentence(), 'content' => $this->faker->paragraph(), ]; } public function withCustomTitle(): self { return $this->state([ 'title' => 'Custom Title', ]); } public function withHeaders(): self { return $this->headers([ 'X-Custom-Header' => 'Custom Value', ]); } }
use Tests\SaloonResponseFactories\PostResponseFactoryFactory; it('should get the post', function () { Saloon::fake([ GetPostsRequest::class => PostResponseFactoryFactory::new()->withHeaders()->create(), ]); });
您还可以使用headers方法添加多个头部。
namespace Tests\SaloonResponseFactories; use Tests\SaloonResponseFactories\PostResponseFactoryFactory; it('should get the post', function () { Saloon::fake([ GetPostsRequest::class => PostResponseFactoryFactory::new()->headers([ 'X-Custom-Header' => 'Custom Value', 'X-Another-Header' => 'Another Value', ])->create(), ]); });
状态
您可以使用status方法更改响应的状态码。
use Tests\SaloonResponseFactories\PostResponseFactoryFactory; it('should get the post', function () { Saloon::fake([ GetPostsRequest::class => PostResponseFactoryFactory::new()->status(404)->create(), ]); });
创建新的工厂
您可以使用artisan命令创建新的工厂。
php artisan make:saloon-response-factory PostResponseFactory
此命令将在tests/SaloonResponseFactories目录中创建一个新的工厂。