Skip to content

Commit 23515df

Browse files
committed
feat: Melhora a verificação do ambiente de testes na classe MetricsCollector e atualiza a lógica de registro de falhas de consulta
1 parent 3bb6e2e commit 23515df

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

src/Helpers/EnvironmentHelper.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,31 @@ public static function isDevelopment(): bool
2222

2323
/**
2424
* Verifica se o ambiente é de testes.
25+
* Considera múltiplas fontes: APP_ENV, PHPUnit e variáveis de servidor.
2526
*/
2627
public static function isTesting(): bool
2728
{
28-
return in_array(env('APP_ENV', ''), ['testing', 'test'], true);
29+
// Check APP_ENV from multiple sources
30+
$envSources = [
31+
$_ENV['APP_ENV'] ?? '',
32+
$_SERVER['APP_ENV'] ?? '',
33+
];
34+
35+
// Add env() function result if available
36+
if (function_exists('env')) {
37+
$envSources[] = env('APP_ENV', '');
38+
}
39+
40+
foreach ($envSources as $envValue) {
41+
if (in_array($envValue, ['testing', 'test'], true)) {
42+
return true;
43+
}
44+
}
45+
46+
// Check if running under PHPUnit
47+
return defined('PHPUNIT_RUNNING') ||
48+
(isset($_ENV['PHPUNIT_RUNNING']) && $_ENV['PHPUNIT_RUNNING']) ||
49+
(isset($_SERVER['PHPUNIT_RUNNING']) && $_SERVER['PHPUNIT_RUNNING']);
2950
}
3051

3152
/**

src/Monitoring/MetricsCollector.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace PivotPHP\CycleORM\Monitoring;
44

5+
use PivotPHP\CycleORM\Helpers\EnvironmentHelper;
6+
57
/**
68
* Coletor de métricas para Cycle ORM.
79
*/
@@ -78,13 +80,7 @@ public static function recordQueryFailure(string $query): void
7880
self::$metrics['queries_failed']++;
7981

8082
// Only log to error_log if not in testing environment
81-
$isTestingEnv = (
82-
(isset($_ENV['APP_ENV']) && $_ENV['APP_ENV'] === 'testing') ||
83-
(isset($_SERVER['APP_ENV']) && $_SERVER['APP_ENV'] === 'testing') ||
84-
defined('PHPUNIT_RUNNING')
85-
);
86-
87-
if (!$isTestingEnv) {
83+
if (!EnvironmentHelper::isTesting()) {
8884
error_log('Cycle ORM Query Failed: Query: ' . substr($query, 0, 100));
8985
}
9086
}

0 commit comments

Comments
 (0)