Зависит от того, какие функции следует тестировать.
Базовый тест для проверки правильности отношения модели установлен?
Я переместил рабочую демонстрацию в GitHub (https://github.com/.../laravel-basic-relationship-tests).
App / Product.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
public function production()
{
return $this->belongsTo(Production::class);
}
}
App / Production.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Production extends Model
{
public function products()
{
return $this->belongsTo(Product::class);
}
}
Фабрика продуктов
<?php
/* @var $factory \Illuminate\Database\Eloquent\Factory */
use App\Product as Model;
use Faker\Generator as Faker;
$factory->define(Model::class, function (Faker $faker) {
$production = factory(\App\Production::class)->make();
$production->save();
return [
'name' => $faker->name,
'price' => $faker->numberBetween($min = 100, $max = 900),
'description' => $faker->text,
'production_id' => $production->id
];
});
Производственный завод
<?php
/* @var $factory \Illuminate\Database\Eloquent\Factory */
use App\Production as Model;
use Faker\Generator as Faker;
$factory->define(Model::class, function (Faker $faker) {
return [
'name' => $faker->name
];
});
$factory->afterCreatingState(Model::class, 'seed', function ($production, $faker) {
$product = factory(\App\Product::class)->make();
$production->products()->associate($product);
});
Испытание продукции
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
class ProductTest extends TestCase
{
/**
* @return void
*/
public function testProductHasWorkingProductionRelationship()
{
$product = factory(\App\Product::class)->create();
$this->assertNotEmpty($product->production);
}
}
Производственное испытание
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
class ProductionTest extends TestCase
{
/**
* @return void
*/
public function testProductionHasWorkingProductRelationship()
{
$production = factory(\App\Production::class)->state('seed')->create();
$this->assertNotEmpty($production->products);
}
}
Надеюсь, это отличная отправная точка для любых дальнейших испытаний.