Parcourir la source

OwnerService 修改

ajun il y a 5 ans
Parent
commit
dfebcc4c82

+ 19 - 7
tests/Services/OwnerService/GetOwnerByCodeTest.php

@@ -21,10 +21,14 @@ class GetOwnerByCodeTest extends TestCase
     {
         parent::setUp(); // TODO: Change the autogenerated stub
         cache()->flush();
-        $basCustomer = factory(OracleBasCustomer::class)->make([]);
-        $this->data['owner'] = factory(Owner::class)->create();
-        $this->data['basCustomer'] = $basCustomer;
         $this->service = app('OwnerService');
+
+        $owner  = factory(Owner::class)->create();
+        $basCustomer = factory(OracleBasCustomer::class)->make(["customer_type"=>'OW',"customerid"=>$owner->code]);
+
+        $this->data['basCustomer'] = $basCustomer;
+        $this->data['owner'] = $owner;
+
         $this->mock('OracleBasCustomerService',function ($mock)use($basCustomer){
             $mock->shouldReceive('first')->andReturn($basCustomer);
         });
@@ -36,8 +40,11 @@ class GetOwnerByCodeTest extends TestCase
     public function getOwnerByCode()
     {
         $owner = $this->service->getOwnerByCode($this->data['owner']['code']);
+
         $this->assertEquals($owner->id,$this->data['owner']['id']);
         $this->assertEquals($owner->name,$this->data['owner']['name']);
+        $this->assertEquals($owner->code,$this->data['owner']['code']);
+
     }
 
     /**
@@ -46,16 +53,21 @@ class GetOwnerByCodeTest extends TestCase
     public function ownerIsNotExist()
     {
         $this->data['owner']->delete();
+        cache()->forget('getOwnerByCode_'.$this->data['owner']['code']);
+
         $owner = $this->service->getOwnerByCode($this->data['owner']['code']);
-        $this->assertEquals($owner->code,$this->data['basCustomer']['customerid']);
-        $this->assertEquals($owner->name,$this->data['basCustomer']['descr_c']);
-        $this->data['owner']= $owner;
 
+        $this->data['owner'] = $owner;
+        $this->assertEquals($owner->id,$this->data['owner']['id']);
+        $this->assertEquals($owner->code,$this->data['owner']['code']);
+        $this->assertEquals($owner->name,$this->data['owner']['name']);
+
+        $this->data['owner']= $owner;
     }
 
     public function tearDown(): void
     {
-        cache()->flush();
+        cache()->forget('getOwnerByCode_'.$this->data['owner']['code']);
         $this->data['owner']->delete();
         parent::tearDown(); // TODO: Change the autogenerated stub
     }

+ 53 - 14
tests/Services/OwnerService/GetOwnerByCodesTest.php

@@ -3,6 +3,7 @@
 namespace Tests\Services\OwnerService;
 
 use App\OracleBasCustomer;
+use App\Owner;
 use App\Services\OwnerService;
 use Illuminate\Foundation\Testing\RefreshDatabase;
 use Illuminate\Foundation\Testing\WithFaker;
@@ -20,32 +21,70 @@ class GetOwnerByCodesTest extends TestCase
     {
         parent::setUp(); // TODO: Change the autogenerated stub
         $this->service = app('OwnerService');
-        $basCustomers = OracleBasCustomer::query()->selectRaw('customerid')->where('Customer_Type','OW')->get();
-        $this->data = $basCustomers->map(function($basCustomer){
-            return $basCustomer->customerid;
+
+        $owner1 = factory(Owner::class)->create();
+        $owner2 = factory(Owner::class)->create();
+        $owner3 = factory(Owner::class)->create();
+
+        $this->data['owner'] = [$owner1,$owner2,$owner3];
+        $this->service = app('OwnerService');
+
+        $basCustomer1 = factory(OracleBasCustomer::class)->make(["customer_type"=>'OW',"customerid"=>$owner1->code]);
+        $basCustomer2 = factory(OracleBasCustomer::class)->make(["customer_type"=>'OW',"customerid"=>$owner2->code]);
+        $basCustomer3 = factory(OracleBasCustomer::class)->make(["customer_type"=>'OW',"customerid"=>$owner3->code]);
+        $basCustomers = collect([$basCustomer1,$basCustomer2,$basCustomer3]);
+
+        $this->mock('OracleBasCustomerService',function ($mock)use($basCustomers,$owner1, $owner2, $owner3){
+            $mock->shouldReceive('first')->once()->with(["customer_type"=>'OW',"customerid"=>$owner1->code])->andReturn($basCustomers->where('customerid',$owner1->code)->first());
+            $mock->shouldReceive('first')->once()->with(["customer_type"=>'OW',"customerid"=>$owner2->code])->andReturn($basCustomers->where('customerid',$owner2->code)->first());
+            $mock->shouldReceive('first')->once()->with(["customer_type"=>'WO',"customerid"=>$owner3->code])->andReturn($basCustomers->where('customerid',$owner3->code)->first());
         });
     }
 
     /**
      * @test
      */
-    public function getOwnerByCodesNormal(){
-        $owners = $this->service->getOwnerByCodes($this->data);
-        if(empty($owners)){
-            $this->assertEmpty($owners);
-            $this->assertEmpty($this->data);
-            return;
-        };
-        $this->assertEquals($owners->count(),$this->data->count());
-        foreach ($this->data as $datum) {
-            $owner = $owners->where('code',$datum);
-            $this->assertNotNull($owner);
+    public function getOwnerByCodesNormal()
+    {
+        $owners = $this->service->getOwnerByCodes(data_get($this->data['owner'],'*.code'));
+        $this->assertNotNull($owners);
+        $this->assertNotEquals(count($owners),0);
+        $this->assertNotEquals(count($owners),1);
+        $this->assertEquals(count($owners),count($this->data['owner']['code']));
+        foreach ($this->data['owner'] as $item) {
+            $owner = $owners->where('code',$item['code'])->first();
+            $this->assertEquals($owner->id,$item['id']);
+            $this->assertEquals($owner->name,$item['name']);
+            $this->assertEquals($owner->code,$item['code']);
+        }
+    }
+
+    /**
+     * @test
+     */
+    public function ownerIsNotExist()
+    {
+        foreach ($this->data['owner'] as $owner)
+            $owner->delete();
+        $owners = $this->service->getOwnerByCodes(data_get($this->data['owner'],'*.code'));
+        $this->assertNotNull($owners);
+        $this->assertNotEquals(count($owners),0);
+        $this->assertNotEquals(count($owners),1);
+        $this->assertEquals(count($owners),count($this->data['owner']['code']));
+        foreach ($this->data['owner'] as $item) {
+            $owner = $owners->where('code',$item['code'])->first();
+            $this->assertEquals($owner->id,$item['id']);
+            $this->assertEquals($owner->name,$item['name']);
+            $this->assertEquals($owner->code,$item['code']);
         }
     }
 
     public function tearDown(): void
     {
         cache()->flush();
+        foreach ($this->data['owner'] as $owner) {
+            $owner->delete();
+        }
         parent::tearDown(); // TODO: Change the autogenerated stub
     }
 }