SyncOrderCommodityTest.php 3.5 KB

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