OrderCountingRecordsTest.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 Tests\TestCase;
  11. class OrderCountingRecordsTest extends TestCase
  12. {
  13. protected $newOrderCountingRecordService;
  14. protected $queryConditionDay;
  15. protected $queryConditionWeek;
  16. protected $queryConditionMonth;
  17. protected $queryConditionYear;
  18. protected $ownerIds;
  19. protected $cache_key = 'order_counting_records_';
  20. protected $step_length = 1;
  21. protected $orderCountingRecordIds = [];
  22. protected $units = ['日', '月', '年'];
  23. protected $orderIds;
  24. protected function setUp(): void
  25. {
  26. parent::setUp(); // TODO: Change the autogenerated stub
  27. cache()->flush();
  28. $this->newOrderCountingRecordService = new NewOrderCountingRecordService();
  29. $this->actingAs(factory(User::class)->create(['name' => 'yang']));
  30. $owners = factory(Owner::class)->times(2)->create();
  31. $this->ownerIds = array_column($owners->toArray(), 'id');
  32. $this->queryConditionDay = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subDays($this->step_length)->toDateString(), Carbon::now()->toDateString(), '日', $this->ownerIds);
  33. $this->queryConditionMonth = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subMonths($this->step_length)->toDateString(), Carbon::now()->toDateString(), '月', $this->ownerIds);
  34. $this->queryConditionYear = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subYears($this->step_length)->toDateString(), Carbon::now()->toDateString(), '年', $this->ownerIds);
  35. }
  36. protected function tearDown(): void
  37. {
  38. cache()->flush();
  39. Owner::destroy($this->ownerIds);
  40. orderCountingRecord::destroy($this->orderCountingRecordIds);
  41. parent::tearDown();
  42. }
  43. /**
  44. * @test
  45. */
  46. public function unit_day()
  47. {
  48. for ($i = 0; $i <= $this->step_length; $i++) {
  49. foreach ($this->ownerIds as $ownerId) {
  50. $dateStr = Carbon::now()->subDays($i)->toDateTimeString();
  51. $order = factory(Order::class)->create([
  52. 'created_at' => $dateStr,
  53. 'owner_id' => $ownerId,
  54. 'wms_status' => '订单完成'
  55. ]);
  56. $this->orderIds = $order->id;
  57. }
  58. }
  59. $start = Carbon::now()->subDays($this->step_length)->toDateString();
  60. $end = Carbon::now()->toDateString();
  61. $result = $this->newOrderCountingRecordService->orderCountingRecords($start, $end, '日', $this->ownerIds);
  62. $this->assertEquals([2,2],$result->pluck('counter')->toArray());
  63. }
  64. }