Skip to content

Commit

Permalink
Merge pull request #24 from yabhq/feat/cart-find-trashed-by-id
Browse files Browse the repository at this point in the history
Feat/cart find trashed by
  • Loading branch information
jimhlad authored Nov 4, 2021
2 parents 1403afc + b671b2b commit c243e76
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,16 @@ public function __construct(protected Cart $cart)
* Find a checkout by an existing ID.
*
* @param string $checkoutId
* @param bool $withTrashed
*
* @return \Yab\ShoppingCart\Checkout
*/
public static function findById(string $checkoutId) : Checkout
public static function findById(string $checkoutId, bool $withTrashed = false) : Checkout
{
$checkout = Cart::find($checkoutId);
$checkout = $withTrashed ? Cart::withTrashed()->find($checkoutId) : Cart::find($checkoutId);

if (!$checkout) {
throw new CheckoutNotFoundException;
if (! $checkout) {
throw new CheckoutNotFoundException();
}

return new Checkout($checkout);
Expand Down
22 changes: 22 additions & 0 deletions tests/Feature/CheckoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Yab\ShoppingCart\Tests\Models\NonPurchaser;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Yab\ShoppingCart\Tests\Models\NonPurchaseable;
use Yab\ShoppingCart\Exceptions\CheckoutNotFoundException;
use Yab\ShoppingCart\Exceptions\PurchaserInvalidException;
use Yab\ShoppingCart\Exceptions\ItemNotPurchaseableException;

Expand Down Expand Up @@ -50,6 +51,27 @@ public function a_checkout_can_be_retrieved_by_the_cart_id()
$this->assertEquals($cart->id, $checkout->getCart()->id);
}

/** @test */
public function a_deleted_checkout_can_be_retrieved_by_the_cart_id()
{
$cart = factory(Cart::class)->create();
$cart->delete();

$this->assertSoftDeleted('carts', [
'id' => $cart->id,
]);

$this->expectException(CheckoutNotFoundException::class);

$checkout = Checkout::findById($cart->id);

// Try again withTrashed set to true
$checkout = Checkout::findById($cart->id, true);

$this->assertTrue($checkout instanceof Checkout);
$this->assertEquals($cart->id, $checkout->getCart()->id);
}

/** @test */
public function a_checkout_can_be_destroyed()
{
Expand Down

0 comments on commit c243e76

Please sign in to comment.