GetOwnerByCodesTest.php 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace Tests\Services\OwnerService;
  3. use App\OracleBasCustomer;
  4. use App\Owner;
  5. use App\Services\OwnerService;
  6. use Illuminate\Foundation\Testing\RefreshDatabase;
  7. use Illuminate\Foundation\Testing\WithFaker;
  8. use Tests\TestCase;
  9. class GetOwnerByCodesTest extends TestCase
  10. {
  11. /** @var OwnerService $service */
  12. private $service;
  13. private $data;
  14. public function setUp(): void
  15. {
  16. parent::setUp(); // TODO: Change the autogenerated stub
  17. $this->service = app('OwnerService');
  18. $owner1 = factory(Owner::class)->create(['code'=>'AABBCC1']);
  19. $owner2 = factory(Owner::class)->create(['code'=>'AABBCC2']);
  20. $owner3 = factory(Owner::class)->create(['code'=>'AABBCC3']);
  21. $this->data['owner'] = [$owner1,$owner2,$owner3];
  22. $this->service = app('OwnerService');
  23. $basCustomer1 = factory(OracleBasCustomer::class)->make(["customer_type"=>'OW',"customerid"=>'AABBCC1']);
  24. $basCustomer2 = factory(OracleBasCustomer::class)->make(["customer_type"=>'OW',"customerid"=>'AABBCC2']);
  25. $basCustomer3 = factory(OracleBasCustomer::class)->make(["customer_type"=>'OW',"customerid"=>'AABBCC3']);
  26. $basCustomers = collect([$basCustomer1,$basCustomer2,$basCustomer3]);
  27. $this->mock('OracleBasCustomerService',function ($mock)use($basCustomers,$owner1, $owner2, $owner3){
  28. $mock->shouldReceive('first')->with(["Customer_Type"=>'OW',"CustomerID"=>$owner1->code])->andReturn($basCustomers->where('customerid',$owner1->code)->first());
  29. $mock->shouldReceive('first')->with(["Customer_Type"=>'OW',"CustomerID"=>$owner2->code])->andReturn($basCustomers->where('customerid',$owner2->code)->first());
  30. $mock->shouldReceive('first')->with(["Customer_Type"=>'OW',"CustomerID"=>$owner3->code])->andReturn($basCustomers->where('customerid',$owner3->code)->first());
  31. });
  32. }
  33. /**
  34. * @test
  35. */
  36. public function getOwnerByCodesNormal()
  37. {
  38. $owners = $this->service->getOwnerByCodes(data_get($this->data['owner'],'*.code'));
  39. $this->assertNotNull($owners);
  40. $this->assertNotEquals(count($owners),0);
  41. $this->assertNotEquals(count($owners),1);
  42. $this->assertEquals(count($owners),count($this->data['owner']));
  43. foreach ($this->data['owner'] as $item) {
  44. $owner = $owners->where('code',$item['code'])->first();
  45. $this->assertEquals($owner->id,$item['id']);
  46. $this->assertEquals($owner->name,$item['name']);
  47. $this->assertEquals($owner->code,$item['code']);
  48. }
  49. }
  50. /**
  51. * @test
  52. */
  53. public function ownerIsNotExist()
  54. {
  55. foreach ($this->data['owner'] as $owner)
  56. $owner->delete();
  57. $owners = $this->service->getOwnerByCodes(data_get($this->data['owner'],'*.code'));
  58. $this->assertNotNull($owners);
  59. $this->assertNotEquals(count($owners),0);
  60. $this->assertNotEquals(count($owners),1);
  61. $this->assertEquals(count($owners),count($this->data['owner']));
  62. foreach ($this->data['owner'] as $item) {
  63. $owner = $owners->where('code',$item['code'])->first();
  64. $this->assertEquals($owner->code,$item['code']);
  65. }
  66. $this->data['owner'] = $owners;
  67. }
  68. public function tearDown(): void
  69. {
  70. foreach ($this->data['owner'] as $owner) {
  71. cache()->forget("getOwnerByCode_{$owner['code']}");
  72. $owner->delete();
  73. }
  74. parent::tearDown(); // TODO: Change the autogenerated stub
  75. }
  76. }