newOrderCountingRecordService = new NewOrderCountingRecordService(); // $this->actingAs(factory(User::class)->create(['name' => 'yang'])); $user = new User([ 'name'=>'yang' ]); $this->be($user); $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 cache_data_empty() { $result = $this->newOrderCountingRecordService->getFromCache($this->queryConditionDay); $this->assertEquals([ 0 => collect(), ['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 cache_data_all() { foreach ($this->ownerIds as $owner_id) { for ( $i = 1; $i >= 0; $i--) { $key = 'order_counting_records_' . Carbon::now()->subDays($i)->toDateString() . '_' . $owner_id . '_' . '日'; cache()->put($key, collect(['aaa'])); } } $result = $this->newOrderCountingRecordService->getFromCache($this->queryConditionDay); $this->assertEquals([ 0 => collect(['aaa','aaa','aaa','aaa']), ['unit' => '日']], $result); } /** * 缓存命中部分数据 * @test */ public function cache_data_some() { foreach ($this->ownerIds as $owner_id) { for ( $i = 0; $i >= 0; $i--) { $key = 'order_counting_records_' . Carbon::now()->subDays($i)->toDateString() . '_' . $owner_id . '_' . '日'; cache()->put($key, collect(['aaa']));//缓存中放入当天的数据 } } $result = $this->newOrderCountingRecordService->getFromCache($this->queryConditionDay); $this->assertEquals([ 0 => collect(['aaa','aaa']), ['unit' => '日', 'data' => [ Carbon::now()->subDays(1)->toDateString() => [0 => $this->ownerIds[0], 1 => $this->ownerIds[1]],//昨天的数据没放在缓存,应该在data中返回 ]]], $result); } }