Add basic Food tests

This commit is contained in:
Christopher C. Wells 2021-03-28 20:46:36 -07:00 committed by Christopher Charbonneau Wells
parent 04aede3893
commit 702c9d1147
5 changed files with 114 additions and 3 deletions

View File

@ -22,9 +22,11 @@ class FoodFactory extends Factory
'detail' => $this->faker->sentence(2),
'brand' => $this->faker->word,
'source' => $this->faker->url,
'notes' => $this->faker->paragraph,
'serving_size' => $this->faker->randomFloat(2, 1/2, 5),
'serving_unit' => $this->faker->randomElement(['tsp', 'tbsp', 'cup']),
'serving_weight' => $this->faker->numberBetween(5, 500),
'serving_unit_name' => $this->faker->word,
'calories' => $this->faker->randomFloat(2, 0, 100),
'fat' => $this->faker->randomFloat(2, 0, 10),
'cholesterol' => $this->faker->randomFloat(2, 0, 100),

View File

@ -3,15 +3,15 @@
namespace Tests;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Foundation\Application;
trait CreatesApplication
{
/**
* Creates the application.
*
* @return \Illuminate\Foundation\Application
*/
public function createApplication()
public function createApplication(): Application
{
$app = require __DIR__.'/../bootstrap/app.php';

View File

@ -0,0 +1,72 @@
<?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()
{
$response = $this->get('/foods');
$response->assertOk();
}
public function testCanAddFood()
{
/** @var \App\Models\Food $food */
$food = Food::factory()->make();
$response = $this->followingRedirects()->post('/foods', $food->toArray());
$response->assertOk();
$response->assertSee("Food {$food->name} updated!");
}
public function testCanViewFood()
{
/** @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()
{
/** @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();
$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()
{
/** @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();
}
}

View File

@ -0,0 +1,15 @@
<?php
namespace Tests;
abstract class LoggedInTestCase extends TestCase
{
use LogsIn;
public function setUp(): void
{
parent::setUp();
$this->loginUser();
}
}

22
tests/LogsIn.php Normal file
View File

@ -0,0 +1,22 @@
<?php
namespace Tests;
use App\Models\User;
trait LogsIn
{
protected User $user;
/**
* Creates a user and logs the user in.
*/
public function loginUser(): void
{
$this->user = User::factory()->create();
$this->post('/login', [
'email' => $this->user->email,
'password' => 'password',
]);
}
}