Updated tests

Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
snipe 2024-07-18 03:48:06 +01:00
parent f56006fb6b
commit 2f0c74aef0
2 changed files with 75 additions and 34 deletions

View file

@ -35,33 +35,42 @@ class AccessoryCheckoutTest extends TestCase
])
->assertOk()
->assertStatusMessageIs('error')
->assertJson([
'messages' => [
'assigned_to' => ['The assigned to field must be an integer.'],
->assertJson(
[
'status' => 'error',
'messages' =>
[
'checkout_qty' =>
[
trans_choice('admin/accessories/message.checkout.checkout_qty.lte', 0,
[
'number_currently_remaining' => 0,
'checkout_qty' => 1,
'number_remaining_after_checkout' => 0
])
],
],
'payload' => null,
])
->assertStatus(200)
->json();
}
public function testAccessoryCanBeCheckedOut()
public function testAccessoryCanBeCheckedOutWithoutQty()
{
$accessory = Accessory::factory()->create();
$user = User::factory()->create();
$admin = User::factory()->checkoutAccessories()->create();
$this->actingAsForApi(User::factory()->checkoutAccessories()->create())
$this->actingAsForApi($admin)
->postJson(route('api.accessories.checkout', $accessory), [
'assigned_to' => $user->id,
])
->assertOk()
->assertStatusMessageIs('success')
->assertStatus(200)
->assertJson([
'messages' => [
'assigned_to' => ['Accessory checked out successfully.'],
],
])
->assertJson(['messages' => trans('admin/accessories/message.checkout.success')])
->json();
$this->assertTrue($accessory->users->contains($user));
@ -75,9 +84,7 @@ class AccessoryCheckoutTest extends TestCase
'item_id' => $accessory->id,
'item_type' => Accessory::class,
'user_id' => $admin->id,
'note' => 'oh hi there',
])->count(),
'Log entry either does not exist or there are more than expected'
])->count(),'Log entry either does not exist or there are more than expected'
);
}
@ -87,7 +94,7 @@ class AccessoryCheckoutTest extends TestCase
$user = User::factory()->create();
$admin = User::factory()->checkoutAccessories()->create();
$this->actingAsForApi(User::factory()->checkoutAccessories()->create())
$this->actingAsForApi($admin)
->postJson(route('api.accessories.checkout', $accessory), [
'assigned_to' => $user->id,
'checkout_qty' => 2,
@ -95,11 +102,7 @@ class AccessoryCheckoutTest extends TestCase
->assertOk()
->assertStatusMessageIs('success')
->assertStatus(200)
->assertJson([
'messages' => [
'assigned_to' => ['The assigned to field must be an integer.'],
],
])
->assertJson(['messages' => trans('admin/accessories/message.checkout.success')])
->json();
$this->assertTrue($accessory->users->contains($user));
@ -113,7 +116,6 @@ class AccessoryCheckoutTest extends TestCase
'item_id' => $accessory->id,
'item_type' => Accessory::class,
'user_id' => $admin->id,
'note' => 'oh hi there',
])->count(),
'Log entry either does not exist or there are more than expected'
);
@ -131,15 +133,10 @@ class AccessoryCheckoutTest extends TestCase
])
->assertOk()
->assertStatusMessageIs('error')
->assertJson([
'messages' => [
'assigned_to' => ['The assigned to field must be an integer.'],
],
])
->assertStatus(200)
->json();
$this->assertTrue($accessory->users->contains($user));
$this->assertFalse($accessory->users->contains($user));
}
public function testUserSentNotificationUponCheckout()

View file

@ -20,23 +20,33 @@ class AccessoryCheckoutTest extends TestCase
public function testValidationWhenCheckingOutAccessory()
{
$this->actingAs(User::factory()->checkoutAccessories()->create())
->post(route('accessories.checkout.store', Accessory::factory()->create()), [
$accessory = Accessory::factory()->create();
$response = $this->actingAs(User::factory()->superuser()->create())
->post(route('accessories.checkout.store', $accessory), [
// missing assigned_to
])
->assertSessionHas('error');
->assertStatus(302)
->assertSessionHas('errors')
->assertRedirect(route('accessories.checkout.store', $accessory));
$this->followRedirects($response)->assertSee(trans('general.error'));
}
public function testAccessoryMustBeAvailableWhenCheckingOut()
{
$this->actingAs(User::factory()->checkoutAccessories()->create())
$response = $this->actingAs(User::factory()->viewAccessories()->checkoutAccessories()->create())
->post(route('accessories.checkout.store', Accessory::factory()->withoutItemsRemaining()->create()), [
'assigned_to' => User::factory()->create()->id,
])
->assertSessionHas('error');
->assertStatus(302)
->assertSessionHas('error')
->assertRedirect(route('accessories.index'));
$this->followRedirects($response)->assertSee(trans('general.error'));
}
public function testAccessoryCanBeCheckedOut()
public function testAccessoryCanBeCheckedOutWithoutQuantity()
{
$accessory = Accessory::factory()->create();
$user = User::factory()->create();
@ -44,9 +54,43 @@ class AccessoryCheckoutTest extends TestCase
$this->actingAs(User::factory()->checkoutAccessories()->create())
->post(route('accessories.checkout.store', $accessory), [
'assigned_to' => $user->id,
'note' => 'oh hi there',
]);
$this->assertTrue($accessory->users->contains($user));
$this->assertDatabaseHas('action_logs', [
'action_type' => 'checkout',
'target_id' => $user->id,
'target_type' => User::class,
'item_id' => $accessory->id,
'item_type' => Accessory::class,
'note' => 'oh hi there',
]);
}
public function testAccessoryCanBeCheckedOutWithQuantity()
{
$accessory = Accessory::factory()->count(5)->create();
$user = User::factory()->create();
$this->actingAs(User::factory()->checkoutAccessories()->create())
->post(route('accessories.checkout.store', $accessory), [
'assigned_to' => $user->id,
'checkout_qty' => 3,
'note' => 'oh hi there',
]);
$this->assertTrue($accessory->users->contains($user));
$this->assertDatabaseHas('action_logs', [
'action_type' => 'checkout',
'target_id' => $user->id,
'target_type' => User::class,
'item_id' => $accessory->id,
'item_type' => Accessory::class,
'note' => 'oh hi there',
]);
}
public function testUserSentNotificationUponCheckout()