ItemTotalRuleCheckerTest.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2018-06-27
  6. * Time: 16:54
  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\ItemTotalRuleChecker;
  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 ItemTotalRuleCheckerTest extends BaseTest
  19. {
  20. public function testIsEligible()
  21. {
  22. $faker = Factory::create('zh_CN');
  23. $discountChecker = $this->app->make(ItemTotalRuleChecker::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. //购物车数量满100,则减去10元
  36. $rule = Rule::create(['discount_id' => $discount->id, 'type' => ItemTotalRuleChecker::TYPE, 'configuration' => json_encode(['amount' => 100])]);
  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' => 100,]);
  42. $result = $discountChecker->isEligible($order, json_decode($rule->configuration, true),$discount);
  43. $this->assertTrue($result);
  44. }
  45. }