1. Go to this page and download the library: Download magdonia/laravel-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/ */
// before
public function test_category_id_should_belong_to_user(): void
{
$user = User::factory()->create();
$this->actingAs($user);
$genre = Genre::factory()->create();
$category = Category::factory()->create();
$response = $this->postJson(route('post.store', ['genre' = >$genre]), ['category_id' => $category->id])
$response->assertJsonValidationErrors('category_id');
$category = Category::factory()->for($user)->create();
$response = $this->postJson(route('post.store', ['genre' = >$genre]), ['category_id' => $category->id])
$response->assertJsonMissingValidationErrors('category_id');
}
// after
public function test_category_id_should_belong_to_user(): void
{
$response = StorePostRequest::factory()->as(User::factory()->create())->validate();
$response->assertJsonMissingValidationErrors('category_id');
$request = StorePostRequest::factory();
$response = $request->validate();
$response->assertJsonMissingValidationErrors('category_id');
// You have access to user or category on the request
$user = $request->user;
$category = $request->category;
}
// before
public function test_guest_can_not_create_post(): void
{
$genre = Genre::factory()->create();
$response = $this->postJson(route('post.store', ['genre' => $genre]), $form)->assertUnauthorized();
}
// after
public function test_guest_can_not_create_post(): void
{
StorePostRequest::factory()->asGuest()->validate()->assertUnauthorized();
}
use Magdonia\LaravelFactories\Concerns\HasRequestFactory;
class YourRequest extends FormRequest
{
use HasRequestFactory;
}
namespace Tests\RequestFactories;
use Magdonia\LaravelFactories\RequestFactory;
class AnotherRequestFactory extends RequestFactory
{
public function definition(): array
{
return [
// Your definition goes here
];
}
}
// Get a factory object
$factory = PostStoreRequest::factory()->create();
// Set attributes on factory
$factory = PostStoreRequest::factory()->create(['title' => $title])
// Get a form from request factory
$form = PostStoreRequest::factory()->form();
// Get a form from request factory and override or add additional fields
$form = PostStoreRequest::factory()->form(['title' => $title, 'added' => 'something']);
// Add states for request form
$form = PostStoreRequest::factory()->category($category)->form();
// Set values on request form
$form = PostStoreRequest::factory()->set($key, $value)->form();
// Unset values on request form
$form = PostStoreRequest::factory()->unset($key)->form();
$form = PostStoreRequest::factory()->unset([$key1, $key2])->form();
// Validate a form request
PostStoreRequest::factory()->validate()
->assertJsonValidationErrors('title');
PostStoreRequest::factory()->validate(['title' => null])
->assertJsonMissingValidationErrors('title');
// Get a request instance from factory
$postStoreRequest = PostStoreRequest::factory()->make();
$postStoreRequest = PostStoreRequest::factory()->make(['title' => $title]);
// Set authenticated user for request instance
$postStoreRequest = PostStoreRequest::factory()->asGuest()->make();
$postStoreRequest = PostStoreRequest::factory()->as($user)->make();
// Set method for request
$postStoreRequest = PostStoreRequest::factory()->method($method)->make();
// Set route params
$postStoreRequest = PostStoreRequest::factory()->routeParam($key, $value)->make();
// Set route
$postStoreRequest = PostStoreRequest::factory()->route('welcome')->make();
use Magdonia\LaravelFactories\Concerns\HasResourceFactory;
class YourResource extends JsonResource
{
use HasResourceFactory;
}
namespace Tests\RequestFactories;
use Magdonia\LaravelFactories\ResourceFactory;
class AnotherResourceFactory extends ResourceFactory
{
public function definition(): \Closure
{
return function (AssertableJson $json) {
// Your assertion goes here
});
}
}
// Get a factory object
$factory = UserResource::factory();
// Set a single model for resource
$factory = UserResource::factory()->model($user);
// Set a collection for resource
$factory = UserResource::factory()->collection($users);
// Set a pagination for resource
$factory = UserResource::factory()->pagination($users);
// Set authenticated user for resource
$factory = UserResource::factory()->user($auth);
// Get a json response from resource
$array = UserResource::factory()->model($user)->json();
$array = UserResource::factory()->collection($users)->json();
$array = UserResource::factory()->pagination($users)->json();
// Get a response object from resource
$testResponse = UserResource::factory()->model($user)->response();
$testResponse = UserResource::factory()->collection($users)->response();
$testResponse = UserResource::factory()->pagination($users)->response();
// Get a resource object from resource
$userResource = UserResource::factory()->model($user)->make();
$userResource = UserResource::factory()->collection($users)->make();
$userResource = UserResource::factory()->pagination($users)->make();
// Assert response
/** @var TestResponse $response */
$response->assertJson(UserResource::factory()->model($user)->create());
$response->assertJson(UserResource::factory()->user($auth)->model($user)->create());
$response->assertJson(UserResource::factory()->collection($users)->create());
$response->assertJson(UserResource::factory()->pagination($users)->create());
// Change wrapper
$response->assertJson(UserResource::factory()->wrapper('anything')->model($user)->create());
// Remove wrapper
$response->assertJson(UserResource::factory()->wrapper(null)->model($user)->create());
// Assertion loaded relations
$response->assertJson(
PostResource::factory()
->model($post->load('creator'))
->with('creator', UserResource::class)
->create()
);
$response->assertJson(
UserResource::factory()
->model($user->load('posts'))
->with('posts', PostCollection::class)
->create()
);
// Assertion loaded relations with different relation name
$response->assertJson(
UserResource::factory()
->model($user->load('posts'))
->with('latest_posts', PostCollection::class, 'posts')
->create()
);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.