CartQuantityRuleCheckerTest.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2018-06-27
  6. * Time: 16:22
  7. */
  8. namespace iBrand\Component\Discount\Test\Checkers;
  9. use Carbon\Carbon;
  10. use iBrand\Component\Discount\Actions\OrderFixedDiscountAction;
  11. use iBrand\Component\Discount\Checkers\CartQuantityRuleChecker;
  12. use iBrand\Component\Discount\Models\Action;
  13. use iBrand\Component\Discount\Models\Discount;
  14. use iBrand\Component\Discount\Models\Rule;
  15. use iBrand\Component\Discount\Test\BaseTest;
  16. use Faker\Factory;
  17. use iBrand\Component\Discount\Test\Models\Order;
  18. class CartQuantityRuleCheckerTest extends BaseTest
  19. {
  20. public function testIsEligible()
  21. {
  22. $faker = Factory::create('zh_CN');
  23. $discountChecker = $this->app->make(CartQuantityRuleChecker::class);
  24. $order = Order::create(['user_id' => $this->user->id
  25. , 'count' => 1, 'items_total' => 50, 'total' => 50,]);
  26. //创建一个有效的优惠活动,订单满数量减
  27. $discount = Discount::create([
  28. 'title' => $faker->word,
  29. 'label' => $faker->word,
  30. 'usage_limit' => $faker->numberBetween(80, 100),
  31. 'used' => 20,
  32. 'starts_at' => Carbon::now()->addDay(-1),
  33. 'ends_at' => Carbon::now()->addDay(2),
  34. ]);
  35. //购物车数量满2,则减去10元
  36. $rule = Rule::create(['discount_id' => $discount->id, 'type' => CartQuantityRuleChecker::TYPE, 'configuration' => json_encode(['count' => 2])]);
  37. Action::create(['discount_id' => $discount->id, 'type' => OrderFixedDiscountAction::TYPE, 'configuration' => json_encode(['amount' => 10])]);
  38. $result = $discountChecker->isEligible($order, json_decode($rule->configuration, true),$discount);
  39. $this->assertFalse($result);
  40. $order = Order::create(['user_id' => $this->user->id
  41. , 'count' => 2, 'items_total' => 50, 'total' => 50,]);
  42. $result = $discountChecker->isEligible($order, json_decode($rule->configuration, true),$discount);
  43. $this->assertTrue($result);
  44. }
  45. }