testing improvements

This commit is contained in:
punkfairie 2025-02-25 18:42:57 -08:00
parent 7ec1c2b7f0
commit 71d401b138
Signed by: punkfairie
GPG key ID: B3C5488E9A1A7CA6
2 changed files with 74 additions and 122 deletions

View file

@ -2,7 +2,9 @@
use App\Http\Controllers\CategoryController;
use App\Models\Category;
use App\Models\User;
use function Pest\Laravel\delete;
use function Pest\Laravel\get;
use function Pest\Laravel\patch;
covers(CategoryController::class);
@ -11,54 +13,39 @@
describe('categories.index', function () {
test('can be rendered to guests', function () {
$response = $this->get(route('categories.index'));
$response->assertOk();
get(route('categories.index'))->assertOk();
});
test('can be rendered to logged in users', function () {
$user = User::factory()->create();
$response = $this->actingAs($user)->get(route('categories.index'));
$response->assertOk();
asAdmin()->get(route('categories.index'))->assertOk();
});
test('contains categories', function () {
$response = $this->get(route('categories.index'));
$response = get(route('categories.index'));
$response->assertViewHas('categories', Category::all());
expect($response['categories'])
->toBeEloquentCollection()
->toContainOnlyInstancesOf(Category::class);
->toContainOnlyInstancesOf(Category::class)
->toEqual(Category::all());
});
test('contains trashedCategories', function () {
$response = $this->get(route('categories.index'));
$response = get(route('categories.index'));
$response->assertViewHas(
'trashedCategories',
Category::onlyTrashed()->get()
);
expect($response['categories'])
expect($response['trashedCategories'])
->toBeEloquentCollection()
->toContainOnlyInstancesOf(Category::class);
->toContainOnlyInstancesOf(Category::class)
->toEqual(Category::onlyTrashed()->get());
});
});
describe('categories.create', function () {
test("can't be rendered to guests", function () {
$response = $this->get(route('categories.create'));
$response->assertForbidden();
get(route('categories.create'))->assertForbidden();
});
test('can be rendered to logged in users', function () {
$user = User::factory()->create();
$response = $this->actingAs($user)->get(route('categories.create'));
$response->assertOk();
asAdmin()->get(route('categories.create'))->assertOk();
});
});
@ -66,90 +53,77 @@
test("can't be accessed by guests", function () {
$category = Category::factory()->make();
$response = $this->post(
$this->post(
route('categories.store'),
['name' => $category->name]
);
$response->assertForbidden();
)->assertForbidden();
expect($category)->not->toMatchDatabaseRecord();
});
test('can be accessed by logged in users', function () {
$user = User::factory()->create();
$category = Category::factory()->make();
$response = $this->actingAs($user)->post(
asAdmin()->post(
route('categories.store'),
['name' => $category->name]
);
$response->assertRedirect(route('categories.index', absolute: false));
)->assertRedirect(route('categories.index'));
expect($category)->toMatchDatabaseRecord();
});
test('stores valid categories', function (string $name) {
$user = User::factory()->create();
$category = Category::factory()->make(['name' => $name]);
$response = $this->actingAs($user)->post(
asAdmin()->post(
route('categories.store'),
['name' => $name]
);
$response->assertRedirect(route('categories.index', absolute: false));
)->assertRedirect(route('categories.index'));
expect($category)->toMatchDatabaseRecord();
})->with('valid-values');
test('does not store invalid categories', function (mixed $name) {
$user = User::factory()->create();
$category = Category::factory()->make(['name' => $name]);
$response = $this->actingAs($user)->post(
asAdmin()->post(
route('categories.store'),
['name' => $name],
);
$response->assertRedirect();
)->assertRedirect();
expect($category)->not->toMatchDatabaseRecord();
})->with('invalid-values');
});
describe('categories.show', function () {
test('can be rendered to guests', function () {
$category = Category::factory()->create();
$response = $this->get(route('categories.show', $category));
$response->assertOk();
get(route('categories.show', Category::factory()->create()))
->assertOk();
});
test('can be rendered to logged in users', function () {
$user = User::factory()->create();
$category = Category::factory()->create();
$response = $this
->actingAs($user)
->get(route('categories.show', $category));
$response->assertOk();
asAdmin()
->get(route('categories.show', Category::factory()->create()))
->assertOk();
});
test('contains the category', function () {
$category = Category::factory()->create();
$response = $this->get(route('categories.show', $category));
$response = get(route('categories.show', $category));
$response->assertViewHas('category', $category);
expect($response['category'])
->id->toBe($category->id)
->name->toBe($category->name);
});
test('can show trashed categories', function () {
$category = Category::factory()->trashed()->create();
$response = $this->get(route('categories.show', $category));
$response = get(route('categories.show', $category));
$response->assertViewHas('category', $category);
expect($category)->toBeSoftDeleted();
// @formatter:off
expect($response['category'])
->id->toBe($category->id)
->name->toBe($category->name)
->and($category)->toBeSoftDeleted();
// @formatter:on
});
});
@ -157,31 +131,26 @@
test("can't be rendered to guests", function () {
$category = Category::factory()->create();
$response = $this->get(route('categories.edit', $category));
$response->assertForbidden();
get(route('categories.edit', $category))
->assertForbidden();
});
test('can be rendered to logged in users', function () {
$user = User::factory()->create();
$category = Category::factory()->create();
$response = $this
->actingAs($user)
->get(route('categories.edit', $category));
$response->assertOk();
asAdmin()
->get(route('categories.edit', $category))
->assertOk();
});
test('contains the category', function () {
$user = User::factory()->create();
$category = Category::factory()->create();
$response = $this
->actingAs($user)
->get(route('categories.edit', $category));
$response = asAdmin()->get(route('categories.edit', $category));
$response->assertViewHas('category', $category);
expect($response['category'])
->id->toBe($category->id)
->name->toBe($category->name);
});
});
@ -190,79 +159,64 @@
$category = Category::factory()->create();
$updated = Category::factory()->make();
$response = $this->patch(route('categories.update', $category), [
'name' => $updated->name
]);
$response->assertForbidden();
patch(
route('categories.update', $category),
['name' => $updated->name]
)->assertForbidden();
expect($category)->toBeInDatabaseExactly();
});
test('can be accessed by logged in users', function () {
$user = User::factory()->create();
$category = Category::factory()->create();
$updated = Category::factory()->make();
$response = $this
->actingAs($user)
->patch(route('categories.update', $category), [
'name' => $updated->name
]);
asAdmin()->patch(
route('categories.update', $category),
['name' => $updated->name]
)->assertRedirect(route('categories.index', absolute: false));
$category->refresh();
$response->assertRedirect(route('categories.index', absolute: false));
expect($category)->toMatchObject($updated);
});
test('updates categories with valid values', function (string $name) {
$user = User::factory()->create();
$category = Category::factory()->create();
$response = $this
->actingAs($user)
->patch(route('categories.update', $category), [
'name' => $name
]);
asAdmin()->patch(
route('categories.update', $category),
['name' => $name]
)->assertRedirect(route('categories.index', absolute: false));
$category->refresh();
$response->assertRedirect(route('categories.index', absolute: false));
expect($category->name)->toEqual($name);
})->with('valid-values');
test(
'does not update categories with invalid values',
function (mixed $name) {
$user = User::factory()->create();
$category = Category::factory()->create();
$response = $this
->actingAs($user)
->patch(route('categories.update', $category), [
'name' => $name
]);
asAdmin()->patch(
route('categories.update', $category),
['name' => $name]
)->assertRedirect();
$updated = Category::find($category->id);
$response->assertRedirect();
expect($updated->name)->toEqual($category->name);
}
)->with('invalid-values');
test('restores trashed categories', function () {
$user = User::factory()->create();
$category = Category::factory()->trashed()->create();
$response = $this
->actingAs($user)
asAdmin()
->patch(route('categories.update', $category), [
'restore' => 1
]);
])->assertRedirect();
$category->refresh();
$response->assertRedirect();
expect($category)->not->toBeSoftDeleted();
});
});
@ -271,21 +225,17 @@ function (mixed $name) {
test("can't be accessed by guests", function () {
$category = Category::factory()->create();
$response = $this->delete(route('categories.destroy', $category));
$response->assertForbidden();
delete(route('categories.destroy', $category))
->assertForbidden();
expect($category)->not->toBeSoftDeleted();
});
test('can be accessed by logged in users', function () {
$user = User::factory()->create();
$category = Category::factory()->create();
$response = $this
->actingAs($user)
->delete(route('categories.destroy', $category));
$response->assertRedirect(route('categories.index', absolute: false));
asAdmin()
->delete(route('categories.destroy', $category))
->assertRedirect(route('categories.index', absolute: false));
expect($category)->toBeSoftDeleted();
});
});

View file

@ -11,8 +11,10 @@
|
*/
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Pest\Expectation;
use Tests\TestCase;
use function Pest\Laravel\assertDatabaseHas;
use function Pest\Laravel\assertModelExists;
@ -82,7 +84,7 @@ function (Closure $next, mixed $expected) {
|
*/
function something()
function asAdmin(): TestCase
{
// ..
return test()->actingAs(User::factory()->create());
}