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