Completed
Pull Request — master (#13)
by Vojta
11:12 queued 04:37
created

ReservationsFacadeTest::testIsCreatedWhileAgo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
1
<?php namespace VojtaSvoboda\Reservations\Tests\Facades;
2
3
use App;
4
use Carbon\Carbon;
5
use Config;
6
use PluginTestCase;
7
use VojtaSvoboda\Reservations\Facades\ReservationsFacade;
8
use VojtaSvoboda\Reservations\Models\Settings;
9
use VojtaSvoboda\Reservations\Models\Status;
10
11
class ReservationsFacadeTest extends PluginTestCase
12
{
13
    /**
14
     * Returns tested class.
15
     *
16
     * @return ReservationsFacade
17
     */
18
    public function getModel()
19
    {
20
        return App::make(ReservationsFacade::class);
21
    }
22
23
    public function testStoreEmptyReservation()
24
    {
25
        $model = $this->getModel();
26
27
        $this->setExpectedException('October\Rain\Exception\ApplicationException');
28
        $model->storeReservation([]);
29
    }
30
31
    public function testStoreReservationWithoutTime()
32
    {
33
        $model = $this->getModel();
34
35
        $this->setExpectedException('October\Rain\Exception\ApplicationException');
36
        $model->storeReservation([
37
            'date' => '18/08/2016',
38
        ]);
39
    }
40
41
    public function testStoreReservation()
42
    {
43
        $model = $this->getModel();
44
        $reservation = $model->storeReservation($this->getTestingReservationData());
45
46
        // check status
47
        $defaultStatusIdent = Config::get('vojtasvoboda.reservations::config.statuses.received', 'received');
48
        $this->assertEquals($defaultStatusIdent, $reservation->status->ident);
49
50
        // check locale
51
        $locale = App::getLocale();
52
        $this->assertEquals($locale, $reservation->locale);
53
54
        // check date and time
55
        $inputDate = $this->getTestingReservationData()['date'] . ' ' . $this->getTestingReservationData()['time'];
56
        $dateTime = Carbon::createFromFormat('d/m/Y H:i', $inputDate)->toDateTimeString();
57
        $this->assertEquals($dateTime, $reservation->date);
58
    }
59
60
    public function testTransformDateTime()
61
    {
62
        $model = $this->getModel();
63
64
        $data = [
65
            'date' => '08/10/2016',
66
            'time' => '15:45',
67
        ];
68
        $date = $model->transformDateTime($data);
69
70
        $this->assertInstanceOf('Carbon\Carbon', $date);
71
        $this->assertEquals('2016-10-08 15:45:00', $date->format('Y-m-d H:i:s'));
72
    }
73
74 View Code Duplication
    public function testIsDateAvailableFailing()
75
    {
76
        $model = $this->getModel();
77
78
        // create reservation
79
        $reservation = $model->storeReservation($this->getTestingReservationData());
80
81
        // change created at date because of 30 seconds robots check
82
        $reservation->created_at = '2016-08-18 14:00:00';
83
        $reservation->save();
84
85
        // try to do second reservation with same date and time
86
        $this->setExpectedException('October\Rain\Exception\ApplicationException');
87
        $model->storeReservation($this->getTestingReservationData());
88
    }
89
90
    public function testIsDateAvailablePassed()
91
    {
92
        $model = $this->getModel();
93
94
        // create reservation
95
        $reservation = $model->storeReservation($this->getTestingReservationData());
96
97
        // change created at date because of 30 seconds robots check
98
        $reservation->created_at = '2016-08-18 14:00:00';
99
        $reservation->save();
100
101
        // try to do second reservation with same date and time after 2 hours
102
        $data = $this->getTestingReservationData();
103
        $data['time'] = '22:00';
104
        $model->storeReservation($data);
105
    }
106
107
    public function testIsDateAvailableForCancelled()
108
    {
109
        $model = $this->getModel();
110
111
        // create reservation
112
        $reservation = $model->storeReservation($this->getTestingReservationData());
113
114
        // cancel status
115
        $cancelledStatuses = Config::get('vojtasvoboda.reservations::config.statuses.cancelled', 'cancelled');
116
        $statusIdent = empty($cancelledStatuses) ? 'cancelled' : $cancelledStatuses[0];
117
118
        // change created at date because of 30 seconds robots check and cancell it
119
        $reservation->created_at = '2016-08-18 14:00:00';
120
        $reservation->status = Status::where('ident', $statusIdent)->first();
121
        $reservation->save();
122
123
        // try to do second reservation with same date and time
124
        $data = $this->getTestingReservationData();
125
        $model->storeReservation($data);
126
    }
127
128 View Code Duplication
    public function testGetReservationsWithSameEmailCount()
129
    {
130
        $model = $this->getModel();
131
132
        // create one reservation with [email protected] email
133
        $model->storeReservation($this->getTestingReservationData());
134
135
        $count = $model->getReservationsWithSameEmailCount('[email protected]');
136
        $this->assertEquals(0, $count);
137
138
        $count = $model->getReservationsWithSameEmailCount('[email protected]');
139
        $this->assertEquals(1, $count);
140
    }
141
142
    public function testIsUserReturning()
143
    {
144
        $model = $this->getModel();
145
146
        // enable Returning Customers function
147
        Settings::set('returning_mark', 1);
148
149
        // is returning without any reservation?
150
        $isReturning = $model->isUserReturning('[email protected]');
151
        $this->assertEquals(false, $isReturning, 'There is no reservation, so customer cant be returning.');
152
153
        // create one reservation with [email protected] email
154
        $model->storeReservation($this->getTestingReservationData());
155
156
        // is returning without any reservation?
157
        $isReturning = $model->isUserReturning('[email protected]');
158
        $this->assertEquals(false, $isReturning, 'Email [email protected] does not has any reservation, so it should not be marked as returning customer.');
159
160
        // is returning with one reservation?
161
        $isReturning = $model->isUserReturning('[email protected]');
162
        $this->assertEquals(true, $isReturning, 'Email [email protected] has one reservation, so it should be marked as returning customer.');
163
    }
164
165
    public function testIsCreatedWhileAgo()
166
    {
167
        $model = $this->getModel();
168
        $exists = $model->isCreatedWhileAgo();
169
170
        $this->assertFalse($exists);
171
172
        // create fake reservation
173
        $model->storeReservation($this->getTestingReservationData());
174
        $exists = $model->isCreatedWhileAgo();
175
176
        $this->assertTrue($exists);
177
    }
178
179 View Code Duplication
    private function getTestingReservationData()
180
    {
181
        return [
182
            'date' => '18/08/2016',
183
            'time' => '20:00',
184
            'email' => '[email protected]',
185
            'phone' => '777111222',
186
            'street' => 'ABCDE',
187
            'name' => 'Vojta Svoboda',
188
            'message' => 'Hello.',
189
        ];
190
    }
191
}
192