ProcessTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace Tests\Services\CacheShelfService;
  3. use App\MaterialBox;
  4. use App\Services\CacheShelfService;
  5. use App\Services\ForeignHaiRoboticsService;
  6. use App\Station;
  7. use App\StationTask;
  8. use App\StationTaskChild;
  9. use App\StationTaskChildren;
  10. use App\StationTaskMaterialBox;
  11. use App\StationType;
  12. use App\Traits\TestMockSubServices;
  13. use Illuminate\Filesystem\Cache;
  14. use Tests\TestCase;
  15. class ProcessTest extends TestCase
  16. {
  17. use TestMockSubServices;
  18. /** @var CacheShelfService $cacheShelfService */
  19. public $cacheShelfService;
  20. /** @var ForeignHaiRoboticsService $foreignHaiRoboticsService */
  21. public $foreignHaiRoboticsService;
  22. public $data = [];
  23. protected function setUp(): void
  24. {
  25. parent::setUp(); // TODO: Change the autogenerated stub
  26. \Illuminate\Support\Facades\Cache::forget('Station_typeName_'.'立库');
  27. $this->cacheShelfService = $this->subMock([
  28. 'class' => CacheShelfService::class,
  29. 'methods' => [
  30. '_stationCacheLightOn' => new MaterialBox(['code' => 200]),
  31. '_stationCacheBroadCast' => true
  32. ],
  33. 'subService' => [
  34. 'serviceName' => 'foreignHaiRoboticsService',
  35. 'class' => ForeignHaiRoboticsService::class,
  36. 'methods' => [
  37. 'controlHaiRobot' => true
  38. ]
  39. ]
  40. ]);
  41. $this->foreignHaiRoboticsService = app(ForeignHaiRoboticsService::class);
  42. $this->data['localCode'] = 'HAITEST-01-01';
  43. $this->data['boxCode'] = 'IDE0000198';
  44. $stationType = StationType::query()->firstOrCreate(['name' => '立库']);
  45. StationType::query()->firstOrCreate(['name' => '缓存架']);
  46. $this->data['station'] = Station::query()->firstOrCreate(['name' => '立库', 'station_type_id' => $stationType['id']]);
  47. }
  48. public function testProcess()
  49. {
  50. $result = $this->cacheShelfService->createStationTask($this->data['localCode'], $this->data['boxCode']);
  51. $this->assertTrue($result['success'], '当前站已有为完成的任务');
  52. $station = Station::query()->where('code', $this->data['localCode'])->first();
  53. $stationTask = StationTask::query()->where('station_id', $station['id'])->first();
  54. $this->assertEquals($stationTask['status'], '待处理');
  55. // 模拟海柔拍灯
  56. $this->cacheShelfService->lightOffTask($this->data['localCode'], 0);
  57. $stationTaskMaterialBoxes = $stationTask->stationTaskMaterialBoxes;
  58. $this->assertEquals($stationTaskMaterialBoxes->first() ? $stationTaskMaterialBoxes->first()['materialBox']['code'] : null, $this->data['boxCode']);
  59. /** @var Station $station */
  60. $station = Station::query()->with(['currentStationTask', 'pendingStationTask'])->where('code', $this->data['localCode'])->first();
  61. $pendingStationTask = $station->pendingStationTask;
  62. $this->assertTrue($pendingStationTask ? true : false);
  63. $this->assertEquals('待处理', $pendingStationTask->status ?? '');
  64. $materialBox = MaterialBox::query()->where('code', $this->data['boxCode'])->first();
  65. $this->assertNotEmpty($materialBox);
  66. $putStationTaskMaterialBox = StationTaskMaterialBox::query()->where('station_id', $this->data['station']['id'])->where('material_box_id', $materialBox['id'])->first();
  67. $this->assertTrue($putStationTaskMaterialBox ? true : false);
  68. // 模拟海柔机器人放箱完成
  69. $result = $this->foreignHaiRoboticsService->taskUpdate($putStationTaskMaterialBox['id'], 1, 0, $this->data['boxCode']);
  70. $this->assertEquals(true, $result);
  71. $station = Station::query()->where('code', $this->data['localCode'])->first();
  72. $this->assertTrue($station ? true : false);
  73. // 料箱任务
  74. $putStationTaskMaterialBox->refresh();
  75. $this->assertEquals('完成', $putStationTaskMaterialBox->status);
  76. $taskStationTaskMaterialBox = StationTaskMaterialBox::query()->where('station_id', $station['id'])->first();
  77. $this->assertEquals('完成', $taskStationTaskMaterialBox->status);
  78. // 栈任务
  79. $this->assertEquals('完成', $taskStationTaskMaterialBox->stationTask->status);
  80. $this->assertEquals('完成', $putStationTaskMaterialBox->stationTask->status);
  81. }
  82. protected function tearDown(): void
  83. {
  84. \Illuminate\Support\Facades\Cache::forget('Station_typeName_'.'立库');
  85. $station = Station::query()->with('parent')->where('code',$this->data['localCode'])->first();
  86. $materialBox = MaterialBox::query()->where('code',$this->data['boxCode'])->first();
  87. if($materialBox){
  88. $StationTaskMaterialBoxes = StationTaskMaterialBox::query()->where('material_box_id',$materialBox['id'])->get();
  89. foreach ($StationTaskMaterialBoxes as $stationTaskMaterialBox) {
  90. if($stationTaskMaterialBox->stationTask) {
  91. StationTaskChildren::query()->where('station_task_id',$stationTaskMaterialBox->stationTask['id'])->delete();
  92. $stationTaskMaterialBox->stationTask ? $stationTaskMaterialBox->stationTask->delete() : null;
  93. }
  94. $stationTaskMaterialBox->delete();
  95. }
  96. }
  97. if($station){
  98. $station->parent->delete();
  99. $station->delete();
  100. }
  101. $materialBox->delete();
  102. Station::query()->where('name','立库')->delete();
  103. parent::tearDown(); // TODO: Change the autogenerated stub
  104. }
  105. }