flush(); $this->newOrderCountingRecordService = new NewOrderCountingRecordService(); $this->actingAs(factory(User::class)->create(['name' => 'yang'])); $owners = factory(Owner::class)->times(2)->create(); $this->ownerIds = array_column($owners->toArray(), 'id'); $this->queryConditionDay = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subDays($this->step_length)->toDateString(), Carbon::now()->toDateString(), '日', $this->ownerIds); $this->queryConditionWeek = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subWeeks($this->step_length)->toDateString(), Carbon::now()->toDateString(), '周', $this->ownerIds); $this->queryConditionMonth = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subMonths($this->step_length)->toDateString(), Carbon::now()->toDateString(), '月', $this->ownerIds); $this->queryConditionYear = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subYears($this->step_length)->toDateString(), Carbon::now()->toDateString(), '年', $this->ownerIds); } protected function tearDown(): void { cache()->flush(); Owner::destroy($this->ownerIds); orderCountingRecord::destroy($this->orderCountingRecordIds); parent::tearDown(); } /** * @test */ public function get_all_from_cache() { for ($i = 0; $i <= $this->step_length; $i++) { foreach ($this->ownerIds as $ownerId) { foreach ($this->units as $unit) { switch ($unit) { case "日": $dateStr = Carbon::now()->subDays($i)->toDateString(); break; case "周": $dateStr = Carbon::now()->subWeeks($i)->toDateString(); break; case "月": $dateStr = Carbon::now()->subMonths($i)->toDateString(); break; default: break; } if ($dateStr) { $orderCountingRecord = factory(OrderCountingRecord::class)->create([ 'date_target' => $dateStr, 'owner_id' => $ownerId, 'counting_unit' => $unit, ]); $key = 'order_counting_records_' . $dateStr . '_' . $ownerId . '_' . $unit; cache()->put($key, collect()->push($orderCountingRecord)); if (empty($this->orderCountingRecordIds[$unit])) $this->orderCountingRecordIds[$unit] = []; $this->orderCountingRecordIds[$unit][] = $orderCountingRecord->id; $dateStr = null; } } } } $resultDay = $this->newOrderCountingRecordService->getOrderCountingRecords($this->queryConditionDay); $resultWeek = $this->newOrderCountingRecordService->getOrderCountingRecords($this->queryConditionWeek); // $resultMonth = $this->newOrderCountingRecordService->getOrderCountingRecords($this->queryConditionMonth); foreach ($this->units as $unit) { if (!empty($this->orderCountingRecordIds[$unit])) { switch ($unit) { case "日": $this->assertEquals($this->orderCountingRecordIds[$unit], array_column($resultDay->sortBy('id')->toArray(), 'id')); break; case "周": $this->assertEquals($this->orderCountingRecordIds[$unit], array_column($resultWeek->sortBy('id')->toArray(), 'id')); break; case "月": // $this->assertEquals($this->orderCountingRecordIds[$unit], array_column($resultMonth->sortBy('id')->toArray(), 'id')); break; } } } } /** * @test */ public function get_all_from_middle_day() { for ($i = 0; $i <= $this->step_length; $i++) { foreach ($this->ownerIds as $ownerId) { $unit = '日'; $dateStr = Carbon::now()->subDays($i)->toDateString(); $orderCountingRecord = factory(OrderCountingRecord::class)->create([ 'date_target' => $dateStr, 'owner_id' => $ownerId, 'counting_unit' => $unit, ]); $this->orderCountingRecordIds[] = $orderCountingRecord->id; } } $result = $this->newOrderCountingRecordService->getOrderCountingRecords($this->queryConditionDay); $this->assertEquals($this->orderCountingRecordIds, array_column($result->sortBy('id')->toArray(), 'id')); } /** * @test */ public function get_all_from_orders_day() { for ($i = 0; $i <= $this->step_length; $i++) { foreach ($this->ownerIds as $ownerId) { $dateStr = Carbon::now()->subDays($i)->toDateTimeString(); $order = factory(Order::class)->create([ 'created_at' => $dateStr, 'owner_id' => $ownerId, 'wms_status' => '订单完成' ]); $this->orderIds = $order->id; } } $result = $this->newOrderCountingRecordService->getOrderCountingRecords($this->queryConditionDay); $this->assertNotEmpty($result); } /** * @test */ public function get_all_from_orders_week() { for ($i = 0; $i <= $this->step_length; $i++) { foreach ($this->ownerIds as $ownerId) { $dateStr = Carbon::now()->subWeeks($i)->toDateTimeString(); $order = factory(Order::class)->create([ 'created_at' => $dateStr, 'owner_id' => $ownerId, 'wms_status' => '订单完成' ]); $this->orderIds = $order->id; } } $result = $this->newOrderCountingRecordService->getOrderCountingRecords($this->queryConditionWeek); dump($result->toArray()); $this->assertEquals([1,1,1,1],array_column($result->toArray(),'amount')); } /** * @test */ public function get_all_from_orders_Month() { for ($i = 0; $i <= $this->step_length; $i++) { foreach ($this->ownerIds as $ownerId) { $dateStr = Carbon::now()->subMonths($i)->toDateTimeString(); $order = factory(Order::class)->create([ 'created_at' => $dateStr, 'owner_id' => $ownerId, 'wms_status' => '订单完成' ]); $this->orderIds = $order->id; } } $result = $this->newOrderCountingRecordService->getOrderCountingRecords($this->queryConditionMonth); dd($result->toArray()); $this->assertEquals([1,1,1,1],array_column($result->toArray(),'amount')); } }