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

orchestral / workbench / 12343061968

15 Dec 2024 11:09PM UTC coverage: 92.611% (+0.4%) from 92.243%
12343061968

push

github

crynobone
wip

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

1 of 1 new or added line in 1 file covered. (100.0%)

19 existing lines in 2 files now uncovered.

564 of 609 relevant lines covered (92.61%)

14.81 hits per line

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

95.96
/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
        $serviceProvider = \sprintf('%sProviders\WorkbenchServiceProvider', Workbench::detectNamespace('app', force: true) ?? 'Workbench\App\\');
23✔
96
        $databaseSeeder = \sprintf('%sDatabaseSeeder', Workbench::detectNamespace('database/seeders', force: true) ?? 'Workbench\Database\Seeders\\');
23✔
97

98
        $filesystem->replaceInFile(
23✔
99
            [
23✔
100
                '{{ WorkbenchServiceProvider }}',
23✔
101
                'Workbench\App\Providers\WorkbenchServiceProvider',
23✔
102
                '{{ WorkbenchDatabaseSeeder }}',
23✔
103
                'Workbench\Database\Seeders\DatabaseSeeder',
23✔
104
            ],
23✔
105
            [
23✔
106
                $serviceProvider,
23✔
107
                $serviceProvider,
23✔
108
                $databaseSeeder,
23✔
109
                $databaseSeeder,
23✔
110
            ],
23✔
111
            $to
23✔
112
        );
23✔
113
    }
114

115
    /**
116
     * Copy the ".env" file.
117
     */
118
    protected function copyTestbenchDotEnvFile(Filesystem $filesystem, string $workingPath): void
119
    {
120
        $workbenchWorkingPath = join_paths($workingPath, 'workbench');
23✔
121

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

124
        if (! $filesystem->isFile($this->laravel->basePath('.env.example'))) {
23✔
UNCOV
125
            return;
×
126
        }
127

128
        /** @var \Illuminate\Support\Collection<int, string> $choices */
129
        $choices = Collection::make($this->environmentFiles())
23✔
130
            ->reject(static fn ($file) => $filesystem->isFile(join_paths($workbenchWorkingPath, $file)))
23✔
131
            ->values();
23✔
132

133
        if (! $this->option('force') && $choices->isEmpty()) {
23✔
134
            $this->components->twoColumnDetail(
1✔
135
                'File [.env] already exists', '<fg=yellow;options=bold>SKIPPED</>'
1✔
136
            );
1✔
137

138
            return;
1✔
139
        }
140

141
        /** @var string|null $targetEnvironmentFile */
142
        $targetEnvironmentFile = $this->input->isInteractive()
22✔
143
            ? select(
22✔
144
                "Export '.env' file as?",
22✔
145
                $choices->prepend('Skip exporting .env'), // @phpstan-ignore argument.type
22✔
146
            ) : null;
22✔
147

148
        if (\in_array($targetEnvironmentFile, [null, 'Skip exporting .env'])) {
22✔
149
            return;
7✔
150
        }
151

152
        $filesystem->ensureDirectoryExists($workbenchWorkingPath);
15✔
153

154
        $this->generateSeparateEnvironmentFileForTestbenchDusk($filesystem, $workbenchWorkingPath, $targetEnvironmentFile);
15✔
155

156
        (new GeneratesFile(
15✔
157
            filesystem: $filesystem,
15✔
158
            components: $this->components,
15✔
159
            force: (bool) $this->option('force'),
15✔
160
        ))->handle(
15✔
161
            $from,
15✔
162
            join_paths($workbenchWorkingPath, $targetEnvironmentFile)
15✔
163
        );
15✔
164

165
        (new GeneratesFile(
15✔
166
            filesystem: $filesystem,
15✔
167
            force: (bool) $this->option('force'),
15✔
168
        ))->handle(
15✔
169
            (string) Workbench::stubFile('gitignore'),
15✔
170
            join_paths($workbenchWorkingPath, '.gitignore')
15✔
171
        );
15✔
172
    }
173

174
    /**
175
     * Replace the default `laravel` skeleton for Testbench Dusk.
176
     *
177
     * @codeCoverageIgnore
178
     */
179
    protected function replaceDefaultLaravelSkeletonInTestbenchConfigurationFile(Filesystem $filesystem, string $workingPath): void
180
    {
181
        if ($this->hasTestbenchDusk === false) {
182
            return;
183
        }
184

185
        $filesystem->replaceInFile(["laravel: '@testbench'"], ["laravel: '@testbench-dusk'"], join_paths($workingPath, 'testbench.yaml'));
186
    }
187

188
    /**
189
     * Generate separate `.env.dusk` equivalent for Testbench Dusk.
190
     *
191
     * @codeCoverageIgnore
192
     */
193
    protected function generateSeparateEnvironmentFileForTestbenchDusk(Filesystem $filesystem, string $workbenchWorkingPath, string $targetEnvironmentFile): void
194
    {
195
        if ($this->hasTestbenchDusk === false) {
196
            return;
197
        }
198

199
        if ($this->components->confirm('Create separate environment file for Testbench Dusk?', false)) {
200
            (new GeneratesFile(
201
                filesystem: $filesystem,
202
                components: $this->components,
203
                force: (bool) $this->option('force'),
204
            ))->handle(
205
                $this->laravel->basePath('.env.example'),
206
                join_paths($workbenchWorkingPath, str_replace('.env', '.env.dusk', $targetEnvironmentFile))
207
            );
208
        }
209
    }
210

211
    /**
212
     * Get possible environment files.
213
     *
214
     * @return array<int, string>
215
     */
216
    protected function environmentFiles(): array
217
    {
218
        return [
23✔
219
            '.env',
23✔
220
            '.env.example',
23✔
221
            '.env.dist',
23✔
222
        ];
23✔
223
    }
224

225
    /**
226
     * Prompt the user for any missing arguments.
227
     *
228
     * @return void
229
     */
230
    protected function promptForMissingArguments(InputInterface $input, OutputInterface $output)
231
    {
232
        $devtool = null;
22✔
233

234
        if ($input->getOption('skip-devtool') === true) {
22✔
UNCOV
235
            $devtool = false;
×
236
        } elseif (\is_null($input->getOption('devtool'))) {
22✔
237
            $devtool = confirm('Run Workbench DevTool installation?', true);
1✔
238
        }
239

240
        if (! \is_null($devtool)) {
22✔
241
            $input->setOption('devtool', $devtool);
1✔
242
        }
243
    }
244

245
    /**
246
     * Get the console command options.
247
     *
248
     * @return array
249
     */
250
    protected function getOptions()
251
    {
252
        return [
23✔
253
            ['force', 'f', InputOption::VALUE_NONE, 'Overwrite any existing files'],
23✔
254
            ['devtool', null, InputOption::VALUE_NEGATABLE, 'Run DevTool installation'],
23✔
255
            ['basic', null, InputOption::VALUE_NONE, 'Skipped routes and discovers installation'],
23✔
256

257
            /** @deprecated */
258
            ['skip-devtool', null, InputOption::VALUE_NONE, 'Skipped DevTool installation (deprecated)'],
23✔
259
        ];
23✔
260
    }
261
}
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