GetOrderCountingRecordsTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. use RefreshDatabase;
  16. protected $newOrderCountingRecordService;
  17. protected $queryConditionDay;
  18. protected $queryConditionWeek;
  19. protected $queryConditionMonth;
  20. protected $queryConditionYear;
  21. protected $ownerIds;
  22. protected $cache_key = 'order_counting_records_';
  23. protected $step_length = 1;
  24. protected $orderCountingRecordIds = [];
  25. protected $units = ['日', '月', '年'];
  26. protected $orderIds;
  27. protected function setUp(): void
  28. {
  29. parent::setUp(); // TODO: Change the autogenerated stub
  30. cache()->flush();
  31. $this->newOrderCountingRecordService = new NewOrderCountingRecordService();
  32. $this->actingAs(factory(User::class)->create(['name' => 'yang']));
  33. $owners = factory(Owner::class)->times(2)->create();
  34. $this->ownerIds = array_column($owners->toArray(), 'id');
  35. $this->queryConditionDay = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subDays($this->step_length)->toDateString(), Carbon::now()->toDateString(), '日', $this->ownerIds);
  36. $this->queryConditionWeek = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subWeeks($this->step_length)->toDateString(), Carbon::now()->toDateString(), '周', $this->ownerIds);
  37. $this->queryConditionMonth = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subMonths($this->step_length)->toDateString(), Carbon::now()->toDateString(), '月', $this->ownerIds);
  38. $this->queryConditionYear = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subYears($this->step_length)->toDateString(), Carbon::now()->toDateString(), '年', $this->ownerIds);
  39. }
  40. protected function tearDown(): void
  41. {
  42. cache()->flush();
  43. Owner::destroy($this->ownerIds);
  44. orderCountingRecord::destroy($this->orderCountingRecordIds);
  45. parent::tearDown();
  46. }
  47. // /**
  48. // * @test
  49. // */
  50. // public function get_all_from_cache()
  51. // {
  52. // for ($i = 0; $i <= $this->step_length; $i++) {
  53. // foreach ($this->ownerIds as $ownerId) {
  54. // foreach ($this->units as $unit) {
  55. // switch ($unit) {
  56. // case "日":
  57. // $dateStr = Carbon::now()->subDays($i)->toDateString();
  58. // break;
  59. // case "月":
  60. // $dateStr = Carbon::now()->subMonths($i)->toDateString();
  61. // break;
  62. // default:
  63. // break;
  64. // }
  65. // if ($dateStr) {
  66. // $orderCountingRecord = factory(OrderCountingRecord::class)->create([
  67. // 'date_target' => $dateStr,
  68. // 'owner_id' => $ownerId,
  69. // 'counting_unit' => $unit,
  70. // ]);
  71. // $key = 'order_counting_records_' . $dateStr . '_' . $ownerId . '_' . $unit;
  72. // cache()->put($key, collect()->push($orderCountingRecord));
  73. // if (empty($this->orderCountingRecordIds[$unit])) $this->orderCountingRecordIds[$unit] = [];
  74. // $this->orderCountingRecordIds[$unit][] = $orderCountingRecord->id;
  75. // $dateStr = null;
  76. // }
  77. // }
  78. // }
  79. // }
  80. // $resultDay = $this->newOrderCountingRecordService->getOrderCountingRecords($this->queryConditionDay);
  81. // $resultWeek = $this->newOrderCountingRecordService->getOrderCountingRecords($this->queryConditionWeek);
  82. //// $resultMonth = $this->newOrderCountingRecordService->getOrderCountingRecords($this->queryConditionMonth);
  83. // foreach ($this->units as $unit) {
  84. // if (!empty($this->orderCountingRecordIds[$unit])) {
  85. // switch ($unit) {
  86. // case "日":
  87. // $this->assertEquals($this->orderCountingRecordIds[$unit], array_column($resultDay->sortBy('id')->toArray(), 'id'));
  88. // break;
  89. // case "月":
  90. //// $this->assertEquals($this->orderCountingRecordIds[$unit], array_column($resultMonth->sortBy('id')->toArray(), 'id'));
  91. // break;
  92. // }
  93. // }
  94. // }
  95. //
  96. // }
  97. /**
  98. * @test
  99. */
  100. public
  101. function get_all_from_middle_day()
  102. {
  103. for ($i = 0; $i <= $this->step_length; $i++) {
  104. foreach ($this->ownerIds as $ownerId) {
  105. $unit = '日';
  106. $dateStr = Carbon::now()->subDays($i)->toDateString();
  107. $orderCountingRecord = factory(OrderCountingRecord::class)->create([
  108. 'date_target' => $dateStr,
  109. 'owner_id' => $ownerId,
  110. 'counting_unit' => $unit,
  111. ]);
  112. $this->orderCountingRecordIds[] = $orderCountingRecord->id;
  113. }
  114. }
  115. $result = $this->newOrderCountingRecordService->getOrderCountingRecords($this->queryConditionDay);
  116. $this->assertEquals($this->orderCountingRecordIds, array_column($result->sortBy('id')->toArray(), 'id'));
  117. }
  118. /**
  119. * @test
  120. */
  121. public function get_all_from_orders_day()
  122. {
  123. for ($i = 0; $i <= $this->step_length; $i++) {
  124. foreach ($this->ownerIds as $ownerId) {
  125. $dateStr = Carbon::now()->subDays($i)->toDateTimeString();
  126. $order = factory(Order::class)->create([
  127. 'created_at' => $dateStr,
  128. 'owner_id' => $ownerId,
  129. 'wms_status' => '订单完成'
  130. ]);
  131. $this->orderIds = $order->id;
  132. }
  133. }
  134. $result = $this->newOrderCountingRecordService->getOrderCountingRecords($this->queryConditionDay);
  135. $this->assertEquals([1, 1, 1, 1], array_column($result->toArray(), 'amount'));
  136. }
  137. /**
  138. * @test
  139. */
  140. public function get_all_from_orders_Month()
  141. {
  142. for ($i = 0; $i <= $this->step_length; $i++) {
  143. foreach ($this->ownerIds as $ownerId) {
  144. $dateStr = Carbon::now()->subMonths($i)->toDateTimeString();
  145. $order = factory(Order::class)->create([
  146. 'created_at' => $dateStr,
  147. 'owner_id' => $ownerId,
  148. 'wms_status' => '订单完成'
  149. ]);
  150. $this->orderIds = $order->id;
  151. }
  152. }
  153. $result = $this->newOrderCountingRecordService->getOrderCountingRecords($this->queryConditionMonth);
  154. $this->assertEquals([1, 1, 1, 1], array_column($result->toArray(), 'amount'));
  155. }
  156. /**
  157. * @test
  158. */
  159. public function get_all_from_orders_Year()
  160. {
  161. for ($i = 0; $i <= $this->step_length; $i++) {
  162. foreach ($this->ownerIds as $ownerId) {
  163. $dateStr = Carbon::now()->subYears($i)->toDateTimeString();
  164. $order = factory(Order::class)->create([
  165. 'created_at' => $dateStr,
  166. 'owner_id' => $ownerId,
  167. 'wms_status' => '订单完成'
  168. ]);
  169. $this->orderIds = $order->id;
  170. }
  171. }
  172. $result = $this->newOrderCountingRecordService->getOrderCountingRecords($this->queryConditionYear);
  173. $this->assertEquals([1, 1, 1, 1], array_column($result->toArray(), 'amount'));
  174. }
  175. }