<?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;
    public $data;
    public function setUp(): void
    {
        parent::setUp(); // TODO: Change the autogenerated stub
        $this->service = app('OrderTrackingService');
        $this->data['orderTracking'] = factory(OrderTracking::class,10)->create();
    }

    public function testFillInOrderTracking()
    {
        $data = new Carbon();
        $orderTracking =  $this->data['orderTracking'];
        $this->service->fillInOrderTracking($orderTracking);
        try {
            $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($tracking->planning_sent_at, $item->signed_at);
                }
            });
        } catch (\Exception $e) {
            OrderTracking::query()->whereIn('id',data_get($orderTracking,'*.id'))->delete();
        }
    }
}
