Sfoglia il codice sorgente

test make命令添加默认数据

LD 5 anni fa
parent
commit
ff33999e77

+ 8 - 1
app/Console/Commands/TestMakeCommand.php

@@ -25,7 +25,7 @@ class TestMakeCommand extends \Illuminate\Foundation\Console\TestMakeCommand
         if ($this->option('services')
             ||$this->option('service')) {
             return $rootNamespace.'\Services\\'
-                .ucfirst($this->getServiceName())
+                .$this->getServiceName()
                 ;
         }
         if ($this->option('unit')) {
@@ -49,6 +49,11 @@ class TestMakeCommand extends \Illuminate\Foundation\Console\TestMakeCommand
         $input= trim($this->argument('name'));
         return ucfirst(explode(':',$input)[0]);
     }
+    protected function getModelName()
+    {
+        $input= trim($this->argument('name'));
+        return str_replace('Service','',(explode(':',$input)[0]));
+    }
     protected function getMethodName()
     {
         $input= trim($this->argument('name'));
@@ -75,6 +80,8 @@ class TestMakeCommand extends \Illuminate\Foundation\Console\TestMakeCommand
         $class = str_replace($this->getNamespace($name).'\\', '', $this->getMethodName());
         $class = str_replace('\\', '', $class);
         $stub = str_replace(['{{ serviceName }}', '{{serviceName}}'], $this->getServiceName(), $stub);
+        $stub = str_replace(['{{ modelName }}', '{{modelName}}'], $this->getModelName(), $stub);
+        $stub = str_replace(['{{ modelNameUc }}', '{{modelNameUc}}'], ucfirst($this->getModelName()), $stub);
 
         return str_replace(['DummyClass', '{{ class }}', '{{class}}'], $class, $stub);
     }

+ 9 - 0
app/Console/Commands/stubs/test.service.stub

@@ -3,16 +3,22 @@
 namespace {{ namespace }};
 use App\Services\{{ serviceName }};
 use Tests\TestCase;
+use App\{{ modelNameUc }};
 
 class {{ class }} extends TestCase
 {
 
     /** @var {{ serviceName }} $service */
     public $service;
+    private $data;
+    private $amount=2;
     function setUp(): void
     {
         parent::setUp();
         $this->service = app('{{ serviceName }}');
+        $this->data['{{ modelName }}s']
+            = factory({{ modelNameUc }}::class, $this->amount)
+            ->create();
     }
 
     public function testReturned()
@@ -22,6 +28,9 @@ class {{ class }} extends TestCase
 
     function tearDown(): void
     {
+        {{ modelNameUc }}::query()
+            ->whereIn('id',data_get($this->data['{{ modelName }}s'],'*.id')??[])
+            ->delete();
         parent::tearDown();
     }
 }