AssignTasksTest.php 4.3 KB

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