testing
This commit is contained in:
parent
450dba1d74
commit
de6c64bb14
2 changed files with 80 additions and 26 deletions
tests
|
@ -12,29 +12,59 @@
|
||||||
$response->assertOk();
|
$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 () {
|
test('logged in users can view category index', function () {
|
||||||
$user = User::factory()->create();
|
$user = User::factory()->create();
|
||||||
$response = $this->actingAs($user)->get(route('categories.index'));
|
$response = $this->actingAs($user)->get(route('categories.index'));
|
||||||
|
|
||||||
$response->assertOk();
|
$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);
|
||||||
|
});
|
||||||
|
|
|
@ -12,7 +12,9 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use Illuminate\Database\Eloquent\Collection;
|
use Illuminate\Database\Eloquent\Collection;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Pest\Expectation;
|
use Pest\Expectation;
|
||||||
|
use function PHPUnit\Framework\assertTrue;
|
||||||
|
|
||||||
pest()
|
pest()
|
||||||
->extend(Tests\TestCase::class)
|
->extend(Tests\TestCase::class)
|
||||||
|
@ -32,20 +34,42 @@
|
||||||
|
|
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
//expect()->extend(
|
// https://github.com/defstudio/pest-plugin-laravel-expectations
|
||||||
// 'collectionToContainModel',
|
|
||||||
// function (Model): Expectation {
|
|
||||||
// return $this->
|
|
||||||
// }
|
|
||||||
//);
|
|
||||||
|
|
||||||
expect()->extend(
|
expect()->extend(
|
||||||
'toBeEloquentCollection',
|
'toBeEloquentCollection',
|
||||||
|
/**
|
||||||
|
* Assert that the value is an instance of \Illuminate\Database\Eloquent\Collection.
|
||||||
|
*/
|
||||||
function (): Expectation {
|
function (): Expectation {
|
||||||
return $this->toBeInstanceOf(Collection::class);
|
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
|
| Functions
|
||||||
|
|
Loading…
Reference in a new issue