This package provides the database factory experience to fake Http calls in your testsuite
You can install the package via composer:
composer require deinternetjongens/laravel-api-factories
php artisan make:api-factory NewsPostResponse
The new api factory class will be placed in your tests/Factories directory.
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 \Illuminate\Support\Facades\Http;
$response = NewsPostResponseFactory::new()
->state(new Sequence(
['author' => 'Taylor'],
['author' => 'Mohammed'],
['author' => 'Dries']
))
->count(15)
->make();
Http::fakeSequence()->push($response);
composer test
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.