add tests for getEnvironment

This commit is contained in:
Jamie Barton 2024-10-18 17:56:02 +01:00
parent b950572124
commit dd4b9153f6
3 changed files with 91 additions and 8 deletions

View File

@ -1 +0,0 @@
{"version":1,"defects":{"Dumbo\\Tests\\Helpers\\JWTTest::testExpiredToken":7},"times":{"Dumbo\\Tests\\CacheMiddlewareTest::testHeadersAreModifiedByMiddleware":0.005,"Dumbo\\Tests\\CacheMiddlewareTest::testReturns304WhenETagMatches":0,"Dumbo\\Tests\\CacheMiddlewareTest::testReturns304WhenIfModifiedSinceMatches":0,"Dumbo\\Tests\\CacheMiddlewareTest::testDoesNotCacheNonGetRequests":0,"Dumbo\\Tests\\CacheMiddlewareTest::testGeneratesCorrectETagForStrictMode":0,"Dumbo\\Tests\\Helpers\\BasicAuthTest::testSimpleBasicAuthSuccess":0,"Dumbo\\Tests\\Helpers\\BasicAuthTest::testSimpleBasicAuthFailure":0,"Dumbo\\Tests\\Helpers\\BasicAuthTest::testAdvancedBasicAuthWithUsersSuccess":0,"Dumbo\\Tests\\Helpers\\BasicAuthTest::testAdvancedBasicAuthWithVerifyUserSuccess":0,"Dumbo\\Tests\\Helpers\\BasicAuthTest::testAdvancedBasicAuthFailure":0,"Dumbo\\Tests\\Helpers\\BearerAuthTest::testSimpleBearerAuthSuccess":0,"Dumbo\\Tests\\Helpers\\BearerAuthTest::testSimpleBearerAuthFailure":0,"Dumbo\\Tests\\Helpers\\BearerAuthTest::testAdvancedBearerAuthWithTokensSuccess":0,"Dumbo\\Tests\\Helpers\\BearerAuthTest::testAdvancedBearerAuthWithVerifyTokenSuccess":0,"Dumbo\\Tests\\Helpers\\BearerAuthTest::testAdvancedBearerAuthFailure":0,"Dumbo\\Tests\\Helpers\\BearerAuthTest::testMissingAuthorizationHeader":0,"Dumbo\\Tests\\Helpers\\BearerAuthTest::testInvalidAuthorizationHeaderFormat":0,"Dumbo\\Tests\\Helpers\\BearerAuthTest::testCustomRealm":0,"Dumbo\\Tests\\Helpers\\JWTTest::testSignAndVerify":0,"Dumbo\\Tests\\Helpers\\JWTTest::testExpiredToken":0,"Dumbo\\Tests\\Helpers\\JWTTest::testInvalidSignature":0,"Dumbo\\Tests\\Helpers\\JWTTest::testDecode":0,"Dumbo\\Tests\\Helpers\\JWTTest::testInvalidTokenFormat":0,"Dumbo\\Tests\\Helpers\\LoggerTest::testLoggerInstance":0,"Dumbo\\Tests\\Helpers\\LoggerTest::testInvokeLogsIncomingAndOutgoingRequest":0.001,"Dumbo\\Tests\\Helpers\\RequestIdTest::testRequestIdMiddleware":0.001,"Dumbo\\Tests\\Helpers\\RequestIdTest::testRequestIdMiddlewareWithExistingHeader":0,"Dumbo\\Tests\\Helpers\\RequestIdTest::testNoRequestIdHeaderWhenMiddlewareNotAdded":0,"Dumbo\\Tests\\RequestWrapperTest::testPath":0,"Dumbo\\Tests\\RequestWrapperTest::testRoutePath":0,"Dumbo\\Tests\\RequestWrapperTest::testParam":0,"Dumbo\\Tests\\RequestWrapperTest::testQueries":0,"Dumbo\\Tests\\RequestWrapperTest::testQuery":0,"Dumbo\\Tests\\RequestWrapperTest::testBody":0,"Dumbo\\Tests\\RequestWrapperTest::testBodyFormUrlEncoded":0,"Dumbo\\Tests\\RequestWrapperTest::testMethod":0,"Dumbo\\Tests\\RequestWrapperTest::testHeaders":0,"Dumbo\\Tests\\RequestWrapperTest::testHeader":0,"Dumbo\\Tests\\RouterTest::testBasicRouting":0,"Dumbo\\Tests\\RouterTest::testNestedRoutes":0,"Dumbo\\Tests\\RouterTest::testMiddlewareApplication":0,"Dumbo\\Tests\\RouterTest::testPathPreparation":0,"Dumbo\\Tests\\RouterTest::testMethodNotAllowed":0,"Dumbo\\Tests\\RouterTest::testRouteNotFound":0,"Dumbo\\Tests\\RouterTest::testMultipleMiddleware":0}}

View File

@ -501,15 +501,19 @@ class Dumbo
/**
* Detect and set the current environment
*
* This method checks for the DUMBO_ENV environment variable and sets
* the appropriate environment. It also configures error reporting
* based on the detected environment.
* @param array $serverVars Array to use instead of $_SERVER (for testing)
* @param callable|null $getenvFunc Function to use instead of getenv (for testing)
*/
private function detectEnvironment(): void
{
public function detectEnvironment(
array $serverVars = null,
callable $getenvFunc = null
): void {
$serverVars = $serverVars ?? $_SERVER;
$getenvFunc = $getenvFunc ?? "getenv";
$env =
$_SERVER["DUMBO_ENV"] ??
(getenv("DUMBO_ENV") ?? self::ENV_DEVELOPMENT);
$serverVars["DUMBO_ENV"] ??
($getenvFunc("DUMBO_ENV") ?? self::ENV_DEVELOPMENT);
$this->environment = in_array($env, [
self::ENV_PRODUCTION,
self::ENV_DEVELOPMENT,

80
tests/DumboTest.php Normal file
View File

@ -0,0 +1,80 @@
<?php
use PHPUnit\Framework\TestCase;
use Dumbo\Dumbo;
class DumboTest extends TestCase
{
public function testEnvironmentDetection()
{
$testCases = [
// Test case: No environment set
[
"serverVars" => [],
"getenvFunc" => function () {
return false;
},
"expectedEnv" => Dumbo::ENV_DEVELOPMENT,
],
// Test case: Environment set in $_SERVER
[
"serverVars" => ["DUMBO_ENV" => "production"],
"getenvFunc" => function () {
return false;
},
"expectedEnv" => Dumbo::ENV_PRODUCTION,
],
// Test case: Environment set via getenv
[
"serverVars" => [],
"getenvFunc" => function () {
return "testing";
},
"expectedEnv" => Dumbo::ENV_TESTING,
],
// Test case: Invalid environment defaults to development
[
"serverVars" => ["DUMBO_ENV" => "invalid"],
"getenvFunc" => function () {
return false;
},
"expectedEnv" => Dumbo::ENV_DEVELOPMENT,
],
// Test case: $_SERVER takes precedence over getenv
[
"serverVars" => ["DUMBO_ENV" => "production"],
"getenvFunc" => function () {
return "development";
},
"expectedEnv" => Dumbo::ENV_PRODUCTION,
],
];
foreach ($testCases as $index => $testCase) {
$app = new Dumbo();
$app->detectEnvironment(
$testCase["serverVars"],
$testCase["getenvFunc"]
);
$this->assertEquals(
$testCase["expectedEnv"],
$app->getEnvironment(),
"Failed assertion for test case $index"
);
}
}
public function testErrorReportingConfiguration()
{
$app = new Dumbo();
$app->detectEnvironment(["DUMBO_ENV" => "production"]);
$this->assertEquals(0, error_reporting());
$this->assertEquals("0", ini_get("display_errors"));
$app = new Dumbo();
$app->detectEnvironment(["DUMBO_ENV" => "development"]);
$this->assertEquals(E_ALL, error_reporting());
$this->assertEquals("1", ini_get("display_errors"));
}
}