AssignTasksTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace Tests\Services\BatchService;
  3. use App\MaterialBox;
  4. use App\Order;
  5. use App\OrderCommodity;
  6. use App\Services\BatchService;
  7. use App\Services\ForeignHaiRoboticsService;
  8. use App\Services\MaterialBoxService;
  9. use App\Services\StationTaskBatchService;
  10. use App\Services\StationTaskCommodityService;
  11. use App\Station;
  12. use App\StationRuleBatch;
  13. use App\StationTask;
  14. use App\StationTaskBatch;
  15. use App\StationTaskCommodity;
  16. use Tests\TestCase;
  17. use App\Batch;
  18. use App\Traits\TestMockSubServices;
  19. class AssignTasksTest extends TestCase
  20. {
  21. use TestMockSubServices;
  22. /** @var BatchService $service */
  23. public $service;
  24. private $data;
  25. private $batchAmount=2;
  26. private $orderAmount=4;
  27. private $orderCommodityAmount=8;
  28. function setUp(): void
  29. {
  30. parent::setUp();
  31. $this->service=$this->subMock([
  32. 'class'=>BatchService::class,
  33. 'subServices'=>[
  34. [
  35. 'serviceName'=>'stationTaskBatchService',
  36. 'class'=>StationTaskBatchService::class,
  37. 'methods'=>[
  38. 'firstOrCreate'=>new MaterialBox(['id'=>1]),
  39. ],
  40. 'subServices'=>[
  41. [
  42. 'serviceName'=>'foreignHaiRoboticsService',
  43. 'class'=>ForeignHaiRoboticsService::class,
  44. 'methods'=>[
  45. 'fetchGroup'=>true,
  46. ],
  47. ],
  48. ]
  49. ],
  50. ]
  51. ]);
  52. $this->data['batches']
  53. = factory(Batch::class, $this->batchAmount)
  54. ->create();
  55. $this->data['orders'] =
  56. factory(Order::class)
  57. ->createMany($this->makeArray($this->orderAmount,[
  58. 'status'=>'未处理',
  59. 'batch_id' => function(){return $this->getTargetIdCirculately($this->data['batches']);}
  60. ]));
  61. $this->data['orderCommodities'] =
  62. factory(OrderCommodity::class)
  63. ->createMany($this->makeArray($this->orderCommodityAmount,[
  64. 'order_id' => function(){return $this->getTargetIdCirculately($this->data['orders']);}
  65. ]));
  66. $this->data['station_rule_batches']
  67. = factory(StationRuleBatch::class)
  68. ->createMany($this->makeArray($this->batchAmount,[
  69. 'owner_id' => function(){return $this->getTargetIdCirculately($this->data['batches'],'','owner_id');}
  70. ]));
  71. }
  72. public function testReturned()
  73. {
  74. $this->service->assignTasks($this->data['batches']);
  75. ($波次任务指向了波次=function(){
  76. $this->assertEquals(
  77. data_get($this->data['batches'],'*.id'),
  78. data_get($this->data['batches'],'*.stationBatchTask.batch_id')
  79. );
  80. })();
  81. }
  82. function tearDown(): void
  83. {
  84. Batch::query()
  85. ->whereIn('id',data_get($this->data['batches'],'*.id')??[])
  86. ->delete();
  87. StationTaskBatch::query()
  88. ->whereIn('id',data_get($this->data['batches'],'*.stationBatchTask.stationTask.id')??[])
  89. ->delete();
  90. $stationTasks=data_get($this->data['batches'],'*.stationBatchTask.stationTask');
  91. $stationTasks[0]->loadMissing('stationTaskCommodities');
  92. $a=$stationTasks[0]['stationTaskCommodities'];
  93. $b=data_get($this->data['batches'],'*.stationBatchTask.stationTask.stationTaskCommodities.id');
  94. StationTaskCommodity::query()
  95. ->whereIn('id',data_get($this->data['batches'],'*.stationBatchTask.stationTask.stationTaskCommodities.id')??[])
  96. ->delete();
  97. StationTask::query()
  98. ->whereIn('id',data_get($this->data['batches'],'*.stationBatchTask.stationTask.id')??[])
  99. ->delete();
  100. Order::query()
  101. ->whereIn('id',data_get($this->data['orders'],'*.id')??[])
  102. ->delete();
  103. OrderCommodity::query()
  104. ->whereIn('id',data_get($this->data['orderCommodities'],'*.id')??[])
  105. ->delete();
  106. StationRuleBatch::query()
  107. ->whereIn('id',data_get($this->data['station_rule_batches'],'*.id')??[])
  108. ->delete();
  109. parent::tearDown();
  110. }
  111. }