EventConfig: Merge event configuration from database to global config

main
Igor Scheller 6 years ago committed by msquare
parent 63d1292bf8
commit 7f61dc95be

@ -1,5 +1,8 @@
<?php
use Carbon\Carbon;
use Engelsystem\Models\EventConfig;
/**
* @return string
*/
@ -20,22 +23,17 @@ function event_config_edit_controller()
}
$request = request();
$event_name = null;
$event_welcome_msg = null;
$buildup_start_date = null;
$event_start_date = null;
$event_end_date = null;
$teardown_end_date = null;
$event_config = EventConfig();
if (!empty($event_config)) {
$event_name = $event_config['event_name'];
$buildup_start_date = $event_config['buildup_start_date'];
$event_start_date = $event_config['event_start_date'];
$event_end_date = $event_config['event_end_date'];
$teardown_end_date = $event_config['teardown_end_date'];
$event_welcome_msg = $event_config['event_welcome_msg'];
}
$config = config();
$event_name = $config->get('name');
$event_welcome_msg = $config->get('welcome_msg');
/** @var Carbon $buildup_start_date */
$buildup_start_date = $config->get('buildup_start');
/** @var Carbon $event_start_date */
$event_start_date = $config->get('event_start');
/** @var Carbon $event_end_date */
$event_end_date = $config->get('event_end');
/** @var Carbon $teardown_end_date */
$teardown_end_date = $config->get('teardown_end');
if ($request->has('submit')) {
$valid = true;
@ -91,24 +89,34 @@ function event_config_edit_controller()
}
if ($valid) {
EventConfig_update(
$event_name,
$buildup_start_date,
$event_start_date,
$event_end_date,
$teardown_end_date,
$event_welcome_msg
);
$eventConfig = new EventConfig();
foreach (
[
'name' => $event_name,
'welcome_msg' => $event_welcome_msg,
'buildup_start' => $buildup_start_date,
'event_start' => $event_start_date,
'event_end' => $event_end_date,
'teardown_end' => $teardown_end_date,
] as $key => $value
) {
$eventConfig
->findOrNew($key)
->setAttribute('name', $key)
->setAttribute('value', $value)
->save();
}
engelsystem_log(
sprintf(
'Changed event config: %s, %s, %s, %s, %s, %s',
$event_name,
$event_welcome_msg,
date('Y-m-d', $buildup_start_date),
date('Y-m-d', $event_start_date),
date('Y-m-d', $event_end_date),
date('Y-m-d', $teardown_end_date)
$buildup_start_date ? $buildup_start_date->format('Y-m-d') : '',
$event_start_date ? $event_start_date->format('Y-m-d') : '',
$event_end_date ? $event_end_date->format('Y-m-d') : '',
$teardown_end_date ? $teardown_end_date->format('Y-m-d') : ''
)
);
success(__('Settings saved.'));

@ -12,7 +12,6 @@ $includeFiles = [
__DIR__ . '/../includes/sys_template.php',
__DIR__ . '/../includes/model/AngelType_model.php',
__DIR__ . '/../includes/model/EventConfig_model.php',
__DIR__ . '/../includes/model/Message_model.php',
__DIR__ . '/../includes/model/NeededAngelTypes_model.php',
__DIR__ . '/../includes/model/Room_model.php',

@ -1,78 +0,0 @@
<?php
use Engelsystem\Database\DB;
/**
* Get event config.
*
* @return array|null
*/
function EventConfig()
{
$config = DB::selectOne('SELECT * FROM `EventConfig` LIMIT 1');
return empty($config) ? null : $config;
}
/**
* Update event config.
*
* @param string $event_name
* @param int $buildup_start_date
* @param int $event_start_date
* @param int $event_end_date
* @param int $teardown_end_date
* @param string $event_welcome_msg
* @return bool
*/
function EventConfig_update(
$event_name,
$buildup_start_date,
$event_start_date,
$event_end_date,
$teardown_end_date,
$event_welcome_msg
) {
$eventConfig = EventConfig();
if (empty($eventConfig)) {
return DB::insert('
INSERT INTO `EventConfig` (
`event_name`,
`buildup_start_date`,
`event_start_date`,
`event_end_date`,
`teardown_end_date`,
`event_welcome_msg`
)
VALUES (?, ?, ?, ?, ?, ?)
',
[
$event_name,
$buildup_start_date,
$event_start_date,
$event_end_date,
$teardown_end_date,
$event_welcome_msg
]
);
}
return (bool)DB::update('
UPDATE `EventConfig` SET
`event_name` = ?,
`buildup_start_date` = ?,
`event_start_date` = ?,
`event_end_date` = ?,
`teardown_end_date` = ?,
`event_welcome_msg` = ?
',
[
$event_name,
$buildup_start_date,
$event_start_date,
$event_end_date,
$teardown_end_date,
$event_welcome_msg,
]
);
}

@ -1,5 +1,6 @@
<?php
use Carbon\Carbon;
use Engelsystem\Database\Db;
/**
@ -128,10 +129,13 @@ function UserWorkLog_create($userWorkLog)
function UserWorkLog_new($user)
{
$work_date = parse_date('Y-m-d H:i', date('Y-m-d 00:00', time()));
$event_config = EventConfig();
if (!empty($event_config['buildup_start_date'])) {
$work_date = parse_date('Y-m-d H:i', date('Y-m-d 00:00', $event_config['buildup_start_date']));
/** @var Carbon $buildup */
$buildup = $buildup = config('buildup_start');
if (!empty($buildup)) {
$work_date = $buildup->format('Y-m-d H:i');
}
return [
'user_id' => $user['UID'],
'work_timestamp' => $work_date,

@ -1,5 +1,6 @@
<?php
use Carbon\Carbon;
use Engelsystem\Database\DB;
use Engelsystem\ValidationResult;
@ -359,19 +360,23 @@ function User_validate_planned_arrival_date($planned_arrival_date)
// null is not okay
return new ValidationResult(false, time());
}
$event_config = EventConfig();
if (empty($event_config)) {
// Nothing to validate against
return new ValidationResult(true, $planned_arrival_date);
}
if (isset($event_config['buildup_start_date']) && $planned_arrival_date < $event_config['buildup_start_date']) {
$config = config();
$buildup = $config->get('buildup_start');
$teardown = $config->get('teardown_end');
/** @var Carbon $buildup */
if (!empty($buildup) && $buildup->greaterThan(Carbon::createFromTimestamp($planned_arrival_date))) {
// Planned arrival can not be before buildup start date
return new ValidationResult(false, $event_config['buildup_start_date']);
return new ValidationResult(false, $buildup->getTimestamp());
}
if (isset($event_config['teardown_end_date']) && $planned_arrival_date > $event_config['teardown_end_date']) {
/** @var Carbon $teardown */
if (!empty($teardown) && $teardown->lessThan(Carbon::createFromTimestamp($planned_arrival_date))) {
// Planned arrival can not be after teardown end date
return new ValidationResult(false, $event_config['teardown_end_date']);
return new ValidationResult(false, $teardown->getTimestamp());
}
return new ValidationResult(true, $planned_arrival_date);
}
@ -388,23 +393,28 @@ function User_validate_planned_departure_date($planned_arrival_date, $planned_de
// null is okay
return new ValidationResult(true, null);
}
if ($planned_arrival_date > $planned_departure_date) {
// departure cannot be before arrival
return new ValidationResult(false, $planned_arrival_date);
}
$event_config = EventConfig();
if (empty($event_config)) {
// Nothing to validate against
return new ValidationResult(true, $planned_departure_date);
}
if (isset($event_config['buildup_start_date']) && $planned_departure_date < $event_config['buildup_start_date']) {
$config = config();
$buildup = $config->get('buildup_start');
$teardown = $config->get('teardown_end');
/** @var Carbon $buildup */
if (!empty($buildup) && $buildup->greaterThan(Carbon::createFromTimestamp($planned_departure_date))) {
// Planned arrival can not be before buildup start date
return new ValidationResult(false, $event_config['buildup_start_date']);
return new ValidationResult(false, $buildup->getTimestamp());
}
if (isset($event_config['teardown_end_date']) && $planned_departure_date > $event_config['teardown_end_date']) {
/** @var Carbon $teardown */
if (!empty($teardown) && $teardown->lessThan(Carbon::createFromTimestamp($planned_departure_date))) {
// Planned arrival can not be after teardown end date
return new ValidationResult(false, $event_config['teardown_end_date']);
return new ValidationResult(false, $teardown->getTimestamp());
}
return new ValidationResult(true, $planned_departure_date);
}

@ -1,5 +1,6 @@
<?php
use Carbon\Carbon;
use Engelsystem\Database\DB;
/**
@ -37,7 +38,7 @@ function guest_register()
$tshirt_sizes = config('tshirt_sizes');
$enable_tshirt_size = config('enable_tshirt_size');
$min_password_length = config('min_password_length');
$event_config = EventConfig();
$config = config();
$request = request();
$session = session();
@ -273,8 +274,8 @@ function guest_register()
}
// If a welcome message is present, display registration success page.
if (!empty($event_config) && !empty($event_config['event_welcome_msg'])) {
return User_registration_success_view($event_config['event_welcome_msg']);
if ($message = $config->get('welcome_msg')) {
return User_registration_success_view($message);
}
redirect(page_link_to('/'));
@ -283,13 +284,14 @@ function guest_register()
$buildup_start_date = time();
$teardown_end_date = null;
if (!empty($event_config)) {
if (isset($event_config['buildup_start_date'])) {
$buildup_start_date = $event_config['buildup_start_date'];
}
if (isset($event_config['teardown_end_date'])) {
$teardown_end_date = $event_config['teardown_end_date'];
if ($buildup = $config->get('buildup_start')) {
/** @var Carbon $buildup */
$buildup_start_date = $buildup->getTimestamp();
}
if ($teardown = $config->get('teardown_end')) {
/** @var Carbon $teardown */
$teardown_end_date = $teardown->getTimestamp();
}
return page_with_title(register_title(), [
@ -452,12 +454,10 @@ function guest_login()
}
}
$event_config = EventConfig();
return page([
div('col-md-12', [
div('row', [
EventConfig_countdown_page($event_config)
EventConfig_countdown_page()
]),
div('row', [
div('col-sm-6 col-sm-offset-3 col-md-4 col-md-offset-4', [

@ -1,5 +1,6 @@
<?php
use Carbon\Carbon;
use Engelsystem\Database\DB;
/**
@ -206,6 +207,7 @@ function user_settings()
{
global $user;
$request = request();
$config = config();
$themes = config('available_themes');
$enable_tshirt_size = config('enable_tshirt_size');
@ -214,14 +216,15 @@ function user_settings()
$buildup_start_date = null;
$teardown_end_date = null;
$event_config = EventConfig();
if (!empty($event_config)) {
if (isset($event_config['buildup_start_date'])) {
$buildup_start_date = $event_config['buildup_start_date'];
}
if (isset($event_config['teardown_end_date'])) {
$teardown_end_date = $event_config['teardown_end_date'];
if ($buildup = $config->get('buildup_start')) {
/** @var Carbon $buildup */
$buildup_start_date = $buildup->getTimestamp();
}
if ($teardown = $config->get('teardown_end')) {
/** @var Carbon $teardown */
$teardown_end_date = $teardown->getTimestamp();
}
$user_source = $user;

@ -1,5 +1,6 @@
<?php
// Methods to build a html form.
use Carbon\Carbon;
/**
* Renders a hidden input
@ -63,6 +64,7 @@ function form_spinner($name, $label, $value)
function form_date($name, $label, $value, $start_date = '', $end_date = '')
{
$dom_id = $name . '-date';
$value = ($value instanceof Carbon) ? $value->getTimestamp() : $value;
$value = is_numeric($value) ? date('Y-m-d', $value) : '';
$start_date = is_numeric($start_date) ? date('Y-m-d', $start_date) : '';
$end_date = is_numeric($end_date) ? date('Y-m-d', $end_date) : '';

@ -1,5 +1,6 @@
<?php
use Carbon\Carbon;
use Engelsystem\ValidationResult;
/**
@ -134,9 +135,16 @@ function check_request_date($name, $error_message = null, $null_allowed = false)
*/
function check_date($input, $error_message = null, $null_allowed = false)
{
if ($tmp = parse_date('Y-m-d H:i', trim($input) . ' 00:00')) {
return new ValidationResult(true, $tmp);
try {
$time = Carbon::createFromFormat('Y-m-d', trim($input));
} catch (InvalidArgumentException $e) {
$time = null;
}
if ($time) {
return new ValidationResult(true, $time);
}
if ($null_allowed) {
return new ValidationResult(true, null);
}

@ -1,59 +1,62 @@
<?php
use Carbon\Carbon;
/**
* Shows basic event infos and countdowns.
*
* @param array $event_config The event configuration
* @return string
*/
function EventConfig_countdown_page($event_config)
function EventConfig_countdown_page()
{
if (empty($event_config)) {
return div('col-md-12 text-center', [
heading(sprintf(__('Welcome to the %s!'), '<span class="icon-icon_angel"></span> ENGELSYSTEM'), 2)
]);
}
$config = config();
$name = $config->get('name', '');
/** @var Carbon $buildup */
$buildup = $config->get('buildup_start');
/** @var Carbon $start */
$start = $config->get('event_start');
/** @var Carbon $end */
$end = $config->get('event_end');
/** @var Carbon $teardown */
$teardown = $config->get('teardown_end');
$elements = [];
if (!is_null($event_config['event_name'])) {
$elements[] = div('col-sm-12 text-center', [
heading(sprintf(
__('Welcome to the %s!'),
$event_config['event_name'] . ' <span class="icon-icon_angel"></span> ENGELSYSTEM'
$name . ' <span class="icon-icon_angel"></span> ENGELSYSTEM'
), 2)
]);
}
if (!is_null($event_config['buildup_start_date']) && time() < $event_config['buildup_start_date']) {
if (!empty($buildup) && $buildup->greaterThan(new Carbon())) {
$elements[] = div('col-sm-3 text-center hidden-xs', [
heading(__('Buildup starts'), 4),
'<span class="moment-countdown text-big" data-timestamp="' . $event_config['buildup_start_date'] . '">%c</span>',
'<small>' . date(__('Y-m-d'), $event_config['buildup_start_date']) . '</small>'
'<span class="moment-countdown text-big" data-timestamp="' . $buildup->getTimestamp() . '">%c</span>',
'<small>' . $buildup->format(__('Y-m-d')) . '</small>'
]);
}
if (!is_null($event_config['event_start_date']) && time() < $event_config['event_start_date']) {
if (!empty($start) && $start->greaterThan(new Carbon())) {
$elements[] = div('col-sm-3 text-center hidden-xs', [
heading(__('Event starts'), 4),
'<span class="moment-countdown text-big" data-timestamp="' . $event_config['event_start_date'] . '">%c</span>',
'<small>' . date(__('Y-m-d'), $event_config['event_start_date']) . '</small>'
'<span class="moment-countdown text-big" data-timestamp="' . $start->getTimestamp() . '">%c</span>',
'<small>' . $start->format(__('Y-m-d')) . '</small>'
]);
}
if (!is_null($event_config['event_end_date']) && time() < $event_config['event_end_date']) {
if (!empty($end) && $end->greaterThan(new Carbon())) {
$elements[] = div('col-sm-3 text-center hidden-xs', [
heading(__('Event ends'), 4),
'<span class="moment-countdown text-big" data-timestamp="' . $event_config['event_end_date'] . '">%c</span>',
'<small>' . date(__('Y-m-d'), $event_config['event_end_date']) . '</small>'
'<span class="moment-countdown text-big" data-timestamp="' . $end->getTimestamp() . '">%c</span>',
'<small>' . $end->format(__('Y-m-d')) . '</small>'
]);
}
if (!is_null($event_config['teardown_end_date']) && time() < $event_config['teardown_end_date']) {
if (!empty($teardown) && $teardown->greaterThan(new Carbon())) {
$elements[] = div('col-sm-3 text-center hidden-xs', [
heading(__('Teardown ends'), 4),
'<span class="moment-countdown text-big" data-timestamp="' . $event_config['teardown_end_date'] . '">%c</span>',
'<small>' . date(__('Y-m-d'), $event_config['teardown_end_date']) . '</small>'
'<span class="moment-countdown text-big" data-timestamp="' . $teardown->getTimestamp() . '">%c</span>',
'<small>' . $teardown->format(__('Y-m-d')) . '</small>'
]);
}

@ -1,5 +1,7 @@
<?php
use Carbon\Carbon;
/**
* Renders user settings page
*
@ -887,10 +889,9 @@ function render_user_arrived_hint()
global $user;
if ($user['Gekommen'] == 0) {
$event_config = EventConfig();
if (!empty($event_config)
&& !is_null($event_config['buildup_start_date'])
&& time() > $event_config['buildup_start_date']) {
/** @var Carbon $buildup */
$buildup = config('buildup_start');
if (!empty($buildup) && $buildup->lessThan(new Carbon())) {
return __('You are not marked as arrived. Please go to heaven\'s desk, get your angel badge and/or tell them that you arrived already.');
}
}

@ -2,25 +2,25 @@
<hr/>
<div class="text-center footer" style="margin-bottom: 10px;">
{% block eventinfo %}
{% if event_config.event_name %}
{% if event_config.event_start_date and event_config.event_end_date %}
{% if config('name') %}
{% if config('event_start') and config('event_end') %}
{{ __('%s, from %s to %s', [
event_config.event_name,
date(event_config.event_start_date).format(__('Y-m-d')),
date(event_config.event_end_date).format(__('Y-m-d'))
config('name'),
config('event_start').format(__('Y-m-d')),
config('event_end').format(__('Y-m-d'))
]) }}
{% elseif event_config.event_start_date %}
{% elseif config('event_start') %}
{{ __('%s, starting %s', [
event_config.event_name,
date(event_config.event_start_date).format(__('Y-m-d'))
config('name'),
config('event_start').format(__('Y-m-d'))
]) }}
{% else %}
{{ event_config.event_name }}
{{ config('name') }}
{% endif %} <br>
{% elseif event_config.event_start_date and event_config.event_end_date %}
{% elseif config('event_start') and config('event_end') %}
{{ __('Event from %s to %s', [
date(event_config.event_start_date).format(__('Y-m-d')),
date(event_config.event_end_date).format(__('Y-m-d'))
config('event_start').format(__('Y-m-d')),
config('event_end').format(__('Y-m-d'))
]) }} <br>
{% endif %}
{% endblock %}

@ -2,14 +2,31 @@
namespace Engelsystem\Config;
use Engelsystem\Application;
use Engelsystem\Container\ServiceProvider;
use Engelsystem\Models\EventConfig;
use Exception;
use Illuminate\Database\QueryException;
class ConfigServiceProvider extends ServiceProvider
{
/** @var array */
protected $configFiles = ['config.default.php', 'config.php'];
/** @var EventConfig */
protected $eventConfig;
/**
* @param Application $app
* @param EventConfig $eventConfig
*/
public function __construct(Application $app, EventConfig $eventConfig = null)
{
parent::__construct($app);
$this->eventConfig = $eventConfig;
}
public function register()
{
$config = $this->app->make(Config::class);
@ -17,7 +34,7 @@ class ConfigServiceProvider extends ServiceProvider
$this->app->instance('config', $config);
foreach ($this->configFiles as $file) {
$file = config_path($file);
$file = $this->getConfigPath($file);
if (!file_exists($file)) {
continue;
@ -33,4 +50,44 @@ class ConfigServiceProvider extends ServiceProvider
throw new Exception('Configuration not found');
}
}
public function boot()
{
if (!$this->eventConfig) {
return;
}
/** @var Config $config */
$config = $this->app->get('config');
/** @var EventConfig[] $values */
try {
$values = $this->eventConfig->newQuery()->get(['name', 'value']);
} catch (QueryException $e) {
return;
}
foreach ($values as $option) {
$data = $option->value;
if (is_array($data) && $config->has($option->name)) {
$data = array_replace_recursive(
$config->get($option->name),
$data
);
}
$config->set($option->name, $data);
}
}
/**
* Get the config path
*
* @param string $path
* @return string
*/
protected function getConfigPath($path = ''): string
{
return config_path($path);
}
}

@ -2,7 +2,6 @@
namespace Engelsystem\Renderer\Twig\Extensions;
use Carbon\Carbon;
use Twig_Extension as TwigExtension;
use Twig_Extension_GlobalsInterface as GlobalsInterface;
@ -17,45 +16,8 @@ class Globals extends TwigExtension implements GlobalsInterface
{
global $user;
$eventConfig = $this->getEventConfig();
if (empty($eventConfig)) {
$eventConfig = [];
}
return [
'user' => isset($user) ? $user : [],
'event_config' => $this->filterEventConfig($eventConfig),
];
}
/**
* @return array
* @codeCoverageIgnore
*/
protected function getEventConfig()
{
return EventConfig();
}
/**
* @param $eventConfig
* @return mixed
*/
protected function filterEventConfig($eventConfig)
{
array_walk($eventConfig, function (&$value, $key) {
if (is_null($value) || !in_array($key, [
'buildup_start_date',
'event_start_date',
'event_end_date',
'teardown_end_date',
])) {
return;
}
$value = Carbon::createFromTimestamp($value);
});
return $eventConfig;
}
}

@ -5,18 +5,22 @@ namespace Engelsystem\Test\Unit\Config;
use Engelsystem\Application;
use Engelsystem\Config\Config;
use Engelsystem\Config\ConfigServiceProvider;
use Engelsystem\Models\EventConfig;
use Engelsystem\Test\Unit\ServiceProviderTest;
use Exception;
use PHPUnit_Framework_MockObject_MockObject;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\QueryException;
use PHPUnit\Framework\MockObject\MockObject;
class ConfigServiceProviderTest extends ServiceProviderTest
{
/**
* @covers \Engelsystem\Config\ConfigServiceProvider::register()
* @covers \Engelsystem\Config\ConfigServiceProvider::register
* @covers \Engelsystem\Config\ConfigServiceProvider::getConfigPath
*/
public function testRegister()
{
/** @var PHPUnit_Framework_MockObject_MockObject|Config $config */
/** @var MockObject|Config $config */
$config = $this->getMockBuilder(Config::class)
->getMock();
@ -53,11 +57,11 @@ class ConfigServiceProviderTest extends ServiceProviderTest
}
/**
* @covers \Engelsystem\Config\ConfigServiceProvider::register()
* @covers \Engelsystem\Config\ConfigServiceProvider::register
*/
public function testRegisterException()
{
/** @var PHPUnit_Framework_MockObject_MockObject|Config $config */
/** @var MockObject|Config $config */
$config = $this->getMockBuilder(Config::class)
->getMock();
@ -81,4 +85,67 @@ class ConfigServiceProviderTest extends ServiceProviderTest
$serviceProvider = new ConfigServiceProvider($app);
$serviceProvider->register();
}
/**
* @covers \Engelsystem\Config\ConfigServiceProvider::__construct
* @covers \Engelsystem\Config\ConfigServiceProvider::boot
*/
public function testBoot()
{
$app = $this->getApp(['get']);
/** @var EventConfig|MockObject $eventConfig */
$eventConfig = $this->createMock(EventConfig::class);
/** @var EloquentBuilder|MockObject $eloquentBuilder */
$eloquentBuilder = $this->getMockBuilder(EloquentBuilder::class)
->disableOriginalConstructor()
->getMock();
$config = new Config(['foo' => 'bar', 'lorem' => ['ipsum' => 'dolor', 'bla' => 'foo']]);
$configs = [
new EventConfig(['name' => 'test', 'value' => 'testing']),
new EventConfig(['name' => 'lorem', 'value' => ['ipsum' => 'tester']]),
];
$returnValue = $eloquentBuilder;
$eventConfig
->expects($this->exactly(3))
->method('newQuery')
->willReturnCallback(function () use (&$returnValue) {
if ($returnValue instanceof EloquentBuilder) {
$return = $returnValue;
$returnValue = null;
return $return;
}
if (is_null($returnValue)) {
throw new QueryException('', [], new Exception());
}
return null;
});
$this->setExpects($eloquentBuilder, 'get', [['name', 'value']], $configs);
$this->setExpects($app, 'get', ['config'], $config, $this->exactly(3));
$serviceProvider = new ConfigServiceProvider($app);
$serviceProvider->boot();
$serviceProvider = new ConfigServiceProvider($app, $eventConfig);
$serviceProvider->boot();
$serviceProvider->boot();
$serviceProvider->boot();
$this->assertArraySubset(
[
'foo' => 'bar',
'lorem' => [
'ipsum' => 'tester',
'bla' => 'foo',
],
'test' => 'testing',
],
$config->get(null)
);
}
}

@ -2,54 +2,25 @@
namespace Engelsystem\Test\Unit\Renderer\Twig\Extensions;
use Carbon\Carbon;
use Engelsystem\Renderer\Twig\Extensions\Globals;
use PHPUnit\Framework\MockObject\MockObject;
class GlobalsTest extends ExtensionTest
{
/**
* @covers \Engelsystem\Renderer\Twig\Extensions\Globals::getGlobals
* @covers \Engelsystem\Renderer\Twig\Extensions\Globals::filterEventConfig
*/
public function testGetGlobals()
{
global $user;
$user = [];
/** @var Globals|MockObject $extension */
$extension = $this->getMockBuilder(Globals::class)
->setMethods(['getEventConfig'])
->getMock();
$extension->expects($this->exactly(2))
->method('getEventConfig')
->willReturnOnConsecutiveCalls(
null,
[
'lorem' => 'ipsum',
'event_end_date' => 1234567890,
]
);
$extension = new Globals();
$globals = $extension->getGlobals();
$this->assertGlobalsExists('user', [], $globals);
$this->assertGlobalsExists('event_config', [], $globals);
$user['foo'] = 'bar';
$globals = $extension->getGlobals();
$this->assertGlobalsExists('user', ['foo' => 'bar'], $globals);
$this->assertGlobalsExists('event_config', ['lorem' => 'ipsum'], $globals);
$config = $globals['event_config'];
$this->assertArrayHasKey('event_end_date', $config);
/** @var Carbon $eventEndDate */
$eventEndDate = $config['event_end_date'];
$this->assertInstanceOf(Carbon::class, $eventEndDate);
$eventEndDate->setTimezone('UTC');
$this->assertEquals('2009-02-13 23:31:30', $eventEndDate->format('Y-m-d H:i:s'));
}
}

Loading…
Cancel
Save