New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

deinternetjongens/laravel-api-factories

This package provides the database factory experience to fake Http calls in your testsuite
21,286 12
Install
composer require deinternetjongens/laravel-api-factories
Latest Version:3.1.0
PHP:^8.2
Laravel:12 · 13
Older releases:8 – 11
License:MIT
Last Updated:Apr 21, 2026
Links: GitHub  ·  Packagist
Maintainer: DIJ

Social Card of Laravel Api Factories

Laravel Api Factories

Latest Version on Packagist PHP Version Support GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This package provides the database factory experience to fake Http calls in your testsuite

Installation

You can install the package via composer:

composer require deinternetjongens/laravel-api-factories

Usage

Generate Api factories

php artisan make:api-factory NewsPostResponse

The new api factory class will be placed in your tests/Factories directory.

Configure api factory

The api factories look moslty the same as the Laravel database factories, except it extend the ApiFactory class and you don't need to specify a model.

namespace Tests\Factories;

use DIJ\ApiFactories\ApiFactory;

class NewsPostResponseFactory extends ApiFactory
{
    protected ?string $wrapper = ResponseFactoryWrapper::class;
    
    /**
     * Define the response's default state.
     *
     * @return array<string,mixed>
     */
    public function definition(): array
    {
        return [
            'title' => $this->faker->title,
            'intro' => $this->faker->paragraph(),
            'article' => $this->faker->paragraphs(4),
            'author' => $this->faker->name,
            'likes' => $this->faker->randomNumber(2),
            'published_at' => $this->faker->dateTime(),
        ];
    }
}

class ResponseFactoryWrapper extends ApiFactory
{
    protected ?string $wrapper = 'data';
    
    /**
     * Define the response's default state.
     *
     * @return array<string,mixed>
     */
    public function definition(): array
    {
        return [
            'items' => $this->children(),
            'meta' => [
                'total' => rand(0, 10),
            ]
        ];
    }
}

Use api factory

use \Illuminate\Support\Facades\Http;

$response = NewsPostResponseFactory::new()
    ->state(new Sequence(
        ['author' => 'Taylor'],
        ['author' => 'Mohammed'],
        ['author' => 'Dries']
    ))
    ->count(15)
    ->make();

Http::fakeSequence()->push($response);

Testing

composer test

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

Related Packages

doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database s...

648,266,899 9,701
laravel/framework

The Laravel Framework.

604,216,237 34,982
laravel/tinker

Powerful REPL for the Laravel framework.

512,508,631 7,443
laravel/serializable-closure

Laravel Serializable Closure provides an easy and secure way to serialize closur...

429,221,767 607
nunomaduro/collision

Cli error handling for console/command-line PHP applications.

407,699,310 4,663

Version History

Version Released PHP Laravel License
3.1.0 ^8.2 ^12.0|^13.0 MIT
3.0.0 ^8.2 ^12.0 MIT
2.3.0 ^7.4|^8.0|^8.1 ^8.37|^9.0|^10.0|^11.0 MIT
2.2 ^7.4|^8.0|^8.1 ^8.37|^9.0|^10.0 MIT
2.1.0 ^7.4|^8.0|^8.1 ^8.37|^9.0 MIT