From 0d5b3fe4fc20f60a6f0dc065f12af88a1d819e91 Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Tue, 20 Apr 2021 15:02:45 -0700 Subject: [PATCH] Add User CRUD controller tests --- .../Factories/IngredientAmountFactory.php | 3 + .../Controllers/HttpControllerTestCase.php | 15 ++++ .../Http/Controllers/UserControllerTest.php | 69 +++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 tests/Feature/Http/Controllers/UserControllerTest.php diff --git a/database/Factories/IngredientAmountFactory.php b/database/Factories/IngredientAmountFactory.php index fb2ce6b..8588a2d 100644 --- a/database/Factories/IngredientAmountFactory.php +++ b/database/Factories/IngredientAmountFactory.php @@ -43,6 +43,9 @@ class IngredientAmountFactory extends Factory ]; } + /** + * {@inheritdoc} + */ public function configure(): static { return $this->afterMaking(function (IngredientAmount $ingredient_amount) { diff --git a/tests/Feature/Http/Controllers/HttpControllerTestCase.php b/tests/Feature/Http/Controllers/HttpControllerTestCase.php index 8e55082..23125c9 100644 --- a/tests/Feature/Http/Controllers/HttpControllerTestCase.php +++ b/tests/Feature/Http/Controllers/HttpControllerTestCase.php @@ -31,6 +31,9 @@ abstract class HttpControllerTestCase extends LoggedInTestCase return $this->factory()->create(); } + /** + * Test instances index. + */ public function testCanLoadIndex(): void { $index_url = action([$this->class(), 'index']); @@ -38,6 +41,9 @@ abstract class HttpControllerTestCase extends LoggedInTestCase $response->assertOk(); } + /** + * Test instance add. + */ public function testCanAddInstance(): void { $create_url = action([$this->class(), 'create']); @@ -49,6 +55,9 @@ abstract class HttpControllerTestCase extends LoggedInTestCase $response->assertSessionHasNoErrors(); } + /** + * Test instance view. + */ public function testCanViewInstance(): void { $instance = $this->createInstance(); @@ -58,6 +67,9 @@ abstract class HttpControllerTestCase extends LoggedInTestCase $response->assertViewHas($this->routeKey()); } + /** + * Test instance edit. + */ public function testCanEditInstance(): void { $instance = $this->createInstance(); @@ -71,6 +83,9 @@ abstract class HttpControllerTestCase extends LoggedInTestCase $response->assertSessionHasNoErrors(); } + /** + * Test instance delete/destroy. + */ public function testCanDeleteInstance(): void { $instance = $this->createInstance(); diff --git a/tests/Feature/Http/Controllers/UserControllerTest.php b/tests/Feature/Http/Controllers/UserControllerTest.php new file mode 100644 index 0000000..f14a751 --- /dev/null +++ b/tests/Feature/Http/Controllers/UserControllerTest.php @@ -0,0 +1,69 @@ +setName('can *not* view instance'); + // Users are not independently viewable. + } + + /** + * @inheritdoc + */ + public function testCanAddInstance(): void + { + $create_url = action([$this->class(), 'create']); + $response = $this->get($create_url); + $response->assertOk(); + $instance = $this->factory()->make(); + $attributes = $instance->toArray(); + $attributes['password'] = 'password'; + $attributes['password_confirmation'] = $attributes['password']; + $store_url = action([$this->class(), 'store']); + $response = $this->post($store_url, $attributes); + $response->assertSessionHasNoErrors(); + } + + public function testCanNotDeleteSelf(): void { + $user = User::first(); + $edit_url = action([$this->class(), 'delete'], [$this->routeKey() => $user]); + $response = $this->get($edit_url); + $response->assertForbidden(); + } + +}