123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2018-06-27
- * Time: 16:31
- */
- namespace iBrand\Component\Discount\Test\Checkers;
- use iBrand\Component\Discount\Checkers\DatesEligibilityChecker;
- use iBrand\Component\Discount\Models\Coupon;
- use iBrand\Component\Discount\Test\BaseTest;
- use Carbon\Carbon;
- use iBrand\Component\Discount\Models\Discount;
- use Faker\Factory;
- class DatesEligibilityCheckerTest extends BaseTest
- {
- public function testIsEligible()
- {
- $faker = Factory::create('zh_CN');
- $discountChecker = $this->app->make(DatesEligibilityChecker::class);
- $discount = Discount::create([
- 'title' => $faker->word,
- 'label' => $faker->word,
- 'usage_limit' => $faker->numberBetween(80, 100),
- 'used' => 20,
- 'starts_at' => Carbon::now()->addDay(1),
- 'ends_at' => Carbon::now()->addDay(2),
- ]);
- $result = $discountChecker->isEligible($discount);
- $this->assertFalse($result);
- $discount = Discount::create([
- 'title' => $faker->word,
- 'label' => $faker->word,
- 'usage_limit' => $faker->numberBetween(80, 100),
- 'used' => 20,
- 'starts_at' => Carbon::now()->addDay(-1),
- 'ends_at' => Carbon::now()->addDay(2),
- ]);
- $result = $discountChecker->isEligible($discount);
- $this->assertTrue($result);
- $discount = Discount::create([
- 'title' => $faker->word,
- 'label' => $faker->word,
- 'usage_limit' => $faker->numberBetween(80, 100),
- 'used' => 20,
- 'starts_at' => Carbon::now()->addDay(-2),
- 'ends_at' => Carbon::now()->addDay(-1),
- ]);
- $result = $discountChecker->isEligible($discount);
- $this->assertFalse($result);
- }
- public function testCouponIsEligible(){
- $faker = Factory::create('zh_CN');
- $checker = $this->app->make(DatesEligibilityChecker::class);
- $discount = Discount::create([
- 'title' => $faker->word,
- 'label' => $faker->word,
- 'usage_limit' => $faker->numberBetween(80, 100),
- 'used' => 20,
- 'starts_at' => Carbon::now()->addDay(-1),
- 'ends_at' => Carbon::now()->addDay(2),
- ]);
- //test coupon expired_at
- $coupon = Coupon::create(['discount_id'=>$discount->id,'user_id'=>$this->user->id,'expires_at'=>Carbon::now()->addDay(1)]);
- $result = $checker->isEligible($coupon);
- $this->assertTrue($result);
- $coupon = Coupon::create(['discount_id'=>$discount->id,'user_id'=>$this->user->id,'expires_at'=>Carbon::now()->addDay(-1)]);
- $result = $checker->isEligible($coupon);
- $this->assertFalse($result);
- //test coupon usestart_at and endstart_at
- //test active time range.
- $discount->usestart_at=Carbon::now()->addDay(-1);
- $discount->useend_at=Carbon::now()->addDay(1);
- $discount->save();
- $coupon = Coupon::create(['discount_id'=>$discount->id,'user_id'=>$this->user->id,'expires_at'=>Carbon::now()->addDay(1)]);
- $result = $checker->isEligible($coupon);
- $this->assertTrue($result);
- //test error usestart_at
- $discount->usestart_at=Carbon::now()->addDay(1);
- $discount->save();
- $coupon = Coupon::create(['discount_id'=>$discount->id,'user_id'=>$this->user->id,'expires_at'=>Carbon::now()->addDay(1)]);
- $result = $checker->isEligible($coupon);
- $this->assertFalse($result);
- //test right usestart_at
- $discount->usestart_at=Carbon::now()->addDay(-1);
- $discount->save();
- $coupon = Coupon::create(['discount_id'=>$discount->id,'user_id'=>$this->user->id,'expires_at'=>Carbon::now()->addDay(1)]);
- $result = $checker->isEligible($coupon);
- $this->assertTrue($result);
- //test error useend_at
- $discount->useend_at=Carbon::now()->addDay(-1);
- $discount->save();
- $coupon = Coupon::create(['discount_id'=>$discount->id,'user_id'=>$this->user->id]);
- $result = $checker->isEligible($coupon);
- $this->assertFalse($result);
- //test right useend_at
- $discount->useend_at=Carbon::now()->addDay(1);
- $discount->save();
- $coupon = Coupon::create(['discount_id'=>$discount->id,'user_id'=>$this->user->id]);
- $result = $checker->isEligible($coupon);
- $this->assertTrue($result);
- }
- }
|