ClearTaskTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. $stationTaskMaterialBox = StationTaskMaterialBox::query()->where('material_box_id',$this->data['materialBox']['id'])->get();
  57. $this->assertEmpty($stationTaskMaterialBox);
  58. }
  59. public function testClearTask_taskIsRunning()
  60. {
  61. StationTask::query()->where('station_id',$this->data['station']['id'])->update(['status' => '处理中']);
  62. $result = $this->service->clearTask($this->data['station']->code);
  63. $stationTask = StationTask::query()->where('station_id',$this->data['station']['id'])->first();
  64. $this->assertEquals('处理中',$stationTask->status);
  65. $this->assertFalse($result['success']);
  66. }
  67. protected function tearDown(): void
  68. {
  69. if($this->data['station']){
  70. $stationTasks = StationTask::query()->where('station_id',$this->data['station']['id'])->get();
  71. foreach ($stationTasks as $stationTask) {
  72. $stationTaskMaterialBoxes = $stationTask->stationTaskMaterialBoxes;
  73. foreach ($stationTaskMaterialBoxes as $stationTaskMaterialBox) {
  74. StationTaskChildren::query()->where(['station_task_id' => $stationTask['id'],'station_taskable_type'=> StationTaskMaterialBox::class,'station_taskable_id'=>$stationTaskMaterialBox['id']])->delete();
  75. $stationTaskMaterialBox->delete();
  76. }
  77. }
  78. Station::query()->where('id',$this->data['station']['id'])->delete();
  79. }
  80. if($this->data['LiKuStation']){
  81. $stationTasks = StationTask::query()->where('station_id',$this->data['LiKuStation']['id'])->get();
  82. foreach ($stationTasks as $stationTask) {
  83. $stationTaskMaterialBoxes = $stationTask->stationTaskMaterialBoxes;
  84. foreach ($stationTaskMaterialBoxes as $stationTaskMaterialBox) {
  85. StationTaskChildren::query()->where(['station_task_id' => $stationTask['id'],'station_taskable_type'=> StationTaskMaterialBox::class,'station_taskable_id'=>$stationTaskMaterialBox['id']])->delete();
  86. $stationTaskMaterialBox->delete();
  87. }
  88. }
  89. Station::query()->where('id',$this->data['LiKuStation']['id'])->delete();
  90. }
  91. if($this->data['materialBox'])MaterialBox::query()->where('id',$this->data['materialBox'])->delete();
  92. parent::tearDown(); // TODO: Change the autogenerated stub
  93. }
  94. }