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

lmc-eu / steward / 11285721178

15 Aug 2024 03:15PM UTC coverage: 92.623%. Remained the same
11285721178

push

github

OndraM
Chore: Change Selenium version for tests to 4.23.0

1833 of 1979 relevant lines covered (92.62%)

20.88 hits per line

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

98.33
/src/Console/Command/InstallCommand.php
1
<?php declare(strict_types=1);
2

3
namespace Lmc\Steward\Console\Command;
4

5
use Lmc\Steward\Console\CommandEvents;
6
use Lmc\Steward\Console\Event\BasicConsoleEvent;
7
use Lmc\Steward\Exception\CommandException;
8
use Lmc\Steward\Selenium\Downloader;
9
use Lmc\Steward\Selenium\Version;
10
use Lmc\Steward\Selenium\VersionResolver;
11
use OndraM\CiDetector\CiDetector;
12
use Symfony\Component\Console\Input\InputArgument;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Output\OutputInterface;
15

16
/**
17
 * Install (download) Selenium standalone server.
18
 */
19
class InstallCommand extends Command
20
{
21
    /** @var Downloader */
22
    protected $downloader;
23
    /** @var VersionResolver */
24
    protected $versionResolver;
25

26
    /**
27
     * Target directory to store the selenium server (relatively to STEWARD_BASE_DIR)
28
     *
29
     * @var string
30
     */
31
    protected $targetDir = '/vendor/bin';
32

33
    /**
34
     * @internal
35
     */
36
    public function setDownloader(Downloader $downloader): void
37
    {
38
        $this->downloader = $downloader;
27✔
39
    }
27✔
40

41
    /**
42
     * @internal
43
     */
44
    public function setVersionResolver(VersionResolver $versionResolver): void
45
    {
46
        $this->versionResolver = $versionResolver;
15✔
47
    }
15✔
48

49
    /**
50
     * Configure command
51
     */
52
    protected function configure(): void
53
    {
54
        $this->setName('install')
30✔
55
            ->setDescription('Download Selenium standalone server jar file')
30✔
56
            ->addArgument(
30✔
57
                'version',
30✔
58
                InputArgument::OPTIONAL,
30✔
59
                'Specific Selenium version to install. If none provided, use the latest version.'
30✔
60
            );
61

62
        $this->getDispatcher()->dispatch(new BasicConsoleEvent($this), CommandEvents::CONFIGURE);
30✔
63
    }
30✔
64

65
    /**
66
     * In interactive or very verbose (-vv) mode provide more output, otherwise only output full path to selenium
67
     * server jar file (so it could be parsed and run).
68
     */
69
    protected function execute(InputInterface $input, OutputInterface $output): int
70
    {
71
        $verboseOutput = false;
30✔
72
        if ($input->isInteractive() || $output->isVerbose()) {
30✔
73
            $verboseOutput = true;
21✔
74
        }
75

76
        $versionInput = $input->getArgument('version'); // exact version could be specified as argument
30✔
77
        if (!$versionInput) {
30✔
78
            $versionInput = $this->askForVersion($input->isInteractive());
15✔
79
        }
80

81
        $version = Version::createFromString($versionInput);
27✔
82
        $downloader = $this->getDownloaderForVersion($version);
27✔
83
        $targetPath = realpath($downloader->getFilePath());
27✔
84

85
        if ($this->io->isVerbose()) {
27✔
86
            $this->io->note(sprintf('Download URL: %s', $downloader->getFileUrl()));
×
87
        }
88

89
        if ($downloader->isAlreadyDownloaded()) {
27✔
90
            if ($verboseOutput) {
6✔
91
                $this->io->note(
3✔
92
                    sprintf('File "%s" already exists - won\'t be downloaded again.', basename($targetPath))
3✔
93
                );
94

95
                $this->io->success('Path to file: ' . $targetPath);
3✔
96
                $this->printLinkToWiki();
3✔
97
            } else {
98
                $this->io->writeln($targetPath); // In non-verbose mode only output path to the file
3✔
99
            }
100

101
            return 0;
6✔
102
        }
103

104
        if ($verboseOutput) {
21✔
105
            $this->io->note(
15✔
106
                sprintf(
15✔
107
                    'Downloading Selenium standalone server version %s%s',
15✔
108
                    $version->toString(),
15✔
109
                    (!(new CiDetector())->isCiDetected() ? ' - just for you! ♥️' : '')
15✔
110
                )
111
            );
112

113
            $this->io->note('Downloading may take a while... (File size is over 20 MB.)');
15✔
114
        }
115

116
        $downloadedSize = $downloader->download();
21✔
117
        $downloadedFilePath = realpath($downloader->getFilePath());
21✔
118

119
        if ($verboseOutput) {
21✔
120
            $this->io->success(
15✔
121
                sprintf('Downloaded %d MB, file saved successfully.', round($downloadedSize / 1024 / 1024, 1))
15✔
122
            );
123
            $this->io->success('Path to file: ' . $downloadedFilePath);
15✔
124
            $this->printLinkToWiki();
15✔
125
        } else {
126
            $this->io->writeln($downloadedFilePath); // In non-verbose mode only output path to the file
6✔
127
        }
128

129
        return 0;
21✔
130
    }
131

132
    /**
133
     * @codeCoverageIgnore
134
     */
135
    protected function getDownloaderForVersion(Version $version): Downloader
136
    {
137
        if ($this->downloader === null) {
138
            $this->downloader = new Downloader(STEWARD_BASE_DIR . $this->targetDir, $version);
139
        }
140

141
        return $this->downloader;
142
    }
143

144
    /**
145
     * @codeCoverageIgnore
146
     */
147
    protected function getVersionResolver(): VersionResolver
148
    {
149
        if ($this->versionResolver === null) {
150
            $this->versionResolver = new VersionResolver();
151
        }
152

153
        return $this->versionResolver;
154
    }
155

156
    private function askForVersion(bool $isInteractiveInput): string
157
    {
158
        $questionText = 'Enter Selenium server version to install';
15✔
159
        $latestVersion = $this->getVersionResolver()->getLatestVersion();
15✔
160

161
        if ($latestVersion !== null) {
15✔
162
            return $this->io->ask($questionText, $latestVersion->toString());
9✔
163
        }
164

165
        // When latest version cannot be detected, the version must always be provided
166
        if (!$isInteractiveInput) { // we have nowhere to get the version number in non-interactive mode
6✔
167
            throw new CommandException('Auto-detection of latest Selenium version failed - version must be provided');
3✔
168
        }
169

170
        // In interactive mode version must specified
171
        return $this->io->ask($questionText, null, function ($answer) {
172
            if (empty($answer)) {
3✔
173
                throw new CommandException('Please provide version to download (latest version auto-detect failed)');
3✔
174
            }
175

176
            return $answer;
3✔
177
        });
3✔
178
    }
179

180
    private function printLinkToWiki(): void
181
    {
182
        $this->io->suggestion(
18✔
183
            'What now? Learn how to start the Selenium server: '
184
            . 'https://github.com/lmc-eu/steward/wiki/Selenium-server-&-browser-drivers'
18✔
185
        );
186
    }
18✔
187
}
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