OrderCountingRecordsTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. $this->newOrderCountingRecordService = new NewOrderCountingRecordService();
  28. $user = User::query()->where('name', 'yang')->first();
  29. $this->actingAs($user);
  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. Owner::destroy($this->ownerIds);
  39. orderCountingRecord::destroy($this->orderCountingRecordIds);
  40. parent::tearDown();
  41. }
  42. /**
  43. * @test
  44. */
  45. public function unit_day()
  46. {
  47. for ($i = 0; $i <= $this->step_length; $i++) {
  48. foreach ($this->ownerIds as $ownerId) {
  49. $dateStr = Carbon::now()->subDays($i)->toDateTimeString();
  50. $order = factory(Order::class)->create([
  51. 'created_at' => $dateStr,
  52. 'owner_id' => $ownerId,
  53. 'wms_status' => '订单完成'
  54. ]);
  55. $this->orderIds = $order->id;
  56. }
  57. }
  58. $start = Carbon::now()->subDays($this->step_length)->toDateString();
  59. $end = Carbon::now()->toDateString();
  60. $result = $this->newOrderCountingRecordService->orderCountingRecords($start, $end, '日', $this->ownerIds);
  61. $this->assertEquals([2,2],$result->pluck('counter')->toArray());
  62. }
  63. }