<?php


namespace Tests\Services\OwnerFeeDetailService;


use App\Logistic;
use App\Owner;
use App\OwnerFeeDetail;
use App\ProcessMethod;
use App\Services\OwnerFeeDetailService;
use App\Shop;
use App\Unit;
use App\UserOwnerGroup;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class OwnerFeeDetailServiceTest extends  TestCase
{
    /** @var OwnerFeeDetailService */
    public $service;
    public $data;

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

        $unit = factory(Unit::class)->create();
        $logistic = factory(Logistic::class)->create();
        $processMethod = factory(ProcessMethod::class)->create([
            "unit_id" => $unit->id,
        ]);
        $userOwnerGroup = factory(\App\UserOwnerGroup::class)->create();
        $owner = factory(\App\Owner::class)->create([
            "user_owner_group_id"=>$userOwnerGroup->id,
        ]);
        $shop = factory(Shop::class)->create([
            "owner_id" => $owner->id,
        ]);

        $this->data["userOwnerGroups"] = [$userOwnerGroup->toArray()];
        $this->data["owners"] = [$owner->toArray()];
        $this->data["shops"] = [$shop->toArray()];
        $this->data["units"] = [$unit->toArray()];
        $this->data["logistics"] = [$logistic->toArray()];
        $this->data["processMethods"] = [$processMethod->toArray()];
        $this->data["models"] = factory(OwnerFeeDetail::class,3)->create([
            "logistic_id"       => $logistic->id,      //物流ID
            "process_method_id" => $processMethod->id,//加工类型ID
            "owner_id" => $owner->id,
            "shop_id" => $shop->id
        ])->toArray();
    }

    /**
     * @group customer
     */
    public function testPaginate()
    {
        $models = $this->service->paginate(array("paginate"=>5));
        $this->assertGreaterThanOrEqual(3,count($models));
        $this->assertLessThanOrEqual(5,count($models));
    }
    /**
     * @group customer
     */
    public function testGetSql()
    {
        $ids = implode(",",array_column($this->data["models"],"id"));
        $sql = $this->service->getSql(["id"=>$ids]);
        $models = DB::select(DB::raw($sql));
        $this->assertCount(3,$models);
    }

    public function tearDown(): void
    {
        UserOwnerGroup::destroy(array_column($this->data["userOwnerGroups"],"id"));
        Owner::destroy(array_column($this->data["owners"],"id"));
        Shop::destroy(array_column($this->data["shops"],"id"));
        Unit::destroy(array_column($this->data["units"],"id"));
        Logistic::destroy(array_column($this->data["logistics"],"id"));
        ProcessMethod::destroy(array_column($this->data["processMethods"],"id"));
        OwnerFeeDetail::destroy(array_column($this->data["models"],"id"));
        parent::tearDown();
    }
}