Configure application timezone globally

main
Igor Scheller 5 years ago committed by msquare
parent e2e2ac0c68
commit 2b1ccb6406

@ -10,6 +10,7 @@ return [
\Engelsystem\Logger\LoggerServiceProvider::class, \Engelsystem\Logger\LoggerServiceProvider::class,
\Engelsystem\Exceptions\ExceptionsServiceProvider::class, \Engelsystem\Exceptions\ExceptionsServiceProvider::class,
\Engelsystem\Config\ConfigServiceProvider::class, \Engelsystem\Config\ConfigServiceProvider::class,
\Engelsystem\Helpers\ConfigureEnvironmentServiceProvider::class,
// Request handling // Request handling
\Engelsystem\Http\UrlGeneratorServiceProvider::class, \Engelsystem\Http\UrlGeneratorServiceProvider::class,

@ -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') { if (config('environment') == 'development') {
$errorHandler = $app->get('error.handler'); $errorHandler = $app->get('error.handler');
$errorHandler->setEnvironment(Handler::ENV_DEVELOPMENT); $errorHandler->setEnvironment(Handler::ENV_DEVELOPMENT);

@ -0,0 +1,29 @@
<?php
namespace Engelsystem\Helpers;
use Carbon\CarbonTimeZone;
use Engelsystem\Config\Config;
use Engelsystem\Container\ServiceProvider;
class ConfigureEnvironmentServiceProvider extends ServiceProvider
{
public function register()
{
/** @var Config $config */
$config = $this->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);
}
}

@ -0,0 +1,35 @@
<?php
namespace Engelsystem\Test\Unit\Helpers;
use Carbon\CarbonTimeZone;
use Engelsystem\Config\Config;
use Engelsystem\Helpers\ConfigureEnvironmentServiceProvider;
use Engelsystem\Test\Unit\ServiceProviderTest;
use PHPUnit\Framework\MockObject\MockObject;
class ConfigureEnvironmentServiceProviderTest extends ServiceProviderTest
{
/**
* @covers \Engelsystem\Helpers\ConfigureEnvironmentServiceProvider::register
*/
public function testRegister()
{
$config = new Config(['timezone' => '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();
}
}
Loading…
Cancel
Save