get(route('categories.index')); $response->assertOk(); }); test('can be rendered to logged in users', function () { $user = User::factory()->create(); $response = $this->actingAs($user)->get(route('categories.index')); $response->assertOk(); }); test('contains categories', function () { $response = $this->get(route('categories.index')); $response->assertViewHas('categories', Category::all()); expect($response['categories']) ->toBeEloquentCollection() ->toContainOnlyInstancesOf(Category::class); }); test('contains trashedCategories', function () { $response = $this->get(route('categories.index')); $response->assertViewHas( 'trashedCategories', Category::onlyTrashed()->get() ); expect($response['categories']) ->toBeEloquentCollection() ->toContainOnlyInstancesOf(Category::class); }); }); describe('categories.create', function () { test("can't be rendered to guests", function () { $response = $this->get(route('categories.create')); $response->assertForbidden(); }); test('can be rendered to logged in users', function () { $user = User::factory()->create(); $response = $this->actingAs($user)->get(route('categories.create')); $response->assertOk(); }); }); describe('categories.store', function () { test("can't be accessed by guests", function () { $category = Category::factory()->make(); $response = $this->post( route('categories.store'), ['name' => $category->name] ); $response->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( route('categories.store'), ['name' => $category->name] ); $response->assertRedirect(route('categories.index', absolute: false)); 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( route('categories.store'), ['name' => $name] ); $response->assertRedirect(route('categories.index', absolute: false)); 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( route('categories.store'), ['name' => $name], ); $response->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(); }); 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(); }); test('contains the category', function () { $category = Category::factory()->create(); $response = $this->get(route('categories.show', $category)); $response->assertViewHas('category', $category); }); test('can show trashed categories', function () { $category = Category::factory()->trashed()->create(); $response = $this->get(route('categories.show', $category)); $response->assertViewHas('category', $category); expect($category)->toBeSoftDeleted(); }); }); describe('categories.edit', function () { test("can't be rendered to guests", function () { $category = Category::factory()->create(); $response = $this->get(route('categories.edit', $category)); $response->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(); }); test('contains the category', function () { $user = User::factory()->create(); $category = Category::factory()->create(); $response = $this ->actingAs($user) ->get(route('categories.edit', $category)); $response->assertViewHas('category', $category); }); }); describe('categories.update', function () { test("can't be accessed by guests", function () { $category = Category::factory()->create(); $updated = Category::factory()->make(); $response = $this->patch(route('categories.update', $category), [ 'name' => $updated->name ]); $response->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 ]); $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 ]); $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 ]); $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) ->patch(route('categories.update', $category), [ 'restore' => 1 ]); $category->refresh(); $response->assertRedirect(); expect($category)->not->toBeSoftDeleted(); }); }); describe('categories.destroy', function () { test("can't be accessed by guests", function () { $category = Category::factory()->create(); $response = $this->delete(route('categories.destroy', $category)); $response->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)); expect($category)->toBeSoftDeleted(); }); });