GetOrderCountingRecordsTest.php 6.8 KB

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