AssignTasksTest.php 4.4 KB

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