Skip to content

Commit 4ace691

Browse files
committed
Merge remote-tracking branch 'trigger/MC-32014-poc' into MC-34427
2 parents dcd805c + 4b7d58c commit 4ace691

File tree

13 files changed

+503
-63
lines changed

13 files changed

+503
-63
lines changed

dev/tests/integration/bin/magento

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
* See COPYING.txt for license details.
66
*/
77

8+
use Magento\Framework\Console\Cli;
9+
use Magento\TestFramework\Console\CliProxy;
10+
811
if (PHP_SAPI !== 'cli') {
912
echo 'bin/magento must be run as a CLI application';
1013
exit(1);
@@ -21,15 +24,20 @@ if (isset($_SERVER['INTEGRATION_TEST_PARAMS'])) {
2124
}
2225

2326
try {
24-
require $_SERVER['MAGE_DIRS']['base']['path'] . '/app/bootstrap.php';
27+
require $_SERVER['INTEGRATION_TESTS_CLI_AUTOLOADER'] ??
28+
($_SERVER['MAGE_DIRS']['base']['path'] . '/app/bootstrap.php');
2529
} catch (\Exception $e) {
2630
echo 'Autoload error: ' . $e->getMessage();
2731
exit(1);
2832
}
2933
try {
3034
$handler = new \Magento\Framework\App\ErrorHandler();
3135
set_error_handler([$handler, 'handler']);
32-
$application = new Magento\Framework\Console\Cli('Magento CLI');
36+
if (isset($_SERVER['INTEGRATION_TESTS_CLI_AUTOLOADER'])) {
37+
$application = new CliProxy('Magento CLI');
38+
} else {
39+
$application = new Cli('Magento CLI');
40+
}
3341
$application->run();
3442
} catch (\Exception $e) {
3543
while ($e) {
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\TestFramework\Console;
9+
10+
use Magento\Framework\Console\Cli;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\ObjectManagerInterface;
13+
use Symfony\Component\Console\Input\InputInterface;
14+
use Symfony\Component\Console\Output\OutputInterface;
15+
16+
/**
17+
* Provides the ability to inject additional DI configuration to call a CLI command
18+
*/
19+
class CliProxy implements \Magento\Framework\ObjectManager\NoninterceptableInterface
20+
{
21+
/**
22+
* @var Cli
23+
*/
24+
private $subject;
25+
26+
/**
27+
* @param string $name
28+
* @param string $version
29+
* @throws \ReflectionException
30+
* @throws LocalizedException
31+
*/
32+
public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN')
33+
{
34+
$this->subject = new Cli($name, $version);
35+
$this->injectDiConfiguration($this->subject);
36+
}
37+
38+
/**
39+
* Runs the current application.
40+
*
41+
* @see \Magento\Framework\Console\Cli::doRun
42+
* @param InputInterface $input
43+
* @param OutputInterface $output
44+
* @return int|null
45+
* @throws \Exception
46+
*/
47+
public function doRun(InputInterface $input, OutputInterface $output)
48+
{
49+
return $this->getSubject()->doRun($input, $output);
50+
}
51+
52+
/**
53+
* Runs the current application.
54+
*
55+
* @see \Symfony\Component\Console\Application::run
56+
* @param InputInterface|null $input
57+
* @param OutputInterface|null $output
58+
* @return int
59+
* @throws \Exception
60+
*/
61+
public function run(InputInterface $input = null, OutputInterface $output = null)
62+
{
63+
return $this->getSubject()->run($input, $output);
64+
}
65+
66+
/**
67+
* Get subject
68+
*
69+
* @return Cli
70+
*/
71+
private function getSubject(): Cli
72+
{
73+
return $this->subject;
74+
}
75+
76+
/**
77+
* Inject additional DI configuration
78+
*
79+
* @param Cli $cli
80+
* @return bool
81+
* @throws LocalizedException
82+
* @throws \ReflectionException
83+
*/
84+
private function injectDiConfiguration(Cli $cli): bool
85+
{
86+
$diPreferences = $this->getDiPreferences();
87+
if ($diPreferences) {
88+
$object = new \ReflectionObject($cli);
89+
90+
$attribute = $object->getProperty('objectManager');
91+
$attribute->setAccessible(true);
92+
93+
/** @var ObjectManagerInterface $objectManager */
94+
$objectManager = $attribute->getValue($cli);
95+
$objectManager->configure($diPreferences);
96+
97+
$attribute->setAccessible(false);
98+
}
99+
100+
return true;
101+
}
102+
103+
/**
104+
* Get additional DI preferences
105+
*
106+
* @return array|array[]
107+
* @throws LocalizedException
108+
* @SuppressWarnings(PHPMD.Superglobals)
109+
*/
110+
private function getDiPreferences(): array
111+
{
112+
$diPreferences = [];
113+
$diPreferencesPath = $_SERVER['TESTS_BASE_DIR'] . '/etc/di/preferences/cli/';
114+
115+
$preferenceFiles = glob($diPreferencesPath . '*.php');
116+
117+
foreach ($preferenceFiles as $file) {
118+
if (!is_readable($file)) {
119+
throw new LocalizedException(__("'%1' is not readable file.", $file));
120+
}
121+
$diPreferences = array_replace($diPreferences, include $file);
122+
}
123+
124+
return $diPreferences ? ['preferences' => $diPreferences] : $diPreferences;
125+
}
126+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
return [
8+
'\Magento\Framework\Mview\TriggerCleaner' => '\Magento\TestFramework\Mview\DummyTriggerCleaner',
9+
];

dev/tests/setup-integration/framework/Magento/TestFramework/Deploy/CliCommand.php

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@
66
namespace Magento\TestFramework\Deploy;
77

88
use Magento\Framework\App\DeploymentConfig;
9+
use Magento\Framework\Exception\LocalizedException;
910
use Magento\Framework\Shell;
1011
use Magento\Framework\Shell\CommandRenderer;
1112
use Magento\Setup\Console\Command\InstallCommand;
1213

1314
/**
1415
* The purpose of this class is enable/disable module and upgrade commands execution.
16+
*
17+
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
1518
*/
1619
class CliCommand
1720
{
1821
/**
19-
* @var \Magento\Framework\Shell
22+
* @var Shell
2023
*/
2124
private $shell;
2225

@@ -36,10 +39,7 @@ class CliCommand
3639
private $deploymentConfig;
3740

3841
/**
39-
* ShellCommand constructor.
40-
*
41-
* @param TestModuleManager $testEnv
42-
* @param DeploymentConfig $deploymentConfig
42+
* @param TestModuleManager $testEnv
4343
* @internal param Shell $shell
4444
*/
4545
public function __construct(
@@ -53,8 +53,9 @@ public function __construct(
5353
/**
5454
* Copy Test module files and execute enable module command.
5555
*
56-
* @param string $moduleName
56+
* @param string $moduleName
5757
* @return string
58+
* @throws LocalizedException
5859
*/
5960
public function introduceModule($moduleName)
6061
{
@@ -65,13 +66,14 @@ public function introduceModule($moduleName)
6566
/**
6667
* Execute enable module command.
6768
*
68-
* @param string $moduleName
69+
* @param string $moduleName
6970
* @return string
71+
* @throws LocalizedException
7072
*/
7173
public function enableModule($moduleName)
7274
{
7375
$initParams = $this->parametersHolder->getInitParams();
74-
$enableModuleCommand = 'php -f ' . BP . '/bin/magento module:enable ' . $moduleName
76+
$enableModuleCommand = $this->getCliScriptCommand() . ' module:enable ' . $moduleName
7577
. ' -n -vvv --magento-init-params="' . $initParams['magento-init-params'] . '"';
7678
return $this->shell->execute($enableModuleCommand);
7779
}
@@ -81,11 +83,12 @@ public function enableModule($moduleName)
8183
*
8284
* @param array $installParams
8385
* @return string
86+
* @throws LocalizedException
8487
*/
8588
public function upgrade($installParams = [])
8689
{
8790
$initParams = $this->parametersHolder->getInitParams();
88-
$upgradeCommand = 'php -f ' . BP . '/bin/magento setup:upgrade -vvv -n --magento-init-params="'
91+
$upgradeCommand = $this->getCliScriptCommandWithDI() . 'setup:upgrade -vvv -n --magento-init-params="'
8992
. $initParams['magento-init-params'] . '"';
9093
$installParams = $this->toCliArguments($installParams);
9194
$upgradeCommand .= ' ' . implode(" ", array_keys($installParams));
@@ -96,13 +99,14 @@ public function upgrade($installParams = [])
9699
/**
97100
* Execute disable module command.
98101
*
99-
* @param string $moduleName
102+
* @param string $moduleName
100103
* @return string
104+
* @throws LocalizedException
101105
*/
102106
public function disableModule($moduleName)
103107
{
104108
$initParams = $this->parametersHolder->getInitParams();
105-
$disableModuleCommand = 'php -f ' . BP . '/bin/magento module:disable '. $moduleName
109+
$disableModuleCommand = $this->getCliScriptCommand() . ' module:disable ' . $moduleName
106110
. ' -vvv --magento-init-params="' . $initParams['magento-init-params'] . '"';
107111
return $this->shell->execute($disableModuleCommand);
108112
}
@@ -111,14 +115,15 @@ public function disableModule($moduleName)
111115
* Split quote db configuration.
112116
*
113117
* @return void
118+
* @throws LocalizedException
114119
*/
115120
public function splitQuote()
116121
{
117122
$initParams = $this->parametersHolder->getInitParams();
118123
$installParams = $this->toCliArguments(
119124
$this->parametersHolder->getDbData('checkout')
120125
);
121-
$command = 'php -f ' . BP . '/bin/magento setup:db-schema:split-quote ' .
126+
$command = $this->getCliScriptCommand() . ' setup:db-schema:split-quote ' .
122127
implode(" ", array_keys($installParams)) .
123128
' -vvv --magento-init-params="' .
124129
$initParams['magento-init-params'] . '"';
@@ -130,14 +135,15 @@ public function splitQuote()
130135
* Split sales db configuration.
131136
*
132137
* @return void
138+
* @throws LocalizedException
133139
*/
134140
public function splitSales()
135141
{
136142
$initParams = $this->parametersHolder->getInitParams();
137143
$installParams = $this->toCliArguments(
138144
$this->parametersHolder->getDbData('sales')
139145
);
140-
$command = 'php -f ' . BP . '/bin/magento setup:db-schema:split-sales ' .
146+
$command = $this->getCliScriptCommand() . ' setup:db-schema:split-sales ' .
141147
implode(" ", array_keys($installParams)) .
142148
' -vvv --magento-init-params="' .
143149
$initParams['magento-init-params'] . '"';
@@ -151,7 +157,7 @@ public function splitSales()
151157
public function cacheClean()
152158
{
153159
$initParams = $this->parametersHolder->getInitParams();
154-
$command = 'php -f ' . BP . '/bin/magento cache:clean ' .
160+
$command = $this->getCliScriptCommand() . ' cache:clean ' .
155161
' -vvv --magento-init-params=' .
156162
$initParams['magento-init-params'];
157163

@@ -162,11 +168,12 @@ public function cacheClean()
162168
* Uninstall module
163169
*
164170
* @param string $moduleName
171+
* @throws LocalizedException
165172
*/
166173
public function uninstallModule($moduleName)
167174
{
168175
$initParams = $this->parametersHolder->getInitParams();
169-
$command = 'php -f ' . BP . '/bin/magento module:uninstall ' . $moduleName . ' --remove-data ' .
176+
$command = $this->getCliScriptCommand() . ' module:uninstall ' . $moduleName . ' --remove-data ' .
170177
' -vvv --non-composer --magento-init-params="' .
171178
$initParams['magento-init-params'] . '"';
172179

@@ -240,4 +247,29 @@ public function afterInstall()
240247
->get(DeploymentConfig::class);
241248
$this->deploymentConfig->resetData();
242249
}
250+
251+
/**
252+
* Get custom magento-cli command with additional DI configuration
253+
*
254+
* @return string
255+
*/
256+
private function getCliScriptCommandWithDI(): string
257+
{
258+
$params['MAGE_DIRS']['base']['path'] = BP;
259+
$params['INTEGRATION_TESTS_CLI_AUTOLOADER'] = TESTS_BASE_DIR . '/framework/autoload.php';
260+
$params['TESTS_BASE_DIR'] = TESTS_BASE_DIR;
261+
return 'INTEGRATION_TEST_PARAMS="' . urldecode(http_build_query($params)) . '"'
262+
. ' ' . PHP_BINARY . ' -f ' . INTEGRATION_TESTS_BASE_DIR
263+
. '/bin/magento ';
264+
}
265+
266+
/**
267+
* Get basic magento-cli command
268+
*
269+
* @return string
270+
*/
271+
private function getCliScriptCommand()
272+
{
273+
return PHP_BINARY . ' -f ' . BP . '/bin/magento ';
274+
}
243275
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\TestFramework\Mview;
9+
10+
/**
11+
* Stub for \Magento\Framework\Mview\TriggerCleaner
12+
*/
13+
class DummyTriggerCleaner
14+
{
15+
/**
16+
* Remove the outdated trigger from the system
17+
*
18+
* @return bool
19+
*/
20+
public function removeTriggers(): bool
21+
{
22+
return true;
23+
}
24+
}

0 commit comments

Comments
 (0)