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

orchestral / workbench / 12509286075

26 Dec 2024 11:04PM UTC coverage: 92.868% (-1.0%) from 93.842%
12509286075

push

github

crynobone
fixes tests

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

11 of 12 new or added lines in 2 files covered. (91.67%)

24 existing lines in 3 files now uncovered.

586 of 631 relevant lines covered (92.87%)

17.17 hits per line

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

88.3
/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\StubRegistrar;
12
use Orchestra\Workbench\Workbench;
13
use Symfony\Component\Console\Attribute\AsCommand;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Input\InputOption;
16
use Symfony\Component\Console\Output\OutputInterface;
17

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

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

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

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

42
        parent::initialize($input, $output);
27✔
43
    }
44

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

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

65
        $workingPath = package_path();
27✔
66

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

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

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

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

78
    /**
79
     * Prepare workbench directories.
80
     */
81
    protected function prepareWorkbenchDirectories(Filesystem $filesystem, string $workingPath): void
82
    {
83
        if (! $this->input->isInteractive()) {
27✔
84
            return;
1✔
85
        }
86

87
        $workbenchWorkingPath = join_paths($workingPath, 'workbench');
26✔
88

89
        foreach (['app' => true, 'providers' => false] as $bootstrap => $default) {
26✔
90
            if (! confirm("Generate `workbench/bootstrap/{$bootstrap}.php` file?", default: $default)) {
26✔
91
                continue;
26✔
92
            }
93

UNCOV
94
            (new GeneratesFile(
×
UNCOV
95
                filesystem: $filesystem,
×
UNCOV
96
                components: $this->components,
×
UNCOV
97
                force: (bool) $this->option('force'),
×
UNCOV
98
            ))->handle(
×
UNCOV
99
                (string) realpath(join_paths(__DIR__, 'stubs', 'bootstrap', "{$bootstrap}.php")),
×
UNCOV
100
                join_paths($workbenchWorkingPath, 'bootstrap', "{$bootstrap}.php")
×
UNCOV
101
            );
×
102
        }
103
    }
104

105
    /**
106
     * Copy the "testbench.yaml" file.
107
     */
108
    protected function copyTestbenchConfigurationFile(Filesystem $filesystem, string $workingPath): void
109
    {
110
        $from = ! \is_null(static::$configurationBaseFile)
27✔
UNCOV
111
            ? (string) realpath(static::$configurationBaseFile)
×
112
            : (string) Workbench::stubFile($this->option('basic') === true ? 'config.basic' : 'config');
27✔
113

114
        $to = join_paths($workingPath, 'testbench.yaml');
27✔
115

116
        (new GeneratesFile(
27✔
117
            filesystem: $filesystem,
27✔
118
            components: $this->components,
27✔
119
            force: (bool) $this->option('force'),
27✔
120
        ))->handle($from, $to);
27✔
121

122
        StubRegistrar::replaceInFile($filesystem, $to);
27✔
123
    }
124

125
    /**
126
     * Copy the ".env" file.
127
     */
128
    protected function copyTestbenchDotEnvFile(Filesystem $filesystem, string $workingPath): void
129
    {
130
        $workbenchWorkingPath = join_paths($workingPath, 'workbench');
27✔
131

132
        $from = $this->laravel->basePath('.env.example');
27✔
133

134
        if (! $filesystem->isFile($this->laravel->basePath('.env.example'))) {
27✔
UNCOV
135
            return;
×
136
        }
137

138
        /** @var \Illuminate\Support\Collection<int, string> $choices */
139
        $choices = Collection::make($this->environmentFiles())
27✔
140
            ->reject(static fn ($file) => $filesystem->isFile(join_paths($workbenchWorkingPath, $file)))
27✔
141
            ->values();
27✔
142

143
        if (! $this->option('force') && $choices->isEmpty()) {
27✔
144
            $this->components->twoColumnDetail(
1✔
145
                'File [.env] already exists', '<fg=yellow;options=bold>SKIPPED</>'
1✔
146
            );
1✔
147

148
            return;
1✔
149
        }
150

151
        /** @var string|null $targetEnvironmentFile */
152
        $targetEnvironmentFile = $this->input->isInteractive()
26✔
153
            ? select(
26✔
154
                "Export '.env' file as?",
26✔
155
                $choices->prepend('Skip exporting .env'), // @phpstan-ignore argument.type
26✔
156
            ) : null;
26✔
157

158
        if (\in_array($targetEnvironmentFile, [null, 'Skip exporting .env'])) {
26✔
159
            return;
8✔
160
        }
161

162
        $filesystem->ensureDirectoryExists($workbenchWorkingPath);
18✔
163

164
        $this->generateSeparateEnvironmentFileForTestbenchDusk($filesystem, $workbenchWorkingPath, $targetEnvironmentFile);
18✔
165

166
        (new GeneratesFile(
18✔
167
            filesystem: $filesystem,
18✔
168
            components: $this->components,
18✔
169
            force: (bool) $this->option('force'),
18✔
170
        ))->handle(
18✔
171
            $from,
18✔
172
            join_paths($workbenchWorkingPath, $targetEnvironmentFile)
18✔
173
        );
18✔
174

175
        (new GeneratesFile(
18✔
176
            filesystem: $filesystem,
18✔
177
            force: (bool) $this->option('force'),
18✔
178
        ))->handle(
18✔
179
            (string) Workbench::stubFile('gitignore'),
18✔
180
            join_paths($workbenchWorkingPath, '.gitignore')
18✔
181
        );
18✔
182
    }
183

184
    /**
185
     * Replace the default `laravel` skeleton for Testbench Dusk.
186
     *
187
     * @codeCoverageIgnore
188
     */
189
    protected function replaceDefaultLaravelSkeletonInTestbenchConfigurationFile(Filesystem $filesystem, string $workingPath): void
190
    {
191
        if ($this->hasTestbenchDusk === false) {
192
            return;
193
        }
194

195
        $filesystem->replaceInFile(["laravel: '@testbench'"], ["laravel: '@testbench-dusk'"], join_paths($workingPath, 'testbench.yaml'));
196
    }
197

198
    /**
199
     * Generate separate `.env.dusk` equivalent for Testbench Dusk.
200
     *
201
     * @codeCoverageIgnore
202
     */
203
    protected function generateSeparateEnvironmentFileForTestbenchDusk(Filesystem $filesystem, string $workbenchWorkingPath, string $targetEnvironmentFile): void
204
    {
205
        if ($this->hasTestbenchDusk === false) {
206
            return;
207
        }
208

209
        if ($this->components->confirm('Create separate environment file for Testbench Dusk?', false)) {
210
            (new GeneratesFile(
211
                filesystem: $filesystem,
212
                components: $this->components,
213
                force: (bool) $this->option('force'),
214
            ))->handle(
215
                $this->laravel->basePath('.env.example'),
216
                join_paths($workbenchWorkingPath, str_replace('.env', '.env.dusk', $targetEnvironmentFile))
217
            );
218
        }
219
    }
220

221
    /**
222
     * Get possible environment files.
223
     *
224
     * @return array<int, string>
225
     */
226
    protected function environmentFiles(): array
227
    {
228
        return [
27✔
229
            '.env',
27✔
230
            '.env.example',
27✔
231
            '.env.dist',
27✔
232
        ];
27✔
233
    }
234

235
    /**
236
     * Prompt the user for any missing arguments.
237
     *
238
     * @return void
239
     */
240
    protected function promptForMissingArguments(InputInterface $input, OutputInterface $output)
241
    {
242
        $devtool = null;
26✔
243

244
        if (\is_null($input->getOption('devtool'))) {
26✔
245
            $devtool = confirm('Run Workbench DevTool installation?', true);
1✔
246
        }
247

248
        if (! \is_null($devtool)) {
26✔
249
            $input->setOption('devtool', $devtool);
1✔
250
        }
251
    }
252

253
    /**
254
     * Get the console command options.
255
     *
256
     * @return array
257
     */
258
    protected function getOptions()
259
    {
260
        return [
27✔
261
            ['force', 'f', InputOption::VALUE_NONE, 'Overwrite any existing files'],
27✔
262
            ['devtool', null, InputOption::VALUE_NEGATABLE, 'Run DevTool installation'],
27✔
263
            ['basic', null, InputOption::VALUE_NONE, 'Skipped routes and discovers installation'],
27✔
264
        ];
27✔
265
    }
266
}
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