CreateByBatchesTest.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace Tests\Services\StationTaskBatchService;
  3. use App\Batch;
  4. use App\Owner;
  5. use App\Services\StationTaskBatchService;
  6. use App\Services\StationTaskService;
  7. use App\StationRuleBatch;
  8. use App\StationTask;
  9. use App\StationTaskBatch;
  10. use App\StationTaskChild;
  11. use App\StationType;
  12. use Tests\TestCase;
  13. class CreateByBatchesTest extends TestCase
  14. {
  15. /** @var StationTaskBatchService $service */
  16. private $service;
  17. /** @var StationTaskService $service */
  18. private $stationTaskService;
  19. private $data = [];
  20. const AmountOfBatch=3;
  21. public function setUp(): void
  22. {
  23. parent::setUp();
  24. $this->service = app('StationTaskBatchService');
  25. $this->stationTaskService = app('StationTaskService');
  26. $this->data['owner'] =
  27. factory(Owner::class)
  28. ->create();
  29. $this->data['batches'] =
  30. factory(Batch::class,
  31. self::AmountOfBatch)
  32. ->create([
  33. 'status'=>'未处理',
  34. 'owner_id'=>$this->data['owner']['id']
  35. ]);
  36. $this->data['stationRuleBatch'] =
  37. factory(StationRuleBatch::class)
  38. -> create([
  39. 'owner_id'=>$this->data['owner']['id'],
  40. ]);
  41. $this->data['stationTask'] =
  42. $this->stationTaskService->create(self::AmountOfBatch);
  43. }
  44. public function testReturned()
  45. {
  46. ($isReturningRightAmount
  47. =function (){
  48. $this->data['stationTaskBatches']
  49. =$this->service->createByBatches($this->data['batches'], $this->data['stationTask']);
  50. $this->assertEquals(self::AmountOfBatch, $this->data['stationTaskBatches']->count());
  51. })();
  52. ($isBindingRightRootTasks
  53. =function (){
  54. $this->assertEquals(
  55. data_get($this->data['stationTask'],'*.id'),
  56. $this->data['stationTaskBatches']->map(function($taskBatch){
  57. $taskBatch->loadMissing('stationTask');
  58. return $taskBatch['stationTask']['id'];
  59. })->toArray()
  60. );
  61. })();
  62. }
  63. public function tearDown(): void
  64. {
  65. Batch::query()->whereIn('id',data_get($this->data['batches'],'*.id')??[])->delete();
  66. StationRuleBatch::query()->where('owner_id',$this->data['owner']['id']??'')->delete();
  67. StationTask::query()->whereIn('id',data_get($this->data['stationTask'],'*.id')??[])->delete();
  68. StationTaskBatch::query()->whereIn('id',data_get($this->data['stationTaskBatches'],'*.id')??[])->delete();
  69. StationTaskChild::query()->whereIn('station_task_id',data_get($this->data['stationTask'],'*.id')??[])->delete();
  70. Owner::query()->where('id',$this->data['owner']['id']??'')->delete();
  71. parent::tearDown();
  72. }
  73. }