SyncOrderCommodityTest.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace Tests\Services\OrderCommodityService;
  3. use App\Commodity;
  4. use App\OracleActAllocationDetails;
  5. use App\OracleDOCOrderHeader;
  6. use App\Order;
  7. use App\OrderCommodity;
  8. use App\Owner;
  9. use App\Services\CommodityService;
  10. use App\Services\OrderCommodityService;
  11. use Carbon\Carbon;
  12. use Illuminate\Foundation\Testing\RefreshDatabase;
  13. use Illuminate\Foundation\Testing\WithFaker;
  14. use Tests\TestCase;
  15. class SyncOrderCommodityTest extends TestCase
  16. {
  17. /** @var OrderCommodityService $service */
  18. private $service;
  19. private $data = [];
  20. public function setUp(): void
  21. {
  22. parent::setUp(); // TODO: Change the autogenerated stub
  23. $this->service = app('OrderCommodityService');
  24. $owners = factory(Owner::class)->times(5)->create();
  25. $this->data['owners'] = $owners;
  26. $commodities = collect();
  27. foreach ($owners as $owner) {
  28. $commodities = $commodities->concat(factory(Commodity::class)->times(rand(5,20))->create(['owner_id'=>$owner->id]));
  29. }
  30. $this->data['commodities'] = $commodities;
  31. $orderHeaders = collect();$orders = collect();
  32. for ($i=0;$i<2000;$i++) {
  33. $owner = $owners->random();
  34. $commodity_list = $commodities->where('owner_id',$owner->id);
  35. $order = factory(Order::class)->create(['owner_id'=>$owner->id]);
  36. $orderHeader = factory(OracleDOCOrderHeader::class)->make(['orderno' => $order->code, 'customerid' => $owner->code]);
  37. $oracleActAllocationDetails = collect();
  38. for($count=1;$count<=4;$count++){
  39. $commodity = $commodity_list->random();
  40. $oracleActAllocationDetail = factory(OracleActAllocationDetails::class)->make([
  41. 'orderno'=>$orderHeader['orderno'],
  42. 'customerid' => $orderHeader['customerid'],
  43. 'sku' => $commodity->sku
  44. ]);
  45. $oracleActAllocationDetails->push($oracleActAllocationDetail);
  46. }
  47. $orderHeader->setRelation('actAllocationDetails',$oracleActAllocationDetails);
  48. $orderHeaders->push($orderHeader);
  49. $orders->push($order);
  50. }
  51. $this->data['orders'] = $orders;
  52. $this->data['orderHeaders'] = $orderHeaders;
  53. $this->mock(CommodityService::class,function($mock)use($commodities){
  54. $mock->shouldReceive('get_')->andReturn($commodities);
  55. });
  56. }
  57. /**
  58. * @test
  59. */
  60. public function syncOrderCommodity()
  61. {
  62. for ($i = 0 ;$i<4; $i++) {
  63. $this->service->syncOrderCommodity($this->data['orderHeaders']);
  64. }
  65. $counts = OrderCommodity::query()->whereIn('order_id',data_get($this->data['orders'],'*.id'))->get()->count();
  66. $actAllocationDetailsCounts = 0;
  67. foreach ($this->data['orderHeaders'] as $orderHeader) {
  68. $actAllocationDetails = $orderHeader->actAllocationDetails;
  69. $actAllocationDetailsCounts += $actAllocationDetails->count();
  70. }
  71. $this->assertEquals($counts,$actAllocationDetailsCounts);
  72. }
  73. public function tearDown(): void
  74. {
  75. cache()->flush();
  76. Order::query()->whereIn('id',data_get($this->data['orders'],'*.id'))->delete();
  77. Owner::query()->whereIn('id',data_get($this->data['owners'],'*.id'))->delete();
  78. Commodity::query()->whereIn('id',data_get($this->data['commodities'],'*.id'))->delete();
  79. parent::tearDown(); // TODO: Change the autogenerated stub
  80. }
  81. }