mirror of https://github.com/kcal-app/kcal.git
Create base test case for controllers
This commit is contained in:
parent
38ca26291f
commit
cae56df555
|
@ -1,95 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Http\Controllers\FoodController;
|
||||
use App\Models\Food;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\LoggedInTestCase;
|
||||
|
||||
class FoodTest extends LoggedInTestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function testCanLoadFoodIndex(): void
|
||||
{
|
||||
$index_url = action([FoodController::class, 'index']);
|
||||
$response = $this->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();
|
||||
}
|
||||
}
|
|
@ -1,78 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Http\Controllers\GoalController;
|
||||
use App\Models\Goal;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\LoggedInTestCase;
|
||||
|
||||
class GoalTest extends LoggedInTestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function testCanLoadGoalIndex(): void
|
||||
{
|
||||
$index_url = action([GoalController::class, 'index']);
|
||||
$response = $this->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();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Feature\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\FoodController;
|
||||
use App\Models\Food;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
class FoodControllerTest extends HttpControllerTestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function class(): string
|
||||
{
|
||||
return FoodController::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function factory(): Factory
|
||||
{
|
||||
return Food::factory();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function routeKey(): string
|
||||
{
|
||||
return 'food';
|
||||
}
|
||||
|
||||
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!");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Feature\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\GoalController;
|
||||
use App\Models\Goal;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
class GoalControllerTest extends HttpControllerTestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function class(): string
|
||||
{
|
||||
return GoalController::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function factory(): Factory
|
||||
{
|
||||
return Goal::factory();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function routeKey(): string
|
||||
{
|
||||
return 'goal';
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,87 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Feature\Http\Controllers;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Tests\LoggedInTestCase;
|
||||
|
||||
abstract class HttpControllerTestCase extends LoggedInTestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* Get the class name of the controller to be tested.
|
||||
*/
|
||||
abstract public function class(): string;
|
||||
|
||||
/**
|
||||
* Get the factory of the model to be tested.
|
||||
*/
|
||||
abstract public function factory(): Factory;
|
||||
|
||||
/**
|
||||
* Get the route key used for the model to be tested.
|
||||
*/
|
||||
abstract public function routeKey(): string;
|
||||
|
||||
public function testCanLoadIndex(): void
|
||||
{
|
||||
$index_url = action([$this->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();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue