<?php

namespace Tests\Services\OrderPackageExceptionTypeCountingRecordService;

use App\OrderPackage;
use App\Services\OrderPackageExceptionTypeCountingRecordService;
use Tests\TestCase;
use App\OrderPackageExceptionTypeCountingRecord;
use App\Traits\TestMockSubServices;

class UpdateOrCreateByDateTest extends TestCase
{
    use TestMockSubServices;

    /** @var OrderPackageExceptionTypeCountingRecordService $service */
    public $service;
    private $data;
    private $amount = 2;

    function setUp(): void
    {
        parent::setUp();
        $this->service = app('OrderPackageExceptionTypeCountingRecordService');
        $this->data['orderPackageExceptionTypeCountingRecords']
            = factory(OrderPackageExceptionTypeCountingRecord::class, $this->amount)
            ->create();
        $this->data['OrderPackage'] = [];
    }

    public function testReturned()
    {
        $this->assertTrue(true);
    }

    function tearDown(): void
    {
        OrderPackageExceptionTypeCountingRecord::query()
            ->whereIn('id', data_get($this->data['orderPackageExceptionTypeCountingRecords'], '*.id') ?? [])
            ->delete();
        OrderPackage::query()
            ->whereIn('id', data_get($this->data['OrderPackage'], '*.id') ?? [])
            ->delete();
        OrderPackageExceptionTypeCountingRecord::query()->where('owner_id', 1008611)->delete();
        parent::tearDown();
    }

    /**
     * @test
     */
    public function add_test()
    {
        $old = OrderPackageExceptionTypeCountingRecord::query()->sum('exception_type_count');
        $orderPackages = factory(OrderPackage::class)->times(2)->create(['sent_at' => now(), 'owner_id' => 1008611]);
        $this->data['OrderPackage'] = array_merge($this->data['OrderPackage'], $orderPackages->toArray());
        $this->service->updateOrCreateByDate(now());
        $this->assertEquals($old + 2, OrderPackageExceptionTypeCountingRecord::query()->sum('exception_type_count'));
    }
}
