<?php

namespace Tests\Feature;

use App\Events\AddOrUpdateOrderIssues;
use App\Order;
use App\OrderIssue;
use App\OrderIssueType;
use App\OrderPackage;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class AddOrUpdateOrderIssuesListenerTest extends TestCase
{
    protected $order;
    protected $orderPackage;
    protected $orderIssueType;
    protected $orderIssue;

    protected function setUp(): void
    {
        parent::setUp(); // TODO: Change the autogenerated stub
        OrderIssueType::query()->truncate();
    }


    protected function tearDown(): void
    {
        try {
            $this->order->delete();
            $this->orderPackage->delete();
            $this->orderIssueType->delete();
            $this->orderIssue->delete();
        } catch (\Exception $e) {
        }
        parent::tearDown(); // TODO: Change the autogenerated stub
    }


    /**
     * @test
     */
    public function update_type_test()
    {
        $this->order = $order = factory(Order::class)->create();
        $this->orderPackage = $orderPackage = factory(OrderPackage::class)->create(['order_id' => $order->id]);
        $this->orderIssueType = $orderIssueType = factory(OrderIssueType::class)->create(['name' => '破损']);
        $this->orderIssue = $orderIssue = factory(OrderIssue::class)->create(['order_id' => $order->id, 'order_issue_type_id' => $orderIssueType->id]);
        event(new AddOrUpdateOrderIssues([$orderPackage->order_id]));
        $this->assertEquals('其他', $orderPackage->refresh()->exception_type);
    }

    /**
     * @test
     */
    public function not_update_type_test()
    {
        $this->order = $order = factory(Order::class)->create();
        $this->orderPackage = $orderPackage = factory(OrderPackage::class)->create(['order_id' => $order->id]);
        $this->orderIssueType = $orderIssueType = factory(OrderIssueType::class)->create(['name' => '拦截']);
        $this->orderIssue = $orderIssue = factory(OrderIssue::class)->create(['order_id' => $order->id, 'order_issue_type_id' => $orderIssueType->id]);
        event(new AddOrUpdateOrderIssues([$orderPackage->order_id]));
        $this->assertEquals('无', $orderPackage->refresh()->exception_type);
    }
}
