<?php

namespace Tests\Services\OrderPackageReceivedSyncService;

use App\OrderPackage;
use App\Services\OrderPackageReceivedSyncService;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class TestUpdate extends TestCase
{
    protected $order_package_ids;

    /** @var OrderPackageReceivedSyncService $service */
    public $service;

    function setUp(): void
    {
        parent::setUp();
        $this->service = app(OrderPackageReceivedSyncService::class);
    }

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

    function tearDown(): void
    {
        OrderPackage::destroy($this->order_package_ids);
        parent::tearDown();
    }

    /**
     * 测试更新
     * @test
     */
    public function update()
    {
        $orderPackage1 = factory(OrderPackage::class)->create(['logistic_number' => 'SF1038651915891', 'exception' => '否', 'received_at' => null,]);
        $orderPackage2 = factory(OrderPackage::class)->create(['logistic_number' => 'SF1038651413847', 'exception' => '否', 'received_at' => null,]);
        $orderPackage3 = factory(OrderPackage::class)->create(['logistic_number' => 'SF1038611050071', 'exception' => '否', 'received_at' => null,]);
        $this->order_package_ids [] = $orderPackage1->id;
        $this->order_package_ids [] = $orderPackage2->id;
        $this->order_package_ids [] = $orderPackage3->id;

        $data = [];
        $data[] = [
            'logistic_number' => 'SF1038651915891',
            'status' => '派送中',
            'received_at' => Carbon::now(),
            'exception' => '否',
            'transfer_status' => '快件正在派送',
        ];
        $data[] = [
            'logistic_number' => 'SF1038651413847',
            'status' => '派送中',
            'received_at' => Carbon::now(),
            'exception' => '否',
            'transfer_status' => '快件正在派送',
        ];
        $data[] = [
            'logistic_number' => 'SF1038611050071',
            'status' => '派送中',
            'received_at' => Carbon::now(),
            'exception' => '否',
            'transfer_status' => '快件正在派送',
        ];

        $this->service->update($data);
        $this->assertDatabaseHas('order_packages',[
            'logistic_number' => 'SF1038651915891',
            'status' => '派送中',
            'received_at' => Carbon::now()->toDateTimeString(),
            'exception' => '否',
        ]);
        $this->assertDatabaseHas('order_packages',[
            'logistic_number' => 'SF1038651413847',
            'status' => '派送中',
            'received_at' => Carbon::now()->toDateTimeString(),
            'exception' => '否',
        ]);
        $this->assertDatabaseHas('order_packages',[
            'logistic_number' => 'SF1038611050071',
            'status' => '派送中',
            'received_at' => Carbon::now()->toDateTimeString(),
            'exception' => '否',
        ]);
    }
}
