1
|
|
|
<?php namespace VojtaSvoboda\Reservations\Tests\Models; |
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\Reservation; |
9
|
|
|
use VojtaSvoboda\Reservations\Models\Status; |
10
|
|
|
|
11
|
|
|
class ReservationTest extends PluginTestCase |
12
|
|
|
{ |
13
|
|
|
private $defaultStatus; |
14
|
|
|
|
15
|
|
|
public function setUp() |
16
|
|
|
{ |
17
|
|
|
parent::setUp(); |
18
|
|
|
|
19
|
|
|
$this->app->bind('vojtasvoboda.reservations.facade', ReservationsFacade::class); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Get tested model. |
24
|
|
|
* |
25
|
|
|
* @return Reservation |
26
|
|
|
*/ |
27
|
|
|
public function getModel() |
28
|
|
|
{ |
29
|
|
|
return App::make(Reservation::class); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testBeforeCreate() |
33
|
|
|
{ |
34
|
|
|
$model = $this->getModel(); |
35
|
|
|
|
36
|
|
|
// create reservation |
37
|
|
|
$reservation = $model->create($this->getTestingReservationData()); |
38
|
|
|
$this->assertNotEmpty($reservation->hash, 'Reservation hash is empty.'); |
39
|
|
|
$this->assertNotEmpty($reservation->number, 'Number hash is empty.'); |
40
|
|
|
$this->assertSame(App::getLocale(), $reservation->locale, 'Reservation locale should be same as app locale.'); |
41
|
|
|
$this->assertNotEmpty($reservation->ip, 'IP address is empty.'); |
42
|
|
|
$this->assertNotEmpty($reservation->user_agent, 'User Agent is empty.'); |
43
|
|
|
$this->assertSame($this->getDefaultStatus(), $reservation->status, 'Reservation status should be ' . $this->getDefaultStatus()->name . '.'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testIsDateAvailableFailing() |
47
|
|
|
{ |
48
|
|
|
$model = $this->getModel(); |
49
|
|
|
|
50
|
|
|
// create reservation |
51
|
|
|
$model->create($this->getTestingReservationData()); |
52
|
|
|
|
53
|
|
|
// try to do second reservation with same date and time |
54
|
|
|
$this->setExpectedException('October\Rain\Exception\ApplicationException'); |
55
|
|
|
$model->create($this->getTestingReservationData()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testIsDateAvailablePassed() |
59
|
|
|
{ |
60
|
|
|
$model = $this->getModel(); |
61
|
|
|
|
62
|
|
|
// create reservation |
63
|
|
|
$model->create($this->getTestingReservationData()); |
64
|
|
|
|
65
|
|
|
// try to do second reservation with same date and time after 2 hours |
66
|
|
|
$data = $this->getTestingReservationData(); |
67
|
|
|
$data['date'] = Carbon::createFromFormat('Y-m-d H:i', '2016-08-18 22:00'); |
68
|
|
|
$model->create($data); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testIsDateAvailableForCancelled() |
72
|
|
|
{ |
73
|
|
|
$model = $this->getModel(); |
74
|
|
|
|
75
|
|
|
// create reservation |
76
|
|
|
$reservation = $model->create($this->getTestingReservationData()); |
77
|
|
|
|
78
|
|
|
// cancel status |
79
|
|
|
$cancelledStatuses = Config::get('vojtasvoboda.reservations::config.statuses.cancelled', 'cancelled'); |
80
|
|
|
$statusIdent = empty($cancelledStatuses) ? 'cancelled' : $cancelledStatuses[0]; |
81
|
|
|
|
82
|
|
|
// cancell reservation |
83
|
|
|
$reservation->status = Status::where('ident', $statusIdent)->first(); |
84
|
|
|
$reservation->save(); |
85
|
|
|
|
86
|
|
|
// try to do second reservation with same date and time |
87
|
|
|
$data = $this->getTestingReservationData(); |
88
|
|
|
$model->create($data); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testGetHash() |
92
|
|
|
{ |
93
|
|
|
$model = $this->getModel(); |
94
|
|
|
|
95
|
|
|
$firstHash = $model->getUniqueHash(); |
96
|
|
|
$secondHash = $model->getUniqueHash(); |
97
|
|
|
|
98
|
|
|
$this->assertNotEquals($firstHash, $secondHash); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function testGetEmptyHash() |
102
|
|
|
{ |
103
|
|
|
$model = $this->getModel(); |
104
|
|
|
Config::set('vojtasvoboda.reservations::config.hash', 0); |
105
|
|
|
$this->assertNull($model->getUniqueHash()); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testGetNumber() |
109
|
|
|
{ |
110
|
|
|
$model = $this->getModel(); |
111
|
|
|
|
112
|
|
|
$firstNumber = $model->getUniqueNumber(); |
113
|
|
|
$secondNumber = $model->getUniqueNumber(); |
114
|
|
|
|
115
|
|
|
$this->assertNotEquals($firstNumber, $secondNumber); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function testGetEmptyNumber() |
119
|
|
|
{ |
120
|
|
|
$model = $this->getModel(); |
121
|
|
|
Config::set('vojtasvoboda.reservations::config.number.min', 0); |
122
|
|
|
$this->assertNull($model->getUniqueNumber()); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function testScopeNotCancelled() |
126
|
|
|
{ |
127
|
|
|
$model = $this->getModel(); |
128
|
|
|
|
129
|
|
|
// create reservation |
130
|
|
|
$reservation = $model->create($this->getTestingReservationData()); |
131
|
|
|
$reservations = $model->notCancelled()->get(); |
132
|
|
|
$this->assertNotEmpty($reservations); |
133
|
|
|
|
134
|
|
|
// change reservation to cancelled |
135
|
|
|
$reservation->status = Status::where('ident', 'cancelled')->first(); |
136
|
|
|
$reservation->save(); |
137
|
|
|
$reservations = $model->notCancelled()->get(); |
138
|
|
|
$this->assertEmpty($reservations); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get testing reservation data. |
143
|
|
|
* |
144
|
|
|
* @return array |
145
|
|
|
*/ |
146
|
|
View Code Duplication |
private function getTestingReservationData() |
|
|
|
|
147
|
|
|
{ |
148
|
|
|
return [ |
149
|
|
|
'date' => Carbon::createFromFormat('Y-m-d H:i', '2016-08-18 20:00'), |
150
|
|
|
'email' => '[email protected]', |
151
|
|
|
'phone' => '777111222', |
152
|
|
|
'street' => 'ABCDE', |
153
|
|
|
'name' => 'Vojta Svoboda', |
154
|
|
|
'message' => 'Hello.', |
155
|
|
|
'status' => $this->getDefaultStatus(), |
156
|
|
|
]; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Get default status object. |
161
|
|
|
* |
162
|
|
|
* @return mixed |
163
|
|
|
*/ |
164
|
|
|
private function getDefaultStatus() |
165
|
|
|
{ |
166
|
|
|
if ($this->defaultStatus === null) { |
167
|
|
|
$statusIdent = 'received'; |
168
|
|
|
$this->defaultStatus = Status::where('ident', $statusIdent)->first(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $this->defaultStatus; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.