Remove hardcoded create statements in factories (WIP)

This commit is contained in:
Christopher C. Wells 2021-03-29 21:27:12 -07:00 committed by Christopher Charbonneau Wells
parent 3c37fe4809
commit c67aa89555
7 changed files with 23 additions and 54 deletions

View File

@ -3,7 +3,6 @@
namespace Database\Factories;
use App\Models\Goal;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
class GoalFactory extends Factory
@ -18,8 +17,6 @@ class GoalFactory extends Factory
*/
public function definition()
{
/** @var \App\Models\User $user */
$user = User::factory()->create();
$from = $this->faker->dateTimeThisMonth;
$to = $this->faker->dateTimeBetween($from, '+1 year');
return [
@ -28,19 +25,6 @@ class GoalFactory extends Factory
'frequency' => $this->faker->randomElement(Goal::$frequencyOptions)['value'],
'name' => $this->faker->randomElement(Goal::getNameOptions())['value'],
'goal' => $this->faker->numberBetween(0, 2000),
'user_id' => $user->id,
];
}
/**
* Define a specific user.
*/
public function user(User $user): static
{
return $this->state(function (array $attributes) use ($user) {
return [
'user_id' => $user->id,
];
});
}
}

View File

@ -21,6 +21,9 @@ class IngredientAmountFactory extends Factory
*/
public function definition(): array
{
// @todo Remove these hard-corded create statements.
// See: https://laravel.com/docs/8.x/database-testing#factory-relationships
/** @var \App\Models\Recipe $recipe */
$recipe = Recipe::factory()->create();

View File

@ -3,7 +3,6 @@
namespace Database\Factories;
use App\Models\JournalEntry;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
class JournalEntryFactory extends Factory
@ -18,10 +17,7 @@ class JournalEntryFactory extends Factory
*/
public function definition(): array
{
/** @var \App\Models\User $user */
$user = User::factory()->create();
return [
'user_id' => $user->id,
'date' => $this->faker->dateTimeThisMonth,
'summary' => $this->faker->realText(50),
'calories' => $this->faker->randomFloat(2, 0, 500),
@ -34,15 +30,4 @@ class JournalEntryFactory extends Factory
];
}
/**
* Define a specific user.
*/
public function user(User $user): static
{
return $this->state(function (array $attributes) use ($user) {
return [
'user_id' => $user->id,
];
});
}
}

View File

@ -2,7 +2,6 @@
namespace Database\Factories;
use App\Models\Recipe;
use App\Models\RecipeSeparator;
use Illuminate\Database\Eloquent\Factories\Factory;
@ -18,10 +17,7 @@ class RecipeSeparatorFactory extends Factory
*/
public function definition()
{
/** @var \App\Models\Recipe $recipe */
$recipe = Recipe::factory()->create();
return [
'recipe_id' => $recipe->id,
'container' => 'ingredients',
'weight' => $this->faker->numberBetween(0, 100),
'text' => $this->faker->optional()->realText(20),

View File

@ -2,7 +2,6 @@
namespace Database\Factories;
use App\Models\Recipe;
use App\Models\RecipeStep;
use Illuminate\Database\Eloquent\Factories\Factory;
@ -18,24 +17,9 @@ class RecipeStepFactory extends Factory
*/
public function definition(): array
{
/** @var \App\Models\Recipe $recipe */
$recipe = Recipe::factory()->create();
return [
'recipe_id' => $recipe->id,
'number' => $this->faker->numberBetween(1, 50),
'step' => $this->faker->realText(500),
];
}
/**
* Define a specific recipe.
*/
public function recipe(Recipe $recipe): static
{
return $this->state(function (array $attributes) use ($recipe) {
return [
'recipe_id' => $recipe->id,
];
});
}
}

View File

@ -5,6 +5,7 @@ namespace Tests\Feature\Http\Controllers;
use App\Http\Controllers\GoalController;
use App\Models\Goal;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Testing\RefreshDatabase;
class GoalControllerTest extends HttpControllerTestCase
@ -35,4 +36,12 @@ class GoalControllerTest extends HttpControllerTestCase
return 'goal';
}
/**
* @inheritdoc
*/
protected function createInstance(): Model
{
return $this->factory()->for($this->user)->create();
}
}

View File

@ -3,6 +3,7 @@
namespace Tests\Feature\Http\Controllers;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Model;
use Tests\LoggedInTestCase;
abstract class HttpControllerTestCase extends LoggedInTestCase
@ -23,6 +24,13 @@ abstract class HttpControllerTestCase extends LoggedInTestCase
*/
abstract public function routeKey(): string;
/**
* Create an instance of the model being tested.
*/
protected function createInstance(): Model {
return $this->factory()->create();
}
public function testCanLoadIndex(): void
{
$index_url = action([$this->class(), 'index']);
@ -45,7 +53,7 @@ abstract class HttpControllerTestCase extends LoggedInTestCase
public function testCanViewInstance(): void
{
$instance = $this->factory()->create();
$instance = $this->createInstance();
$view_url = action([$this->class(), 'show'], [$this->routeKey() => $instance]);
$response = $this->get($view_url);
$response->assertOk();
@ -54,7 +62,7 @@ abstract class HttpControllerTestCase extends LoggedInTestCase
public function testCanEditInstance(): void
{
$instance = $this->factory()->create();
$instance = $this->createInstance();
$edit_url = action([$this->class(), 'edit'], [$this->routeKey() => $instance]);
$response = $this->get($edit_url);
$response->assertOk();
@ -69,7 +77,7 @@ abstract class HttpControllerTestCase extends LoggedInTestCase
public function testCanDeleteInstance(): void
{
$instance = $this->factory()->create();
$instance = $this->createInstance();
$delete_url = action([$this->class(), 'delete'], [$this->routeKey() => $instance]);
$response = $this->get($delete_url);
$response->assertOk();