1
|
|
|
<?php namespace VojtaSvoboda\Reservations\Tests\Models; |
2
|
|
|
|
3
|
|
|
use App; |
4
|
|
|
use Carbon\Carbon; |
5
|
|
|
use Config; |
6
|
|
|
use Illuminate\Support\Facades\Validator; |
7
|
|
|
use PluginTestCase; |
8
|
|
|
use October\Rain\Database\ModelException; |
9
|
|
|
use VojtaSvoboda\Reservations\Facades\ReservationsFacade; |
10
|
|
|
use VojtaSvoboda\Reservations\Models\Reservation; |
11
|
|
|
use VojtaSvoboda\Reservations\Models\Status; |
12
|
|
|
use VojtaSvoboda\Reservations\Validators\ReservationsValidators; |
13
|
|
|
|
14
|
|
|
class ReservationTest extends PluginTestCase |
15
|
|
|
{ |
16
|
|
|
private $defaultStatus; |
17
|
|
|
|
18
|
|
View Code Duplication |
public function setUp() |
19
|
|
|
{ |
20
|
|
|
parent::setUp(); |
21
|
|
|
|
22
|
|
|
$this->app->bind('vojtasvoboda.reservations.facade', ReservationsFacade::class); |
23
|
|
|
|
24
|
|
|
// registrate reservations validators |
25
|
|
|
Validator::resolver(function($translator, $data, $rules, $messages, $customAttributes) { |
26
|
|
|
return new ReservationsValidators($translator, $data, $rules, $messages, $customAttributes); |
27
|
|
|
}); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Get tested model. |
32
|
|
|
* |
33
|
|
|
* @return Reservation |
34
|
|
|
*/ |
35
|
|
|
public function getModel() |
36
|
|
|
{ |
37
|
|
|
return App::make(Reservation::class); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testBeforeCreate() |
41
|
|
|
{ |
42
|
|
|
$model = $this->getModel(); |
43
|
|
|
|
44
|
|
|
// create reservation |
45
|
|
|
$reservation = $model->create($this->getTestingReservationData()); |
46
|
|
|
$this->assertNotEmpty($reservation->hash, 'Reservation hash is empty.'); |
47
|
|
|
$this->assertNotEmpty($reservation->number, 'Number hash is empty.'); |
48
|
|
|
$this->assertSame(App::getLocale(), $reservation->locale, 'Reservation locale should be same as app locale.'); |
49
|
|
|
$this->assertNotEmpty($reservation->ip, 'IP address is empty.'); |
50
|
|
|
$this->assertNotEmpty($reservation->user_agent, 'User Agent is empty.'); |
51
|
|
|
$this->assertSame($this->getDefaultStatus(), $reservation->status, 'Reservation status should be ' . $this->getDefaultStatus()->name . '.'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testIsDateAvailableFailing() |
55
|
|
|
{ |
56
|
|
|
$model = $this->getModel(); |
57
|
|
|
|
58
|
|
|
// create reservation |
59
|
|
|
$model->create($this->getTestingReservationData()); |
60
|
|
|
|
61
|
|
|
// try to do second reservation with same date and time |
62
|
|
|
$this->setExpectedException(ModelException::class, 'Date 18.08.2016 20:00 is already booked.'); |
63
|
|
|
$model->create($this->getTestingReservationData()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testIsDateAvailablePassed() |
67
|
|
|
{ |
68
|
|
|
$model = $this->getModel(); |
69
|
|
|
|
70
|
|
|
// create reservation |
71
|
|
|
$model->create($this->getTestingReservationData()); |
72
|
|
|
|
73
|
|
|
// try to do second reservation with same date and time after 2 hours |
74
|
|
|
$data = $this->getTestingReservationData(); |
75
|
|
|
$data['date'] = Carbon::createFromFormat('Y-m-d H:i', '2016-08-18 22:00'); |
76
|
|
|
$model->create($data); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testIsDateAvailableForCancelled() |
80
|
|
|
{ |
81
|
|
|
$model = $this->getModel(); |
82
|
|
|
|
83
|
|
|
// create reservation |
84
|
|
|
$reservation = $model->create($this->getTestingReservationData()); |
85
|
|
|
|
86
|
|
|
// cancel status |
87
|
|
|
$cancelledStatuses = Config::get('vojtasvoboda.reservations::config.statuses.cancelled', ['cancelled']); |
88
|
|
|
$statusIdent = empty($cancelledStatuses) ? 'cancelled' : $cancelledStatuses[0]; |
89
|
|
|
|
90
|
|
|
// cancell reservation |
91
|
|
|
$reservation->status = Status::where('ident', $statusIdent)->first(); |
92
|
|
|
$reservation->save(); |
93
|
|
|
|
94
|
|
|
// try to do second reservation with same date and time |
95
|
|
|
$data = $this->getTestingReservationData(); |
96
|
|
|
$model->create($data); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testIsCancelled() |
100
|
|
|
{ |
101
|
|
|
$model = $this->getModel(); |
102
|
|
|
|
103
|
|
|
$reservation = $model->create($this->getTestingReservationData()); |
104
|
|
|
$this->assertFalse($reservation->isCancelled()); |
105
|
|
|
$this->assertTrue($reservation->isCancelled('cancelled')); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testGetHash() |
109
|
|
|
{ |
110
|
|
|
$model = $this->getModel(); |
111
|
|
|
|
112
|
|
|
$firstHash = $model->getUniqueHash(); |
113
|
|
|
$secondHash = $model->getUniqueHash(); |
114
|
|
|
|
115
|
|
|
$this->assertNotEquals($firstHash, $secondHash); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function testGetEmptyHash() |
119
|
|
|
{ |
120
|
|
|
$model = $this->getModel(); |
121
|
|
|
Config::set('vojtasvoboda.reservations::config.hash', 0); |
122
|
|
|
$this->assertNull($model->getUniqueHash()); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function testGetNumber() |
126
|
|
|
{ |
127
|
|
|
$model = $this->getModel(); |
128
|
|
|
|
129
|
|
|
$firstNumber = $model->getUniqueNumber(); |
130
|
|
|
$secondNumber = $model->getUniqueNumber(); |
131
|
|
|
|
132
|
|
|
$this->assertNotEquals($firstNumber, $secondNumber); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function testGetEmptyNumber() |
136
|
|
|
{ |
137
|
|
|
$model = $this->getModel(); |
138
|
|
|
Config::set('vojtasvoboda.reservations::config.number.min', 0); |
139
|
|
|
$this->assertNull($model->getUniqueNumber()); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function testScopeNotCancelled() |
143
|
|
|
{ |
144
|
|
|
$model = $this->getModel(); |
145
|
|
|
|
146
|
|
|
// create reservation |
147
|
|
|
$reservation = $model->create($this->getTestingReservationData()); |
148
|
|
|
$reservations = $model->notCancelled()->get(); |
149
|
|
|
$this->assertNotEmpty($reservations); |
150
|
|
|
|
151
|
|
|
// change reservation to cancelled |
152
|
|
|
$reservation->status = Status::where('ident', 'cancelled')->first(); |
153
|
|
|
$reservation->save(); |
154
|
|
|
$reservations = $model->notCancelled()->get(); |
155
|
|
|
$this->assertEmpty($reservations); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Get testing reservation data. |
160
|
|
|
* |
161
|
|
|
* @return array |
162
|
|
|
*/ |
163
|
|
View Code Duplication |
private function getTestingReservationData() |
164
|
|
|
{ |
165
|
|
|
return [ |
166
|
|
|
'date' => Carbon::createFromFormat('Y-m-d H:i', '2016-08-18 20:00'), |
167
|
|
|
'email' => '[email protected]', |
168
|
|
|
'phone' => '777111222', |
169
|
|
|
'street' => 'ABCDE', |
170
|
|
|
'name' => 'Vojta Svoboda', |
171
|
|
|
'message' => 'Hello.', |
172
|
|
|
'status' => $this->getDefaultStatus(), |
173
|
|
|
]; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Get default status object. |
178
|
|
|
* |
179
|
|
|
* @return mixed |
180
|
|
|
*/ |
181
|
|
|
private function getDefaultStatus() |
182
|
|
|
{ |
183
|
|
|
if ($this->defaultStatus === null) { |
184
|
|
|
$statusIdent = 'received'; |
185
|
|
|
$this->defaultStatus = Status::where('ident', $statusIdent)->first(); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return $this->defaultStatus; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|