summaryrefslogtreecommitdiff
path: root/tests/units/TimetableTest.php
blob: 4b4dcc83430ecf28b4140e62ad99d2d6f1ced8be (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
130
<?php

require_once __DIR__.'/Base.php';

use Model\User;
use Model\Timetable;
use Model\TimetableDay;
use Model\TimetableWeek;
use Model\TimetableOff;
use Model\TimetableExtra;

class TimetableTest extends Base
{
    public function testCalculateWorkDays()
    {
        $w = new TimetableWeek($this->container);
        $t = new Timetable($this->container);

        $this->assertNotFalse($w->create(1, 1, '09:30', '12:00'));
        $this->assertNotFalse($w->create(1, 1, '13:00', '17:00'));
        $this->assertNotFalse($w->create(1, 2, '09:30', '12:00'));
        $this->assertNotFalse($w->create(1, 2, '13:00', '17:00'));

        $monday = new DateTime('next Monday');

        $timetable = $t->calculate(1, $monday, new DateTime('next Monday + 6 days'));
        $this->assertNotEmpty($timetable);
        $this->assertCount(4, $timetable);

        $this->assertEquals($monday->format('Y-m-d').' 09:30', $timetable[0][0]->format('Y-m-d H:i'));
        $this->assertEquals($monday->format('Y-m-d').' 12:00', $timetable[0][1]->format('Y-m-d H:i'));
        $this->assertEquals($monday->format('Y-m-d').' 13:00', $timetable[1][0]->format('Y-m-d H:i'));
        $this->assertEquals($monday->format('Y-m-d').' 17:00', $timetable[1][1]->format('Y-m-d H:i'));

        $this->assertEquals($monday->add(new DateInterval('P1D'))->format('Y-m-d').' 09:30', $timetable[2][0]->format('Y-m-d H:i'));
        $this->assertEquals($monday->format('Y-m-d').' 12:00', $timetable[2][1]->format('Y-m-d H:i'));
        $this->assertEquals($monday->format('Y-m-d').' 13:00', $timetable[3][0]->format('Y-m-d H:i'));
        $this->assertEquals($monday->format('Y-m-d').' 17:00', $timetable[3][1]->format('Y-m-d H:i'));
    }

    public function testCalculateOverTime()
    {
        $d = new TimetableDay($this->container);
        $w = new TimetableWeek($this->container);
        $e = new TimetableExtra($this->container);
        $t = new Timetable($this->container);

        $monday = new DateTime('next Monday');
        $tuesday = new DateTime('next Monday + 1 day');
        $friday = new DateTime('next Monday + 4 days');

        $this->assertNotFalse($d->create(1, '08:00', '12:00'));
        $this->assertNotFalse($d->create(1, '14:00', '18:00'));

        $this->assertNotFalse($w->create(1, 1, '09:30', '12:00'));
        $this->assertNotFalse($w->create(1, 1, '13:00', '17:00'));
        $this->assertNotFalse($w->create(1, 2, '09:30', '12:00'));
        $this->assertNotFalse($w->create(1, 2, '13:00', '17:00'));

        $this->assertNotFalse($e->create(1, $tuesday->format('Y-m-d'), 0, '17:00', '22:00'));
        $this->assertNotFalse($e->create(1, $friday->format('Y-m-d'), 1));

        $timetable = $t->calculate(1, $monday, new DateTime('next Monday + 6 days'));
        $this->assertNotEmpty($timetable);
        $this->assertCount(7, $timetable);

        $this->assertEquals($monday->format('Y-m-d').' 09:30', $timetable[0][0]->format('Y-m-d H:i'));
        $this->assertEquals($monday->format('Y-m-d').' 12:00', $timetable[0][1]->format('Y-m-d H:i'));
        $this->assertEquals($monday->format('Y-m-d').' 13:00', $timetable[1][0]->format('Y-m-d H:i'));
        $this->assertEquals($monday->format('Y-m-d').' 17:00', $timetable[1][1]->format('Y-m-d H:i'));

        $this->assertEquals($tuesday->format('Y-m-d').' 09:30', $timetable[2][0]->format('Y-m-d H:i'));
        $this->assertEquals($tuesday->format('Y-m-d').' 12:00', $timetable[2][1]->format('Y-m-d H:i'));
        $this->assertEquals($tuesday->format('Y-m-d').' 13:00', $timetable[3][0]->format('Y-m-d H:i'));
        $this->assertEquals($tuesday->format('Y-m-d').' 17:00', $timetable[3][1]->format('Y-m-d H:i'));

        $this->assertEquals($tuesday->format('Y-m-d').' 17:00', $timetable[4][0]->format('Y-m-d H:i'));
        $this->assertEquals($tuesday->format('Y-m-d').' 22:00', $timetable[4][1]->format('Y-m-d H:i'));

        $this->assertEquals($friday->format('Y-m-d').' 08:00', $timetable[5][0]->format('Y-m-d H:i'));
        $this->assertEquals($friday->format('Y-m-d').' 12:00', $timetable[5][1]->format('Y-m-d H:i'));

        $this->assertEquals($friday->format('Y-m-d').' 14:00', $timetable[6][0]->format('Y-m-d H:i'));
        $this->assertEquals($friday->format('Y-m-d').' 18:00', $timetable[6][1]->format('Y-m-d H:i'));
    }

    public function testCalculateTimeOff()
    {
        $d = new TimetableDay($this->container);
        $w = new TimetableWeek($this->container);
        $o = new TimetableOff($this->container);
        $t = new Timetable($this->container);

        $monday = new DateTime('next Monday');
        $tuesday = new DateTime('next Monday + 1 day');
        $friday = new DateTime('next Monday + 4 days');

        $this->assertNotFalse($d->create(1, '08:00', '12:00'));
        $this->assertNotFalse($d->create(1, '14:00', '18:00'));

        $this->assertNotFalse($w->create(1, 1, '09:30', '12:00'));
        $this->assertNotFalse($w->create(1, 1, '13:00', '17:00'));
        $this->assertNotFalse($w->create(1, 2, '09:30', '12:00'));
        $this->assertNotFalse($w->create(1, 2, '13:00', '17:00'));
        $this->assertNotFalse($w->create(1, 5, '09:30', '12:00'));
        $this->assertNotFalse($w->create(1, 5, '13:00', '17:00'));

        $this->assertNotFalse($o->create(1, $tuesday->format('Y-m-d'), 0, '14:00', '15:00'));
        $this->assertNotFalse($o->create(1, $monday->format('Y-m-d'), 1));

        $timetable = $t->calculate(1, $monday, new DateTime('next Monday + 6 days'));
        $this->assertNotEmpty($timetable);
        $this->assertCount(5, $timetable);

        $this->assertEquals($tuesday->format('Y-m-d').' 09:30', $timetable[0][0]->format('Y-m-d H:i'));
        $this->assertEquals($tuesday->format('Y-m-d').' 12:00', $timetable[0][1]->format('Y-m-d H:i'));

        $this->assertEquals($tuesday->format('Y-m-d').' 13:00', $timetable[1][0]->format('Y-m-d H:i'));
        $this->assertEquals($tuesday->format('Y-m-d').' 14:00', $timetable[1][1]->format('Y-m-d H:i'));

        $this->assertEquals($tuesday->format('Y-m-d').' 15:00', $timetable[2][0]->format('Y-m-d H:i'));
        $this->assertEquals($tuesday->format('Y-m-d').' 17:00', $timetable[2][1]->format('Y-m-d H:i'));

        $this->assertEquals($friday->format('Y-m-d').' 09:30', $timetable[3][0]->format('Y-m-d H:i'));
        $this->assertEquals($friday->format('Y-m-d').' 12:00', $timetable[3][1]->format('Y-m-d H:i'));

        $this->assertEquals($friday->format('Y-m-d').' 13:00', $timetable[4][0]->format('Y-m-d H:i'));
        $this->assertEquals($friday->format('Y-m-d').' 17:00', $timetable[4][1]->format('Y-m-d H:i'));
    }
}