<?php

namespace Tests\Services\StoreService;

use App\OracleDOCASNHeader;
use App\Services\StoreService;
use Illuminate\Support\Facades\Cache;
use Tests\TestCase;

class SetLastRecordsByRedisTest extends TestCase
{
    /** @var StoreService $service */
    public $service;
    public $data;
    public $create_set;
    public $create_keys;
    public $create_key;
    public $update_set;
    public $update_keys;
    public $update_key;
    public $createRecord;
    public $updateRecord;
    public function setUp(): void
    {
        parent::setUp(); // TODO: Change the autogenerated stub
        $this->service = app(StoreService::class);
        $this->create_set = config('sync.asn_sync.cache_prefix.create_set');
        $this->create_keys = config('sync.asn_sync.cache_prefix.create_keys');
        $this->create_key = config('sync.asn_sync.cache_prefix.create');
        $this->update_set = config('sync.asn_sync.cache_prefix.update_set');
        $this->update_keys = config('sync.asn_sync.cache_prefix.update_keys');
        $this->update_key = config('sync.asn_sync.cache_prefix.update');
    }
    /**
     * @test
     */
    public function testSetLastRecordsByRedisCreated()
    {
        $this->createRecord=collect();
        $last_record=OracleDOCASNHeader::query()->orderByDesc('addtime')->first();
        $this->createRecord->add($last_record);
        if (!Cache::get($this->create_set)){
            $this->assertNull(Cache::get($this->create_set));
            $this->assertNotNull($this->createRecord);
            return;
        }
        $this->service->setLastRecordsByRedis($this->create_key,$this->create_set,$this->create_keys,$this->createRecord);
        $this->assertNotNull($this->createRecord);
        $this->assertEquals(true,Cache::get($this->create_set));
        $this->assertNotNull(Cache::get($this->create_keys));
    }

    /**
     * @test
     */
    public function testSetLastRecordsByRedisUpdated()
    {
        $this->updateRecord=collect();
        $last_record=OracleDOCASNHeader::query()->orderByDesc('edittime')->first();
        $this->updateRecord->add($last_record);
        if (!Cache::get($this->update_set)){
            $this->assertNull(Cache::get($this->update_set));
            $this->assertNotNull($this->updateRecord);
            return;
        }
        $this->service->setLastRecordsByRedis($this->update_key,$this->update_set,$this->update_keys,$this->updateRecord);
        $this->assertNotNull($this->updateRecord);
        $this->assertEquals(true,Cache::get($this->update_set));
        $this->assertNotNull(Cache::get($this->update_keys));
    }

    public function tearDown(): void
    {
        if (!Cache::get($this->create_set)) return;
        $cacheKeys = Cache::get($this->create_keys);
        if (!$cacheKeys) return;
        foreach ($cacheKeys as $cacheKey) {
            Cache::forget($cacheKey);
        }
        Cache::forget($this->create_keys);

        if (!Cache::get($this->update_set)) return;
        $cacheKeys = Cache::get($this->update_keys);
        if (!$cacheKeys) return;
        foreach ($cacheKeys as $cacheKey) {
            Cache::forget($cacheKey);
        }
        Cache::forget($this->update_keys);
        parent::tearDown(); // TODO: Change the autogenerated stub
    }
}
