SyncOrderCommodityTest.php 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. }
  58. /**
  59. * @test
  60. */
  61. public function syncOrderCommodity()
  62. {
  63. for ($i = 0 ;$i<4; $i++) {
  64. $this->service->syncOrderCommodity($this->data['orderHeaders']);
  65. }
  66. $counts = OrderCommodity::query()->whereIn('order_id',data_get($this->data['orders'],'*.id'))->get()->count();
  67. $actAllocationDetailsCounts = 0;
  68. foreach ($this->data['orderHeaders'] as $orderHeader) {
  69. $actAllocationDetails = $orderHeader->actAllocationDetails;
  70. $actAllocationDetailsCounts += $actAllocationDetails->count();
  71. }
  72. $this->assertEquals($counts,$actAllocationDetailsCounts);
  73. }
  74. public function tearDown(): void
  75. {
  76. cache()->flush();
  77. Order::query()->whereIn('id',data_get($this->data['orders'],'*.id'))->delete();
  78. Owner::query()->whereIn('id',data_get($this->data['owners'],'*.id'))->delete();
  79. Commodity::query()->whereIn('id',data_get($this->data['commodities'],'*.id'))->delete();
  80. parent::tearDown(); // TODO: Change the autogenerated stub
  81. }
  82. }