BaseTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. namespace iBrand\Component\Discount\Test;
  3. use Carbon\Carbon;
  4. use Faker\Factory;
  5. use iBrand\Component\Discount\Actions\OrderFixedDiscountAction;
  6. use iBrand\Component\Discount\Actions\OrderPercentageDiscountAction;
  7. use iBrand\Component\Discount\Checkers\CartQuantityRuleChecker;
  8. use iBrand\Component\Discount\Checkers\ItemTotalRuleChecker;
  9. use iBrand\Component\Discount\Contracts\AdjustmentContract;
  10. use iBrand\Component\Discount\Models\Action;
  11. use iBrand\Component\Discount\Models\Coupon;
  12. use iBrand\Component\Discount\Models\Discount;
  13. use iBrand\Component\Discount\Models\Rule;
  14. use iBrand\Component\Discount\Repositories\DiscountRepository;
  15. use iBrand\Component\Discount\Test\Models\Adjustment;
  16. use iBrand\Component\Discount\Test\Models\User;
  17. use Orchestra\Testbench\TestCase;
  18. use Illuminate\Foundation\Testing\DatabaseMigrations;
  19. /**
  20. * Class BaseTest
  21. * @package iBrand\Component\Discount\Test
  22. */
  23. abstract class BaseTest extends TestCase
  24. {
  25. use DatabaseMigrations;
  26. protected $user;
  27. /**
  28. *
  29. */
  30. protected function setUp()
  31. {
  32. parent::setUp(); // TODO: Change the autogenerated stub
  33. $this->loadMigrationsFrom(__DIR__ . '/database');
  34. $this->seedData();
  35. $this->app->bind(AdjustmentContract::class,Adjustment::class);
  36. }
  37. /**
  38. * @param $app
  39. */
  40. protected function getEnvironmentSetUp($app)
  41. {
  42. // Setup default database to use sqlite :memory:
  43. $app['config']->set('database.default', 'testing');
  44. $app['config']->set('database.connections.testing', [
  45. 'driver' => 'sqlite',
  46. 'database' => ':memory:',
  47. ]);
  48. $app['config']->set('repository.cache.enabled', true);
  49. }
  50. /**
  51. * @param $app
  52. * @return array
  53. */
  54. protected function getPackageProviders($app)
  55. {
  56. return [
  57. \Prettus\Repository\Providers\RepositoryServiceProvider::class,
  58. \Orchestra\Database\ConsoleServiceProvider::class,
  59. \iBrand\Component\Discount\Providers\DiscountServiceProvider::class
  60. ];
  61. }
  62. /**
  63. *
  64. */
  65. protected function seedData()
  66. {
  67. $faker = Factory::create('zh_CN');
  68. $this->seedDiscount($faker);
  69. $this->seedCoupon($faker);
  70. $this->user = new User([
  71. 'id' => 1,
  72. 'name' => 'ibrand'
  73. ]);
  74. $this->seedUserCoupon($faker);
  75. }
  76. /**
  77. * @param $faker
  78. */
  79. protected function seedDiscount($faker)
  80. {
  81. //创建一个未开始的活动
  82. Discount::create([
  83. 'title' => $faker->word,
  84. 'label' => $faker->word,
  85. 'usage_limit' => $faker->numberBetween(3, 100),
  86. 'used' => $faker->randomDigitNotNull,
  87. 'starts_at' => Carbon::now()->addDay(1),
  88. 'ends_at' => Carbon::now()->addDay(2),
  89. ]);
  90. //创建一个状态未禁用的活动
  91. Discount::create([
  92. 'title' => $faker->word,
  93. 'label' => $faker->word,
  94. 'usage_limit' => $faker->numberBetween(3, 100),
  95. 'used' => $faker->randomDigitNotNull,
  96. 'starts_at' => Carbon::now()->addDay(-1),
  97. 'ends_at' => Carbon::now()->addDay(2),
  98. 'status' => 0,
  99. ]);
  100. //创建一个已经全部用完活动
  101. Discount::create([
  102. 'title' => $faker->word,
  103. 'label' => $faker->word,
  104. 'usage_limit' => 100,
  105. 'used' => 100,
  106. 'starts_at' => Carbon::now()->addDay(-1),
  107. 'ends_at' => Carbon::now()->addDay(2),
  108. 'status' => 0,
  109. ]);
  110. //创建一个有效的优惠活动,订单满数量减
  111. $discount = Discount::create([
  112. 'title' => $faker->word,
  113. 'label' => $faker->word,
  114. 'usage_limit' => $faker->numberBetween(80, 100),
  115. 'used' => 20,
  116. 'starts_at' => Carbon::now()->addDay(-1),
  117. 'ends_at' => Carbon::now()->addDay(2),
  118. ]);
  119. //购物车数量满2,则减去10元
  120. Rule::create(['discount_id' => $discount->id, 'type' => CartQuantityRuleChecker::TYPE, 'configuration' => json_encode(['count' => 2])]);
  121. Action::create(['discount_id' => $discount->id, 'type' => OrderFixedDiscountAction::TYPE, 'configuration' => json_encode(['amount' => 10])]);
  122. //创建一个有效的优惠活动,订单满金额减
  123. $discount = Discount::create([
  124. 'title' => $faker->word,
  125. 'label' => $faker->word,
  126. 'usage_limit' => $faker->numberBetween(80, 100),
  127. 'used' => 20,
  128. 'starts_at' => Carbon::now()->addDay(-1),
  129. 'ends_at' => Carbon::now()->addDay(2),
  130. ]);
  131. //订单满100-10
  132. Rule::create(['discount_id' => $discount->id, 'type' => ItemTotalRuleChecker::TYPE, 'configuration' => json_encode(['amount' => 100])]);
  133. Action::create(['discount_id' => $discount->id, 'type' => OrderFixedDiscountAction::TYPE, 'configuration' => json_encode(['amount' => 10])]);
  134. //创建一个有效的优惠活动,订单满金额打折
  135. $discount = Discount::create([
  136. 'title' => $faker->word,
  137. 'label' => $faker->word,
  138. 'usage_limit' => $faker->numberBetween(80, 100),
  139. 'used' => 20,
  140. 'starts_at' => Carbon::now()->addDay(-1),
  141. 'ends_at' => Carbon::now()->addDay(2),
  142. ]);
  143. //订单满120打8折
  144. Rule::create(['discount_id' => $discount->id, 'type' => ItemTotalRuleChecker::TYPE, 'configuration' => json_encode(['amount' => 120])]);
  145. Action::create(['discount_id' => $discount->id, 'type' => OrderPercentageDiscountAction::TYPE, 'configuration' => json_encode(['percentage' => 80])]);
  146. }
  147. /**
  148. * @param $faker
  149. */
  150. protected function seedCoupon($faker)
  151. {
  152. //创建一个未开始的优惠券
  153. Discount::create([
  154. 'title' => $faker->word,
  155. 'label' => $faker->word,
  156. 'usage_limit' => $faker->numberBetween(3, 100),
  157. 'used' => $faker->randomDigitNotNull,
  158. 'starts_at' => Carbon::now()->addDay(1),
  159. 'ends_at' => Carbon::now()->addDay(2),
  160. 'coupon_based' => 1,
  161. ]);
  162. //创建一个状态禁用的优惠券
  163. Discount::create([
  164. 'title' => $faker->word,
  165. 'label' => $faker->word,
  166. 'usage_limit' => $faker->numberBetween(3, 100),
  167. 'used' => $faker->randomDigitNotNull,
  168. 'starts_at' => Carbon::now()->addDay(-1),
  169. 'ends_at' => Carbon::now()->addDay(2),
  170. 'status' => 0,
  171. 'coupon_based' => 1,
  172. ]);
  173. //创建一个已经全部用完优惠券
  174. Discount::create([
  175. 'title' => $faker->word,
  176. 'label' => $faker->word,
  177. 'usage_limit' => 100,
  178. 'used' => 100,
  179. 'starts_at' => Carbon::now()->addDay(-1),
  180. 'ends_at' => Carbon::now()->addDay(2),
  181. 'status' => 0,
  182. 'coupon_based' => 1,
  183. ]);
  184. //创建一个有效的优惠优惠券,订单满数量减
  185. $discount = Discount::create([
  186. 'title' => $faker->word,
  187. 'label' => $faker->word,
  188. 'usage_limit' => $faker->numberBetween(80, 100),
  189. 'used' => 20,
  190. 'starts_at' => Carbon::now()->addDay(-1),
  191. 'ends_at' => Carbon::now()->addDay(2),
  192. 'coupon_based' => 1,
  193. ]);
  194. //购物车数量满2,则减去10元
  195. Rule::create(['discount_id' => $discount->id, 'type' => CartQuantityRuleChecker::TYPE, 'configuration' => json_encode(['count' => 2])]);
  196. Action::create(['discount_id' => $discount->id, 'type' => OrderFixedDiscountAction::TYPE, 'configuration' => json_encode(['amount' => 10])]);
  197. //创建一个有效的优惠优惠券,订单满金额减
  198. $discount = Discount::create([
  199. 'title' => $faker->word,
  200. 'label' => $faker->word,
  201. 'usage_limit' => $faker->numberBetween(80, 100),
  202. 'used' => 20,
  203. 'starts_at' => Carbon::now()->addDay(-1),
  204. 'ends_at' => Carbon::now()->addDay(2),
  205. 'coupon_based' => 1,
  206. ]);
  207. //订单满100-10
  208. Rule::create(['discount_id' => $discount->id, 'type' => ItemTotalRuleChecker::TYPE, 'configuration' => json_encode(['amount' => 100])]);
  209. Action::create(['discount_id' => $discount->id, 'type' => OrderFixedDiscountAction::TYPE, 'configuration' => json_encode(['amount' => 10])]);
  210. //创建一个有效的优惠优惠券,订单满金额打折
  211. $discount = Discount::create([
  212. 'title' => $faker->word,
  213. 'label' => $faker->word,
  214. 'usage_limit' => $faker->numberBetween(80, 100),
  215. 'used' => 20,
  216. 'starts_at' => Carbon::now()->addDay(-1),
  217. 'ends_at' => Carbon::now()->addDay(2),
  218. 'coupon_based' => 1,
  219. ]);
  220. //订单满100打8折
  221. Rule::create(['discount_id' => $discount->id, 'type' => ItemTotalRuleChecker::TYPE, 'configuration' => json_encode(['amount' => 120])]);
  222. Action::create(['discount_id' => $discount->id, 'type' => OrderFixedDiscountAction::TYPE, 'configuration' => json_encode(['percentage' => 80])]);
  223. //创建一个有效的优惠券,订单满金额打折
  224. $discount = Discount::create([
  225. 'title' => $faker->word,
  226. 'label' => $faker->word,
  227. 'usage_limit' => $faker->numberBetween(80, 100),
  228. 'used' => 20,
  229. 'starts_at' => Carbon::now()->addDay(-1),
  230. 'ends_at' => Carbon::now()->addDay(2),
  231. 'coupon_based' => 1,
  232. ]);
  233. //订单数量满3件打9折
  234. Rule::create(['discount_id' => $discount->id, 'type' => CartQuantityRuleChecker::TYPE, 'configuration' => json_encode(['count' => 3])]);
  235. Action::create(['discount_id' => $discount->id, 'type' => OrderFixedDiscountAction::TYPE, 'configuration' => json_encode(['percentage' => 90])]);
  236. }
  237. protected function seedUserCoupon($faker)
  238. {
  239. $repository =$this->app->make(DiscountRepository::class);
  240. //get active discount coupons
  241. $discounts = $repository->findActive(1);
  242. //生成20张有效券
  243. for ($i=0;$i<20;$i++){
  244. Coupon::create(['discount_id'=>$discounts->random()->id,'user_id'=>$this->user->id,]);
  245. }
  246. }
  247. }