From bf221d2b0413a3b0547aacff0a52814f1415edaf Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Wed, 31 Mar 2021 08:22:10 -0700 Subject: [PATCH] Add Recipe controller tests --- .../Http/Controllers/RecipeControllerTest.php | 140 ++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 tests/Feature/Http/Controllers/RecipeControllerTest.php diff --git a/tests/Feature/Http/Controllers/RecipeControllerTest.php b/tests/Feature/Http/Controllers/RecipeControllerTest.php new file mode 100644 index 0000000..52a4d84 --- /dev/null +++ b/tests/Feature/Http/Controllers/RecipeControllerTest.php @@ -0,0 +1,140 @@ +factory() + ->hasIngredientAmounts(10) + ->hasSteps(6) + ->hasIngredientSeparators(2) + ->create(); + } + + public function testCanAddInstance(): void + { + $create_url = action([$this->class(), 'create']); + $response = $this->get($create_url); + $response->assertOk(); + + + $ingredient_amounts = IngredientAmount::factory() + ->count(10) + ->make(['parent_id' => null, 'parent_type' => null]); + + $data = [ + 'ingredients' => $this->createFormDataFromIngredientAmounts($ingredient_amounts), + 'steps' => $this->createFormDataFromRecipeSteps(RecipeStep::factory()->count(6)->make()), + 'separators' => $this->createFormDataFromRecipeSeparators(RecipeSeparator::factory()->count(2)->make()), + ] + $this->factory()->makeOne()->toArray(); + + $store_url = action([$this->class(), 'store']); + $response = $this->post($store_url, $data); + $response->assertSessionHasNoErrors(); + } + + public function testCanEditInstance(): void + { + $instance = $this->createInstance(); + $edit_url = action([$this->class(), 'edit'], [$this->routeKey() => $instance]); + $response = $this->get($edit_url); + $response->assertOk(); + + $data = [ + 'ingredients' => $this->createFormDataFromIngredientAmounts($instance->ingredientAmounts), + 'steps' => $this->createFormDataFromRecipeSteps($instance->steps), + 'separators' => $this->createFormDataFromRecipeSeparators($instance->ingredientSeparators), + ] + $this->factory()->makeOne()->toArray(); + + $put_url = action([$this->class(), 'update'], [$this->routeKey() => $instance]); + $response = $this->put($put_url, $data); + $response->assertSessionHasNoErrors(); + } + + /** + * Convert ingredient amount instances in to a form data style array. + */ + private function createFormDataFromIngredientAmounts(Collection $ingredient_amounts): array { + $ingredients = []; + /** @var \App\Models\IngredientAmount $ingredient_amount */ + foreach ($ingredient_amounts as $key => $ingredient_amount) { + $ingredients['amount'][] = $ingredient_amount->amount; + $ingredients['unit'][] = $ingredient_amount->unit; + $ingredients['detail'][] = $this->faker->words(2, true); + $ingredients['id'][] = $ingredient_amount->ingredient->id; + $ingredients['type'][] = $ingredient_amount->ingredient->type; + $ingredients['weight'][] = $this->faker->unique()->numberBetween(0, 9); + $ingredients['key'][] = $ingredient_amount->exists ? $key : null; + } + return $ingredients; + } + + /** + * Convert recipe step instances in to a form data style array. + */ + private function createFormDataFromRecipeSteps(Collection $recipe_steps): array { + $steps = []; + /** @var \App\Models\RecipeStep $recipe_step */ + foreach ($recipe_steps as $key => $recipe_step) { + $steps['step'][] = $recipe_step->step; + $steps['key'][] = $recipe_step->exists ? $key : null; + } + return $steps; + } + + /** + * Convert recipe separator instances in to a form data style array. + */ + private function createFormDataFromRecipeSeparators(Collection $recipe_separators): array { + $separators = []; + /** @var \App\Models\RecipeSeparator $recipe_separator */ + foreach ($recipe_separators as $key => $recipe_separator) { + $separators['text'][] = $recipe_separator->text; + $separators['weight'][] = $recipe_separator->weight; + $separators['key'][] = $recipe_separator->exists ? $key : null; + } + return $separators; + } + +}