From c67aa89555b0dfefab7d29c86d198c11f735fd76 Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Mon, 29 Mar 2021 21:27:12 -0700 Subject: [PATCH] Remove hardcoded create statements in factories (WIP) --- database/factories/GoalFactory.php | 16 ---------------- database/factories/IngredientAmountFactory.php | 3 +++ database/factories/JournalEntryFactory.php | 15 --------------- database/factories/RecipeSeparatorFactory.php | 4 ---- database/factories/RecipeStepFactory.php | 16 ---------------- .../Http/Controllers/GoalControllerTest.php | 9 +++++++++ .../Http/Controllers/HttpControllerTestCase.php | 14 +++++++++++--- 7 files changed, 23 insertions(+), 54 deletions(-) diff --git a/database/factories/GoalFactory.php b/database/factories/GoalFactory.php index 3ded4e4..2878c7b 100644 --- a/database/factories/GoalFactory.php +++ b/database/factories/GoalFactory.php @@ -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, - ]; - }); - } } diff --git a/database/factories/IngredientAmountFactory.php b/database/factories/IngredientAmountFactory.php index bc46690..bb1bf98 100644 --- a/database/factories/IngredientAmountFactory.php +++ b/database/factories/IngredientAmountFactory.php @@ -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(); diff --git a/database/factories/JournalEntryFactory.php b/database/factories/JournalEntryFactory.php index b21599f..f81bf15 100644 --- a/database/factories/JournalEntryFactory.php +++ b/database/factories/JournalEntryFactory.php @@ -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, - ]; - }); - } } diff --git a/database/factories/RecipeSeparatorFactory.php b/database/factories/RecipeSeparatorFactory.php index dcc8a1b..db1ec52 100644 --- a/database/factories/RecipeSeparatorFactory.php +++ b/database/factories/RecipeSeparatorFactory.php @@ -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), diff --git a/database/factories/RecipeStepFactory.php b/database/factories/RecipeStepFactory.php index f87e8ed..1b45723 100644 --- a/database/factories/RecipeStepFactory.php +++ b/database/factories/RecipeStepFactory.php @@ -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, - ]; - }); - } } diff --git a/tests/Feature/Http/Controllers/GoalControllerTest.php b/tests/Feature/Http/Controllers/GoalControllerTest.php index ab4defa..f8fbede 100644 --- a/tests/Feature/Http/Controllers/GoalControllerTest.php +++ b/tests/Feature/Http/Controllers/GoalControllerTest.php @@ -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(); + } + } diff --git a/tests/Feature/Http/Controllers/HttpControllerTestCase.php b/tests/Feature/Http/Controllers/HttpControllerTestCase.php index 2617893..b4dad6d 100644 --- a/tests/Feature/Http/Controllers/HttpControllerTestCase.php +++ b/tests/Feature/Http/Controllers/HttpControllerTestCase.php @@ -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();