From dd4b9153f6f2024cebf9f6d9047b0b361bfcc4ec Mon Sep 17 00:00:00 2001 From: Jamie Barton Date: Fri, 18 Oct 2024 17:56:02 +0100 Subject: [PATCH] add tests for getEnvironment --- .phpunit.result.cache | 1 - src/Dumbo.php | 18 ++++++---- tests/DumboTest.php | 80 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 8 deletions(-) delete mode 100644 .phpunit.result.cache create mode 100644 tests/DumboTest.php diff --git a/.phpunit.result.cache b/.phpunit.result.cache deleted file mode 100644 index 3b2fde7..0000000 --- a/.phpunit.result.cache +++ /dev/null @@ -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}} \ No newline at end of file diff --git a/src/Dumbo.php b/src/Dumbo.php index 43a397c..b92a83e 100644 --- a/src/Dumbo.php +++ b/src/Dumbo.php @@ -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, diff --git a/tests/DumboTest.php b/tests/DumboTest.php new file mode 100644 index 0000000..35b32a9 --- /dev/null +++ b/tests/DumboTest.php @@ -0,0 +1,80 @@ + [], + "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")); + } +}