<?php

namespace Tests\Services\OrderService;

use App\Services\OrderService;
use App\ValueStore;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Str;
use Tests\TestCase;

class SetOrderSyncAtTest extends TestCase
{
    /** @var OrderService $service */
    private $service;
    private $data = [];
    public function setUp(): void
    {
        parent::setUp(); // TODO: Change the autogenerated stub
        $this->service = app('OrderService');
        $this->data['key1']= Str::uuid();
        $this->data['key2']= Str::uuid();
        $this->data['data1'] = Carbon::now();
        $this->data['data2'] = Carbon::now();
    }

    /**
     * @test
     */
    public function testSetOrderSyncAt()
    {
        /** @var Carbon $time */


        ValueStore::query()->create(['name'=>$this->data['key1']]);

        $this->service->setOrderSyncAt($this->data['key1'],$this->data['data1'],true);
        $item = ValueStore::query()->where('name',$this->data['key1'])->first();
        $this->assertEquals((string)($this->data['data1']->subSeconds(1)),(string)$item->value);


        $data = Carbon::now();
        ValueStore::query()->create(['name'=>$this->data['key2']]);

        $this->service->setOrderSyncAt($this->data['key2'],$this->data['data2'],false);
        $item = ValueStore::query()->where('name',$this->data['key2'])->first();
        $this->assertEquals((string)$data,(string)$item->value);
    }

    public function tearDown(): void
    {
        ValueStore::query()->where('name',$this->data['key2'])->delete();
        ValueStore::query()->where('name',$this->data['key1'])->delete();
        parent::tearDown(); // TODO: Change the autogenerated stub
    }
}
