PHP code example of deinternetjongens / laravel-api-factories
1. Go to this page and download the library: Download deinternetjongens/laravel-api-factories library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
deinternetjongens / laravel-api-factories example snippets
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);
bash
php artisan make:api-factory NewsPostResponse