2018-09-24 14:19:13 +02:00
|
|
|
<?php
|
|
|
|
|
2023-02-03 20:41:59 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-09-24 14:19:13 +02:00
|
|
|
namespace Engelsystem\Test\Unit\Models;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use Engelsystem\Models\EventConfig;
|
|
|
|
|
2020-05-11 19:57:25 +02:00
|
|
|
class EventConfigTest extends ModelTest
|
2018-09-24 14:19:13 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Models\EventConfig::setValueAttribute
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testSetValueAttribute(): void
|
2018-09-24 14:19:13 +02:00
|
|
|
{
|
|
|
|
(new EventConfig())
|
|
|
|
->setAttribute('name', 'foo')
|
|
|
|
->setAttribute('value', 'bar')
|
|
|
|
->save();
|
|
|
|
$this->assertEquals(
|
|
|
|
'"bar"',
|
|
|
|
$this->database
|
|
|
|
->selectOne("SELECT `value` FROM event_config WHERE name='foo'")
|
|
|
|
->value
|
|
|
|
);
|
|
|
|
|
|
|
|
(new EventConfig())
|
|
|
|
->setAttribute('name', 'buildup_start')
|
|
|
|
->setAttribute('value', new Carbon('2000-01-01 10:20'))
|
|
|
|
->save();
|
|
|
|
$this->assertEquals(
|
2021-01-02 01:22:05 +01:00
|
|
|
'"2000-01-01 10:20"',
|
2018-09-24 14:19:13 +02:00
|
|
|
$this->database
|
|
|
|
->selectOne("SELECT `value` FROM event_config WHERE name='buildup_start'")
|
|
|
|
->value
|
|
|
|
);
|
|
|
|
|
|
|
|
($this->getEventConfig())
|
|
|
|
->setAttribute('name', 'event_start')
|
|
|
|
->setValueCast('event_start', 'datetime')
|
|
|
|
->setAttribute('value', new Carbon('2010-11-11 20:22'))
|
|
|
|
->save();
|
|
|
|
$this->assertEquals(
|
|
|
|
'"' . (new Carbon('2010-11-11 20:22'))->format(Carbon::ATOM) . '"',
|
|
|
|
$this->database
|
|
|
|
->selectOne("SELECT `value` FROM event_config WHERE name='event_start'")
|
|
|
|
->value
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Models\EventConfig::getValueAttribute
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testGetValueAttribute(): void
|
2018-09-24 14:19:13 +02:00
|
|
|
{
|
|
|
|
$model = new EventConfig(['name', 'buildup_start', 'value' => '']);
|
|
|
|
$this->assertEquals('', $model->value);
|
|
|
|
|
|
|
|
(new EventConfig())
|
|
|
|
->setAttribute('name', 'buildup_start')
|
|
|
|
->setAttribute('value', new Carbon('2001-02-03 11:12'))
|
|
|
|
->save();
|
|
|
|
$this->assertEquals(
|
2021-01-02 01:22:05 +01:00
|
|
|
'2001-02-03 11:12',
|
2019-11-06 13:16:00 +01:00
|
|
|
(new EventConfig())->find('buildup_start')
|
2018-09-24 14:19:13 +02:00
|
|
|
->value
|
|
|
|
->format('Y-m-d H:i')
|
|
|
|
);
|
|
|
|
|
|
|
|
($this->getEventConfig())
|
|
|
|
->setAttribute('name', 'event_start')
|
|
|
|
->setValueCast('event_start', 'datetime')
|
|
|
|
->setAttribute('value', new Carbon('2010-11-11 20:22'))
|
|
|
|
->save();
|
|
|
|
$this->assertEquals(
|
|
|
|
'2010-11-11 20:22',
|
|
|
|
($this->getEventConfig())->find('event_start')
|
|
|
|
->setValueCast('event_start', 'datetime')
|
|
|
|
->value
|
|
|
|
->format('Y-m-d H:i')
|
|
|
|
);
|
2021-12-01 00:27:46 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
null,
|
|
|
|
($this->getEventConfig())->getValueAttribute(null)
|
|
|
|
);
|
2018-09-24 14:19:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Models\EventConfig::getValueCast
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testGetValueCast(): void
|
2018-09-24 14:19:13 +02:00
|
|
|
{
|
2022-12-14 00:28:34 +01:00
|
|
|
$model = new EventConfig(['name' => 'foo', 'value' => 'bar']);
|
2018-09-24 14:19:13 +02:00
|
|
|
$this->assertEquals('bar', $model->value);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Init a new EventConfig class
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
protected function getEventConfig(): EventConfig
|
2018-09-24 14:19:13 +02:00
|
|
|
{
|
2019-10-08 13:57:50 +02:00
|
|
|
return new class extends EventConfig
|
|
|
|
{
|
2022-12-14 19:15:20 +01:00
|
|
|
public function setValueCast(string $value, string $type): EventConfig
|
2018-09-24 14:19:13 +02:00
|
|
|
{
|
|
|
|
$this->valueCasts[$value] = $type;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|