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

orchestral / workbench / 12239578785

09 Dec 2024 04:08PM UTC coverage: 92.599% (-1.9%) from 94.49%
12239578785

push

github

web-flow
[7.x] Improves `workbench:install` workflow and tests (#58)

* [7.x] Test Improvements

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

* wip

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

* wip

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

* wip

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

* wip

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

* wip

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

* wip

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

* wip

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

* wip

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

* wip

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

* wip

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

* wip

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

* wip

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

* wip

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

* wip

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

* wip

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

---------

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

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

23 existing lines in 2 files now uncovered.

538 of 581 relevant lines covered (92.6%)

15.26 hits per line

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

97.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\Filesystem\Filesystem;
8
use Illuminate\Support\Collection;
9
use Orchestra\Testbench\Foundation\Console\Actions\GeneratesFile;
10
use Orchestra\Workbench\Workbench;
11
use Symfony\Component\Console\Attribute\AsCommand;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Input\InputOption;
14
use Symfony\Component\Console\Output\OutputInterface;
15

16
use function Orchestra\Testbench\join_paths;
17
use function Orchestra\Testbench\package_path;
18

19
#[AsCommand(name: 'workbench:install', description: 'Setup Workbench for package development')]
20
class InstallCommand extends Command
21
{
22
    use Concerns\InteractsWithFiles;
23

24
    /**
25
     * The `testbench.yaml` default configuration file.
26
     */
27
    public static ?string $configurationBaseFile = null;
28

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

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

40
        parent::initialize($input, $output);
26✔
41
    }
42

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

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

65
        $workingPath = package_path();
26✔
66

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

70
        $this->replaceDefaultLaravelSkeletonInTestbenchConfigurationFile($filesystem, $workingPath);
26✔
71

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

74
        return Command::SUCCESS;
26✔
75
    }
76

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

86
        $to = join_paths($workingPath, 'testbench.yaml');
26✔
87

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

95
    /**
96
     * Copy the ".env" file.
97
     */
98
    protected function copyTestbenchDotEnvFile(Filesystem $filesystem, string $workingPath): void
99
    {
100
        $workbenchWorkingPath = join_paths($workingPath, 'workbench');
26✔
101

102
        $from = $this->laravel->basePath('.env.example');
26✔
103

104
        if (! $filesystem->isFile($this->laravel->basePath('.env.example'))) {
26✔
UNCOV
105
            return;
×
106
        }
107

108
        $choices = Collection::make($this->environmentFiles())
26✔
109
            ->reject(static fn ($file) => $filesystem->isFile(join_paths($workbenchWorkingPath, $file)))
26✔
110
            ->values();
26✔
111

112
        if (! $this->option('force') && $choices->isEmpty()) {
26✔
113
            $this->components->twoColumnDetail(
1✔
114
                'File [.env] already exists', '<fg=yellow;options=bold>SKIPPED</>'
1✔
115
            );
1✔
116

117
            return;
1✔
118
        }
119

120
        /** @var string|null $targetEnvironmentFile */
121
        $targetEnvironmentFile = $this->components->choice(
25✔
122
            "Export '.env' file as?",
25✔
123
            $choices->prepend('Skip exporting .env')->all()
25✔
124
        );
25✔
125

126
        if (\is_null($targetEnvironmentFile) || $targetEnvironmentFile === 'Skip exporting .env') {
25✔
127
            return;
10✔
128
        }
129

130
        $filesystem->ensureDirectoryExists($workbenchWorkingPath);
15✔
131

132
        $this->generateSeparateEnvironmentFileForTestbenchDusk($filesystem, $workbenchWorkingPath, $targetEnvironmentFile);
15✔
133

134
        (new GeneratesFile(
15✔
135
            filesystem: $filesystem,
15✔
136
            components: $this->components,
15✔
137
            force: (bool) $this->option('force'),
15✔
138
        ))->handle(
15✔
139
            $from,
15✔
140
            join_paths($workbenchWorkingPath, $targetEnvironmentFile)
15✔
141
        );
15✔
142

143
        (new GeneratesFile(
15✔
144
            filesystem: $filesystem,
15✔
145
            force: (bool) $this->option('force'),
15✔
146
        ))->handle(
15✔
147
            (string) Workbench::stubFile('gitignore'),
15✔
148
            join_paths($workbenchWorkingPath, '.gitignore')
15✔
149
        );
15✔
150
    }
151

152
    /**
153
     * Replace the default `laravel` skeleton for Testbench Dusk.
154
     *
155
     * @codeCoverageIgnore
156
     */
157
    protected function replaceDefaultLaravelSkeletonInTestbenchConfigurationFile(Filesystem $filesystem, string $workingPath): void
158
    {
159
        if ($this->hasTestbenchDusk === false) {
160
            return;
161
        }
162

163
        $this->replaceInFile($filesystem, ["laravel: '@testbench'"], ["laravel: '@testbench-dusk'"], join_paths($workingPath, 'testbench.yaml'));
164
    }
165

166
    /**
167
     * Generate separate `.env.dusk` equivalent for Testbench Dusk.
168
     *
169
     * @codeCoverageIgnore
170
     */
171
    protected function generateSeparateEnvironmentFileForTestbenchDusk(Filesystem $filesystem, string $workbenchWorkingPath, string $targetEnvironmentFile): void
172
    {
173
        if ($this->hasTestbenchDusk === false) {
174
            return;
175
        }
176

177
        if ($this->components->confirm('Create separate environment file for Testbench Dusk?', false)) {
178
            (new GeneratesFile(
179
                filesystem: $filesystem,
180
                components: $this->components,
181
                force: (bool) $this->option('force'),
182
            ))->handle(
183
                $this->laravel->basePath('.env.example'),
184
                join_paths($workbenchWorkingPath, str_replace('.env', '.env.dusk', $targetEnvironmentFile))
185
            );
186
        }
187
    }
188

189
    /**
190
     * Get possible environment files.
191
     *
192
     * @return array<int, string>
193
     */
194
    protected function environmentFiles(): array
195
    {
196
        return [
26✔
197
            '.env',
26✔
198
            '.env.example',
26✔
199
            '.env.dist',
26✔
200
        ];
26✔
201
    }
202

203
    /**
204
     * Get the console command options.
205
     *
206
     * @return array
207
     */
208
    protected function getOptions()
209
    {
210
        return [
26✔
211
            ['force', 'f', InputOption::VALUE_NONE, 'Overwrite any existing files'],
26✔
212
            ['devtool', null, InputOption::VALUE_NEGATABLE, 'Run DevTool installation'],
26✔
213
            ['basic', null, InputOption::VALUE_NONE, 'Skipped routes and discovers installation'],
26✔
214

215
            /** @deprecated */
216
            ['skip-devtool', null, InputOption::VALUE_NONE, 'Skipped DevTool installation (deprecated)'],
26✔
217
        ];
26✔
218
    }
219
}
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