summaryrefslogtreecommitdiff
path: root/tests/Unit/Models/EventConfigTest.php
blob: 4ab2fc75e42f6e08481f5429e3cdb81ff061bbf9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php

namespace Engelsystem\Test\Unit\Models;

use Carbon\Carbon;
use Engelsystem\Models\EventConfig;
use Engelsystem\Test\Unit\HasDatabase;
use Engelsystem\Test\Unit\TestCase;

class EventConfigTest extends TestCase
{
    use HasDatabase;

    /**
     * @covers \Engelsystem\Models\EventConfig::setValueAttribute
     */
    public function testSetValueAttribute()
    {
        (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(
            '"2000-01-01"',
            $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
     */
    public function testGetValueAttribute()
    {
        $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(
            '2001-02-03 00:00',
            (new EventConfig())->find('buildup_start')
                ->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')
        );
    }

    /**
     * @covers \Engelsystem\Models\EventConfig::getValueCast
     */
    public function testGetValueCast()
    {
        $model = new EventConfig(['value' => 'bar']);
        $this->assertEquals('bar', $model->value);

        return;
    }

    /**
     * Init a new EventConfig class
     *
     * @return EventConfig
     */
    protected function getEventConfig()
    {
        return new class extends EventConfig
        {
            /**
             * @param string $value
             * @param string $type
             * @return EventConfig
             */
            public function setValueCast($value, $type)
            {
                $this->valueCasts[$value] = $type;

                return $this;
            }
        };
    }

    /**
     * Prepare test
     */
    protected function setUp(): void
    {
        parent::setUp();
        $this->initDatabase();
    }
}