diff --git a/tests/Feature/Http/Controllers/JournalEntryControllerTest.php b/tests/Feature/Http/Controllers/JournalEntryControllerTest.php index 6a1c967..2a4c43f 100644 --- a/tests/Feature/Http/Controllers/JournalEntryControllerTest.php +++ b/tests/Feature/Http/Controllers/JournalEntryControllerTest.php @@ -75,12 +75,44 @@ class JournalEntryControllerTest extends HttpControllerTestCase $response->assertSessionHasNoErrors(); } - public function testCanAddInstanceFromIngredients(): void + public function testCanAddInstanceFromIngredientsGrouped(): void { $create_url = action([$this->class(), 'create']); $response = $this->get($create_url); $response->assertOk(); + $data = $this->createIngredientsDataArray(); + $store_url = action([$this->class(), 'store']); + $response = $this->post($store_url, $data); + $response->assertSessionHasNoErrors(); + } + + public function testCanAddInstanceFromIngredientsUnGrouped(): void + { + $data = $this->createIngredientsDataArray(); + $data['group_entries'] = false; + $store_url = action([$this->class(), 'store']); + $response = $this->post($store_url, $data); + $response->assertSessionHasNoErrors(); + } + + public function testSessionKeepsOldInput(): void { + $data = $this->createIngredientsDataArray(); + + // Set first amount to an invalid string. + $data['ingredients']['amount'][0] = 'abcd'; + + $store_url = action([$this->class(), 'store']); + $response = $this->post($store_url, $data); + $response->assertRedirect(); + $response->assertSessionHasErrors(); + $response->assertSessionHasInput('ingredients', $data['ingredients']); + } + + /** + * Create a test array for creating an entry from ingredients data. + */ + private function createIngredientsDataArray(): array { // Create ingredients based on ingredient amounts. $ingredients = [ 'date' => [], 'meal' => [], 'amount' => [], 'unit' => [], @@ -99,15 +131,7 @@ class JournalEntryControllerTest extends HttpControllerTestCase $ingredients['id'][] = $ingredient_amount->ingredient->id; $ingredients['type'][] = $ingredient_amount->ingredient->type; } - $data = ['ingredients' => $ingredients, 'group_entries' => true]; - $store_url = action([$this->class(), 'store']); - $response = $this->post($store_url, $data); - $response->assertSessionHasNoErrors(); - - $data['group_entries'] = false; - $store_url = action([$this->class(), 'store']); - $response = $this->post($store_url, $data); - $response->assertSessionHasNoErrors(); + return ['ingredients' => $ingredients, 'group_entries' => true]; } } diff --git a/tests/Feature/Http/Controllers/RecipeControllerTest.php b/tests/Feature/Http/Controllers/RecipeControllerTest.php index 08ca4d4..12f3983 100644 --- a/tests/Feature/Http/Controllers/RecipeControllerTest.php +++ b/tests/Feature/Http/Controllers/RecipeControllerTest.php @@ -11,6 +11,8 @@ use Database\Factories\RecipeFactory; use Illuminate\Database\Eloquent\Collection; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; +use Illuminate\Http\UploadedFile; +use Illuminate\Support\Facades\Storage; class RecipeControllerTest extends HttpControllerTestCase { @@ -49,7 +51,8 @@ class RecipeControllerTest extends HttpControllerTestCase ->hasIngredientAmounts(10) ->hasSteps(6) ->hasIngredientSeparators(2) - ->create(); + ->hasTags(5) + ->createOneWithMedia(); } public function testCanAddInstance(): void @@ -63,11 +66,12 @@ class RecipeControllerTest extends HttpControllerTestCase ->count(10) ->make(['parent_id' => null, 'parent_type' => null]); - $data = [ + $data = $this->factory()->makeOne()->toArray() + [ '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(); + 'image' => UploadedFile::fake()->image('recipe.jpg', 1600, 900), + ]; $store_url = action([$this->class(), 'store']); $response = $this->post($store_url, $data); @@ -81,17 +85,45 @@ class RecipeControllerTest extends HttpControllerTestCase $response = $this->get($edit_url); $response->assertOk(); - $data = [ + // Remove one of each item. + $instance->ingredientAmounts[1]->delete(); + $instance->steps[1]->delete(); + $instance->separators[1]->delete(); + $instance->refresh(); + + $data = $this->factory()->makeOne()->toArray() + [ 'ingredients' => $this->createFormDataFromIngredientAmounts($instance->ingredientAmounts), 'steps' => $this->createFormDataFromRecipeSteps($instance->steps), 'separators' => $this->createFormDataFromRecipeSeparators($instance->ingredientSeparators), - ] + $this->factory()->makeOne()->toArray(); + 'image' => UploadedFile::fake()->image('recipe.jpg', 1600, 900), + ]; $put_url = action([$this->class(), 'update'], [$this->routeKey() => $instance]); $response = $this->put($put_url, $data); $response->assertSessionHasNoErrors(); } + public function testSessionKeepsOldInput(): void { + $instance = $this->createInstance(); + + $data = [ + 'ingredients' => $this->createFormDataFromIngredientAmounts($instance->ingredientAmounts), + 'steps' => $this->createFormDataFromRecipeSteps($instance->steps), + 'separators' => $this->createFormDataFromRecipeSeparators($instance->ingredientSeparators), + ] + $instance->toArray(); + + // Remove the first amount value to force a form error. + $data['ingredients']['amount'][0] = NULL; + + $put_url = action([$this->class(), 'update'], [$this->routeKey() => $instance]); + $response = $this->put($put_url, $data); + $response->assertRedirect(); + $response->assertSessionHasErrors(); + $response->assertSessionHasInput('ingredients', $data['ingredients']); + $response->assertSessionHasInput('steps', $data['steps']); + $response->assertSessionHasInput('separators', $data['separators']); + } + /** * Convert ingredient amount instances in to a form data style array. */