<?php

namespace Tests\Services\OrderTrackingService;

use App\OrderTracking;
use App\Services\OrderTrackingService;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class FillInOrderTrackingTest extends TestCase
{

    /** @var OrderTrackingService $service */
    public $service;
    private $data = [];
    public function setUp(): void
    {
        parent::setUp(); // TODO: Change the autogenerated stub
        $this->service = app('OrderTrackingService');
    }

    /**
     * @test
     */
    public function fillInOrderTracking()
    {
        $data = new Carbon();
        $orderTracking =  factory(OrderTracking::class,10)->create();
        $this->service->fillInOrderTracking($orderTracking);
        $orderTracking_list = OrderTracking::query()->whereIn('id', data_get($orderTracking, '*.id'))->get();
        $orderTracking_list->each(function ($item) use ($data, $orderTracking) {
            $tracking = $orderTracking->where('id', $item->id)->first();
            if ($item->created_at != '0000-00-00 00:00:00' &&
                $item->planning_sent_at !== '0000-00-00 00:00:00' &&
                $item->planning_sent_at < $data) {
                $this->assertEquals(date_format($tracking->planning_sent_at,"Y-m-d H:i:s"), (string)$item->signed_at);
            }
        });
        $this->data['orderTrackings'] = $orderTracking;
    }

    public function tearDown(): void
    {
        OrderTracking::query()->where('id',data_get($this->data['orderTrackings'],'*.id'))->delete();
        parent::tearDown(); // TODO: Change the autogenerated stub
    }
}
