Laravel-将关系添加到工厂创建的模型

时间:2019-11-13 22:04:20

标签: php laravel eloquent phpunit

我正在测试一个急切的加载关系,其中包含多对多关系。现在,我在测试中有查询和附件。我想知道是否有办法将它们移入工厂,而不是将其作为测试的一部分。这将限制测试的规模,然后每次创建电影工厂时都可以创建和使用这些关系。

test

public function grabFilmTest()
{
    $film = factory(Film::class)->create();

    $categories = Category::where('main-cat', 'Science')->where('sub-cat', 'Fiction')->first();
    $languages = Languages::where('name', 'english')->first();

    $film->categories()->attach($categories->id);
    $film->languages()->attach($languages->id);

    $response = $this->json('GET', '/film/' . $film->id)
        ->assertStatus(200);

    $response
        ->assertExactJson([
            'id' => $film->id,
            'name' => $film->name,
            'description' => $film->description,
            'categories' => $film->categories->toArray(),
            'languages' => $film->languages->toArray()

}

filmFactory

$factory->define(\App\Models\Film::class, function (Faker $faker){
    return [
        'id' => $faker->uuid,
        'name' => $faker->text,
        'description' => $faker->paragraph,
    ];
});

如果有人可以帮助我如何做到这一点或举一个例子,那将是很好的:D

2 个答案:

答案 0 :(得分:1)

您可以使用factory callbacks在出厂文件中执行此操作:

<?php
use \App\Models\Film;
use \App\Models\Category;
use \App\Models\Languages;

$factory->define(Film::class, function(Faker $faker){
    return [
        'id'          => $faker->uuid,
        'name'        => $faker->text,
        'description' => $faker->paragraph,
    ];
});

$factory->afterCreating(Film::class, function(Film $film, Faker $faker) {
    $category = Category::where('main-cat', 'Science')->where('sub-cat', 'Fiction')->first();
    $language = Languages::where('name', 'english')->first();
    $film->categories()->attach($category);
    $film->languages()->attach($language);
});

答案 1 :(得分:1)

您可以使用factory statesfactory callbacks

$factory->define(\App\Models\Film::class, function (Faker $faker){
    return [
        'id' => $faker->uuid,
        'name' => $faker->text,
        'description' => $faker->paragraph,
    ];
});
$factory->define(\App\Models\Category::class, function (Faker $faker){
    return [
        // Category fields 
    ];
});
$factory->define(\App\Models\Language::class, function (Faker $faker){
    return [
        // Language fields 
    ];
});
$factory->afterCreatingState(\App\Models\Film::class, 'with-category', function (\App\Models\Film $film) {
    $category = factory(\App\Models\Category::class)->create();
    $film->categories()->attach($category->id);
});
$factory->afterCreatingState(\App\Models\Film::class, 'with-language', function (\App\Models\Film $film) {
    $language = factory(\App\Models\Language::class)->create();
    $film->categories()->attach($language->id);
});

然后您可以在这样的测试中使用:

public function grabFilmTest()
{
    $film = factory(Film::class)->create();
    $filmWithCategory = factory(Film::class)->state('with-category')->create();
    $filmWithLanguage = factory(Film::class)->state('with-language')->create();
    $filmWithCategoryAnLanguage = factory(Film::class)->states(['with-category', 'with-language'])->create();
    // ...
}

PS:我不建议使用现有数据。根据经验,我可以告诉你,这可能会变得非常痛苦。