forked from magento/magento2-functional-testing-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoctorCommand.php
210 lines (185 loc) · 6.62 KB
/
DoctorCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types = 1);
namespace Magento\FunctionalTestingFramework\Console;
use Codeception\Configuration;
use Magento\FunctionalTestingFramework\Util\Path\UrlFormatter;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Codeception\SuiteManager;
use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig;
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Magento\FunctionalTestingFramework\Util\ModuleResolver;
use Magento\FunctionalTestingFramework\Module\MagentoWebDriver;
use Magento\FunctionalTestingFramework\Module\MagentoWebDriverDoctor;
use Symfony\Component\Console\Style\SymfonyStyle;
class DoctorCommand extends Command
{
const CODECEPTION_AUTOLOAD_FILE = PROJECT_ROOT . '/vendor/codeception/codeception/autoload.php';
const MFTF_CODECEPTION_CONFIG_FILE = ENV_FILE_PATH . 'codeception.yml';
const SUITE = 'functional';
/**
* Console output style
*
* @var SymfonyStyle
*/
private $ioStyle;
/**
* Exception Context
*
* @var array
*/
private $context = [];
/**
* Configures the current command.
*
* @return void
*/
protected function configure()
{
$this->setName('doctor')
->setDescription(
'This command checks environment readiness for generating and running MFTF tests.'
);
}
/**
* Executes the current command.
*
* @param InputInterface $input
* @param OutputInterface $output
* @return integer
* @throws TestFrameworkException
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
// For output style
$this->ioStyle = new SymfonyStyle($input, $output);
$cmdStatus = true;
// Config application
$verbose = $output->isVerbose();
MftfApplicationConfig::create(
false,
MftfApplicationConfig::GENERATION_PHASE,
$verbose,
MftfApplicationConfig::LEVEL_DEVELOPER,
false
);
// Check authentication to Magento Admin
$status = $this->checkAuthenticationToMagentoAdmin();
$cmdStatus = $cmdStatus && !$status ? false : $cmdStatus;
// Check connection to Selenium
$status = $this->checkContextOnStep(
MagentoWebDriverDoctor::EXCEPTION_CONTEXT_SELENIUM,
'Connecting to Selenium Server'
);
$cmdStatus = $cmdStatus && !$status ? false : $cmdStatus;
// Check opening Magento Admin in web browser
$status = $this->checkContextOnStep(
MagentoWebDriverDoctor::EXCEPTION_CONTEXT_ADMIN,
'Loading Admin page'
);
$cmdStatus = $cmdStatus && !$status ? false : $cmdStatus;
// Check opening Magento Storefront in web browser
$status = $this->checkContextOnStep(
MagentoWebDriverDoctor::EXCEPTION_CONTEXT_STOREFRONT,
'Loading Storefront page'
);
$cmdStatus = $cmdStatus && !$status ? false : $cmdStatus;
// Check access to Magento CLI
$status = $this->checkContextOnStep(
MagentoWebDriverDoctor::EXCEPTION_CONTEXT_CLI,
'Running Magento CLI'
);
$cmdStatus = $cmdStatus && !$status ? false : $cmdStatus;
return $cmdStatus ? 0 : 1;
}
/**
* Check admin account authentication
*
* @return boolean
*/
private function checkAuthenticationToMagentoAdmin()
{
$result = false;
try {
$this->ioStyle->text("Requesting API token for admin user through cURL ...");
ModuleResolver::getInstance()->getAdminToken();
$this->ioStyle->success('Successful');
$result = true;
} catch (TestFrameworkException $e) {
if (getenv('MAGENTO_BACKEND_BASE_URL')) {
$urlVar = 'MAGENTO_BACKEND_BASE_URL';
} else {
$urlVar = 'MAGENTO_BASE_URL';
}
$this->ioStyle->error(
$e->getMessage() . "\nPlease verify if " . $urlVar . ", "
. "MAGENTO_ADMIN_USERNAME and MAGENTO_ADMIN_PASSWORD in .env are valid."
);
}
return $result;
}
/**
* Check exception context after runMagentoWebDriverDoctor
*
* @param string $exceptionType
* @param string $message
* @return boolean
* @throws TestFrameworkException
*/
private function checkContextOnStep($exceptionType, $message)
{
$this->ioStyle->text($message . ' ...');
$this->runMagentoWebDriverDoctor();
if (isset($this->context[$exceptionType])) {
$this->ioStyle->error($this->context[$exceptionType]);
return false;
} else {
$this->ioStyle->success('Successful');
return true;
}
}
/**
* Run diagnose through MagentoWebDriverDoctor
*
* @return void
* @throws TestFrameworkException
*/
private function runMagentoWebDriverDoctor()
{
if (!empty($this->context)) {
return;
}
$magentoWebDriver = '\\' . MagentoWebDriver::class;
$magentoWebDriverDoctor = '\\' . MagentoWebDriverDoctor::class;
require_once realpath(self::CODECEPTION_AUTOLOAD_FILE);
$config = Configuration::config(realpath(self::MFTF_CODECEPTION_CONFIG_FILE));
$settings = Configuration::suiteSettings(self::SUITE, $config);
// Enable MagentoWebDriverDoctor
$settings['modules']['enabled'][] = $magentoWebDriverDoctor;
$settings['modules']['config'][$magentoWebDriverDoctor] =
$settings['modules']['config'][$magentoWebDriver];
// Disable MagentoWebDriver to avoid conflicts
foreach ($settings['modules']['enabled'] as $index => $module) {
if ($module == $magentoWebDriver) {
unset($settings['modules']['enabled'][$index]);
break;
}
}
unset($settings['modules']['config'][$magentoWebDriver]);
$dispatcher = new EventDispatcher();
$suiteManager = new SuiteManager($dispatcher, self::SUITE, $settings);
try {
$suiteManager->initialize();
$this->context = ['Successful'];
} catch (TestFrameworkException $e) {
$this->context = $e->getContext();
}
}
}