Passed
Pull Request — master (#13)
by Vojta
06:53
created

ReservationTest::getModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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()
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 testGetHash()
100
    {
101
        $model = $this->getModel();
102
103
        $firstHash = $model->getUniqueHash();
104
        $secondHash = $model->getUniqueHash();
105
106
        $this->assertNotEquals($firstHash, $secondHash);
107
    }
108
109
    public function testGetEmptyHash()
110
    {
111
        $model = $this->getModel();
112
        Config::set('vojtasvoboda.reservations::config.hash', 0);
113
        $this->assertNull($model->getUniqueHash());
114
    }
115
116
    public function testGetNumber()
117
    {
118
        $model = $this->getModel();
119
120
        $firstNumber = $model->getUniqueNumber();
121
        $secondNumber = $model->getUniqueNumber();
122
123
        $this->assertNotEquals($firstNumber, $secondNumber);
124
    }
125
126
    public function testGetEmptyNumber()
127
    {
128
        $model = $this->getModel();
129
        Config::set('vojtasvoboda.reservations::config.number.min', 0);
130
        $this->assertNull($model->getUniqueNumber());
131
    }
132
133
    public function testScopeNotCancelled()
134
    {
135
        $model = $this->getModel();
136
137
        // create reservation
138
        $reservation = $model->create($this->getTestingReservationData());
139
        $reservations = $model->notCancelled()->get();
140
        $this->assertNotEmpty($reservations);
141
142
        // change reservation to cancelled
143
        $reservation->status = Status::where('ident', 'cancelled')->first();
144
        $reservation->save();
145
        $reservations = $model->notCancelled()->get();
146
        $this->assertEmpty($reservations);
147
    }
148
149
    /**
150
     * Get testing reservation data.
151
     *
152
     * @return array
153
     */
154 View Code Duplication
    private function getTestingReservationData()
155
    {
156
        return [
157
            'date' => Carbon::createFromFormat('Y-m-d H:i', '2016-08-18 20:00'),
158
            'email' => '[email protected]',
159
            'phone' => '777111222',
160
            'street' => 'ABCDE',
161
            'name' => 'Vojta Svoboda',
162
            'message' => 'Hello.',
163
            'status' => $this->getDefaultStatus(),
164
        ];
165
    }
166
167
    /**
168
     * Get default status object.
169
     *
170
     * @return mixed
171
     */
172
    private function getDefaultStatus()
173
    {
174
        if ($this->defaultStatus === null) {
175
            $statusIdent = 'received';
176
            $this->defaultStatus = Status::where('ident', $statusIdent)->first();
177
        }
178
179
        return $this->defaultStatus;
180
    }
181
}
182