ReservationTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 0
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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, 'vojtasvoboda.reservations::lang.errors.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
        $nextMonday = Carbon::parse('next monday')->format('Y-m-d 13:00');
76
        $data['date'] = Carbon::createFromFormat('Y-m-d H:i', $nextMonday);
77
        $model->create($data);
78
    }
79
80
    public function testIsDateAvailableForCancelled()
81
    {
82
        $model = $this->getModel();
83
84
        // create reservation
85
        $reservation = $model->create($this->getTestingReservationData());
86
87
        // cancel status
88
        $cancelledStatuses = Config::get('vojtasvoboda.reservations::config.statuses.cancelled', ['cancelled']);
89
        $statusIdent = empty($cancelledStatuses) ? 'cancelled' : $cancelledStatuses[0];
90
91
        // cancell reservation
92
        $reservation->status = Status::where('ident', $statusIdent)->first();
93
        $reservation->save();
94
95
        // try to do second reservation with same date and time
96
        $data = $this->getTestingReservationData();
97
        $model->create($data);
98
    }
99
100
    public function testIsCancelled()
101
    {
102
        $model = $this->getModel();
103
104
        $reservation = $model->create($this->getTestingReservationData());
105
        $this->assertFalse($reservation->isCancelled());
106
        $this->assertTrue($reservation->isCancelled('cancelled'));
107
    }
108
109
    public function testGetHash()
110
    {
111
        $model = $this->getModel();
112
113
        $firstHash = $model->getUniqueHash();
114
        $secondHash = $model->getUniqueHash();
115
116
        $this->assertNotEquals($firstHash, $secondHash);
117
    }
118
119
    public function testGetEmptyHash()
120
    {
121
        $model = $this->getModel();
122
        Config::set('vojtasvoboda.reservations::config.hash', 0);
123
        $this->assertNull($model->getUniqueHash());
124
    }
125
126
    public function testGetNumber()
127
    {
128
        $model = $this->getModel();
129
130
        $firstNumber = $model->getUniqueNumber();
131
        $secondNumber = $model->getUniqueNumber();
132
133
        $this->assertNotEquals($firstNumber, $secondNumber);
134
    }
135
136
    public function testGetEmptyNumber()
137
    {
138
        $model = $this->getModel();
139
        Config::set('vojtasvoboda.reservations::config.number.min', 0);
140
        $this->assertNull($model->getUniqueNumber());
141
    }
142
143 View Code Duplication
    public function testScopeNotCancelled()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
144
    {
145
        $model = $this->getModel();
146
147
        // create reservation
148
        $reservation = $model->create($this->getTestingReservationData());
149
        $reservations = $model->notCancelled()->get();
150
        $this->assertNotEmpty($reservations);
151
152
        // change reservation to cancelled
153
        $reservation->status = Status::where('ident', 'cancelled')->first();
154
        $reservation->save();
155
        $reservations = $model->notCancelled()->get();
156
        $this->assertEmpty($reservations);
157
    }
158
159 View Code Duplication
    public function testScopeCurrentDate()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
160
    {
161
        $model = $this->getModel();
162
163
        // create reservation
164
        $reservation = $model->create($this->getTestingReservationData());
165
        $reservations = $model->currentDate()->get();
166
        $this->assertNotEmpty($reservations);
167
168
        // change reservation to the past
169
        $reservation->date = Carbon::parse('-1 month');
170
        $reservation->save();
171
        $reservations = $model->currentDate()->get();
172
        $this->assertEmpty($reservations);
173
    }
174
175
    /**
176
     * Get testing reservation data.
177
     *
178
     * @return array
179
     */
180 View Code Duplication
    private function getTestingReservationData()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
181
    {
182
        $nextMonday = Carbon::parse('next monday')->format('Y-m-d 11:00');
183
184
        return [
185
            'date' => Carbon::createFromFormat('Y-m-d H:i', $nextMonday),
186
            'email' => '[email protected]',
187
            'phone' => '777111222',
188
            'street' => 'ABCDE',
189
            'name' => 'Vojta Svoboda',
190
            'message' => 'Hello.',
191
            'status' => $this->getDefaultStatus(),
192
        ];
193
    }
194
195
    /**
196
     * Get default status object.
197
     *
198
     * @return mixed
199
     */
200
    private function getDefaultStatus()
201
    {
202
        if ($this->defaultStatus === null) {
203
            $statusIdent = 'received';
204
            $this->defaultStatus = Status::where('ident', $statusIdent)->first();
205
        }
206
207
        return $this->defaultStatus;
208
    }
209
}
210