CreateStationTask.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace Tests\Services\CacheShelfService;
  3. use App\MaterialBox;
  4. use App\Services\CacheShelfService;
  5. use App\Services\ForeignHaiRoboticsService;
  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\Traits\TestMockSubServices;
  14. use Tests\TestCase;
  15. class CreateStationTask extends TestCase
  16. {
  17. use TestMockSubServices;
  18. /** @var CacheShelfService $service */
  19. protected $service;
  20. /** @var StationTaskService $stationTaskService */
  21. protected $stationTaskService;
  22. /** @var StationTaskMaterialBoxService $stationTaskMaterialBoxService */
  23. protected $stationTaskMaterialBoxService;
  24. /** @var StationTaskChildService $stationTaskChildService */
  25. protected $stationTaskChildService;
  26. protected $data = [];
  27. /**
  28. * createStationTask
  29. * 注册站任务
  30. * 注册站料箱任务
  31. * 注册站任务子任务
  32. */
  33. protected function setUp(): void
  34. {
  35. parent::setUp();
  36. $this->stationTaskService = new StationTaskService();
  37. $this->stationTaskMaterialBoxService = new StationTaskMaterialBoxService();
  38. $this->stationTaskChildService = new StationTaskChildService();
  39. $this->service = $this->subMock([
  40. 'class' => CacheShelfService::class,
  41. 'methods' => [
  42. '_stationCacheLightOn' => new MaterialBox(['code' => 200])
  43. ],
  44. 'subService' => [[
  45. 'serviceName' => 'foreignHaiRoboticsService',
  46. 'class' => ForeignHaiRoboticsService::class,
  47. 'methods' => [
  48. 'controlHaiRobot' => true
  49. ]
  50. ], [
  51. 'serviceName' => 'stationTaskService',
  52. 'class' => StationTaskService::class
  53. ], [
  54. 'serviceName' => 'stationTaskMaterialBoxService',
  55. 'class' => StationTaskMaterialBoxService::class,
  56. ]
  57. ]
  58. ]);
  59. $this->stationTaskService = $this->subMock([
  60. 'class' => StationTaskService::class,
  61. ]);
  62. $this->data['parentStation'] = factory(Station::class)->create();
  63. $this->data['station'] = factory(Station::class)->create(['parent_id' => $this->data['parentStation']['id']]);
  64. $this->data['materialBox'] = factory(MaterialBox::class)->create();
  65. }
  66. public function testCreatStationTask()
  67. {
  68. $data = $this->service->createStationTask($this->data['station']['code'], $this->data['materialBox']['code']);
  69. $this->assertTrue($data['success']);
  70. $stationTask = StationTask::query()->where('station_id', $this->data['station']['id'])->with('stationTaskMaterialBoxes')->first();
  71. $stationTaskMaterialBox = StationTaskMaterialBox::query()->with('materialBox')->where('station_id', $this->data['station']['id'])->first();
  72. $this->assertEquals($stationTask['status'], '待处理');
  73. $this->assertEquals($stationTaskMaterialBox['materialBox']['code'], $this->data['materialBox']['code']);
  74. $station = Station::query()->with(['pendingStationTask.stationTaskMaterialBoxes'])->where('id', $this->data['station']['id'])->first();
  75. $this->assertEquals($station['pendingStationTask']['stationTaskMaterialBoxes']->first()['station_id'], $stationTask['station_id']);
  76. }
  77. protected function tearDown(): void
  78. {
  79. Station::query()->where('id', $this->data['station']['id'])->delete();
  80. Station::query()->where('id', $this->data['parentStation']['id'])->delete();
  81. StationTask::query()->where('station_id', $this->data['station']['id'])->delete();
  82. MaterialBox::query()->where('id', $this->data['materialBox']['id'])->delete();
  83. StationTaskChildren::query()
  84. ->where('station_taskable_type', 'App\StationTaskMaterialBox')
  85. ->where('station_taskable_id',StationTaskMaterialBox::query()->where('station_id', $this->data['station']['id'])->first()['id'])->delete();
  86. StationTaskMaterialBox::query()->where('station_id', $this->data['station']['id'])->delete();
  87. if ($this->data['stationTask'] ?? false) StationTask::query()->whereIn('id', data_get($this->data['stationTask'], '*.id'))->delete();
  88. parent::tearDown();
  89. }
  90. }