• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

orchestral / workbench / 12384004366

18 Dec 2024 12:36AM UTC coverage: 92.663% (-0.1%) from 92.777%
12384004366

push

github

crynobone
Release 8.15.1

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

581 of 627 relevant lines covered (92.66%)

15.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.73
/src/Console/InstallCommand.php
1
<?php
2

3
namespace Orchestra\Workbench\Console;
4

5
use Composer\InstalledVersions;
6
use Illuminate\Console\Command;
7
use Illuminate\Contracts\Console\PromptsForMissingInput;
8
use Illuminate\Filesystem\Filesystem;
9
use Illuminate\Support\Collection;
10
use Orchestra\Testbench\Foundation\Console\Actions\GeneratesFile;
11
use Orchestra\Workbench\Workbench;
12
use Symfony\Component\Console\Attribute\AsCommand;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Console\Output\OutputInterface;
16

17
use function Laravel\Prompts\confirm;
18
use function Laravel\Prompts\select;
19
use function Orchestra\Testbench\join_paths;
20
use function Orchestra\Testbench\package_path;
21

22
#[AsCommand(name: 'workbench:install', description: 'Setup Workbench for package development')]
23
class InstallCommand extends Command implements PromptsForMissingInput
24
{
25
    /**
26
     * The `testbench.yaml` default configuration file.
27
     */
28
    public static ?string $configurationBaseFile = null;
29

30
    /**
31
     * Determine if Package also uses Testbench Dusk.
32
     */
33
    protected ?bool $hasTestbenchDusk = null;
34

35
    /** {@inheritDoc} */
36
    #[\Override]
37
    protected function initialize(InputInterface $input, OutputInterface $output)
38
    {
39
        $this->hasTestbenchDusk = InstalledVersions::isInstalled('orchestra/testbench-dusk');
23✔
40

41
        parent::initialize($input, $output);
23✔
42
    }
43

44
    /**
45
     * Execute the console command.
46
     *
47
     * @return int
48
     */
49
    public function handle(Filesystem $filesystem)
50
    {
51
        if (! $this->option('skip-devtool')) {
23✔
52
            $devtool = match (true) {
23✔
53
                \is_bool($this->option('devtool')) => $this->option('devtool'),
23✔
54
                default => $this->components->confirm('Install Workbench DevTool?', true),
×
55
            };
23✔
56

57
            if ($devtool === true) {
23✔
58
                $this->call('workbench:devtool', [
4✔
59
                    '--force' => $this->option('force'),
4✔
60
                    '--no-install' => true,
4✔
61
                    '--basic' => $this->option('basic'),
4✔
62
                ]);
4✔
63
            }
64
        }
65

66
        $workingPath = package_path();
23✔
67

68
        $this->copyTestbenchConfigurationFile($filesystem, $workingPath);
23✔
69
        $this->copyTestbenchDotEnvFile($filesystem, $workingPath);
23✔
70

71
        $this->replaceDefaultLaravelSkeletonInTestbenchConfigurationFile($filesystem, $workingPath);
23✔
72

73
        $this->call('workbench:create-sqlite-db', ['--force' => true]);
23✔
74

75
        return Command::SUCCESS;
23✔
76
    }
77

78
    /**
79
     * Copy the "testbench.yaml" file.
80
     */
81
    protected function copyTestbenchConfigurationFile(Filesystem $filesystem, string $workingPath): void
82
    {
83
        $from = ! \is_null(static::$configurationBaseFile)
23✔
84
            ? (string) realpath(static::$configurationBaseFile)
×
85
            : (string) Workbench::stubFile($this->option('basic') === true ? 'config.basic' : 'config');
23✔
86

87
        $to = join_paths($workingPath, 'testbench.yaml');
23✔
88

89
        (new GeneratesFile(
23✔
90
            filesystem: $filesystem,
23✔
91
            components: $this->components,
23✔
92
            force: (bool) $this->option('force'),
23✔
93
        ))->handle($from, $to);
23✔
94

95
        $workbenchAppNamespacePrefix = Workbench::detectNamespace('app', force: true) ?? 'Workbench\App\\';
23✔
96
        $workbenchSeederNamespacePrefix = Workbench::detectNamespace('database/seeders', force: true) ?? 'Workbench\Database\Seeders\\';
23✔
97

98
        $serviceProvider = \sprintf('%sProviders\WorkbenchServiceProvider', $workbenchAppNamespacePrefix);
23✔
99
        $databaseSeeder = \sprintf('%sDatabaseSeeder', $workbenchSeederNamespacePrefix);
23✔
100

101
        $filesystem->replaceInFile(
23✔
102
            [
23✔
103
                '{{WorkbenchAppNamespace}}',
23✔
104
                '{{ WorkbenchAppNamespace }}',
23✔
105
                '{{WorkbenchSeederNamespace}}',
23✔
106
                '{{ WorkbenchSeederNamespace }}',
23✔
107

108
                '{{WorkbenchServiceProvider}}',
23✔
109
                '{{ WorkbenchServiceProvider }}',
23✔
110
                'Workbench\App\Providers\WorkbenchServiceProvider',
23✔
111

112
                '{{WorkbenchDatabaseSeeder}}',
23✔
113
                '{{ WorkbenchDatabaseSeeder }}',
23✔
114
                'Workbench\Database\Seeders\DatabaseSeeder',
23✔
115

116
                '    - migrate-fresh',
23✔
117
            ],
23✔
118
            [
23✔
119
                $workbenchAppNamespacePrefix,
23✔
120
                $workbenchAppNamespacePrefix,
23✔
121
                $workbenchSeederNamespacePrefix,
23✔
122
                $workbenchSeederNamespacePrefix,
23✔
123

124
                $serviceProvider,
23✔
125
                $serviceProvider,
23✔
126
                $serviceProvider,
23✔
127

128
                $databaseSeeder,
23✔
129
                $databaseSeeder,
23✔
130
                $databaseSeeder,
23✔
131

132
                $databaseSeeder === 'Database\Seeders\DatabaseSeeder'
23✔
133
                    ? '    - migrate-fresh'
×
134
                    : '    - migrate-fresh:'.PHP_EOL.'        --seed: true',
23✔
135
            ],
23✔
136
            $to
23✔
137
        );
23✔
138
    }
139

140
    /**
141
     * Copy the ".env" file.
142
     */
143
    protected function copyTestbenchDotEnvFile(Filesystem $filesystem, string $workingPath): void
144
    {
145
        $workbenchWorkingPath = join_paths($workingPath, 'workbench');
23✔
146

147
        $from = $this->laravel->basePath('.env.example');
23✔
148

149
        if (! $filesystem->isFile($this->laravel->basePath('.env.example'))) {
23✔
150
            return;
×
151
        }
152

153
        /** @var \Illuminate\Support\Collection<int, string> $choices */
154
        $choices = Collection::make($this->environmentFiles())
23✔
155
            ->reject(static fn ($file) => $filesystem->isFile(join_paths($workbenchWorkingPath, $file)))
23✔
156
            ->values();
23✔
157

158
        if (! $this->option('force') && $choices->isEmpty()) {
23✔
159
            $this->components->twoColumnDetail(
1✔
160
                'File [.env] already exists', '<fg=yellow;options=bold>SKIPPED</>'
1✔
161
            );
1✔
162

163
            return;
1✔
164
        }
165

166
        /** @var string|null $targetEnvironmentFile */
167
        $targetEnvironmentFile = $this->input->isInteractive()
22✔
168
            ? select(
22✔
169
                "Export '.env' file as?",
22✔
170
                $choices->prepend('Skip exporting .env'), // @phpstan-ignore argument.type
22✔
171
            ) : null;
22✔
172

173
        if (\in_array($targetEnvironmentFile, [null, 'Skip exporting .env'])) {
22✔
174
            return;
7✔
175
        }
176

177
        $filesystem->ensureDirectoryExists($workbenchWorkingPath);
15✔
178

179
        $this->generateSeparateEnvironmentFileForTestbenchDusk($filesystem, $workbenchWorkingPath, $targetEnvironmentFile);
15✔
180

181
        (new GeneratesFile(
15✔
182
            filesystem: $filesystem,
15✔
183
            components: $this->components,
15✔
184
            force: (bool) $this->option('force'),
15✔
185
        ))->handle(
15✔
186
            $from,
15✔
187
            join_paths($workbenchWorkingPath, $targetEnvironmentFile)
15✔
188
        );
15✔
189

190
        (new GeneratesFile(
15✔
191
            filesystem: $filesystem,
15✔
192
            force: (bool) $this->option('force'),
15✔
193
        ))->handle(
15✔
194
            (string) Workbench::stubFile('gitignore'),
15✔
195
            join_paths($workbenchWorkingPath, '.gitignore')
15✔
196
        );
15✔
197
    }
198

199
    /**
200
     * Replace the default `laravel` skeleton for Testbench Dusk.
201
     *
202
     * @codeCoverageIgnore
203
     */
204
    protected function replaceDefaultLaravelSkeletonInTestbenchConfigurationFile(Filesystem $filesystem, string $workingPath): void
205
    {
206
        if ($this->hasTestbenchDusk === false) {
207
            return;
208
        }
209

210
        $filesystem->replaceInFile(["laravel: '@testbench'"], ["laravel: '@testbench-dusk'"], join_paths($workingPath, 'testbench.yaml'));
211
    }
212

213
    /**
214
     * Generate separate `.env.dusk` equivalent for Testbench Dusk.
215
     *
216
     * @codeCoverageIgnore
217
     */
218
    protected function generateSeparateEnvironmentFileForTestbenchDusk(Filesystem $filesystem, string $workbenchWorkingPath, string $targetEnvironmentFile): void
219
    {
220
        if ($this->hasTestbenchDusk === false) {
221
            return;
222
        }
223

224
        if ($this->components->confirm('Create separate environment file for Testbench Dusk?', false)) {
225
            (new GeneratesFile(
226
                filesystem: $filesystem,
227
                components: $this->components,
228
                force: (bool) $this->option('force'),
229
            ))->handle(
230
                $this->laravel->basePath('.env.example'),
231
                join_paths($workbenchWorkingPath, str_replace('.env', '.env.dusk', $targetEnvironmentFile))
232
            );
233
        }
234
    }
235

236
    /**
237
     * Get possible environment files.
238
     *
239
     * @return array<int, string>
240
     */
241
    protected function environmentFiles(): array
242
    {
243
        return [
23✔
244
            '.env',
23✔
245
            '.env.example',
23✔
246
            '.env.dist',
23✔
247
        ];
23✔
248
    }
249

250
    /**
251
     * Prompt the user for any missing arguments.
252
     *
253
     * @return void
254
     */
255
    protected function promptForMissingArguments(InputInterface $input, OutputInterface $output)
256
    {
257
        $devtool = null;
22✔
258

259
        if ($input->getOption('skip-devtool') === true) {
22✔
260
            $devtool = false;
×
261
        } elseif (\is_null($input->getOption('devtool'))) {
22✔
262
            $devtool = confirm('Run Workbench DevTool installation?', true);
1✔
263
        }
264

265
        if (! \is_null($devtool)) {
22✔
266
            $input->setOption('devtool', $devtool);
1✔
267
        }
268
    }
269

270
    /**
271
     * Get the console command options.
272
     *
273
     * @return array
274
     */
275
    protected function getOptions()
276
    {
277
        return [
23✔
278
            ['force', 'f', InputOption::VALUE_NONE, 'Overwrite any existing files'],
23✔
279
            ['devtool', null, InputOption::VALUE_NEGATABLE, 'Run DevTool installation'],
23✔
280
            ['basic', null, InputOption::VALUE_NONE, 'Skipped routes and discovers installation'],
23✔
281

282
            /** @deprecated */
283
            ['skip-devtool', null, InputOption::VALUE_NONE, 'Skipped DevTool installation (deprecated)'],
23✔
284
        ];
23✔
285
    }
286
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc