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

artisan make:test

LD 5 éve
szülő
commit
43679bd2f3

+ 90 - 0
app/Console/Commands/TestMakeCommand.php

@@ -0,0 +1,90 @@
+<?php
+
+
+namespace App\Console\Commands;
+
+
+use Illuminate\Filesystem\Filesystem;
+use Symfony\Component\Console\Input\InputArgument;
+
+class TestMakeCommand extends \Illuminate\Foundation\Console\TestMakeCommand
+{
+    protected $signature = 'make:test {name : The name of the class}
+    {--unit : Create a unit test}
+    {--controller : Create a controller test}
+    {--controllers : Create a controllers test}
+    {--service : Create a service test}
+    {--services : Create a services test}
+    ';
+    protected function getDefaultNamespace($rootNamespace)
+    {
+        if ($this->option('controller')
+            ||$this->option('controllers')) {
+            return $rootNamespace.'\Controllers';
+        }
+        if ($this->option('services')
+            ||$this->option('service')) {
+            return $rootNamespace.'\Services\\'
+                .ucfirst($this->getServiceName())
+                ;
+        }
+        if ($this->option('unit')) {
+            return $rootNamespace.'\Unit';
+        } else {
+            return $rootNamespace.'\Feature';
+        }
+    }
+
+    protected function getNameInput()
+    {
+        $input= trim($this->argument('name'));
+        $inputs = explode(':', $input);
+        return $inputs[1]
+            ?$this->getMethodName()
+            :$inputs[0]
+            ;
+    }
+    protected function getServiceName()
+    {
+        $input= trim($this->argument('name'));
+        return explode(':',$input)[0];
+    }
+    protected function getMethodName()
+    {
+        $input= trim($this->argument('name'));
+        return '\Test'.ucfirst(explode(':',$input)[1]);
+    }
+
+    protected function getStub()
+    {
+        if ($this->option('controller')
+            ||$this->option('controllers')) {
+            return __DIR__.('/stubs/test.controller.stub');
+        }
+        if ($this->option('services')
+            ||$this->option('service')) {
+            return __DIR__.('/stubs/test.service.stub');
+        }
+        return $this->option('unit')
+            ? $this->resolveStubPath('/stubs/test.unit.stub')
+            : $this->resolveStubPath('/stubs/test.stub');
+    }
+    protected function getArguments()
+    {
+        $args=parent::getArguments();
+        $args[]=['xx', InputArgument::REQUIRED, 'xx'];
+    }
+
+    protected function buildClass($name)
+    {
+        $stub = $this->files->get($this->getStub());
+
+        return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name);
+    }
+    protected function replaceClass($stub, $name)
+    {
+        $class = str_replace($this->getNamespace($name).'\\', '', $name);
+
+        return str_replace(['DummyClass', '{{ class }}', '{{class}}'], $class, $stub);
+    }
+}

+ 15 - 0
app/Console/Commands/stubs/test.controller.stub

@@ -0,0 +1,15 @@
+<?php
+
+namespace {{ namespace }};
+
+use PHPUnit\Framework\TestCase;
+
+class {{ class }} extends TestCase
+{
+
+    public function testExample()
+    {
+    {{ class }};
+        $this->assertTrue(true);
+    }
+}

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

@@ -0,0 +1,22 @@
+<?php
+
+namespace {{ namespace }};
+
+use PHPUnit\Framework\TestCase;
+
+class {{ class }} extends TestCase
+{
+
+    /** @var CacheService $service */
+    public $service;
+    public function setUp(): void
+    {
+        parent::setUp();
+        $this->cacheService = app('CacheService');
+    }
+
+    public function testExample()
+    {
+        $this->assertTrue(true);
+    }
+}

+ 22 - 0
tests/Services/Bbb/TestA.php

@@ -0,0 +1,22 @@
+<?php
+
+namespace Tests\Services\Bbb;
+
+use PHPUnit\Framework\TestCase;
+
+class TestA extends TestCase
+{
+
+    /** @var CacheService $service */
+    public $service;
+    public function setUp(): void
+    {
+        parent::setUp();
+        $this->cacheService = app('CacheService');
+    }
+
+    public function testExample()
+    {
+        $this->assertTrue(true);
+    }
+}