This commit is contained in:
punkfairie 2025-02-23 21:14:39 -08:00
parent 450dba1d74
commit de6c64bb14
Signed by: punkfairie
GPG key ID: B3C5488E9A1A7CA6
2 changed files with 80 additions and 26 deletions

View file

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

View file

@ -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