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->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 { Owner::destroy($this->ownerIds); OrderCountingRecord::destroy($this->orderCountingRecordIds); Order::destroy($this->orderIds); OrderCountingRecord::query()->whereIn('owner_id', $this->ownerIds)->delete(); parent::tearDown(); } /** * 中间表没有任何数据 * @test */ public function data_empty() { $result = $this->newOrderCountingRecordService->getFromMiddleTable($this->queryConditionDay); $this->assertEquals([ 0 => new Collection(), ['unit' => '日', 'data' => [ Carbon::now()->subDays(1)->toDateString() => [0 => $this->ownerIds[0], 1 => $this->ownerIds[1]], Carbon::now()->subDays(0)->toDateString() => [0 => $this->ownerIds[0], 1 => $this->ownerIds[1]], ]]], $result); } /** * 中间表命中全部数据 * @test */ public function data_all() { $orderCountingRecords = collect(); foreach ($this->ownerIds as $owner_id) { for ($i = 1; $i >= 0; $i--) { $orderCountingRecords->push(factory(OrderCountingRecord::class)->create([ 'date_target' => Carbon::now()->subDays($i)->toDateString(), 'owner_id' => $owner_id, ])); } } $this->orderCountingRecordIds = array_column($orderCountingRecords->toArray(), 'id'); $result = $this->newOrderCountingRecordService->getFromMiddleTable($this->queryConditionDay); $orderCountingRecords = OrderCountingRecord::query()->whereIn('owner_id', $this->ownerIds)->orderByDesc('owner_id')->get(); $this->assertEquals([ 0 => $orderCountingRecords, ['unit' => '日','data'=>[]]], $result); } /** * 中间表命中部分数据 * @test */ public function data_some() { $orderCountingRecords = collect(); foreach ($this->ownerIds as $owner_id) { for ($i = 0; $i >= 0; $i--) { $orderCountingRecords->push(factory(OrderCountingRecord::class)->create([ 'date_target' => Carbon::now()->subDays($i)->toDateString(), 'owner_id' => $owner_id, ])); } } $this->orderCountingRecordIds = array_column($orderCountingRecords->toArray(), 'id'); $result = $this->newOrderCountingRecordService->getFromMiddleTable($this->queryConditionDay); $this->assertEquals( ['unit' => '日','data' => [ Carbon::now()->subDays(1)->toDateString() => [0 => $this->ownerIds[0], 1 => $this->ownerIds[1]],//昨天的数据没放在缓存,应该在data中返回 ]], $result[1]); } }