From de9456cfea6e99e43ba6ee208cc80eedb8ce3374 Mon Sep 17 00:00:00 2001 From: Mattias Persson Date: Thu, 22 Aug 2019 17:10:20 +0200 Subject: [PATCH] Updated the discount controller to use the new API #10 --- src/Cart/Cart.php | 7 +- src/Controllers/CartDiscountController.php | 28 +--- .../DiscountValidationException.php | 43 ++++++ tests/Http/Discounts/AddDiscountHttpTest.php | 44 ++++++ tests/REST/Discounts/AddDiscountHttpTest.php | 39 ------ .../AddDiscountValidationHttpTest.php | 125 ------------------ 6 files changed, 97 insertions(+), 189 deletions(-) create mode 100644 src/Exceptions/DiscountValidationException.php create mode 100644 tests/Http/Discounts/AddDiscountHttpTest.php delete mode 100644 tests/REST/Discounts/AddDiscountHttpTest.php delete mode 100644 tests/REST/Discounts/AddDiscountValidationHttpTest.php diff --git a/src/Cart/Cart.php b/src/Cart/Cart.php index 094f3aa..cb642ec 100644 --- a/src/Cart/Cart.php +++ b/src/Cart/Cart.php @@ -3,6 +3,7 @@ namespace Happypixels\Shopr\Cart; use Illuminate\Support\Collection; +use Happypixels\Shopr\Models\Order; use Illuminate\Support\Facades\Event; use Happypixels\Shopr\Money\Formatter; use Happypixels\Shopr\Contracts\Shoppable; @@ -10,6 +11,7 @@ use Illuminate\Contracts\Support\Arrayable; use Happypixels\Shopr\Models\DiscountCoupon; use Happypixels\Shopr\Exceptions\CartItemNotFoundException; +use Happypixels\Shopr\Exceptions\DiscountValidationException; class Cart implements Arrayable { @@ -169,7 +171,10 @@ public function addDiscount($coupon) collect(config('shopr.discount_coupons.validation_rules'))->each(function ($rule) use ($coupon) { $rule = new $rule; - throw_if(!$rule->passes('code', $coupon->code), new \Exception($rule->message(), 422)); + throw_if( + !$rule->passes('code', $coupon->code), + new DiscountValidationException($rule->message()) + ); }); if (is_string($coupon)) { diff --git a/src/Controllers/CartDiscountController.php b/src/Controllers/CartDiscountController.php index e88ce28..dd3b7d6 100644 --- a/src/Controllers/CartDiscountController.php +++ b/src/Controllers/CartDiscountController.php @@ -3,22 +3,14 @@ namespace Happypixels\Shopr\Controllers; use Illuminate\Http\Request; -use Happypixels\Shopr\Cart\Cart; use Illuminate\Routing\Controller; -use Happypixels\Shopr\Models\DiscountCoupon; +use Happypixels\Shopr\Facades\Cart; use Illuminate\Foundation\Validation\ValidatesRequests; class CartDiscountController extends Controller { use ValidatesRequests; - protected $cart; - - public function __construct(Cart $cart) - { - $this->cart = $cart; - } - /** * Applies a discount coupon to the cart. * @@ -27,22 +19,10 @@ public function __construct(Cart $cart) */ public function store(Request $request) { - // Default validation rules that are always checked. - $rules = ['required', 'string']; - - // Configurated rules. - if (! empty(config('shopr.discount_coupons.validation_rules'))) { - foreach (config('shopr.discount_coupons.validation_rules') as $rule) { - $rules[] = new $rule; - } - } - - $this->validate($request, ['code' => $rules]); - - $coupon = DiscountCoupon::where('code', $request->code)->first(); + $this->validate($request, ['code' => ['required', 'string']]); - $this->cart->addDiscount($coupon); + Cart::addDiscount($request->code); - return $this->cart->summary(); + return Cart::get(); } } diff --git a/src/Exceptions/DiscountValidationException.php b/src/Exceptions/DiscountValidationException.php new file mode 100644 index 0000000..8cd7a13 --- /dev/null +++ b/src/Exceptions/DiscountValidationException.php @@ -0,0 +1,43 @@ +message = $message; + } + + /** + * Render the exception into an HTTP response. + * + * @param \Illuminate\Http\Request + * @return \Illuminate\Http\Response + */ + public function render($request) + { + return response()->json(['message' => $this->message], $this->code); + } +} diff --git a/tests/Http/Discounts/AddDiscountHttpTest.php b/tests/Http/Discounts/AddDiscountHttpTest.php new file mode 100644 index 0000000..960cbe3 --- /dev/null +++ b/tests/Http/Discounts/AddDiscountHttpTest.php @@ -0,0 +1,44 @@ +withoutExceptionHandling(); + + $discount = factory(DiscountCoupon::class)->create(); + + Cart::shouldReceive('addDiscount')->once()->with($discount->code); + Cart::shouldReceive('get')->once()->andReturn(['result']); + + $this->json('POST', 'api/shopr/cart/discounts', ['code' => $discount->code]) + ->assertStatus(200) + ->assertJson(['result']); + } + + /** @test */ + public function it_validates_the_code() + { + $this->json('POST', 'api/shopr/cart/discounts', ['code' => ''])->assertStatus(422); + } + + /** @test */ + public function it_validates_configurated_rules() + { + $discount = factory(DiscountCoupon::class)->create(); + + $this->json('POST', 'api/shopr/cart/discounts', ['code' => $discount->code]) + ->assertStatus(422) + ->assertJsonFragment(['message' => trans('shopr::cart.cart_is_empty')]); + } +} diff --git a/tests/REST/Discounts/AddDiscountHttpTest.php b/tests/REST/Discounts/AddDiscountHttpTest.php deleted file mode 100644 index 7e5c284..0000000 --- a/tests/REST/Discounts/AddDiscountHttpTest.php +++ /dev/null @@ -1,39 +0,0 @@ -create(['is_fixed' => true, 'value' => 50]); - $cart = app(Cart::class); - - $this->addCartItem(); - - $this->json('POST', 'api/shopr/cart/discounts', ['code' => $discount->code])->assertStatus(200); - - $this->assertEquals(450, $cart->total()); - $this->assertTrue($cart->discounts()->first()->shoppable->isDiscount()); - } - - /** @test */ - public function it_returns_the_cart_summary() - { - $discount = factory(DiscountCoupon::class)->create(['is_fixed' => true, 'value' => 50]); - - $this->addCartItem(); - - $this->json('POST', 'api/shopr/cart/discounts', ['code' => $discount->code]) - ->assertStatus(200) - ->assertJsonStructure(['count', 'total', 'sub_total', 'tax_total', 'items']); - } -} diff --git a/tests/REST/Discounts/AddDiscountValidationHttpTest.php b/tests/REST/Discounts/AddDiscountValidationHttpTest.php deleted file mode 100644 index 83e4edd..0000000 --- a/tests/REST/Discounts/AddDiscountValidationHttpTest.php +++ /dev/null @@ -1,125 +0,0 @@ -json('POST', 'api/shopr/cart/discounts') - ->assertStatus(422) - ->assertJsonFragment(['The code field is required.']); - } - - /** @test */ - public function cart_must_have_items() - { - $response = $this->json('POST', 'api/shopr/cart/discounts', ['code' => 'TEST']) - ->assertStatus(422) - ->assertJsonFragment(['Your cart is empty.']); - } - - /** @test */ - public function only_one_coupon_allowed() - { - $discounts = factory(DiscountCoupon::class, 2)->create(); - - $this->addCartItem(); - - app(Cart::class)->addDiscount($discounts->first()); - - $response = $this->json('POST', 'api/shopr/cart/discounts', ['code' => $discounts->last()->code]) - ->assertStatus(422) - ->assertJsonFragment(['A discount coupon has already been applied.']); - } - - /** @test */ - public function coupon_has_not_been_applied() - { - $discount = factory(DiscountCoupon::class)->create(); - - $this->addCartItem(); - - app(Cart::class)->addDiscount($discount); - - $response = $this->json('POST', 'api/shopr/cart/discounts', ['code' => $discount->code]) - ->assertStatus(422) - ->assertJsonFragment(['That discount coupon has already been applied.']); - } - - /** @test */ - public function coupon_exists() - { - $this->addCartItem(); - - $response = $this->json('POST', 'api/shopr/cart/discounts', ['code' => 'Test']) - ->assertStatus(422) - ->assertJsonFragment(['Invalid discount coupon.']); - } - - /** @test */ - public function coupon_is_valid() - { - $discount = factory(DiscountCoupon::class)->create(['valid_until' => now()->subDays(1)]); - - $this->addCartItem(); - - $response = $this->json('POST', 'api/shopr/cart/discounts', ['code' => $discount->code]) - ->assertStatus(422) - ->assertJsonFragment(['Invalid discount coupon.']); - } - - /** @test */ - public function cart_value_is_high_enough() - { - $discount = factory(DiscountCoupon::class)->create(['lower_cart_limit' => 600]); - $discount2 = factory(DiscountCoupon::class)->create(['value' => 600, 'is_fixed' => 1]); - - $this->addCartItem(); - - $response = $this->json('POST', 'api/shopr/cart/discounts', ['code' => $discount->code]) - ->assertStatus(422) - ->assertJsonFragment([trans('shopr::discounts.invalid_coupon')]); - - $response = $this->json('POST', 'api/shopr/cart/discounts', ['code' => $discount2->code]) - ->assertStatus(422) - ->assertJsonFragment([trans('shopr::discounts.invalid_coupon')]); - } - - /** @test */ - public function it_validates_custom_rules() - { - $rules = config('shopr.discount_coupons.validation_rules'); - $rules[] = new DiscountTestRule; - config(['shopr.discount_coupons.validation_rules' => $rules]); - - $discount = factory(DiscountCoupon::class)->create(); - - $this->addCartItem(); - - $response = $this->json('POST', 'api/shopr/cart/discounts', ['code' => $discount->code]) - ->assertStatus(422) - ->assertJsonFragment(['The test rule failed.']); - } - - /** @test */ - public function it_skips_rules_that_are_not_enabled() - { - config(['shopr.discount_coupons.validation_rules' => []]); - - // An invalid discount coupon. - $discount = factory(DiscountCoupon::class)->create(['valid_until' => now()->subDays(1)]); - - $response = $this->json('POST', 'api/shopr/cart/discounts', ['code' => $discount->code]) - ->assertStatus(200); - } -}