deinternetjongens / laravel-api-factories
This package provides the database factory experience to fake Http calls in your testsuite
Installs: 15 283
Dependents: 0
Suggesters: 0
Security: 0
Stars: 12
Watchers: 9
Forks: 1
Open Issues: 0
Requires
- php: ^7.4|^8.0|^8.1
- illuminate/contracts: ^8.37|^9.0|^10.0|^11.0
- spatie/laravel-package-tools: ^1.11
Requires (Dev)
- brianium/paratest: ^6.2|^7.4
- nunomaduro/collision: ^5.3|^6.0|^7.0|^8.0
- orchestra/testbench: ^6.0|^7.0|^8.0|^9.0
- phpunit/phpunit: ^9.4|^10.5
- spatie/laravel-ray: ^1.26
- vimeo/psalm: ^4.4|^5.22
README
Laravel Api Factories
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.