Improve readability?
This commit is contained in:
@@ -10,6 +10,7 @@ use App\Models\User;
|
||||
use App\Notifications\AcceptanceAssetAcceptedNotification;
|
||||
use App\Notifications\AcceptanceAssetDeclinedNotification;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use Tests\TestCase;
|
||||
|
||||
class AccessoryAcceptanceTest extends TestCase
|
||||
@@ -98,11 +99,66 @@ class AccessoryAcceptanceTest extends TestCase
|
||||
$this->assertNull($acceptance->fresh()->accepted_at);
|
||||
}
|
||||
|
||||
public static function data()
|
||||
{
|
||||
yield 'Current behavior' => [
|
||||
function () {
|
||||
return function (User $assignee) {
|
||||
return CheckoutAcceptance::query()
|
||||
->where([
|
||||
'assigned_to_id' => $assignee->id,
|
||||
'qty' => 3,
|
||||
])
|
||||
->whereNull('accepted_at')
|
||||
->whereNull('declined_at')
|
||||
->whereHasMorph(
|
||||
'checkoutable',
|
||||
[Accessory::class],
|
||||
)
|
||||
->sole();
|
||||
};
|
||||
}
|
||||
];
|
||||
|
||||
yield 'Previous behavior' => [
|
||||
function () {
|
||||
return function (User $assignee) {
|
||||
$checkoutAcceptance = CheckoutAcceptance::query()
|
||||
->where([
|
||||
'assigned_to_id' => $assignee->id,
|
||||
'qty' => 3,
|
||||
])
|
||||
->whereNull('accepted_at')
|
||||
->whereNull('declined_at')
|
||||
->whereHasMorph(
|
||||
'checkoutable',
|
||||
[Accessory::class],
|
||||
)
|
||||
->sole();
|
||||
|
||||
// previous behavior did not set `qty`.
|
||||
$checkoutAcceptance->qty = null;
|
||||
$checkoutAcceptance->save();
|
||||
|
||||
return $checkoutAcceptance;
|
||||
};
|
||||
}
|
||||
];
|
||||
|
||||
// @todo:
|
||||
// ensure existing checkouts for the user are not affected.
|
||||
// in other words, make sure the removal of rows from `accessories_checkout` is not too eager, especially around legacy behavior.
|
||||
// ie...if a user accepted previous accessories then those should not be touched.
|
||||
}
|
||||
|
||||
/**
|
||||
* @link https://github.com/grokability/snipe-it/issues/17589
|
||||
*/
|
||||
public function test_all_accessory_checkouts_are_removed_when_user_declines_acceptance()
|
||||
#[DataProvider('data')]
|
||||
public function test_all_accessory_checkouts_are_removed_when_user_declines_acceptance($provided)
|
||||
{
|
||||
$getCheckoutAcceptance = $provided();
|
||||
|
||||
$assignee = User::factory()->create();
|
||||
|
||||
$this->actingAs(User::factory()->checkoutAccessories()->create());
|
||||
@@ -110,7 +166,7 @@ class AccessoryAcceptanceTest extends TestCase
|
||||
// create accessory that requires acceptance
|
||||
$accessory = Accessory::factory()->requiringAcceptance()->create(['qty' => 5]);
|
||||
|
||||
// check out the accessory to a user with qty of 3 using the new behavior: `checkout_acceptances.qty` is 3
|
||||
// checkout 3 accessories to the user
|
||||
$this->post(route('accessories.checkout.store', $accessory), [
|
||||
'assigned_user' => $assignee->id,
|
||||
'checkout_qty' => 3,
|
||||
@@ -118,18 +174,8 @@ class AccessoryAcceptanceTest extends TestCase
|
||||
|
||||
$originalAccessoryCheckoutCount = AccessoryCheckout::count();
|
||||
|
||||
$checkoutAcceptance = CheckoutAcceptance::query()
|
||||
->where([
|
||||
'assigned_to_id' => $assignee->id,
|
||||
'qty' => 3,
|
||||
])
|
||||
->whereNull('accepted_at')
|
||||
->whereNull('declined_at')
|
||||
->whereHasMorph(
|
||||
'checkoutable',
|
||||
[Accessory::class],
|
||||
)
|
||||
->sole();
|
||||
// get the checkout acceptance via the function that will put it in a state ready for testing
|
||||
$checkoutAcceptance = $getCheckoutAcceptance($assignee);
|
||||
|
||||
// decline the checkout
|
||||
$this->actingAs($assignee)
|
||||
@@ -138,52 +184,5 @@ class AccessoryAcceptanceTest extends TestCase
|
||||
]);
|
||||
|
||||
$this->assertEquals($originalAccessoryCheckoutCount - 3, AccessoryCheckout::count());
|
||||
|
||||
// @todo:
|
||||
// ensure existing checkouts for the user are not affected.
|
||||
// in other words, make sure the removal of rows from `accessories_checkout` is not too eager, especially around legacy behavior.
|
||||
// ie...if a user accepted previous accessories then those should not be touched.
|
||||
}
|
||||
|
||||
public function test_all_previous_accessory_checkouts_are_removed_when_user_declines_acceptance()
|
||||
{
|
||||
$assignee = User::factory()->create();
|
||||
|
||||
$this->actingAs(User::factory()->checkoutAccessories()->create());
|
||||
|
||||
// create accessory that requires acceptance
|
||||
$accessory = Accessory::factory()->requiringAcceptance()->create(['qty' => 5]);
|
||||
|
||||
$this->post(route('accessories.checkout.store', $accessory), [
|
||||
'assigned_user' => $assignee->id,
|
||||
'checkout_qty' => 3,
|
||||
]);
|
||||
|
||||
$originalAccessoryCheckoutCount = AccessoryCheckout::count();
|
||||
|
||||
$legacyCheckoutAcceptance = CheckoutAcceptance::query()
|
||||
->where([
|
||||
'assigned_to_id' => $assignee->id,
|
||||
'qty' => 3,
|
||||
])
|
||||
->whereNull('accepted_at')
|
||||
->whereNull('declined_at')
|
||||
->whereHasMorph(
|
||||
'checkoutable',
|
||||
[Accessory::class],
|
||||
)
|
||||
->sole();
|
||||
|
||||
// previous behavior did not set `qty`.
|
||||
$legacyCheckoutAcceptance->qty = null;
|
||||
$legacyCheckoutAcceptance->save();
|
||||
|
||||
// decline the checkout
|
||||
$this->actingAs($assignee)
|
||||
->post(route('account.store-acceptance', $legacyCheckoutAcceptance), [
|
||||
'asset_acceptance' => 'declined',
|
||||
]);
|
||||
|
||||
$this->assertEquals($originalAccessoryCheckoutCount - 3, AccessoryCheckout::count());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user