GetOrderCountingRecordsTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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()->subWeeks($i)->toDateString();
  61. break;
  62. case "月":
  63. $dateStr = Carbon::now()->subMonths($i)->toDateString();
  64. break;
  65. default:
  66. break;
  67. }
  68. if ($dateStr) {
  69. $orderCountingRecord = factory(OrderCountingRecord::class)->create([
  70. 'date_target' => $dateStr,
  71. 'owner_id' => $ownerId,
  72. 'counting_unit' => $unit,
  73. ]);
  74. $key = 'order_counting_records_' . $dateStr . '_' . $ownerId . '_' . $unit;
  75. cache()->put($key, collect()->push($orderCountingRecord));
  76. if (empty($this->orderCountingRecordIds[$unit])) $this->orderCountingRecordIds[$unit] = [];
  77. $this->orderCountingRecordIds[$unit][] = $orderCountingRecord->id;
  78. $dateStr = null;
  79. }
  80. }
  81. }
  82. }
  83. $resultDay = $this->newOrderCountingRecordService->getOrderCountingRecords($this->queryConditionDay);
  84. $resultWeek = $this->newOrderCountingRecordService->getOrderCountingRecords($this->queryConditionWeek);
  85. // $resultMonth = $this->newOrderCountingRecordService->getOrderCountingRecords($this->queryConditionMonth);
  86. foreach ($this->units as $unit) {
  87. if (!empty($this->orderCountingRecordIds[$unit])) {
  88. switch ($unit) {
  89. case "日":
  90. $this->assertEquals($this->orderCountingRecordIds[$unit], array_column($resultDay->sortBy('id')->toArray(), 'id'));
  91. break;
  92. case "周":
  93. $this->assertEquals($this->orderCountingRecordIds[$unit], array_column($resultWeek->sortBy('id')->toArray(), 'id'));
  94. break;
  95. case "月":
  96. // $this->assertEquals($this->orderCountingRecordIds[$unit], array_column($resultMonth->sortBy('id')->toArray(), 'id'));
  97. break;
  98. }
  99. }
  100. }
  101. }
  102. /**
  103. * @test
  104. */
  105. public
  106. function get_all_from_middle_day()
  107. {
  108. for ($i = 0; $i <= $this->step_length; $i++) {
  109. foreach ($this->ownerIds as $ownerId) {
  110. $unit = '日';
  111. $dateStr = Carbon::now()->subDays($i)->toDateString();
  112. $orderCountingRecord = factory(OrderCountingRecord::class)->create([
  113. 'date_target' => $dateStr,
  114. 'owner_id' => $ownerId,
  115. 'counting_unit' => $unit,
  116. ]);
  117. $this->orderCountingRecordIds[] = $orderCountingRecord->id;
  118. }
  119. }
  120. $result = $this->newOrderCountingRecordService->getOrderCountingRecords($this->queryConditionDay);
  121. $this->assertEquals($this->orderCountingRecordIds, array_column($result->sortBy('id')->toArray(), 'id'));
  122. }
  123. /**
  124. * @test
  125. */
  126. public function get_all_from_orders_day()
  127. {
  128. for ($i = 0; $i <= $this->step_length; $i++) {
  129. foreach ($this->ownerIds as $ownerId) {
  130. $dateStr = Carbon::now()->subDays($i)->toDateTimeString();
  131. $order = factory(Order::class)->create([
  132. 'created_at' => $dateStr,
  133. 'owner_id' => $ownerId,
  134. 'wms_status' => '订单完成'
  135. ]);
  136. $this->orderIds = $order->id;
  137. }
  138. }
  139. $result = $this->newOrderCountingRecordService->getOrderCountingRecords($this->queryConditionDay);
  140. $this->assertNotEmpty($result);
  141. }
  142. /**
  143. * @test
  144. */
  145. public function get_all_from_orders_week()
  146. {
  147. for ($i = 0; $i <= $this->step_length; $i++) {
  148. foreach ($this->ownerIds as $ownerId) {
  149. $dateStr = Carbon::now()->subWeeks($i)->toDateTimeString();
  150. $order = factory(Order::class)->create([
  151. 'created_at' => $dateStr,
  152. 'owner_id' => $ownerId,
  153. 'wms_status' => '订单完成'
  154. ]);
  155. $this->orderIds = $order->id;
  156. }
  157. }
  158. $result = $this->newOrderCountingRecordService->getOrderCountingRecords($this->queryConditionWeek);
  159. dump($result->toArray());
  160. $this->assertEquals([1,1,1,1],array_column($result->toArray(),'amount'));
  161. }
  162. /**
  163. * @test
  164. */
  165. public function get_all_from_orders_Month()
  166. {
  167. for ($i = 0; $i <= $this->step_length; $i++) {
  168. foreach ($this->ownerIds as $ownerId) {
  169. $dateStr = Carbon::now()->subMonths($i)->toDateTimeString();
  170. $order = factory(Order::class)->create([
  171. 'created_at' => $dateStr,
  172. 'owner_id' => $ownerId,
  173. 'wms_status' => '订单完成'
  174. ]);
  175. $this->orderIds = $order->id;
  176. }
  177. }
  178. $result = $this->newOrderCountingRecordService->getOrderCountingRecords($this->queryConditionMonth);
  179. dd($result->toArray());
  180. $this->assertEquals([1,1,1,1],array_column($result->toArray(),'amount'));
  181. }
  182. }