承接 bit-mx/saloon-response-factories 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

bit-mx/saloon-response-factories

Composer 安装命令:

composer require bit-mx/saloon-response-factories

包简介

Test saloon requests easily

README 文档

README

Table of Contents

Installation

You can install the package via composer:

composer require bitmx/saloon-response-factories

Requirements

This package requires Laravel 10.0 or higher and PHP 8.1 or higher.

Use

You can use factories to create fake data for your Saloon tests

To create a factory you should extend the SaloonResponseFactory class and implement the definition method.

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(),
        ];
    }
}

You can use the faker property to generate fake data.

use Tests\SaloonResponseFactories\PostResponseFactoryFactory;

it('should get the post', function () {
    Saloon::fake([
        GetPostsRequest::class => PostResponseFactoryFactory::new()->create(),
    ]);
});

Wrapping the response

You can use the wrap method to wrap the response in a custom structure.

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

This create a response like this:

\Saloon\Http\Faking\MockResponse::make([
    'data' => [
        'id' => 1,
        'title' => 'Title 1',
        'content' => 'Content 1',
    ],
]);

Metadata

You can use the metadata method to add metadata to the response.

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,
        ];
    }
}

This creates a response like this:

\Saloon\Http\Faking\MockResponse::make([
    'data' => [
        'id' => 1,
        'title' => 'Title 1',
        'content' => 'Content 1',
    ],
    'metadata' => [
        'total' => 10,
    ],
]);

Count

You can also use the count method to create an array of fake data.

use Tests\SaloonResponseFactories\PostResponseFactoryFactory;

it('should get the post', function () {
    Saloon::fake([
        GetPostsRequest::class => PostResponseFactoryFactory::new()->count(5)->create(),
    ]);
});

This code create a MockResponse like this:

\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',
    ],
]);

You can use the state method to change the default values of the factory.

it('should get the post', function () {
    Saloon::fake([
        GetPostsRequest::class => PostResponseFactoryFactory::new()->state([
            'title' => 'Custom Title',
        ])->create(),
    ]);
});

Or create a new method in the factory to change the default values.

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

You can use the headers method to add headers to the response.

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(),
    ]);
});

You can also use the headers method to add multiple 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

You can use the status method to change the status code of the response.

use Tests\SaloonResponseFactories\PostResponseFactoryFactory;

it('should get the post', function () {
    Saloon::fake([
        GetPostsRequest::class => PostResponseFactoryFactory::new()->status(404)->create(),
    ]);
});

Create a new factory

You can create a new factory using the artisan command.

php artisan make:saloon-response-factory PostResponseFactory

This command will create a new factory in the tests/SaloonResponseFactories directory.

bit-mx/saloon-response-factories 适用场景与选型建议

bit-mx/saloon-response-factories 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 859 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 04 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 bit-mx/saloon-response-factories 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 bit-mx/saloon-response-factories 我们能提供哪些服务?
定制开发 / 二次开发

基于 bit-mx/saloon-response-factories 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-04-15