bit-mx/saloon-response-factories

Test saloon requests easily

1.0.2 2024-04-15 22:33 UTC

This package is auto-updated.

Last update: 2024-06-15 22:49:39 UTC


README

Saloon Response Factories

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.