diff --git a/tests/Feature/CategoryTest.php b/tests/Feature/CategoryTest.php index b9bf250..fed7cf7 100644 --- a/tests/Feature/CategoryTest.php +++ b/tests/Feature/CategoryTest.php @@ -12,29 +12,59 @@ $response->assertOk(); }); -test('category index request contains categories', function () { - $response = $this->get(route('categories.index')); - - $response->assertViewHas('categories'); - expect($response['categories']) - ->toBeEloquentCollection() - ->toContainOnlyInstancesOf(Category::class) - ->toEqual(Category::all()); -}); - -test('category index request contains trashedCategories', function () { - $response = $this->get(route('categories.index')); - - $response->assertViewHas('trashedCategories'); - expect($response['trashedCategories']) - ->toBeEloquentCollection() - ->toContainOnlyInstancesOf(Category::class) - ->toEqual(Category::onlyTrashed()->get()); -}); - test('logged in users can view category index', function () { $user = User::factory()->create(); $response = $this->actingAs($user)->get(route('categories.index')); $response->assertOk(); }); + +test('categories.index request contains categories', function () { + $response = $this->get(route('categories.index')); + + $response->assertViewHas('categories', Category::all()); + expect($response['categories']) + ->toBeEloquentCollection() + ->toContainOnlyInstancesOf(Category::class); +}); + +test('categories.index request contains trashedCategories', function () { + $response = $this->get(route('categories.index')); + + $response + ->assertViewHas('trashedCategories', Category::onlyTrashed()->get()); + expect($response['categories']) + ->toBeEloquentCollection() + ->toContainOnlyInstancesOf(Category::class); +}); + +test('guests can view categories', function () { + $category = Category::factory()->create(); + $response = $this->get(route('categories.show', $category)); + + $response->assertOk(); +}); + +test('logged in users can view categories', function () { + $user = User::factory()->create(); + $category = Category::factory()->create(); + $response = $this + ->actingAs($user) + ->get(route('categories.show', $category)); + + $response->assertOk(); +}); + +test('categories.show contains the category', function () { + $category = Category::factory()->create(); + $response = $this->get(route('categories.show', $category)); + + $response->assertViewHas('category', $category); +}); + +test('categories.show can show trashed categories', function () { + $category = Category::factory()->trashed()->create(); + $response = $this->get(route('categories.show', $category)); + + $response->assertViewHas('category', $category); +}); diff --git a/tests/Pest.php b/tests/Pest.php index 2e87deb..4da3399 100755 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -12,7 +12,9 @@ */ use Illuminate\Database\Eloquent\Collection; +use Illuminate\Database\Eloquent\Model; use Pest\Expectation; +use function PHPUnit\Framework\assertTrue; pest() ->extend(Tests\TestCase::class) @@ -32,20 +34,42 @@ | */ -//expect()->extend( -// 'collectionToContainModel', -// function (Model): Expectation { -// return $this-> -// } -//); +// https://github.com/defstudio/pest-plugin-laravel-expectations expect()->extend( 'toBeEloquentCollection', + /** + * Assert that the value is an instance of \Illuminate\Database\Eloquent\Collection. + */ function (): Expectation { return $this->toBeInstanceOf(Collection::class); } ); + +expect()->extend( + 'toBeSameModelAs', + /** + * Assert that the given model has the same ID and belong to the same table of another model. + */ + function (Model $model): Expectation { + /** @var Model $value */ + $value = $this->value; + + assertTrue($value->is($model), + 'Failed asserting that two models have the same ID and belong to the same table'); + + return $this; + } +); + +expect()->intercept( + 'toBe', + Model::class, + function (Model $anotherModel) { + return expect($this->value)->toBeSameModelAs($anotherModel); + }); + /* |-------------------------------------------------------------------------- | Functions