Forráskód Böngészése

伪造子桩件的简易封装

LD 5 éve
szülő
commit
341ee51c1b

+ 0 - 2
app/Services/StationTypeService.php

@@ -22,8 +22,6 @@ class StationTypeService
     function getByBatch(Batch $batch)
     {
         $this->instant($this->stationRuleBatchService,'StationRuleBatchService');
-        return $this->stationRuleBatchService->getByBatch($batch);
-//        $stationRuleBatch=$this->stationRuleBatchService->getByBatch($batch);
 //        return $stationRuleBatch['stationType'];
         return StationType::query()->where('name','料箱监视器')->first();
     }

+ 63 - 20
tests/Services/StationTaskCommodityService/CreateByBatchTest.php

@@ -78,28 +78,71 @@ class CreateByBatchTest extends TestCase
                 ]);
     }
 
+    function subMock(array $serviceDefinition){
+            $mockingService=$this->createPartialMock(
+                $serviceDefinition['class'],
+                array_keys($serviceDefinition['methods'])
+            );
+            foreach ($serviceDefinition['methods'] as $mName=>$mVal){
+                $mockingService->method($mName)->willReturn($mVal);
+            }
+
+            foreach ($serviceDefinition['subServices'] as $subService){
+                $mockingService->instant(
+                    $subService['serviceName'],
+                    ($subServiceMock=function()use($subService){
+                        $subServiceMock=$this->createPartialMock(
+                            $subService['class'],
+                            array_keys($subService['methods'])
+                        );
+                        foreach ($subService['methods'] as $methodName=>$methodVal){
+                            $subServiceMock->method($methodName)->willReturn($methodVal);
+                        }
+                        return $subServiceMock;
+                    })()
+                );
+            }
+            return $mockingService;
+    }
+
     public function testReturned()
     {
-        $stationTypeServiceMock=( function (){
-            $stationTypeServiceMock=$this->createPartialMock(
-                StationTypeService::class,
-                ['aaa']
-            );
-            $stationRuleBatchServiceStr = 'stationRuleBatchService';
-            $stationTypeServiceMock->instant(
-                $stationRuleBatchServiceStr,
-                ($stationRuleBatchServiceMock=function(){
-                    $mock=$this->createPartialMock(
-                        StationRuleBatchService::class,
-                        ['getByBatch']
-                    );
-                    $mock->method('getByBatch')->willReturn('ddd');
-                    return $mock;
-                })()
-            );
-            return $stationTypeServiceMock;
-        })();
+        $stationTypeServiceMock=$this->subMock([
+                'class'=>StationTypeService::class,
+                'methods'=>[
+//                    'getByBatch'=>111,
+                ],
+                'subServices'=>[
+                    [
+                        'serviceName'=>'stationRuleBatchService',
+                        'class'=>stationRuleBatchService::class,
+                        'methods'=>[
+                            'getByBatch'=>4242,
+                            'aaa'=>222,
+                        ],
+                    ],
+                ]
+        ]);
         dd($stationTypeServiceMock->getByBatch(new Batch()));
+//        $stationTypeServiceMock=( function (){
+//            $stationTypeServiceMock=$this->createPartialMock(
+//                StationTypeService::class,
+//                ['aaa']
+//            );
+//            $stationRuleBatchServiceStr = 'stationRuleBatchService';
+//            $stationTypeServiceMock->instant(
+//                $stationRuleBatchServiceStr,
+//                ($stationRuleBatchServiceMock=function(){
+//                    $mock=$this->createPartialMock(
+//                        StationRuleBatchService::class,
+//                        ['getByBatch']
+//                    );
+//                    $mock->method('getByBatch')->willReturn('ddd');
+//                    return $mock;
+//                })()
+//            );
+//            return $stationTypeServiceMock;
+//        })();
 
 
         $this->data['stationTaskCommodities']
@@ -109,7 +152,7 @@ class CreateByBatchTest extends TestCase
             foreach($this->data['batches'] as $batch){
                 $this->data['stationTaskCommodities']
                     =$this->data['stationTaskCommodities']->merge(
-                        $this->service->createByBatch($batch)//<<---testing it
+                    $this->service->createByBatch($batch)//<<---testing it
                 );
             }
             return $this->data['stationTaskCommodities'];