Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -105,7 +109,7 @@ test(hooks): add comprehensive hook testing

declare(strict_types=1);

namespace WpSpaghetti\WpEnv;
namespace WpSpaghetti\ExampleNamespace;

/**
* Class documentation.
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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**
Expand Down
2 changes: 1 addition & 1 deletion .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# These are supported funding model platforms

github: [wp-spaghetti]
github: [frugan-dev]
buy_me_a_coffee: frugan
2 changes: 1 addition & 1 deletion .github/SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
| ------- | ------------------ |
Expand Down
4 changes: 2 additions & 2 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ updates:
time: "09:00"
open-pull-requests-limit: 10
assignees:
- "wp-spaghetti"
- "frugan-dev"
commit-message:
prefix: "deps"
include: "scope"
Expand All @@ -25,7 +25,7 @@ updates:
time: "09:00"
open-pull-requests-limit: 5
assignees:
- "wp-spaghetti"
- "frugan-dev"
commit-message:
prefix: "ci"
include: "scope"
Expand Down
62 changes: 60 additions & 2 deletions src/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
}