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