Add User CRUD controller tests

This commit is contained in:
Christopher C. Wells 2021-04-20 15:02:45 -07:00
parent a9fad1bff0
commit 0d5b3fe4fc
3 changed files with 87 additions and 0 deletions

View File

@ -43,6 +43,9 @@ class IngredientAmountFactory extends Factory
];
}
/**
* {@inheritdoc}
*/
public function configure(): static
{
return $this->afterMaking(function (IngredientAmount $ingredient_amount) {

View File

@ -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();

View File

@ -0,0 +1,69 @@
<?php
namespace Tests\Feature\Http\Controllers;
use App\Http\Controllers\UserController;
use App\Models\User;
use Database\Factories\UserFactory;
class UserControllerTest extends HttpControllerTestCase
{
/**
* @inheritdoc
*/
public function class(): string
{
return UserController::class;
}
/**
* @inheritdoc
*/
public function factory(): UserFactory
{
return User::factory();
}
/**
* @inheritdoc
*/
public function routeKey(): string
{
return 'user';
}
/**
* @doesNotPerformAssertions
*/
public function testCanViewInstance(): void
{
$this->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();
}
}