Skip to content

Commit

Permalink
Updated the discount controller to use the new API
Browse files Browse the repository at this point in the history
  • Loading branch information
mattias-persson committed Aug 22, 2019
1 parent 162b93d commit de9456c
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 189 deletions.
7 changes: 6 additions & 1 deletion src/Cart/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
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;
use Happypixels\Shopr\Contracts\CartDriver;
use Illuminate\Contracts\Support\Arrayable;
use Happypixels\Shopr\Models\DiscountCoupon;
use Happypixels\Shopr\Exceptions\CartItemNotFoundException;
use Happypixels\Shopr\Exceptions\DiscountValidationException;

class Cart implements Arrayable
{
Expand Down Expand Up @@ -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)) {
Expand Down
28 changes: 4 additions & 24 deletions src/Controllers/CartDiscountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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();
}
}
43 changes: 43 additions & 0 deletions src/Exceptions/DiscountValidationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Happypixels\Shopr\Exceptions;

use Exception;

class DiscountValidationException extends Exception
{
/**
* The message of the exception.
*
* @var string
*/
protected $message;

/**
* The status code of the exception.
*
* @var int
*/
protected $code = 422;

/**
* Create an instance of the exception.
*
* @param string $message
*/
public function __construct($message)
{
$this->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);
}
}
44 changes: 44 additions & 0 deletions tests/Http/Discounts/AddDiscountHttpTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Happypixels\Shopr\Tests\Http\Discounts;

use Happypixels\Shopr\Facades\Cart;
use Happypixels\Shopr\Tests\TestCase;
use Happypixels\Shopr\Models\DiscountCoupon;
use Happypixels\Shopr\Tests\Support\Traits\InteractsWithCart;

class AddDiscountHttpTest extends TestCase
{
use InteractsWithCart;

/** @test */
public function it_adds_the_coupon()
{
$this->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')]);
}
}
39 changes: 0 additions & 39 deletions tests/REST/Discounts/AddDiscountHttpTest.php

This file was deleted.

125 changes: 0 additions & 125 deletions tests/REST/Discounts/AddDiscountValidationHttpTest.php

This file was deleted.

0 comments on commit de9456c

Please sign in to comment.