SyncOrderCommodityTest.php 3.6 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 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. // use RefreshDatabase;
  18. /** @var OrderCommodityService $service */
  19. private $service;
  20. private $data = [];
  21. public function setUp(): void
  22. {
  23. parent::setUp(); // TODO: Change the autogenerated stub
  24. $this->service = app('OrderCommodityService');
  25. $owners = factory(Owner::class)->times(5)->create();
  26. $this->data['owners'] = $owners;
  27. $commodities = collect();
  28. foreach ($owners as $owner) {
  29. $commodities = $commodities->concat(factory(Commodity::class)->times(rand(5,20))->create(['owner_id'=>$owner->id]));
  30. }
  31. $this->data['commodities'] = $commodities;
  32. $orderHeaders = collect();$orders = collect();
  33. for ($i=0;$i<2000;$i++) {
  34. $owner = $owners->random();
  35. $commodity_list = $commodities->where('owner_id',$owner->id);
  36. $order = factory(Order::class)->create(['owner_id'=>$owner->id]);
  37. $orderHeader = factory(OracleDOCOrderHeader::class)->make(['orderno' => $order->code, 'customerid' => $owner->code]);
  38. $oracleActAllocationDetails = collect();
  39. for($count=1;$count<=4;$count++){
  40. $commodity = $commodity_list->random();
  41. $oracleActAllocationDetail = factory(OracleActAllocationDetails::class)->make([
  42. 'orderno'=>$orderHeader['orderno'],
  43. 'customerid' => $orderHeader['customerid'],
  44. 'sku' => $commodity->sku
  45. ]);
  46. $oracleActAllocationDetails->push($oracleActAllocationDetail);
  47. }
  48. $orderHeader->setRelation('actAllocationDetails',$oracleActAllocationDetails);
  49. $orderHeaders->push($orderHeader);
  50. $orders->push($order);
  51. }
  52. $this->data['orders'] = $orders;
  53. $this->data['orderHeaders'] = $orderHeaders;
  54. $this->mock(CommodityService::class,function($mock)use($commodities){
  55. $mock->shouldReceive('get_')->andReturn($commodities);
  56. });
  57. dump((string)Carbon::now());
  58. }
  59. /**
  60. * @test
  61. */
  62. public function syncOrderCommodity()
  63. {
  64. for ($i = 0 ;$i<4; $i++) {
  65. dump('syncOrderCommodity'.(string)Carbon::now());
  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. }