GetOwnerByCodesTest.php 3.5 KB

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