OrderCountingRecordServiceGetTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. $this->createCurrentDateOrders($owner);
  33. $this->createSubDayOrders($owner);
  34. $result = $this->orderCountingRecordService->orderCountingRecords($start, $end)->toArray();
  35. $this->assertEquals([0, 10, 20], array_column($result, 'counter'));
  36. cache()->flush();
  37. $this->createCurrentDateOrders($owner);
  38. $result = $this->orderCountingRecordService->orderCountingRecords($start, $end)->toArray();
  39. $this->assertEquals([0, 10, 40], array_column($result, 'counter'));
  40. cache()->flush();
  41. }
  42. /**
  43. * @test
  44. */
  45. public function counting_unit_week()
  46. {
  47. cache()->flush();
  48. $start = Carbon::now()->subWeek()->format('Y-m-d');
  49. $end = (new Carbon())->toDateString();
  50. $this->actingAs(factory(User::class)->create(['name' => 'yang']));
  51. $owner = factory(Owner::class)->create();
  52. $this->createCurrentDateOrders($owner);
  53. $this->createSubWeekOrders($owner);
  54. $result = $this->orderCountingRecordService->orderCountingRecords($start, $end, null, '周')->toArray();
  55. $this->assertEquals([20, 20], array_column($result, 'counter'));
  56. cache()->flush();
  57. $this->createCurrentDateOrders($owner);
  58. $result = $this->orderCountingRecordService->orderCountingRecords($start, $end, null, '周')->toArray();
  59. $this->assertEquals([20, 40], array_column($result, 'counter'));
  60. }
  61. /**
  62. * @test
  63. */
  64. public function counting_unit_month()
  65. {
  66. cache()->flush();
  67. $start = Carbon::now()->subMonth()->format('Y-m-d');
  68. $end = (new Carbon())->toDateString();
  69. $this->actingAs(factory(User::class)->create(['name' => 'yang']));
  70. $owner = factory(Owner::class)->create();
  71. $this->createCurrentDateOrders($owner);
  72. $this->createSubMonthOrders($owner);
  73. $result = $this->orderCountingRecordService->orderCountingRecords($start, $end, null, '月')->toArray();
  74. $this->assertEquals([20, 20], array_column($result, 'counter'));
  75. cache()->flush();
  76. $this->createCurrentDateOrders($owner);
  77. $result = $this->orderCountingRecordService->orderCountingRecords($start, $end, null, '月')->toArray();
  78. $this->assertEquals([20, 40], array_column($result, 'counter'));
  79. }
  80. /**
  81. * @test
  82. */
  83. public function current_date_ttl_day()
  84. {
  85. cache()->flush();
  86. $start = Carbon::now()->subDays(2)->format('Y-m-d');
  87. $end = (new Carbon())->toDateString();
  88. $this->actingAs($user = factory(User::class)->create(['name' => 'yang']));
  89. $owner = factory(Owner::class)->create();
  90. $this->createCurrentDateOrders($owner);
  91. $this->createSubDayOrders($owner);
  92. $result = $this->orderCountingRecordService->orderCountingRecords($start, $end)->toArray();
  93. $this->assertEquals([0, 10, 20], array_column($result, 'counter'));
  94. sleep(4);
  95. $this->createCurrentDateOrders($owner);
  96. $result = $this->orderCountingRecordService->orderCountingRecords($start, $end)->toArray();
  97. $this->assertEquals([0, 10, 40], array_column($result, 'counter'));
  98. cache()->flush();
  99. }
  100. /**
  101. * @test
  102. */
  103. public function current_date_ttl_week()
  104. {
  105. cache()->flush();
  106. $start = Carbon::now()->subWeek()->format('Y-m-d');
  107. $end = (new Carbon())->toDateString();
  108. $this->actingAs(factory(User::class)->create(['name' => 'yang']));
  109. $owner = factory(Owner::class)->create();
  110. $this->createCurrentDateOrders($owner);
  111. $this->createSubWeekOrders($owner);
  112. $result = $this->orderCountingRecordService->orderCountingRecords($start, $end, null, '周')->toArray();
  113. $this->assertEquals([20, 20], array_column($result, 'counter'));
  114. sleep(4);
  115. $this->createCurrentDateOrders($owner);
  116. $result = $this->orderCountingRecordService->orderCountingRecords($start, $end, null, '周')->toArray();
  117. $this->assertEquals([20, 40], array_column($result, 'counter'));
  118. }
  119. /**
  120. * @test
  121. */
  122. public function current_date_ttl_month()
  123. {
  124. cache()->flush();
  125. $start = Carbon::now()->subMonth()->format('Y-m-d');
  126. $end = (new Carbon())->toDateString();
  127. $this->actingAs(factory(User::class)->create(['name' => 'yang']));
  128. $owner = factory(Owner::class)->create();
  129. $this->createCurrentDateOrders($owner);
  130. $this->createSubMonthOrders($owner);
  131. $result = $this->orderCountingRecordService->orderCountingRecords($start, $end, null, '月')->toArray();
  132. $this->assertEquals([20, 20], array_column($result, 'counter'));
  133. sleep(4);
  134. $this->createCurrentDateOrders($owner);
  135. $result = $this->orderCountingRecordService->orderCountingRecords($start, $end, null, '月')->toArray();
  136. $this->assertEquals([20, 40], array_column($result, 'counter'));
  137. }
  138. /**
  139. * @test
  140. */
  141. public function set_null()
  142. {
  143. $dateStr = '2020-11-26';
  144. $this->assertTrue($dateStr != Carbon::now()->format('Y-m-d'));
  145. }
  146. /**
  147. * @test
  148. */
  149. public function isNotCurrentDate()
  150. {
  151. $day_ture = $this->orderCountingRecordService->isNotCurrentDate(Carbon::now()->subDay()->format('Y-m-d'));
  152. $day_false = $this->orderCountingRecordService->isNotCurrentDate(Carbon::now()->format('Y-m-d'));
  153. $week_ture = $this->orderCountingRecordService->isNotCurrentDate(Carbon::now()->subWeek()->year . '-' . Carbon::now()->subWeek()->week);
  154. $week_false = $this->orderCountingRecordService->isNotCurrentDate(Carbon::now()->year . '-' . Carbon::now()->week);
  155. $month_true = $this->orderCountingRecordService->isNotCurrentDate(Carbon::now()->subMonth()->year . '-' . Carbon::now()->subMonth()->month);
  156. $month_false = $this->orderCountingRecordService->isNotCurrentDate(Carbon::now()->year . '-' . Carbon::now()->month);
  157. $this->assertTrue($day_ture);
  158. $this->assertFalse($day_false);
  159. $this->assertTrue($week_ture);
  160. $this->assertFalse($week_false);
  161. $this->assertTrue($month_true);
  162. $this->assertFalse($month_false);
  163. }
  164. public function tearDown(): void
  165. {
  166. Order::query()->delete();
  167. Owner::query()->delete();
  168. User::query()->delete();
  169. }
  170. protected function createCurrentDateOrders($owner): void
  171. {
  172. factory(Order::class)->times(20)->create([
  173. 'created_at' => Carbon::now(),
  174. 'owner_id' => $owner->id,
  175. 'wms_status' => '订单完成']);
  176. }
  177. protected function createSubWeekOrders($owner): void
  178. {
  179. factory(Order::class)->times(20)->create([
  180. 'created_at' => Carbon::now()->subWeek(),
  181. 'owner_id' => $owner->id,
  182. 'wms_status' => '订单完成']);
  183. }
  184. protected function createSubMonthOrders($owner): void
  185. {
  186. factory(Order::class)->times(20)->create([
  187. 'created_at' => Carbon::now()->subMonth(),
  188. 'owner_id' => $owner->id,
  189. 'wms_status' => '订单完成']);
  190. }
  191. protected function createSubDayOrders($owner): void
  192. {
  193. factory(Order::class)->times(10)->create([
  194. 'created_at' => Carbon::now()->subDays(1),
  195. 'owner_id' => $owner->id,
  196. 'wms_status' => '订单完成']);
  197. }
  198. }