DatesEligibilityCheckerTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2018-06-27
  6. * Time: 16:31
  7. */
  8. namespace iBrand\Component\Discount\Test\Checkers;
  9. use iBrand\Component\Discount\Checkers\DatesEligibilityChecker;
  10. use iBrand\Component\Discount\Models\Coupon;
  11. use iBrand\Component\Discount\Test\BaseTest;
  12. use Carbon\Carbon;
  13. use iBrand\Component\Discount\Models\Discount;
  14. use Faker\Factory;
  15. class DatesEligibilityCheckerTest extends BaseTest
  16. {
  17. public function testIsEligible()
  18. {
  19. $faker = Factory::create('zh_CN');
  20. $discountChecker = $this->app->make(DatesEligibilityChecker::class);
  21. $discount = Discount::create([
  22. 'title' => $faker->word,
  23. 'label' => $faker->word,
  24. 'usage_limit' => $faker->numberBetween(80, 100),
  25. 'used' => 20,
  26. 'starts_at' => Carbon::now()->addDay(1),
  27. 'ends_at' => Carbon::now()->addDay(2),
  28. ]);
  29. $result = $discountChecker->isEligible($discount);
  30. $this->assertFalse($result);
  31. $discount = Discount::create([
  32. 'title' => $faker->word,
  33. 'label' => $faker->word,
  34. 'usage_limit' => $faker->numberBetween(80, 100),
  35. 'used' => 20,
  36. 'starts_at' => Carbon::now()->addDay(-1),
  37. 'ends_at' => Carbon::now()->addDay(2),
  38. ]);
  39. $result = $discountChecker->isEligible($discount);
  40. $this->assertTrue($result);
  41. $discount = Discount::create([
  42. 'title' => $faker->word,
  43. 'label' => $faker->word,
  44. 'usage_limit' => $faker->numberBetween(80, 100),
  45. 'used' => 20,
  46. 'starts_at' => Carbon::now()->addDay(-2),
  47. 'ends_at' => Carbon::now()->addDay(-1),
  48. ]);
  49. $result = $discountChecker->isEligible($discount);
  50. $this->assertFalse($result);
  51. }
  52. public function testCouponIsEligible(){
  53. $faker = Factory::create('zh_CN');
  54. $checker = $this->app->make(DatesEligibilityChecker::class);
  55. $discount = Discount::create([
  56. 'title' => $faker->word,
  57. 'label' => $faker->word,
  58. 'usage_limit' => $faker->numberBetween(80, 100),
  59. 'used' => 20,
  60. 'starts_at' => Carbon::now()->addDay(-1),
  61. 'ends_at' => Carbon::now()->addDay(2),
  62. ]);
  63. //test coupon expired_at
  64. $coupon = Coupon::create(['discount_id'=>$discount->id,'user_id'=>$this->user->id,'expires_at'=>Carbon::now()->addDay(1)]);
  65. $result = $checker->isEligible($coupon);
  66. $this->assertTrue($result);
  67. $coupon = Coupon::create(['discount_id'=>$discount->id,'user_id'=>$this->user->id,'expires_at'=>Carbon::now()->addDay(-1)]);
  68. $result = $checker->isEligible($coupon);
  69. $this->assertFalse($result);
  70. //test coupon usestart_at and endstart_at
  71. //test active time range.
  72. $discount->usestart_at=Carbon::now()->addDay(-1);
  73. $discount->useend_at=Carbon::now()->addDay(1);
  74. $discount->save();
  75. $coupon = Coupon::create(['discount_id'=>$discount->id,'user_id'=>$this->user->id,'expires_at'=>Carbon::now()->addDay(1)]);
  76. $result = $checker->isEligible($coupon);
  77. $this->assertTrue($result);
  78. //test error usestart_at
  79. $discount->usestart_at=Carbon::now()->addDay(1);
  80. $discount->save();
  81. $coupon = Coupon::create(['discount_id'=>$discount->id,'user_id'=>$this->user->id,'expires_at'=>Carbon::now()->addDay(1)]);
  82. $result = $checker->isEligible($coupon);
  83. $this->assertFalse($result);
  84. //test right usestart_at
  85. $discount->usestart_at=Carbon::now()->addDay(-1);
  86. $discount->save();
  87. $coupon = Coupon::create(['discount_id'=>$discount->id,'user_id'=>$this->user->id,'expires_at'=>Carbon::now()->addDay(1)]);
  88. $result = $checker->isEligible($coupon);
  89. $this->assertTrue($result);
  90. //test error useend_at
  91. $discount->useend_at=Carbon::now()->addDay(-1);
  92. $discount->save();
  93. $coupon = Coupon::create(['discount_id'=>$discount->id,'user_id'=>$this->user->id]);
  94. $result = $checker->isEligible($coupon);
  95. $this->assertFalse($result);
  96. //test right useend_at
  97. $discount->useend_at=Carbon::now()->addDay(1);
  98. $discount->save();
  99. $coupon = Coupon::create(['discount_id'=>$discount->id,'user_id'=>$this->user->id]);
  100. $result = $checker->isEligible($coupon);
  101. $this->assertTrue($result);
  102. }
  103. }