GetOrderCountingRecordsTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace NewOrderCountingRecordService;
  3. use App\Order;
  4. use App\OrderCountingRecord;
  5. use App\Owner;
  6. use App\Services\NewOrderCountingRecordService;
  7. use App\User;
  8. use Carbon\Carbon;
  9. use Illuminate\Foundation\Testing\RefreshDatabase;
  10. use Illuminate\Support\Collection;
  11. use Tests\TestCase;
  12. use Tightenco\Collect\Support\Arr;
  13. class GetOrderCountingRecordsTest extends TestCase
  14. {
  15. protected $newOrderCountingRecordService;
  16. protected $queryConditionDay;
  17. protected $queryConditionWeek;
  18. protected $queryConditionMonth;
  19. protected $queryConditionYear;
  20. protected $ownerIds;
  21. protected $cache_key = 'order_counting_records_';
  22. protected $step_length = 1;
  23. protected $orderCountingRecordIds = [];
  24. protected $units = ['日', '月', '年'];
  25. protected $orderIds;
  26. protected function setUp(): void
  27. {
  28. parent::setUp(); // TODO: Change the autogenerated stub
  29. cache()->flush();
  30. $this->newOrderCountingRecordService = new NewOrderCountingRecordService();
  31. $this->actingAs(factory(User::class)->create(['name' => 'yang']));
  32. $owners = factory(Owner::class)->times(2)->create();
  33. $this->ownerIds = array_column($owners->toArray(), 'id');
  34. $this->queryConditionDay = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subDays($this->step_length)->toDateString(), Carbon::now()->toDateString(), '日', $this->ownerIds);
  35. $this->queryConditionMonth = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subMonths($this->step_length)->toDateString(), Carbon::now()->toDateString(), '月', $this->ownerIds);
  36. $this->queryConditionYear = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subYears($this->step_length)->toDateString(), Carbon::now()->toDateString(), '年', $this->ownerIds);
  37. }
  38. protected function tearDown(): void
  39. {
  40. cache()->flush();
  41. Owner::destroy($this->ownerIds);
  42. OrderCountingRecord::destroy($this->orderCountingRecordIds);
  43. Order::destroy($this->orderIds);
  44. OrderCountingRecord::query()->whereIn('owner_id', $this->ownerIds)->delete();
  45. parent::tearDown();
  46. }
  47. /**
  48. * @test
  49. */
  50. public
  51. function get_all_from_middle_day()
  52. {
  53. for ($i = 0; $i <= $this->step_length; $i++) {
  54. foreach ($this->ownerIds as $ownerId) {
  55. $unit = '日';
  56. $dateStr = Carbon::now()->subDays($i)->toDateString();
  57. $orderCountingRecord = factory(OrderCountingRecord::class)->create([
  58. 'date_target' => $dateStr,
  59. 'owner_id' => $ownerId,
  60. 'counting_unit' => $unit,
  61. ]);
  62. $this->orderCountingRecordIds[] = $orderCountingRecord->id;
  63. }
  64. }
  65. $result = $this->newOrderCountingRecordService->getOrderCountingRecords($this->queryConditionDay);
  66. $this->assertEquals($this->orderCountingRecordIds, array_column($result->sortBy('id')->toArray(), 'id'));
  67. }
  68. /**
  69. * @test
  70. */
  71. public function get_all_from_orders_day()
  72. {
  73. for ($i = 0; $i <= $this->step_length; $i++) {
  74. foreach ($this->ownerIds as $ownerId) {
  75. $dateStr = Carbon::now()->subDays($i)->toDateTimeString();
  76. $order = factory(Order::class)->create([
  77. 'created_at' => $dateStr,
  78. 'owner_id' => $ownerId,
  79. 'wms_status' => '订单完成'
  80. ]);
  81. $this->orderIds = $order->id;
  82. }
  83. }
  84. $result = $this->newOrderCountingRecordService->getOrderCountingRecords($this->queryConditionDay);
  85. $this->assertEquals([1, 1, 1, 1], array_column($result->toArray(), 'amount'));
  86. }
  87. /**
  88. * @test
  89. */
  90. public function get_all_from_orders_Month()
  91. {
  92. for ($i = 0; $i <= $this->step_length; $i++) {
  93. foreach ($this->ownerIds as $ownerId) {
  94. $dateStr = Carbon::now()->subMonths($i)->toDateTimeString();
  95. $order = factory(Order::class)->create([
  96. 'created_at' => $dateStr,
  97. 'owner_id' => $ownerId,
  98. 'wms_status' => '订单完成'
  99. ]);
  100. $this->orderIds = $order->id;
  101. }
  102. }
  103. $result = $this->newOrderCountingRecordService->getOrderCountingRecords($this->queryConditionMonth);
  104. $this->assertEquals([1, 1, 1, 1], array_column($result->toArray(), 'amount'));
  105. }
  106. /**
  107. * @test
  108. */
  109. public function get_all_from_orders_Year()
  110. {
  111. for ($i = 0; $i <= $this->step_length; $i++) {
  112. foreach ($this->ownerIds as $ownerId) {
  113. $dateStr = Carbon::now()->subYears($i)->toDateTimeString();
  114. $order = factory(Order::class)->create([
  115. 'created_at' => $dateStr,
  116. 'owner_id' => $ownerId,
  117. 'wms_status' => '订单完成'
  118. ]);
  119. $this->orderIds = $order->id;
  120. }
  121. }
  122. $result = $this->newOrderCountingRecordService->getOrderCountingRecords($this->queryConditionYear);
  123. $this->assertEquals([1, 1, 1, 1], array_column($result->toArray(), 'amount'));
  124. }
  125. /**
  126. * 按照月份查询插入中间表的测试
  127. * @test
  128. */
  129. public function insert_monthly_order_counting_records()
  130. {
  131. //前一个月的订单2个
  132. $carbon = Carbon::now()->subMonths(1);
  133. $orders1 = factory(Order::class)->times(2)->create(['created_at' => $carbon, 'owner_id' => $this->ownerIds[0], 'wms_status' => '订单完成']);
  134. //本月本日的订单2个
  135. $orders2 = factory(Order::class)->times(2)->create(['created_at' => Carbon::now(), 'owner_id' => $this->ownerIds[0], 'wms_status' => '订单完成']);
  136. $orders = $orders1->merge($orders2);
  137. $this->orderIds = array_column($orders->toArray(), 'id');
  138. //判断中间表中没有上一个月,'counting_unit' => '月' 'owner_id' => $this->ownerIds[0], 的数据
  139. $this->assertDatabaseMissing('order_counting_records', [
  140. 'date_target' => $carbon->startOfMonth()->toDateString(),
  141. 'owner_id' => $this->ownerIds[0],
  142. 'counting_unit' => '月',
  143. 'amount' => '2',
  144. ]);
  145. $result = $this->newOrderCountingRecordService->getOrderCountingRecords($this->queryConditionMonth);
  146. $this->assertDatabaseHas('order_counting_records', [
  147. 'date_target' => $carbon->startOfMonth()->toDateString(),
  148. 'owner_id' => $this->ownerIds[0],
  149. 'counting_unit' => '月',
  150. 'amount' => '2',
  151. ]);
  152. dump($result->toArray());
  153. }
  154. }