| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- <?php
- namespace Tests\Services\NewOrderCountingRecordService;
- use App\Order;
- use App\OrderCountingRecord;
- use App\Owner;
- use App\Services\NewOrderCountingRecordService;
- use App\User;
- use Carbon\Carbon;
- use Tests\TestCase;
- class GetFromCacheTest extends TestCase
- {
- protected $newOrderCountingRecordService;
- protected $queryConditionDay;
- protected $queryConditionWeek;
- protected $queryConditionMonth;
- protected $queryConditionYear;
- protected $ownerIds;
- protected $cache_key = 'order_counting_records_';
- protected $step_length = 1;
- protected $orderCountingRecordIds = [];
- protected $units = ['日', '月', '年'];
- protected $orderIds;
- protected function setUp(): void
- {
- parent::setUp(); // TODO: Change the autogenerated stub
- $this->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);
- }
- }
|