CreateByBatchesTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace Tests\Services\StationTaskMaterialBoxService;
  3. use App\Batch;
  4. use App\MaterialBox;
  5. use App\Order;
  6. use App\OrderCommodity;
  7. use App\Services\StationTaskMaterialBoxService;
  8. use App\StationTask;
  9. use Tests\TestCase;
  10. use App\StationTaskMaterialBox;
  11. use App\Traits\TestMockSubServices;
  12. class CreateByBatchesTest extends TestCase
  13. {
  14. use TestMockSubServices;
  15. /** @var StationTaskMaterialBoxService $service */
  16. public $service;
  17. private $data;
  18. private $batchAmount=2;
  19. private $orderAmount=4;
  20. private $materialBoxAmount=8;
  21. function setUp(): void
  22. {
  23. parent::setUp();
  24. $this->service = app('StationTaskMaterialBoxService');
  25. $this->data['batches']
  26. = factory(Batch::class, $this->batchAmount)
  27. ->create();
  28. $this->data['orders']
  29. = factory(Order::class)
  30. ->createMany($this->makeArray($this->orderAmount,[
  31. 'batch_id' => function(){return $this->getTargetFieldCirculately($this->data['batches']);}
  32. ]));
  33. $this->data['materialBoxes']
  34. = factory(MaterialBox::class, $this->materialBoxAmount)
  35. ->create();
  36. $this->data['orderCommodities']
  37. = factory(OrderCommodity::class)
  38. ->createMany($this->makeArray($this->materialBoxAmount,[
  39. 'order_id' => function(){return $this->getTargetFieldCirculately($this->data['orders']);},
  40. 'location' => function(){return $this->getTargetFieldCirculately($this->data['materialBoxes'],'','code');},
  41. ]));
  42. $this->data['stationTask']
  43. = factory(StationTask::class, $this->batchAmount)
  44. ->create();
  45. }
  46. public function testReturned()
  47. {
  48. $this->data['stationTaskMaterialBoxes']=$this->service->createByBatches($this->data['batches'],$this->data['stationTask'] );
  49. $this->assertTrue($this->data['stationTaskMaterialBoxes']->isNotEmpty());
  50. $this->assertEquals(StationTaskMaterialBox::class, get_class($this->data['stationTaskMaterialBoxes']->first()));
  51. $this->assertEquals($this->materialBoxAmount, $this->data['stationTaskMaterialBoxes']->count());
  52. $this->assertEquals($this->materialBoxAmount, (function(){
  53. $taskBoxes=collect();
  54. foreach ($this->data['stationTask'] as $stationTask){
  55. $stationTask->loadMissing('stationTaskMaterialBoxes');
  56. $taskBoxes=$taskBoxes->merge($stationTask['stationTaskMaterialBoxes']);
  57. }
  58. return $taskBoxes->count();
  59. })());
  60. }
  61. function tearDown(): void
  62. {
  63. StationTaskMaterialBox::query()
  64. ->whereIn('id',data_get($this->data['batches'],'*.id')??[])
  65. ->delete();
  66. Order::query()
  67. ->whereIn('id',data_get($this->data['orders'],'*.id')??[])
  68. ->delete();
  69. MaterialBox::query()
  70. ->whereIn('id',data_get($this->data['materialBoxes'],'*.id')??[])
  71. ->delete();
  72. OrderCommodity::query()
  73. ->whereIn('id',data_get($this->data['orderCommodities'],'*.id')??[])
  74. ->delete();
  75. StationTask::query()
  76. ->whereIn('id',data_get($this->data['stationTask'],'*.id')??[])
  77. ->delete();
  78. Batch::query()
  79. ->whereIn('id',data_get( $this->data['batches'],'*.id')??[])
  80. ->delete();
  81. parent::tearDown();
  82. }
  83. }