This commit is contained in:
2026-09-15 10:26:34 +08:00
commit ecf36afa56
10493 changed files with 1404888 additions and 0 deletions
@@ -0,0 +1,82 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\TestFramework\Bootstrap;
use Magento\TestFramework\Application;
/**
* Bootstrap of the custom DocBlock annotations
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class DocBlock
{
/**
* @var string
*/
protected $_fixturesBaseDir;
/**
* @param string $fixturesBaseDir
*/
public function __construct($fixturesBaseDir)
{
$this->_fixturesBaseDir = $fixturesBaseDir;
}
/**
* Activate custom DocBlock annotations along with more-or-less permanent workarounds
*
* @param Application $application
*/
public function registerAnnotations(Application $application)
{
$eventManager = new \Magento\TestFramework\EventManager($this->_getSubscribers($application));
\Magento\TestFramework\Event\PhpUnit::setDefaultEventManager($eventManager);
\Magento\TestFramework\Event\Magento::setDefaultEventManager($eventManager);
}
/**
* Get list of subscribers.
*
* Note: order of registering (and applying) annotations is important.
* To allow config fixtures to deal with fixture stores, data fixtures should be processed first.
* ConfigFixture applied twice because data fixtures could clean config and clean custom settings
*
* @param Application $application
* @return array
*/
protected function _getSubscribers(Application $application)
{
return [
new \Magento\TestFramework\Workaround\Segfault(),
new \Magento\TestFramework\Workaround\Cleanup\TestCaseProperties(),
new \Magento\TestFramework\Workaround\Cleanup\StaticProperties(),
new \Magento\TestFramework\Isolation\FlushDataFixtureStorage(),
new \Magento\TestFramework\Isolation\WorkingDirectory(),
new \Magento\TestFramework\Isolation\DeploymentConfig(),
new \Magento\TestFramework\Workaround\Override\Fixture\Resolver\TestSetter(),
new \Magento\TestFramework\Annotation\AppIsolation($application),
new \Magento\TestFramework\Annotation\IndexerDimensionMode(),
new \Magento\TestFramework\Isolation\AppConfig(),
new \Magento\TestFramework\Annotation\ConfigFixture(),
new \Magento\TestFramework\Annotation\DataFixtureBeforeTransaction(),
new \Magento\TestFramework\Event\Transaction(
new \Magento\TestFramework\EventManager(
[
new \Magento\TestFramework\Annotation\DbIsolation(),
new \Magento\TestFramework\Annotation\DataFixture(),
]
)
),
new \Magento\TestFramework\Annotation\ComponentRegistrarFixture($this->_fixturesBaseDir),
new \Magento\TestFramework\Annotation\AppArea($application),
new \Magento\TestFramework\Annotation\Cache(),
new \Magento\TestFramework\Annotation\AdminConfigFixture(),
new \Magento\TestFramework\Annotation\ConfigFixture(),
];
}
}
@@ -0,0 +1,39 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Bootstrap of the HTTP environment
*/
namespace Magento\TestFramework\Bootstrap;
class Environment
{
/**
* Emulate properties typical to an HTTP request
*
* @param array $serverVariables
*/
public function emulateHttpRequest(array &$serverVariables)
{
// emulate HTTP request
$serverVariables['HTTP_HOST'] = 'localhost';
// emulate entry point to ensure that tests generate invariant URLs
$serverVariables['SCRIPT_FILENAME'] = 'index.php';
}
/**
* Emulate already started PHP session
*
* @param array|null $sessionVariables
*/
public function emulateSession(&$sessionVariables)
{
// prevent session_start, because it may rely on cookies
$sessionVariables = [];
// application relies on a non-empty session ID
session_id(uniqid());
}
}
@@ -0,0 +1,68 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Bootstrap of the memory monitoring
*/
namespace Magento\TestFramework\Bootstrap;
class Memory
{
/**
* Policy to perform requested actions on shutdown
*/
const POLICY_SHUTDOWN = 'register_shutdown_function';
/**
* @var \Magento\TestFramework\MemoryLimit
*/
private $_memoryLimit;
/**
* @var callable
*/
private $_activationPolicy;
/**
* @param \Magento\TestFramework\MemoryLimit $memoryLimit
* @param callable|string $activationPolicy
* @throws \InvalidArgumentException
*/
public function __construct(
\Magento\TestFramework\MemoryLimit $memoryLimit,
$activationPolicy = self::POLICY_SHUTDOWN
) {
if (!is_callable($activationPolicy)) {
throw new \InvalidArgumentException('Activation policy is expected to be a callable.');
}
$this->_memoryLimit = $memoryLimit;
$this->_activationPolicy = $activationPolicy;
}
/**
* Display memory usage statistics
*/
public function displayStats()
{
echo $this->_memoryLimit->printHeader() . $this->_memoryLimit->printStats() . PHP_EOL;
}
/**
* Activate displaying of the memory usage statistics
*/
public function activateStatsDisplaying()
{
call_user_func($this->_activationPolicy, [$this, 'displayStats']);
}
/**
* Activate validation of the memory usage/leak limitations
*/
public function activateLimitValidation()
{
call_user_func($this->_activationPolicy, [$this->_memoryLimit, 'validateUsage']);
}
}
@@ -0,0 +1,39 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\TestFramework\Bootstrap;
class MemoryFactory
{
/**
* @var \Magento\Framework\Shell
*/
private $shell;
/**
* @param \Magento\Framework\Shell $shell
*/
public function __construct(\Magento\Framework\Shell $shell)
{
$this->shell = $shell;
}
/**
* @param string $memUsageLimit
* @param string $memLeakLimit
* @return Memory
*/
public function create($memUsageLimit, $memLeakLimit)
{
return new \Magento\TestFramework\Bootstrap\Memory(
new \Magento\TestFramework\MemoryLimit(
$memUsageLimit,
$memLeakLimit,
new \Magento\TestFramework\Helper\Memory($this->shell)
)
);
}
}
@@ -0,0 +1,77 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Bootstrap of the application profiler
*/
namespace Magento\TestFramework\Bootstrap;
class Profiler
{
/**
* Profiler driver instance
*
* @var \Magento\Framework\Profiler\Driver\Standard
*/
protected $_driver;
/**
* Whether a profiler driver has been already registered or not
*
* @var bool
*/
protected $_isDriverRegistered = false;
/**
* Constructor
*
* @param \Magento\Framework\Profiler\Driver\Standard $driver
*/
public function __construct(\Magento\Framework\Profiler\Driver\Standard $driver)
{
$this->_driver = $driver;
}
/**
* Register profiler driver to involve it into the results processing
*/
protected function _registerDriver()
{
if (!$this->_isDriverRegistered) {
$this->_isDriverRegistered = true;
\Magento\Framework\Profiler::add($this->_driver);
}
}
/**
* Register file-based profiling
*
* @param string $profilerOutputFile
*/
public function registerFileProfiler($profilerOutputFile)
{
$this->_registerDriver();
$this->_driver->registerOutput(
new \Magento\Framework\Profiler\Driver\Standard\Output\Csvfile(['filePath' => $profilerOutputFile])
);
}
/**
* Register profiler with Bamboo-friendly output format
*
* @param string $profilerOutputFile
* @param string $profilerMetricsFile
*/
public function registerBambooProfiler($profilerOutputFile, $profilerMetricsFile)
{
$this->_registerDriver();
$this->_driver->registerOutput(
new \Magento\TestFramework\Profiler\OutputBamboo(
['filePath' => $profilerOutputFile, 'metrics' => require $profilerMetricsFile]
)
);
}
}
@@ -0,0 +1,152 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\TestFramework\Bootstrap;
use Magento\Framework\Filesystem\Glob;
/**
* Convenient access to the bootstrap settings
*/
class Settings
{
/**
* Base directory to be used to resolve relative paths
*
* @var string
*/
private $_baseDir;
/**
* Key-value pairs of the settings
*
* @var array
*/
private $_settings = [];
/**
* Constructor
*
* @param string $baseDir
* @param array $settings
* @throws \InvalidArgumentException
*/
public function __construct($baseDir, array $settings)
{
if (!is_dir($baseDir)) {
throw new \InvalidArgumentException("Base path '{$baseDir}' has to be an existing directory.");
}
$this->_baseDir = realpath($baseDir);
$this->_settings = $settings;
}
/**
* Retrieve a setting value as is
*
* @param string $settingName
* @param mixed $defaultValue
* @return mixed
*/
public function get($settingName, $defaultValue = null)
{
return array_key_exists($settingName, $this->_settings) ? $this->_settings[$settingName] : $defaultValue;
}
/**
* Interpret a setting value as a switch and return TRUE when it equals to the string "enabled" or FALSE otherwise
*
* @param string $settingName
* @return bool
* @SuppressWarnings(PHPMD.BooleanGetMethodName)
*/
public function getAsBoolean($settingName)
{
return $this->get($settingName) === 'enabled';
}
/**
* Interpret a setting value as a relative file name and return absolute path to it
*
* @param string $settingName
* @param string $defaultValue
* @return string
*/
public function getAsFile($settingName, $defaultValue = '')
{
$result = $this->get($settingName, $defaultValue);
if ($result !== '') {
$result = $this->_resolvePath($result);
}
return $result;
}
/**
* Interpret a setting value as a file optionally falling back to the '.dist' file and return absolute path to it
*
* @param string $settingName
* @return string
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getAsConfigFile($settingName)
{
$result = $this->getAsFile($settingName);
if ($result !== '') {
if (!is_file($result) && substr($result, -5) != '.dist') {
$result .= '.dist';
}
if (is_file($result)) {
return $result;
}
}
throw new \Magento\Framework\Exception\LocalizedException(
__("Setting '%1' specifies the non-existing file '%2'.", $settingName, $result)
);
}
/**
* Interpret a setting value as a semicolon-separated relative glob pattern(s) and return matched absolute paths
*
* @param string $settingName
* @return array
*/
public function getAsMatchingPaths($settingName)
{
$settingValue = $this->get($settingName, '');
if ($settingValue !== '') {
return $this->_resolvePathPattern($settingValue);
}
return [];
}
/**
* Return an absolute path by a relative one without checking its validity
*
* @param string $relativePath
* @return string
*/
protected function _resolvePath($relativePath)
{
return $this->_baseDir . '/' . $relativePath;
}
/**
* Resolve semicolon-separated relative glob pattern(s) to matched absolute paths
*
* @param string $pattern
* @return array
*/
protected function _resolvePathPattern($pattern)
{
$result = [];
$allPatterns = preg_split('/\s*;\s*/', trim($pattern), -1, PREG_SPLIT_NO_EMPTY);
foreach ($allPatterns as $onePattern) {
$onePattern = $this->_resolvePath($onePattern);
$files = Glob::glob($onePattern, Glob::GLOB_BRACE);
$result = array_merge($result, $files);
}
return $result;
}
}