<?php

namespace Tests\Services\StationTaskCommodityService;
use App\Batch;
use App\MaterialBox;
use App\Order;
use App\OrderBin;
use App\OrderCommodity;
use App\Services\MaterialBoxService;
use App\Services\StationRuleBatchService;
use App\Services\StationTaskCommodityService;
use App\Services\StationTypeService;
use Tests\TestCase;
use App\StationTaskCommodity;
use App\Traits\TestMockSubServices;

class CreateByBatchTest extends TestCase
{
    use TestMockSubServices;
    /** @var StationTaskCommodityService $service */
    public $service;
    private $data;
    private $batchAmount=1;
    private $orderAmount=4;
    private $orderCommodityAmount=8;
    function setUp(): void
    {
        parent::setUp();
        $this->service = app('StationTaskCommodityService');
        $this->data['batches'] =
            factory(Batch::class,
                $this->batchAmount)
                ->create([
                    'status'=>'未处理',
                ]);
        $this->data['orders'] =
            factory(Order::class)
                ->createMany($this->makeArray($this->orderAmount,[
                    'status'=>'未处理',
                    'batch_id' => function(){return $this->getTargetFieldCirculately($this->data['batches']);}
                ]));
        $this->data['orderBins'] =
            factory(OrderBin::class)
                ->createMany($this->makeArray($this->orderAmount,[
                    'order_id' => function(){return $this->getTargetFieldCirculately($this->data['orders']);},
                    'number'=>$this->getIntegerIncreasely('orderBins_number'),
                ]));
        $this->data['orderCommodities'] =
            factory(OrderCommodity::class)
                ->createMany($this->makeArray($this->orderCommodityAmount,[
                    'order_id' => function(){return $this->getTargetFieldCirculately($this->data['orders']);},
                ]));
    }

    public function testReturned()
    {
        $this->service=$this->subMock([
            'class'=>StationTaskCommodityService::class,
            'subServices'=>[
                [
                    'serviceName'=>'materialBoxService',
                    'class'=>MaterialBoxService::class,
                    'methods'=>[
                        'firstOrCreate'=>new MaterialBox(['id'=>1]),
                    ],
                ],
            ]
        ]);

        $this->data['stationTaskCommodities']
            =(function(){
            if(!isset($this->data['stationTaskCommodities']))
                $this->data['stationTaskCommodities'] = collect();
            foreach($this->data['batches'] as $batch){
                $this->data['stationTaskCommodities']
                    =$this->data['stationTaskCommodities']->merge(
                    $this->service->createByBatch($batch)
                );
            }
            return $this->data['stationTaskCommodities'];
        })();
        $this->assertEquals($this->orderCommodityAmount, $this->data['stationTaskCommodities']->count());
    }

    function tearDown(): void
    {
        StationTaskCommodity::query()
            ->whereIn('id',data_get($this->data['stationTaskCommodities'],'*.id')??[])
            ->delete();
        Batch::query()
            ->whereIn('id',data_get($this->data['batches'],'*.id')??[])
            ->delete();
        Order::query()
            ->whereIn('id',data_get($this->data['orders'],'*.id')??[])
            ->delete();
        OrderBin::query()
            ->whereIn('id',data_get($this->data['orderBins'],'*.id')??[])
            ->delete();
        OrderCommodity::query()
            ->whereIn('id',data_get($this->data['orderCommodities'],'*.id')??[])
            ->delete();
        parent::tearDown();
    }
}
