OrderCountingRecordServiceGetTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace Tests\Services\OrderCountingRecordService;
  3. use App\Order;
  4. use App\Owner;
  5. use App\Services\CacheService;
  6. use App\Services\OrderCountingRecordService;
  7. use App\User;
  8. use Carbon\Carbon;
  9. use Illuminate\Foundation\Testing\RefreshDatabase;
  10. use Tests\TestCase;
  11. class OrderCountingRecordServiceGetTest extends TestCase
  12. {
  13. use RefreshDatabase;
  14. /** @var OrderCountingRecordService $orderCountingRecordService */
  15. public $orderCountingRecordService;
  16. public function setUp(): void
  17. {
  18. parent::setUp();
  19. app()->singleton('CacheService', CacheService::class);
  20. $this->orderCountingRecordService = app(OrderCountingRecordService::class);
  21. }
  22. /**
  23. * @test
  24. */
  25. public function counting_unit_date()
  26. {
  27. cache()->flush();
  28. $start = Carbon::now()->subDays(2)->format('Y-m-d');
  29. $end = (new Carbon())->toDateString();
  30. $this->actingAs($user = factory(User::class)->create(['name' => 'yang']));
  31. $owner = factory(Owner::class)->create();
  32. $orders = factory(Order::class)->times(20)->create([
  33. 'created_at' => Carbon::now(),
  34. 'owner_id' => $owner->id,
  35. 'wms_status' => '订单完成']);
  36. $orders = factory(Order::class)->times(10)->create([
  37. 'created_at' => Carbon::now()->subDays(1),
  38. 'owner_id' => $owner->id,
  39. 'wms_status' => '订单完成']);
  40. $result = $this->orderCountingRecordService->orderCountingRecords($start, $end)->toArray();
  41. $this->assertEquals([0, 10, 20], array_column($result, 'counter'));
  42. cache()->flush();
  43. $orders = factory(Order::class)->times(20)->create([
  44. 'created_at' => Carbon::now(),
  45. 'owner_id' => $owner->id,
  46. 'wms_status' => '订单完成']);
  47. $result = $this->orderCountingRecordService->orderCountingRecords($start, $end)->toArray();
  48. $this->assertEquals([0, 10, 40], array_column($result, 'counter'));
  49. cache()->flush();
  50. Order::query()->delete();
  51. Owner::query()->delete();
  52. User::query()->delete();
  53. }
  54. /**
  55. * @test
  56. */
  57. public function counting_unit_week()
  58. {
  59. cache()->flush();
  60. $start = Carbon::now()->subWeek()->format('Y-m-d');
  61. $end = (new Carbon())->toDateString();
  62. $this->actingAs(factory(User::class)->create(['name' => 'yang']));
  63. $owner = factory(Owner::class)->create();
  64. $orders1 = factory(Order::class)->times(20)->create([
  65. 'created_at' => Carbon::now(),
  66. 'owner_id' => $owner->id,
  67. 'wms_status' => '订单完成']);
  68. $orders2 = factory(Order::class)->times(20)->create([
  69. 'created_at' => Carbon::now()->subWeek(),
  70. 'owner_id' => $owner->id,
  71. 'wms_status' => '订单完成']);
  72. $result = $this->orderCountingRecordService->orderCountingRecords($start, $end, null, '周')->toArray();
  73. $this->assertEquals([20, 20], array_column($result, 'counter'));
  74. cache()->flush();
  75. $orders1 = factory(Order::class)->times(20)->create([
  76. 'created_at' => Carbon::now(),
  77. 'owner_id' => $owner->id,
  78. 'wms_status' => '订单完成']);
  79. $result = $this->orderCountingRecordService->orderCountingRecords($start, $end, null, '周')->toArray();
  80. $this->assertEquals([20, 40], array_column($result, 'counter'));
  81. Order::query()->delete();
  82. Owner::query()->delete();
  83. User::query()->delete();
  84. }
  85. /**
  86. * @test
  87. */
  88. public function counting_unit_month()
  89. {
  90. cache()->flush();
  91. $start = Carbon::now()->subMonth()->format('Y-m-d');
  92. $end = (new Carbon())->toDateString();
  93. $this->actingAs(factory(User::class)->create(['name' => 'yang']));
  94. $owner = factory(Owner::class)->create();
  95. $orders1 = factory(Order::class)->times(20)->create([
  96. 'created_at' => Carbon::now(),
  97. 'owner_id' => $owner->id,
  98. 'wms_status' => '订单完成']);
  99. $orders2 = factory(Order::class)->times(20)->create([
  100. 'created_at' => Carbon::now()->subMonth(),
  101. 'owner_id' => $owner->id,
  102. 'wms_status' => '订单完成']);
  103. $result = $this->orderCountingRecordService->orderCountingRecords($start, $end, null, '月')->toArray();
  104. $this->assertEquals([20, 20], array_column($result, 'counter'));
  105. cache()->flush();
  106. $orders1 = factory(Order::class)->times(20)->create([
  107. 'created_at' => Carbon::now(),
  108. 'owner_id' => $owner->id,
  109. 'wms_status' => '订单完成']);
  110. $result = $this->orderCountingRecordService->orderCountingRecords($start, $end, null, '月')->toArray();
  111. $this->assertEquals([20, 40], array_column($result, 'counter'));
  112. Order::query()->delete();
  113. Owner::query()->delete();
  114. User::query()->delete();
  115. }
  116. /**
  117. * @test
  118. */
  119. public function set_null()
  120. {
  121. $dateStr = '2020-11-26';
  122. $this->assertTrue($dateStr != Carbon::now()->format('Y-m-d'));
  123. }
  124. /**
  125. * @test
  126. */
  127. public function isNotCurrentDate()
  128. {
  129. $day_ture = $this->orderCountingRecordService->isNotCurrentDate(Carbon::now()->subDay()->format('Y-m-d'));
  130. $day_false = $this->orderCountingRecordService->isNotCurrentDate(Carbon::now()->format('Y-m-d'));
  131. $week_ture = $this->orderCountingRecordService->isNotCurrentDate(Carbon::now()->subWeek()->year . '-' . Carbon::now()->subWeek()->week);
  132. $week_false = $this->orderCountingRecordService->isNotCurrentDate(Carbon::now()->year . '-' . Carbon::now()->week);
  133. $month_true = $this->orderCountingRecordService->isNotCurrentDate(Carbon::now()->subMonth()->year . '-' . Carbon::now()->subMonth()->month);
  134. $month_false = $this->orderCountingRecordService->isNotCurrentDate(Carbon::now()->year . '-' . Carbon::now()->month);
  135. $this->assertTrue($day_ture);
  136. $this->assertFalse($day_false);
  137. $this->assertTrue($week_ture);
  138. $this->assertFalse($week_false);
  139. $this->assertTrue($month_true);
  140. $this->assertFalse($month_false);
  141. }
  142. }