From ff9f0f7834a96ec7a4e0efe93cca82e646d26b0f Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Mon, 29 Mar 2021 06:06:24 -0700 Subject: [PATCH] Create base test case for controllers --- tests/Feature/FoodTest.php | 95 ------------------- tests/Feature/GoalTest.php | 78 --------------- .../Http/Controllers/FoodControllerTest.php | 55 +++++++++++ .../Http/Controllers/GoalControllerTest.php | 38 ++++++++ .../Controllers/HttpControllerTestCase.php | 87 +++++++++++++++++ 5 files changed, 180 insertions(+), 173 deletions(-) delete mode 100644 tests/Feature/FoodTest.php delete mode 100644 tests/Feature/GoalTest.php create mode 100644 tests/Feature/Http/Controllers/FoodControllerTest.php create mode 100644 tests/Feature/Http/Controllers/GoalControllerTest.php create mode 100644 tests/Feature/Http/Controllers/HttpControllerTestCase.php diff --git a/tests/Feature/FoodTest.php b/tests/Feature/FoodTest.php deleted file mode 100644 index d634d20..0000000 --- a/tests/Feature/FoodTest.php +++ /dev/null @@ -1,95 +0,0 @@ -get($index_url); - $response->assertOk(); - } - - public function testCanAddFood(): void - { - $create_url = action([FoodController::class, 'create']); - $response = $this->get($create_url); - $response->assertOk(); - - /** @var \App\Models\Food $food */ - $food = Food::factory()->make(); - $store_url = action([FoodController::class, 'store']); - $response = $this->followingRedirects()->post($store_url, $food->toArray()); - $response->assertOk(); - $response->assertSee("Food {$food->name} updated!"); - } - - public function testCanAddFoodWithoutNutrients(): void - { - /** @var \App\Models\Food $food */ - $food = Food::factory()->make([ - 'calories' => NULL, - 'fat' => NULL, - 'cholesterol' => NULL, - 'sodium' => NULL, - 'carbohydrates' => NULL, - 'protein' => NULL, - ]); - $store_url = action([FoodController::class, 'store']); - $response = $this->followingRedirects()->post($store_url, $food->toArray()); - $response->assertOk(); - $response->assertSee("Food {$food->name} updated!"); - } - - public function testCanViewFood(): void - { - /** @var \App\Models\Food $food */ - $food = Food::factory()->create(); - $view_url = action([FoodController::class, 'show'], ['food' => $food]); - $response = $this->get($view_url); - $response->assertOk(); - $response->assertSee($food->name); - } - - public function testCanEditFood(): void - { - /** @var \App\Models\Food $food */ - $food = Food::factory()->create(); - $edit_url = action([FoodController::class, 'edit'], ['food' => $food]); - $response = $this->get($edit_url); - $response->assertOk(); - - /** @var \App\Models\Food $new_food */ - $new_food = Food::factory()->make(['tags' => []]); - $put_url = action([FoodController::class, 'update'], ['food' => $food]); - $response = $this->followingRedirects()->put($put_url, $new_food->toArray()); - $response->assertOk(); - $response->assertSee("Food {$new_food->name} updated!"); - } - - public function testCanDeleteFood(): void - { - /** @var \App\Models\Food $food */ - $food = Food::factory()->create(); - $delete_url = action([FoodController::class, 'delete'], ['food' => $food]); - $response = $this->get($delete_url); - $response->assertOk(); - $response->assertSee("Delete {$food->name}?"); - - $destroy_url = action([FoodController::class, 'destroy'], ['food' => $food]); - $response = $this->followingRedirects()->delete($destroy_url); - $response->assertOk(); - - $view_url = action([FoodController::class, 'show'], ['food' => $food]); - $response = $this->get($view_url); - $response->assertNotFound(); - } -} diff --git a/tests/Feature/GoalTest.php b/tests/Feature/GoalTest.php deleted file mode 100644 index cd7fc94..0000000 --- a/tests/Feature/GoalTest.php +++ /dev/null @@ -1,78 +0,0 @@ -get($index_url); - $response->assertOk(); - } - - public function testCanAddGoal(): void - { - $create_url = action([GoalController::class, 'create']); - $response = $this->get($create_url); - $response->assertOk(); - - /** @var \App\Models\Goal $goal */ - $goal = Goal::factory()->make(); - $store_url = action([GoalController::class, 'store']); - $response = $this->followingRedirects()->post($store_url, $goal->toArray()); - $response->assertOk(); - $response->assertSee('Goal updated!'); - } - - public function testCanViewGoal(): void - { - /** @var \App\Models\Goal $goal */ - $goal = Goal::factory()->create(); - $view_url = action([GoalController::class, 'show'], ['goal' => $goal]); - $response = $this->get($view_url); - $response->assertOk(); - $response->assertSee($goal->summary); - } - - public function testCanEditGoal(): void - { - /** @var \App\Models\Goal $goal */ - $goal = Goal::factory()->create(); - $edit_url = action([GoalController::class, 'edit'], ['goal' => $goal]); - $response = $this->get($edit_url); - $response->assertOk(); - - /** @var \App\Models\Goal $new_food */ - $new_food = Goal::factory()->make(['tags' => []]); - $put_url = action([GoalController::class, 'update'], ['goal' => $goal]); - $response = $this->followingRedirects()->put($put_url, $new_food->toArray()); - $response->assertOk(); - $response->assertSee('Goal updated!'); - } - - public function testCanDeleteGoal(): void - { - /** @var \App\Models\Goal $goal */ - $goal = Goal::factory()->create(); - $delete_url = action([GoalController::class, 'delete'], ['goal' => $goal]); - $response = $this->get($delete_url); - $response->assertOk(); - $response->assertSee("Delete {$goal->summary} goal?"); - - $destroy_url = action([GoalController::class, 'destroy'], ['goal' => $goal]); - $response = $this->followingRedirects()->delete($destroy_url); - $response->assertOk(); - - $view_url = action([GoalController::class, 'show'], ['goal' => $goal]); - $response = $this->get($view_url); - $response->assertNotFound(); - } -} diff --git a/tests/Feature/Http/Controllers/FoodControllerTest.php b/tests/Feature/Http/Controllers/FoodControllerTest.php new file mode 100644 index 0000000..542a84d --- /dev/null +++ b/tests/Feature/Http/Controllers/FoodControllerTest.php @@ -0,0 +1,55 @@ +make([ + 'calories' => NULL, + 'fat' => NULL, + 'cholesterol' => NULL, + 'sodium' => NULL, + 'carbohydrates' => NULL, + 'protein' => NULL, + ]); + $store_url = action([FoodController::class, 'store']); + $response = $this->followingRedirects()->post($store_url, $food->toArray()); + $response->assertOk(); + $response->assertSee("Food {$food->name} updated!"); + } + +} diff --git a/tests/Feature/Http/Controllers/GoalControllerTest.php b/tests/Feature/Http/Controllers/GoalControllerTest.php new file mode 100644 index 0000000..ab4defa --- /dev/null +++ b/tests/Feature/Http/Controllers/GoalControllerTest.php @@ -0,0 +1,38 @@ +class(), 'index']); + $response = $this->get($index_url); + $response->assertOk(); + } + + public function testCanAddInstance(): void + { + $create_url = action([$this->class(), 'create']); + $response = $this->get($create_url); + $response->assertOk(); + $instance = $this->factory()->make(); + $store_url = action([$this->class(), 'store']); + $response = $this->followingRedirects()->post($store_url, $instance->toArray()); + $response->assertOk(); + $response->assertSessionHasNoErrors(); + $response->assertViewHas($this->routeKey()); + } + + public function testCanViewInstance(): void + { + $instance = $this->factory()->create(); + $view_url = action([$this->class(), 'show'], [$this->routeKey() => $instance]); + $response = $this->get($view_url); + $response->assertOk(); + $response->assertViewHas($this->routeKey()); + } + + public function testCanEditInstance(): void + { + $instance = $this->factory()->create(); + $edit_url = action([$this->class(), 'edit'], [$this->routeKey() => $instance]); + $response = $this->get($edit_url); + $response->assertOk(); + + $new_instance = $this->factory()->make(); + $put_url = action([$this->class(), 'update'], [$this->routeKey() => $instance]); + $response = $this->followingRedirects()->put($put_url, $new_instance->toArray()); + $response->assertOk(); + $response->assertSessionHasNoErrors(); + $response->assertViewHas($this->routeKey()); + } + + public function testCanDeleteInstance(): void + { + $instance = $this->factory()->create(); + $delete_url = action([$this->class(), 'delete'], [$this->routeKey() => $instance]); + $response = $this->get($delete_url); + $response->assertOk(); + $response->assertViewHas($this->routeKey()); + + $destroy_url = action([$this->class(), 'destroy'], [$this->routeKey() => $instance]); + $response = $this->followingRedirects()->delete($destroy_url); + $response->assertOk(); + + $view_url = action([$this->class(), 'show'], [$this->routeKey() => $instance]); + $response = $this->get($view_url); + $response->assertNotFound(); + } + +}