<?php

namespace Tests\Services\OrderService;

use App\OracleDOCOrderHeader;
use App\Services\OrderService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
use Tests\TestCase;

class PushOrderCreatedCacheTest extends TestCase
{
    /** @var OrderService $service */
    public $service;
    public $data = [];
    public function setUp(): void
    {
        parent::setUp(); // TODO: Change the autogenerated stub
        $this->service = app('OrderService');
        $arr = [Str::uuid(),Str::uuid(),Str::uuid(),Str::uuid(),Str::uuid()];
        $list = [];
        foreach ($arr as $item) {
            $orderHeader= new OracleDOCOrderHeader();
            $orderHeader->orderno = (string)$item;
            $list[] = $orderHeader;
        }
        $this->data['OrderHeader'] = $list;
        $this->data['renewal_list'] = 'renewal_list->test';
        $this->data['hasKey'] = 'renewal_has->test';
        $this->data['prefixKey'] = 'renewal->test';
    }
    /**
     * @test
     */
    public function pushOrderCreatedCache()
    {
        $renewal_list   = $this->data['renewal_list'];
        $hasKey         = $this->data['hasKey'];
        $prefixKey      = $this->data['prefixKey'];

        $this->service->pushOrderCache($this->data['OrderHeader'],$prefixKey,$hasKey,$renewal_list);
        try {
            Cache::get($hasKey);
            $this->assertNotNull(Cache::get($hasKey));
            foreach ($this->data['OrderHeader'] as $datum) {
                $this->assertNotEmpty(Cache::get($prefixKey.$datum->orderno));
            }
            $this->assertNotNull($this->data['OrderHeader']);
        } catch (\Exception $e) {
            $this->assertNotNull($e);
        }
    }

    public function tearDown(): void
    {

        foreach ($this->data['OrderHeader'] as $datum) {
            Cache::forget($this->data['prefixKey'].$datum->orderno);
        }
        Cache::forget($this->data['renewal_list']);
        Cache::forget($this->data['prefixKey']);
        parent::tearDown(); // TODO: Change the autogenerated stub
    }
}
