From b83fb5376e42570bf82bb943de7ec9466695ae45 Mon Sep 17 00:00:00 2001 From: Frugan Date: Thu, 4 Sep 2025 11:39:35 +0200 Subject: [PATCH] fix: add isPhpUnitRunning method --- .github/CONTRIBUTING.md | 28 ++++++++++--------- .github/FUNDING.yml | 2 +- .github/SECURITY.md | 2 +- .github/dependabot.yml | 4 +-- src/Environment.php | 62 +++++++++++++++++++++++++++++++++++++++-- 5 files changed, 79 insertions(+), 19 deletions(-) diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index cb83e60..da09c07 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -1,6 +1,10 @@ -# Contributing to WP Env +# Contributing -Thank you for considering contributing to WP Env! This document outlines the process for contributing to this project. +Thank you for considering contributing! This document outlines the process for contributing to this project. + +When contributing to this repository, please first discuss the change you wish to make via issue, email, or any other method with the owners of this repository before making a change. + +Please note we have a [code of conduct](CODE_OF_CONDUCT.md), please follow it in all your interactions with the project. ## Development Workflow @@ -54,13 +58,13 @@ vendor/bin/phpunit --coverage-html coverage/ ### Code Quality ```bash # Run all quality checks -composer qa +composer quality # Individual tools -composer lint # PHP CS Fixer + PHPStan + Rector -composer static-analysis # PHPStan only +composer lint # Linters +composer analysis # Static analysis composer security # Security check -composer code-quality # PHPMND + PHP Parser +composer quality # Code quality ``` ## Coding Standards @@ -105,7 +109,7 @@ test(hooks): add comprehensive hook testing declare(strict_types=1); -namespace WpSpaghetti\WpEnv; +namespace WpSpaghetti\ExampleNamespace; /** * Class documentation. @@ -135,16 +139,14 @@ class ExampleClass ```php public function testMethodReturnsExpectedValue(): void { - $result = Environment::get('TEST_VAR', 'default'); + $result = ExampleClass::get('TEST_VAR', 'default'); self::assertSame('expected', $result); } ``` ### Mock Data Use the provided mock functions for testing: -- `set_mock_constant()` -- `set_mock_env_var()` -- `set_mock_file()` +- `set_mock_*()` ## Documentation @@ -175,8 +177,8 @@ When adding new features: 3. **Test Your Changes** ```bash - composer qa # Run all quality checks - composer test # Run all tests + composer quality # Run all quality checks + composer test # Run all tests ``` 4. **Commit Changes** diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml index 63f3ae2..fbca419 100644 --- a/.github/FUNDING.yml +++ b/.github/FUNDING.yml @@ -1,4 +1,4 @@ # These are supported funding model platforms -github: [wp-spaghetti] +github: [frugan-dev] buy_me_a_coffee: frugan diff --git a/.github/SECURITY.md b/.github/SECURITY.md index 21acf8f..3964a7d 100644 --- a/.github/SECURITY.md +++ b/.github/SECURITY.md @@ -2,7 +2,7 @@ ## Supported Versions -We actively support the following versions of WP Env with security updates: +We actively support the following versions with security updates: | Version | Supported | | ------- | ------------------ | diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 7e8fbfd..eb3afb2 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -9,7 +9,7 @@ updates: time: "09:00" open-pull-requests-limit: 10 assignees: - - "wp-spaghetti" + - "frugan-dev" commit-message: prefix: "deps" include: "scope" @@ -25,7 +25,7 @@ updates: time: "09:00" open-pull-requests-limit: 5 assignees: - - "wp-spaghetti" + - "frugan-dev" commit-message: prefix: "ci" include: "scope" diff --git a/src/Environment.php b/src/Environment.php index d93da4a..34a5772 100644 --- a/src/Environment.php +++ b/src/Environment.php @@ -419,8 +419,8 @@ public static function isProduction(): bool */ public static function isTesting(): bool { - // First check PHPUnit runtime (not affected by mocks) - if (\defined('PHPUNIT_COMPOSER_INSTALL') || class_exists(TestCase::class)) { + // Check if PHPUnit is actually running (not just installed) + if (self::isPhpUnitRunning()) { return true; } @@ -723,4 +723,62 @@ private static function detectEnvironmentByIndicators(): string // Production is the safe default return self::ENV_PRODUCTION; } + + /** + * Check if PHPUnit is actually running (not just installed). + */ + private static function isPhpUnitRunning(): bool + { + // Check for PHPUnit-specific runtime indicators + if (\defined('PHPUNIT_COMPOSER_INSTALL')) { + return true; + } + + // Check if PHPUnit configuration file is loaded + if (isset($GLOBALS['__PHPUNIT_CONFIGURATION_FILE'])) { + return true; + } + + // Check for PHPUnit result cache (set during execution) + if (isset($_ENV['PHPUNIT_RESULT_CACHE'])) { + return true; + } + + // Check if we're in PHPUnit process by examining the call stack + if (\function_exists('debug_backtrace')) { + $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 15); + foreach ($backtrace as $frame) { + if (isset($frame['file']) && str_contains($frame['file'], '/phpunit')) { + return true; + } + + if (isset($frame['class']) && str_starts_with($frame['class'], 'PHPUnit\\')) { + return true; + } + } + } + + // Check for specific PHPUnit environment variables + $phpunitVars = [ + 'PHPUNIT_RESULT_CACHE', + 'PHPUNIT_VERSION', + 'PHPUNIT_RUNNING', + ]; + + foreach ($phpunitVars as $phpunitVar) { + if (!empty($_ENV[$phpunitVar]) || !empty($_SERVER[$phpunitVar])) { + return true; + } + } + + // Last resort: check if TestCase class exists AND we can find PHPUnit-specific globals + if (class_exists(TestCase::class) + && (isset($GLOBALS['__PHPUNIT_PHAR__']) + || isset($GLOBALS['__PHPUNIT_ISOLATION_BLACKLIST__']) + || isset($GLOBALS['__PHPUNIT_BOOTSTRAP__']))) { + return true; + } + + return false; + } }