<?php


namespace Tests\Services\CacheShelfService;

use App\MaterialBox;
use App\Services\CacheShelfService;
use App\Services\ForeignHaiRoboticsService;
use App\Services\StationCacheShelfGridService;
use App\Station;
use App\StationTask;
use App\StationTaskChildren;
use App\StationTaskMaterialBox;
use App\Traits\TestMockSubServices;
use Tests\TestCase;


class PutBinToStoreTest extends TestCase
{
    use TestMockSubServices;

    /** @var CacheShelfService $cacheShelfService */
    protected $cacheShelfService;
    protected $data = [];

    protected function setup(): void
    {
        parent::setup(); // todo: change the autogenerated stub
        $this->data['station'] = factory(Station::class)->create();
        $this->data['materialBox'] = factory(MaterialBox::class)->create();
        $this->data['stationTask'] = factory(StationTask::class)->create();
        $this->data['stationTask']['station_id'] = $this->data['station']['id'];
        $this->data['stationTask']->save();

        $this->data['stationTaskMaterialBox'] = factory(StationTaskMaterialBox::class)->create([
            'station_id' => $this->data['station']['id'],
            'material_box_id' => $this->data['materialBox']['id'],
            'status' => '待处理'
        ]);

        $this->data['stationTaskMaterialBox']['station_task_id'] = $this->data['stationTask']['id'];
        $this->data['stationTaskMaterialBox']->save();

        $this->data['stationTaskChildren']  = StationTaskChildren::query()->create([
            "station_task_id" => $this->data['stationTask']['id'],
        ]);
        $this->data['stationTaskChildren']["station_taskable_type"] = StationTaskMaterialBox::class;
        $this->data['stationTaskChildren']["station_taskable_id"]= $this->data['stationTaskMaterialBox']['id'];
        $this->data['stationTaskChildren']->save();

        $this->cacheShelfService = $this->subMock([
            'class' => CacheShelfService::class,
            'subService' => [
                [
                    'serviceName' => 'foreignHaiRoboticsService',
                    'class' => ForeignHaiRoboticsService::class,
                    'methods' => [
                        'controlHaiRobot' => true
                    ]
                ]
            ]
        ]);
    }

    public function testPutBinToStore()
    {
        $bool = $this->cacheShelfService->putBinToStore($this->data['station']);
        $boxTask = StationTaskMaterialBox::query()->where('material_box_id', $this->data['materialBox']['id'])->get();
        $this->assertTrue($bool);
        $this->assertNotEmpty($boxTask);
        $this->assertEquals(2,$boxTask->count());
    }

    protected function tearDown(): void
    {
        $materialBox = MaterialBox::query()->where('id', $this->data['materialBox']['id'])->first();
        if ($materialBox) {
            $stationTaskMaterialBoxes = StationTaskMaterialBox::query()->where('material_box_id', $materialBox['id'])->get();
            foreach ($stationTaskMaterialBoxes as $stationTaskMaterialBox) {
                if ($stationTaskMaterialBox->station) $stationTaskMaterialBox->station->delete();
                if ($stationTaskMaterialBox->stationTask) $stationTaskMaterialBox->stationTask->delete();
                $stationTaskMaterialBox->delete();
            }
            $materialBox->delete();
        }
        if($this->data['stationTaskChildren'])StationTaskChildren::query()->where('id',$this->data['stationTaskChildren']['id'])->delete();
        parent::tearDown();
    }

}
