1
|
|
|
<?php namespace VojtaSvoboda\Reservations\Tests\Facades; |
2
|
|
|
|
3
|
|
|
use App; |
4
|
|
|
use Carbon\Carbon; |
5
|
|
|
use Config; |
6
|
|
|
use Illuminate\Support\Facades\Validator; |
7
|
|
|
use PluginTestCase; |
8
|
|
|
use VojtaSvoboda\Reservations\Facades\ReservationsFacade; |
9
|
|
|
use VojtaSvoboda\Reservations\Models\Settings; |
10
|
|
|
use VojtaSvoboda\Reservations\Validators\ReservationsValidators; |
11
|
|
|
|
12
|
|
|
class ReservationsFacadeTest extends PluginTestCase |
13
|
|
|
{ |
14
|
|
View Code Duplication |
public function setUp() |
15
|
|
|
{ |
16
|
|
|
parent::setUp(); |
17
|
|
|
|
18
|
|
|
$this->app->bind('vojtasvoboda.reservations.facade', ReservationsFacade::class); |
19
|
|
|
|
20
|
|
|
// registrate reservations validators |
21
|
|
|
Validator::resolver(function($translator, $data, $rules, $messages, $customAttributes) { |
22
|
|
|
return new ReservationsValidators($translator, $data, $rules, $messages, $customAttributes); |
23
|
|
|
}); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Returns tested class. |
28
|
|
|
* |
29
|
|
|
* @return ReservationsFacade |
30
|
|
|
*/ |
31
|
|
|
public function getModel() |
32
|
|
|
{ |
33
|
|
|
return App::make(ReservationsFacade::class); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testStoreEmptyReservation() |
37
|
|
|
{ |
38
|
|
|
$model = $this->getModel(); |
39
|
|
|
|
40
|
|
|
$this->setExpectedException('October\Rain\Exception\ApplicationException'); |
41
|
|
|
$model->storeReservation([]); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testStoreReservationWithoutTime() |
45
|
|
|
{ |
46
|
|
|
$model = $this->getModel(); |
47
|
|
|
|
48
|
|
|
$this->setExpectedException('October\Rain\Exception\ApplicationException'); |
49
|
|
|
$model->storeReservation([ |
50
|
|
|
'date' => '18/08/2016', |
51
|
|
|
]); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testStoreReservation() |
55
|
|
|
{ |
56
|
|
|
$model = $this->getModel(); |
57
|
|
|
$reservation = $model->storeReservation($this->getTestingReservationData()); |
58
|
|
|
|
59
|
|
|
// check status |
60
|
|
|
$defaultStatusIdent = Config::get('vojtasvoboda.reservations::config.statuses.received', 'received'); |
61
|
|
|
$this->assertEquals($defaultStatusIdent, $reservation->status->ident); |
62
|
|
|
|
63
|
|
|
// check locale |
64
|
|
|
$locale = App::getLocale(); |
65
|
|
|
$this->assertEquals($locale, $reservation->locale); |
66
|
|
|
|
67
|
|
|
// check date and time |
68
|
|
|
$testingData = $this->getTestingReservationData(); |
69
|
|
|
$inputDate = $testingData['date'] . ' ' . $testingData['time']; |
70
|
|
|
$dateTime = Carbon::createFromFormat('d/m/Y H:i', $inputDate); |
71
|
|
|
$this->assertEquals($dateTime, $reservation->date); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testDoubleStoreReservationUnder30Seconds() |
75
|
|
|
{ |
76
|
|
|
$model = $this->getModel(); |
77
|
|
|
$testingData = $this->getTestingReservationData(); |
78
|
|
|
$model->storeReservation($testingData); |
79
|
|
|
|
80
|
|
|
$this->setExpectedException('October\Rain\Exception\ApplicationException'); |
81
|
|
|
$model->storeReservation($testingData); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testTransformDateTime() |
85
|
|
|
{ |
86
|
|
|
$model = $this->getModel(); |
87
|
|
|
|
88
|
|
|
$data = [ |
89
|
|
|
'date' => '08/10/2016', |
90
|
|
|
'time' => '15:45', |
91
|
|
|
]; |
92
|
|
|
$date = $model->transformDateTime($data); |
93
|
|
|
|
94
|
|
|
$this->assertInstanceOf('Carbon\Carbon', $date); |
95
|
|
|
$this->assertEquals('2016-10-08 15:45:00', $date->format('Y-m-d H:i:s')); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testGetReservationsCountByMail() |
99
|
|
|
{ |
100
|
|
|
$model = $this->getModel(); |
101
|
|
|
|
102
|
|
|
// create one reservation with [email protected] email |
103
|
|
|
$model->storeReservation($this->getTestingReservationData()); |
104
|
|
|
|
105
|
|
|
$count = $model->getReservationsCountByMail('[email protected]'); |
106
|
|
|
$this->assertEquals(0, $count); |
107
|
|
|
|
108
|
|
|
$count = $model->getReservationsCountByMail('[email protected]'); |
109
|
|
|
$this->assertEquals(1, $count); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function testIsUserReturning() |
113
|
|
|
{ |
114
|
|
|
$model = $this->getModel(); |
115
|
|
|
|
116
|
|
|
// enable Returning Customers function |
117
|
|
|
Settings::set('returning_mark', 1); |
118
|
|
|
|
119
|
|
|
// is returning without any reservation? |
120
|
|
|
$isReturning = $model->isUserReturning('[email protected]'); |
121
|
|
|
$this->assertEquals(false, $isReturning, 'There is no reservation, so customer cant be returning.'); |
122
|
|
|
|
123
|
|
|
// create one reservation with [email protected] email |
124
|
|
|
$model->storeReservation($this->getTestingReservationData()); |
125
|
|
|
|
126
|
|
|
// is returning without any reservation? |
127
|
|
|
$isReturning = $model->isUserReturning('[email protected]'); |
128
|
|
|
$this->assertEquals(false, $isReturning, 'Email [email protected] does not has any reservation, so it should not be marked as returning customer.'); |
129
|
|
|
|
130
|
|
|
// is returning with one reservation? |
131
|
|
|
$isReturning = $model->isUserReturning('[email protected]'); |
132
|
|
|
$this->assertEquals(true, $isReturning, 'Email [email protected] has one reservation, so it should be marked as returning customer.'); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function testIsCreatedWhileAgo() |
136
|
|
|
{ |
137
|
|
|
$model = $this->getModel(); |
138
|
|
|
$exists = $model->isCreatedWhileAgo(); |
139
|
|
|
|
140
|
|
|
$this->assertFalse($exists); |
141
|
|
|
|
142
|
|
|
// create fake reservation |
143
|
|
|
$model->storeReservation($this->getTestingReservationData()); |
144
|
|
|
$exists = $model->isCreatedWhileAgo(); |
145
|
|
|
|
146
|
|
|
$this->assertTrue($exists); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
View Code Duplication |
private function getTestingReservationData() |
150
|
|
|
{ |
151
|
|
|
return [ |
152
|
|
|
'date' => '18/08/2016', |
153
|
|
|
'time' => '20:00', |
154
|
|
|
'email' => '[email protected]', |
155
|
|
|
'phone' => '777111222', |
156
|
|
|
'street' => 'ABCDE', |
157
|
|
|
'name' => 'Vojta Svoboda', |
158
|
|
|
'message' => 'Hello.', |
159
|
|
|
]; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|