From 2b1ccb640671c501e730eb0e34eccdedd09a38b3 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Tue, 21 Apr 2020 11:53:19 +0200 Subject: [PATCH] Configure application timezone globally --- config/app.php | 1 + includes/application.php | 5 +-- .../ConfigureEnvironmentServiceProvider.php | 29 +++++++++++++++ ...onfigureEnvironmentServiceProviderTest.php | 35 +++++++++++++++++++ 4 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 src/Helpers/ConfigureEnvironmentServiceProvider.php create mode 100644 tests/Unit/Helpers/ConfigureEnvironmentServiceProviderTest.php diff --git a/config/app.php b/config/app.php index 8ede567e..f05a85d1 100644 --- a/config/app.php +++ b/config/app.php @@ -10,6 +10,7 @@ return [ \Engelsystem\Logger\LoggerServiceProvider::class, \Engelsystem\Exceptions\ExceptionsServiceProvider::class, \Engelsystem\Config\ConfigServiceProvider::class, + \Engelsystem\Helpers\ConfigureEnvironmentServiceProvider::class, // Request handling \Engelsystem\Http\UrlGeneratorServiceProvider::class, diff --git a/includes/application.php b/includes/application.php index b62b082e..94a02fd2 100644 --- a/includes/application.php +++ b/includes/application.php @@ -22,11 +22,8 @@ $app->bootstrap($appConfig); /** - * Configure application + * Configure environment */ -$timezone = $app->get('config')->get('timezone'); -ini_set('date.timezone', $timezone); - if (config('environment') == 'development') { $errorHandler = $app->get('error.handler'); $errorHandler->setEnvironment(Handler::ENV_DEVELOPMENT); diff --git a/src/Helpers/ConfigureEnvironmentServiceProvider.php b/src/Helpers/ConfigureEnvironmentServiceProvider.php new file mode 100644 index 00000000..9f244fc7 --- /dev/null +++ b/src/Helpers/ConfigureEnvironmentServiceProvider.php @@ -0,0 +1,29 @@ +app->get('config'); + + $timezone = new CarbonTimeZone($config->get('timezone')); + $this->setTimeZone($timezone); + } + + /** + * @param CarbonTimeZone $timeZone + * @codeCoverageIgnore + */ + protected function setTimeZone(CarbonTimeZone $timeZone) + { + ini_set('date.timezone', $timeZone); + date_default_timezone_set($timeZone); + } +} diff --git a/tests/Unit/Helpers/ConfigureEnvironmentServiceProviderTest.php b/tests/Unit/Helpers/ConfigureEnvironmentServiceProviderTest.php new file mode 100644 index 00000000..30313c33 --- /dev/null +++ b/tests/Unit/Helpers/ConfigureEnvironmentServiceProviderTest.php @@ -0,0 +1,35 @@ + 'Australia/Eucla']); + $this->app->instance('config', $config); + + /** @var ConfigureEnvironmentServiceProvider|MockObject $serviceProvider */ + $serviceProvider = $this->getMockBuilder(ConfigureEnvironmentServiceProvider::class) + ->setConstructorArgs([$this->app]) + ->onlyMethods(['setTimeZone']) + ->getMock(); + + $serviceProvider->expects($this->once()) + ->method('setTimeZone') + ->willReturnCallback(function (CarbonTimeZone $timeZone) { + $this->assertEquals('Australia/Eucla', $timeZone->getName()); + }); + + $serviceProvider->register(); + } +}