⚝
One Hat Cyber Team
⚝
Your IP:
216.73.216.45
Server IP:
185.238.29.86
Server:
Linux server2 6.8.12-6-pve #1 SMP PREEMPT_DYNAMIC PMX 6.8.12-6 (2024-12-19T19:05Z) x86_64
Server Software:
nginx/1.18.0
PHP Version:
8.1.31
Buat File
|
Buat Folder
Eksekusi
Dir :
~
/
var
/
www
/
shopware
/
vendor
/
shopware
/
core
/
Test
/
View File Name :
FeatureFlagExtension.php
<?php declare(strict_types=1); namespace Shopware\Core\Test; use Doctrine\Common\Annotations\AnnotationReader; use PHPUnit\Runner\AfterTestHook; use PHPUnit\Runner\BeforeTestHook; use Shopware\Core\Framework\Feature; use Shopware\Core\Test\Annotation\DisabledFeatures; /** * @internal * * This extension guarantees a clean feature environment for pure unit tests */ class FeatureFlagExtension implements BeforeTestHook, AfterTestHook { private AnnotationReader $annotationReader; private string $namespacePrefix; /** * @var array<mixed>|null */ private ?array $savedFeatureConfig = null; /** * @var array<mixed>|null */ private ?array $savedServerVars = null; private bool $testMode; public function __construct(string $namespacePrefix = 'Shopware\\Tests\\', bool $testMode = false) { $this->annotationReader = new AnnotationReader(); $this->namespacePrefix = $namespacePrefix; $this->testMode = $testMode; } public function executeBeforeTest(string $test): void { preg_match('/([^:]+)::([^$ ]+)($| )/', $test, $matches); if (empty($matches)) { debug_print_backtrace(5); } $class = $matches[1]; $method = $matches[2]; // do not run when this class is unit tested if (!$this->testMode && $class === 'Shopware\Tests\Unit\Core\Test\FeatureFlagExtensionTest') { // @codeCoverageIgnoreStart return; // @codeCoverageIgnoreEnd } $reflectedMethod = new \ReflectionMethod($class, $method); /** @var DisabledFeatures[] $features */ $features = array_filter([ $this->annotationReader->getMethodAnnotation($reflectedMethod, DisabledFeatures::class) ?? [], $this->annotationReader->getClassAnnotation($reflectedMethod->getDeclaringClass(), DisabledFeatures::class) ?? [], ]); $this->savedFeatureConfig = null; if (!str_starts_with($class, $this->namespacePrefix)) { return; } $this->savedFeatureConfig = Feature::getRegisteredFeatures(); $this->savedServerVars = $_SERVER; Feature::resetRegisteredFeatures(); foreach ($_SERVER as $key => $value) { if (str_starts_with($key, 'v6.') || $key === 'PERFORMANCE_TWEAKS' || str_starts_with($key, 'FEATURE_') || str_starts_with($key, 'V6_')) { // set to false so that $_ENV is not checked $_SERVER[$key] = false; } } $disabledFlags = []; foreach ($features as $feature) { foreach ($feature->features as $featureName) { $disabledFlags[Feature::normalizeName($featureName)] = true; } } foreach ($this->savedFeatureConfig as $flag => $config) { $flag = Feature::normalizeName($flag); $_SERVER[$flag] = !\array_key_exists($flag, $disabledFlags); } } public function executeAfterTest(string $test, float $time): void { preg_match('/([^:]+)::([^$ ]+)($| )/', $test, $matches); $class = $matches[1]; // do not run when this class is unit tested if (!$this->testMode && $class === 'Shopware\Tests\Unit\Core\Test\FeatureFlagExtensionTest') { // @codeCoverageIgnoreStart return; // @codeCoverageIgnoreEnd } if ($this->savedFeatureConfig === null) { return; } $_SERVER = $this->savedServerVars; Feature::resetRegisteredFeatures(); Feature::registerFeatures($this->savedFeatureConfig); } }