<?php

namespace Tests\Services\OrderPackageExceptionTypeCountingRecordService;

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

class UpdateOrCreateTest 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['OrderPackage'] = [];
//        $this->data['OrderPackageExceptionTypeCountingRecord']
//            = factory(OrderPackageExceptionTypeCountingRecord::class, $this->amount)
//            ->create();
    }

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

    function tearDown(): void
    {
//        OrderPackageExceptionTypeCountingRecord::query()
//            ->whereIn('id',data_get($this->data['OrderPackageExceptionTypeCountingRecordService'],'*.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 init_test()
    {
        $old = OrderPackageExceptionTypeCountingRecord::query()->sum('exception_type_count');
        for ($i = 10; $i > 0; $i--) {
            $sent_at = now()->startOfDay()->subDays($i);
            $orderPackages = factory(OrderPackage::class)->times(2)->create(['sent_at' => $sent_at,'owner_id' => 1008611]);
            $this->data['OrderPackage'] = array_merge($this->data['OrderPackage'], $orderPackages->toArray());
        }
        $this->service->updateOrCreate(11);
        $this->assertEquals($old + 20, OrderPackageExceptionTypeCountingRecord::query()->sum('exception_type_count'));
    }
}
