ClearTaskTest.php 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace Tests\Services\CacheShelfService;
  3. use App\Material;
  4. use App\MaterialBox;
  5. use App\Services\CacheShelfService;
  6. use App\Services\StationTaskChildService;
  7. use App\Services\StationTaskMaterialBoxService;
  8. use App\Services\StationTaskService;
  9. use App\Station;
  10. use App\StationTask;
  11. use App\StationTaskChildren;
  12. use App\StationTaskMaterialBox;
  13. use App\StationType;
  14. use Tests\TestCase;
  15. class ClearTaskTest extends TestCase
  16. {
  17. /** @var CacheShelfService $service */
  18. /** @var StationTaskService $stationTaskService */
  19. /** @var StationTaskMaterialBoxService $stationTaskMaterialBoxService */
  20. /** @var StationTaskChildService $stationTaskChildService */
  21. public $service;
  22. public $stationTaskService;
  23. public $stationTaskMaterialBoxService;
  24. public $stationTaskChildService;
  25. public $data =[];
  26. protected function setUp(): void
  27. {
  28. parent::setUp(); // TODO: Change the autogenerated stub
  29. $this->service = app(CacheShelfService::class);
  30. $this->stationTaskService = app(StationTaskService::class);
  31. $this->stationTaskMaterialBoxService = app(StationTaskMaterialBoxService::class);
  32. $this->stationTaskChildService = app(StationTaskChildService::class);
  33. $stationType = StationType::query()->firstOrCreate(['name'=>'立库']);
  34. $this->data['LiKuStation'] = factory(Station::class)->create(['station_type_id'=>$stationType['id']]);
  35. $this->data['station'] = factory(Station::class)->create();
  36. $this->data['stationTask'] = [];
  37. $this->data['stationTaskMaterialBoxes'] = [];
  38. $this->data['materialBox'] = factory(MaterialBox::class)->create();
  39. $this->data['stationTask'] = $this->stationTaskService->create(1); // 生成站任务
  40. $this->data['stationTaskMaterialBoxes'][] = $this->stationTaskMaterialBoxService->createByStationAndMaterialBox($this->data['station'],$this->data['materialBox']); // 创建料箱任务
  41. $this->stationTaskService->registerStations($this->data['stationTask'],[$this->data['station']['id']]); // 注册站任务站
  42. $this->data['stationTaskMaterialBoxes'][0]['station_task_id'] = $this->data['stationTask'][0]['id'];
  43. $this->data['stationTaskMaterialBoxes'][0]->update();
  44. $params = [[
  45. 'station_task_id'=>$this->data['stationTask'][0]->first()['id'],
  46. 'station_taskable_type'=>StationTaskMaterialBox::class,
  47. 'station_taskable_id'=>$this->data['stationTaskMaterialBoxes'][0]['id']
  48. ]];
  49. $this->stationTaskChildService->insert($params); // 任务任务注册
  50. }
  51. public function testClearTask()
  52. {
  53. $result = $this->service->clearTask($this->data['station']->code);
  54. $stationTask = StationTask::query()->where('station_id',$this->data['station']['id'])->first();
  55. $this->assertEmpty($stationTask);
  56. }
  57. public function testClearTask_taskIsRunning()
  58. {
  59. StationTask::query()->where('station_id',$this->data['station']['id'])->update(['status' => '处理中']);
  60. $result = $this->service->clearTask($this->data['station']->code);
  61. $stationTask = StationTask::query()->where('station_id',$this->data['station']['id'])->first();
  62. $this->assertEquals('处理中',$stationTask->status);
  63. $this->assertFalse($result['success']);
  64. }
  65. protected function tearDown(): void
  66. {
  67. StationTask::query()->whereIn('id',data_get($this->data['stationTask'],'*.id'))->delete();
  68. StationTaskChildren::query()
  69. // ->where('station_task_id', data_get($this->data['stationTask'],'*.id'))
  70. ->where('station_taskable_type', 'App\StationTaskMaterialBox')
  71. ->whereIn('station_taskable_id',data_get($this->data['stationTaskMaterialBoxes'],'*.id'))->delete();
  72. Station::query()->where('id',$this->data['LiKuStation']['id'])->delete();
  73. Station::query()->where('id',$this->data['station']['id'])->delete();
  74. StationTaskMaterialBox::query()->where('material_box_id',$this->data['materialBox']['id'])->delete();
  75. if($this->data['materialBox'])MaterialBox::query()->where('id',$this->data['materialBox']['id'])->delete();
  76. parent::tearDown(); // TODO: Change the autogenerated stub
  77. }
  78. }