<?php
//
//
//namespace Tests\Services\OwnerPriceOperationService;
//
//use App\Commodity;
//use App\Owner;
//use App\OwnerPriceOperation;
//use App\OwnerPriceOperationItem;
//use App\Services\FeatureService;
//use App\Services\OwnerPriceOperationService;
//use App\Unit;
//use Mockery\Mock;
//use Tests\TestCase;
//
//class OwnerPriceOperationServiceTest extends  TestCase
//{
//    /** @var OwnerPriceOperationService */
//    public $service;
//    public $data;
//
//    protected function setUp(): void
//    {
//        parent::setUp();
//        $this->service = app(OwnerPriceOperationService::class);
//
//        $this->data["units"] = [];
//        $pieces = Unit::query()->where("name","件")->first();
//        $box = Unit::query()->where("name","箱")->first();
//        $single = Unit::query()->where("name","单")->first();
//        if (!$pieces){
//            $pieces = factory(Unit::class)->create(["name"=>"件"]);
//            $this->data["units"][] = $pieces->toArray();
//        }
//        if (!$box){
//            $box = factory(Unit::class)->create(["name"=>"箱"]);
//            $this->data["units"][] = $box->toArray();
//        }
//        if (!$single){
//            $single = factory(Unit::class)->create(["name"=>"单"]);
//            $this->data["units"][] = $single->toArray();
//        }
//        $model1 = factory(OwnerPriceOperation::class)->create([
//            "operation_type"=>"入库",   //操作类型
//            "strategy"      =>"默认",         //策略
//        ]);
//        $model2 = factory(OwnerPriceOperation::class)->create([
//            "operation_type"=>"入库",   //操作类型
//            "strategy"      =>"特征",         //策略
//        ]);
//        $model3 = factory(OwnerPriceOperation::class)->create([
//            "operation_type"=>"出库",   //操作类型
//            "strategy"      =>"默认",         //策略
//        ]);
//        $model4 = factory(OwnerPriceOperation::class)->create([
//            "operation_type"=>"出库",   //操作类型
//            "strategy"      =>"特征",         //策略
//        ]);
//        $this->data["models"] = [$model1->toArray(),$model2->toArray(),$model3->toArray(),$model4->toArray()];
//
//        $item1 = factory(OwnerPriceOperationItem::class)->create([
//            "owner_price_operation_id"          => $model1->id,
//            "strategy"                          => "默认",
//            "amount"                            => 6,
//            "unit_id"                           => $box->id,
//            "unit_price"                        => 2.22,
//        ]);
//        $item2 = factory(OwnerPriceOperationItem::class)->create([
//            "owner_price_operation_id"          => $model3->id,
//            "strategy"                          => "特征",
//            "amount"                            => 6,
//            "unit_id"                           => $box->id,
//            "unit_price"                        => 3.22,
//        ]);
//        $item3 = factory(OwnerPriceOperationItem::class)->create([
//            "owner_price_operation_id"          => $model2->id,
//            "strategy"                          =>"默认",
//            "amount"                            => 6,
//            "unit_id"                           => $box->id,
//            "unit_price"                        => 4.22,
//        ]);
//        $item4 = factory(OwnerPriceOperationItem::class)->create([
//            "owner_price_operation_id"          => $model2->id,
//            "strategy"                          =>"特征",
//            "amount"                            => 6,
//            "unit_id"                           => $box->id,
//            "unit_price"                        => 5.1,
//        ]);
//        $item5 = factory(OwnerPriceOperationItem::class)->create([
//            "owner_price_operation_id"          => $model3->id,
//            "strategy"                          => "起步",
//            "amount"                            => 6,
//            "unit_id"                           => $box->id,
//            "unit_price"                        => 5.2,
//        ]);
//        $item6 = factory(OwnerPriceOperationItem::class)->create([
//            "owner_price_operation_id"          => $model3->id,
//            "strategy"                          =>"默认",
//            "amount"                            => 6,
//            "unit_id"                           => $box->id,
//            "unit_price"                        => 5.3,
//        ]);
//        $item7 = factory(OwnerPriceOperationItem::class)->create([
//            "owner_price_operation_id"          => $model3->id,
//            "strategy"                          =>"特征",
//            "amount"                            => 6,
//            "unit_id"                           => $box->id,
//            "unit_price"                        => 5.4,
//        ]);
//        $item8 = factory(OwnerPriceOperationItem::class)->create([
//            "owner_price_operation_id"          => $model4->id,
//            "strategy"                          => "起步",
//            "amount"                            => 6,
//            "unit_id"                           => $box->id,
//            "unit_price"                        => 5.5,
//        ]);
//        $item9 = factory(OwnerPriceOperationItem::class)->create([
//            "owner_price_operation_id"          => $model4->id,
//            "strategy"                          =>"默认",
//            "amount"                            => 6,
//            "unit_id"                           => $box->id,
//            "unit_price"                        => 5.6,
//        ]);
//        $item10 = factory(OwnerPriceOperationItem::class)->create([
//            "owner_price_operation_id"          => $model4->id,
//            "strategy"                          =>"特征",
//            "amount"                            => 6,
//            "unit_id"                           => $box->id,
//            "unit_price"                        => 5.7,
//        ]);
//        $this->data["item"] = [$item1->toArray(),
//            $item2->toArray(),
//            $item3->toArray(),
//            $item4->toArray(),
//            $item5->toArray(),
//            $item6->toArray(),
//            $item7->toArray(),
//            $item8->toArray(),
//            $item9->toArray(),
//            $item10->toArray()];
//        $owner = factory(Owner::class)->create([
//            "user_owner_group_id" => 1,
//        ]);
//        $model1->ownerPriceOperationOwners()->sync([$owner->id]);
//        $model2->ownerPriceOperationOwners()->sync([$owner->id]);
//        $model3->ownerPriceOperationOwners()->sync([$owner->id]);
//        $model4->ownerPriceOperationOwners()->sync([$owner->id]);
//        $this->data["owners"] = [$owner->toArray()];
//        $this->data["commodities"] = [];
//        $commodity = factory(Commodity::class)->create([
//            'sku' => md5(date('Ymd').\Illuminate\Support\Str::random(3)),
//            'owner_id' => $owner->id,
//            "pack_spec" => 3
//        ]);
//        $this->data["commodities"][] = $commodity->toArray();
//    }
//
//    /**
//     * @group customer
//     */
//    public function testMatching()
//    {
//        $this->partialMock(FeatureService::class,function ($mock){
//            /** @var Mock $mock */
//            $mock->shouldReceive("matchFeature")->andReturn(true);
//        });
//        $result = $this->service->matching(["packages"=>[["commodity"=>"测试","amount"=>55,"sku"=>$this->data["commodities"][0]["sku"]]]],[],$this->data["owners"][0]["id"],"入库");
//        $this->assertEquals(96.9,$result);
//        $result = $this->service->matching(["packages"=>[["commodity"=>"测试","amount"=>57,"sku"=>$this->data["commodities"][0]["sku"]]]],[],$this->data["owners"][0]["id"],"出库");
//        $this->assertEquals(79.60000000000001,$result);
//    }
//
//    public function tearDown(): void
//    {
//        Unit::destroy(array_column($this->data["units"],"id"));
//        OwnerPriceOperation::destroy(array_column($this->data["models"],"id"));
//        foreach ($this->data["models"] as $d){
//            $model = new OwnerPriceOperation();
//            $model->id = $d["id"];
//            $model->ownerPriceOperationOwners()->sync([]);
//        }
//        OwnerPriceOperationItem::destroy(array_column($this->data["item"],"id"));
//        Owner::destroy(array_column($this->data["owners"],"id"));
//        Commodity::destroy(array_column($this->data["commodities"],"id"));
//        parent::tearDown();
//    }
//}
