Skip to content

Instantly share code, notes, and snippets.

@MrPunyapal
Last active January 17, 2024 16:04
Show Gist options
  • Save MrPunyapal/4f3c89ade162721de2a0ec20945593eb to your computer and use it in GitHub Desktop.
Save MrPunyapal/4f3c89ade162721de2a0ec20945593eb to your computer and use it in GitHub Desktop.
test with seeders
<?php
use App\Models\Team;
use App\Models\User;
use Database\Seeders\RoleAndPermissionSeeder;
use Database\Seeders\TeamSeeder;
use Database\Seeders\UserSeeder;
use function Pest\Laravel\actingAs;
use function Pest\Laravel\get;
use function Pest\Laravel\post;
use function Pest\Laravel\seed;
beforeEach(function () {
seed([
TeamSeeder::class,
UserSeeder::class,
RoleAndPermissionSeeder::class,
]);
$this->user = User::factory()->create([
'team_id' => Team::first()->id,
]);
actingAs($this->user);
});
test('product page loads', function () {
get(route('products.index'))
->assertStatus(200)
->assertSee(['Create Product']);
});
test('product can be created by admin', function () {
$this->user->assignRole('admin');
post(route('products.store'), [
'product_name' => 'Test Product',
'description' => 'Test Description',
'team_id' => $this->user->team_id,
'price' => 12.99,
'discount_type' => 'None',
'discount amount' => null,
])->assertStatus(302)
->assertRedirect(route('products.index'))
->assertSessionHas('success');
});
test('product can not be created by user', function () {
$this->user->revokePermissionTo('create-product');
post(route('products.store'), [
'product_name' => 'Test Product',
'description' => 'Test Description',
'price' => 12.99,
'team_id' => $this->user->team_id,
'discount_type' => 'None',
'discount_amount' => null,
])->assertStatus(401);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment