GetOwnerByCodesTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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();
  20. $owner2 = factory(Owner::class)->create();
  21. $owner3 = factory(Owner::class)->create();
  22. $this->data['owner'] = [$owner1,$owner2,$owner3];
  23. $this->service = app('OwnerService');
  24. $basCustomer1 = factory(OracleBasCustomer::class)->make(["customer_type"=>'OW',"customerid"=>$owner1->code]);
  25. $basCustomer2 = factory(OracleBasCustomer::class)->make(["customer_type"=>'OW',"customerid"=>$owner2->code]);
  26. $basCustomer3 = factory(OracleBasCustomer::class)->make(["customer_type"=>'OW',"customerid"=>$owner3->code]);
  27. $basCustomers = collect([$basCustomer1,$basCustomer2,$basCustomer3]);
  28. $this->mock('OracleBasCustomerService',function ($mock)use($basCustomers,$owner1, $owner2, $owner3){
  29. $mock->shouldReceive('first')->once()->with(["customer_type"=>'OW',"customerid"=>$owner1->code])->andReturn($basCustomers->where('customerid',$owner1->code)->first());
  30. $mock->shouldReceive('first')->once()->with(["customer_type"=>'OW',"customerid"=>$owner2->code])->andReturn($basCustomers->where('customerid',$owner2->code)->first());
  31. $mock->shouldReceive('first')->once()->with(["customer_type"=>'WO',"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']['code']));
  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']['code']));
  63. foreach ($this->data['owner'] as $item) {
  64. $owner = $owners->where('code',$item['code'])->first();
  65. $this->assertEquals($owner->id,$item['id']);
  66. $this->assertEquals($owner->name,$item['name']);
  67. $this->assertEquals($owner->code,$item['code']);
  68. }
  69. }
  70. public function tearDown(): void
  71. {
  72. cache()->flush();
  73. foreach ($this->data['owner'] as $owner) {
  74. $owner->delete();
  75. }
  76. parent::tearDown(); // TODO: Change the autogenerated stub
  77. }
  78. }