Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 399 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Foundation\Console;
4
 
5
use Illuminate\Console\GeneratorCommand;
6
use Illuminate\Support\Str;
7
use Symfony\Component\Console\Attribute\AsCommand;
399 lars 8
use Symfony\Component\Console\Input\InputInterface;
148 lars 9
use Symfony\Component\Console\Input\InputOption;
399 lars 10
use Symfony\Component\Console\Output\OutputInterface;
148 lars 11
 
12
#[AsCommand(name: 'make:test')]
13
class TestMakeCommand extends GeneratorCommand
14
{
15
    /**
16
     * The console command name.
17
     *
18
     * @var string
19
     */
20
    protected $name = 'make:test';
21
 
22
    /**
23
     * The name of the console command.
24
     *
25
     * This name is used to identify the command during lazy loading.
26
     *
27
     * @var string|null
28
     *
29
     * @deprecated
30
     */
31
    protected static $defaultName = 'make:test';
32
 
33
    /**
34
     * The console command description.
35
     *
36
     * @var string
37
     */
38
    protected $description = 'Create a new test class';
39
 
40
    /**
41
     * The type of class being generated.
42
     *
43
     * @var string
44
     */
45
    protected $type = 'Test';
46
 
47
    /**
48
     * Get the stub file for the generator.
49
     *
50
     * @return string
51
     */
52
    protected function getStub()
53
    {
54
        $suffix = $this->option('unit') ? '.unit.stub' : '.stub';
55
 
56
        return $this->option('pest')
57
            ? $this->resolveStubPath('/stubs/pest'.$suffix)
58
            : $this->resolveStubPath('/stubs/test'.$suffix);
59
    }
60
 
61
    /**
62
     * Resolve the fully-qualified path to the stub.
63
     *
64
     * @param  string  $stub
65
     * @return string
66
     */
67
    protected function resolveStubPath($stub)
68
    {
69
        return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
70
                        ? $customPath
71
                        : __DIR__.$stub;
72
    }
73
 
74
    /**
75
     * Get the destination class path.
76
     *
77
     * @param  string  $name
78
     * @return string
79
     */
80
    protected function getPath($name)
81
    {
82
        $name = Str::replaceFirst($this->rootNamespace(), '', $name);
83
 
84
        return base_path('tests').str_replace('\\', '/', $name).'.php';
85
    }
86
 
87
    /**
88
     * Get the default namespace for the class.
89
     *
90
     * @param  string  $rootNamespace
91
     * @return string
92
     */
93
    protected function getDefaultNamespace($rootNamespace)
94
    {
95
        if ($this->option('unit')) {
96
            return $rootNamespace.'\Unit';
97
        } else {
98
            return $rootNamespace.'\Feature';
99
        }
100
    }
101
 
102
    /**
103
     * Get the root namespace for the class.
104
     *
105
     * @return string
106
     */
107
    protected function rootNamespace()
108
    {
109
        return 'Tests';
110
    }
111
 
112
    /**
113
     * Get the console command options.
114
     *
115
     * @return array
116
     */
117
    protected function getOptions()
118
    {
119
        return [
120
            ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the test already exists'],
121
            ['unit', 'u', InputOption::VALUE_NONE, 'Create a unit test'],
122
            ['pest', 'p', InputOption::VALUE_NONE, 'Create a Pest test'],
123
        ];
124
    }
399 lars 125
 
126
    /**
127
     * Interact further with the user if they were prompted for missing arguments.
128
     *
129
     * @param  \Symfony\Component\Console\Input\InputInterface  $input
130
     * @param  \Symfony\Component\Console\Output\OutputInterface  $output
131
     * @return void
132
     */
133
    protected function afterPromptingForMissingArguments(InputInterface $input, OutputInterface $output)
134
    {
621 lars 135
        if ($this->isReservedName($this->getNameInput()) || $this->didReceiveOptions($input)) {
399 lars 136
            return;
137
        }
138
 
139
        $type = $this->components->choice('Which type of test would you like', [
140
            'feature',
141
            'unit',
142
            'pest feature',
143
            'pest unit',
144
        ], default: 0);
145
 
146
        match ($type) {
147
            'feature' => null,
148
            'unit' => $input->setOption('unit', true),
149
            'pest feature' => $input->setOption('pest', true),
150
            'pest unit' => tap($input)->setOption('pest', true)->setOption('unit', true),
151
        };
152
    }
148 lars 153
}