I'm new in Laravel. I have 2 tables Productions and Products. Also, I have 2 factories ProductionFactory and ProductFactory. I want to test them via phpunit. Their connection is via production_id.
Error is ErrorException: Undefined variable: production.
I don't get it.
Thanks in advance.
This is a code.
ProductionFactory.php
$factory->define(App\Production::class, function (Faker $faker) {
return [
'name' =>$faker->name,
];
});
ProductFactory.php
$factory->define(App\Product::class, function (Faker $faker) {
$production_id = App\Production::pluck('id');
if(!$production_id->isEmpty()){
$production = $production_id->random();
}
return [
'id' =>$faker->uuid,
'name' =>$faker->name,
'price' => $faker->numberBetween($min = 100, $max = 900),
'description' =>Str::random(10),
'production_id'=> $production,
];
ProductionTest.php
class ProductionTest extends TestCase
{
use RefreshDatabase;
/**
* A basic unit test example.
* @test
* @return void
*/
public function production()
{
factory(Production::class)->make();
$this->assertTrue(true);
}
}
ProductTest.php
class ProductTest extends TestCase
{
use RefreshDatabase;
/**
* A basic unit test example.
* @test
* @return void
*/
public function product()
{
factory(Product::class)->make();
$this->assertTrue(true);
}
}