Test Failed
Push — master ( 6f4a38...4a04a2 )
by Vojta
05:36
created

testStoreReservationWorkingDays()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 2
eloc 10
nc 2
nop 0
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()
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...
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 View Code Duplication
    public function testStoreReservationWithoutTime()
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...
45
    {
46
        $model = $this->getModel();
47
48
        $this->setExpectedException('October\Rain\Exception\ApplicationException');
49
        $nextMonday = Carbon::parse('next monday')->format('d/m/Y');
50
        $model->storeReservation([
51
            'date' => $nextMonday,
52
        ]);
53
    }
54
55
    public function testStoreReservationDaysOff()
56
    {
57
        $model = $this->getModel();
58
        $default = Settings::get('work_days', ['monday', 'tuesday', 'wednesday', 'thursday', 'friday']);
59
        Settings::set('work_days', []);
60
61
        $data = $this->getTestingReservationData();
62
        foreach (['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'] as $dayOfWeek) {
63
            $exceptionTest = null;
64
            try {
65
                $data['date'] = Carbon::parse('next '.$dayOfWeek)->format('d/m/Y');
66
                $model->storeReservation($data);
67
            } catch (\Exception $exception) {
68
                $exceptionTest = $exception;
69
            }
70
            $this->assertEquals('October\Rain\Exception\ApplicationException', get_class($exceptionTest));
71
            $this->assertEquals('vojtasvoboda.reservations::lang.errors.days_off', $exceptionTest->getMessage());
72
        }
73
74
        Settings::set('work_days', $default);
75
    }
76
77
    public function testStoreReservationWorkingDays()
78
    {
79
        $default = Config::get('vojtasvoboda.reservations::config.protection_time', '-30 seconds');
80
        Config::set('vojtasvoboda.reservations::config.protection_time', '0 seconds');
81
        $model = $this->getModel();
82
        Settings::set('work_days', ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']);
83
84
        $data = $this->getTestingReservationData();
85
        foreach (['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'] as $dayOfWeek) {
86
            $data['date'] = Carbon::parse('next '.$dayOfWeek)->format('d/m/Y');
87
            $model->storeReservation($data);
88
        }
89
90
        Config::set('vojtasvoboda.reservations::config.protection_time', $default);
91
    }
92
93
    public function testStoreReservationOutOfHours()
94
    {
95
        $model = $this->getModel();
96
97
        $data = $this->getTestingReservationData();
98
        $data['time'] = '19:00';
99
100
        $this->setExpectedException('October\Rain\Exception\ApplicationException');
101
        $model->storeReservation($data);
102
    }
103
104 View Code Duplication
    public function testStoreReservationInThePast()
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...
105
    {
106
        $model = $this->getModel();
107
108
        $data = $this->getTestingReservationData();
109
        $data['date'] = Carbon::parse("last monday - 7 days")->format('d/m/Y');
110
111
        $this->setExpectedException('October\Rain\Exception\ApplicationException');
112
        $model->storeReservation($data);
113
    }
114
115
    public function testStoreReservation()
116
    {
117
        $model = $this->getModel();
118
        $reservation = $model->storeReservation($this->getTestingReservationData());
119
120
        // check status
121
        $defaultStatusIdent = Config::get('vojtasvoboda.reservations::config.statuses.received', 'received');
122
        $this->assertEquals($defaultStatusIdent, $reservation->status->ident);
123
124
        // check locale
125
        $locale = App::getLocale();
126
        $this->assertEquals($locale, $reservation->locale);
127
128
        // check date and time
129
        $testingData = $this->getTestingReservationData();
130
        $inputDate = $testingData['date'] . ' ' . $testingData['time'];
131
        $dateTime = Carbon::createFromFormat('d/m/Y H:i', $inputDate);
132
        $this->assertEquals($dateTime, $reservation->date);
133
    }
134
135
    public function testDoubleStoreReservationUnder30Seconds()
136
    {
137
        $model = $this->getModel();
138
        $testingData = $this->getTestingReservationData();
139
        $model->storeReservation($testingData);
140
141
        $this->setExpectedException('October\Rain\Exception\ApplicationException');
142
        $model->storeReservation($testingData);
143
    }
144
145
    public function testTransformDateTime()
146
    {
147
        $model = $this->getModel();
148
149
        $nextMonday = Carbon::parse('next monday');
150
        $data = [
151
            'date' => $nextMonday->format('d/m/Y'),
152
            'time' => '15:45',
153
        ];
154
        $date = $model->transformDateTime($data);
155
156
        $this->assertInstanceOf('Carbon\Carbon', $date);
157
        $this->assertEquals($nextMonday->format('Y-m-d').' 15:45:00', $date->format('Y-m-d H:i:s'));
158
    }
159
160
    public function testGetReservationsCountByMail()
161
    {
162
        $model = $this->getModel();
163
164
        // create one reservation with [email protected] email
165
        $model->storeReservation($this->getTestingReservationData());
166
167
        $count = $model->getReservationsCountByMail('[email protected]');
168
        $this->assertEquals(0, $count);
169
170
        $count = $model->getReservationsCountByMail('[email protected]');
171
        $this->assertEquals(1, $count);
172
    }
173
174
    public function testIsUserReturning()
175
    {
176
        $model = $this->getModel();
177
178
        // enable Returning Customers function
179
        Settings::set('returning_mark', 1);
180
181
        // is returning without any reservation?
182
        $isReturning = $model->isUserReturning('[email protected]');
183
        $this->assertEquals(false, $isReturning, 'There is no reservation, so customer cant be returning.');
184
185
        // create one reservation with [email protected] email
186
        $model->storeReservation($this->getTestingReservationData());
187
188
        // is returning without any reservation?
189
        $isReturning = $model->isUserReturning('[email protected]');
190
        $this->assertEquals(false, $isReturning, 'Email [email protected] does not has any reservation, so it should not be marked as returning customer.');
191
192
        // is returning with one reservation?
193
        $isReturning = $model->isUserReturning('[email protected]');
194
        $this->assertEquals(true, $isReturning, 'Email [email protected] has one reservation, so it should be marked as returning customer.');
195
    }
196
197
    public function testIsCreatedWhileAgo()
198
    {
199
        $model = $this->getModel();
200
        $exists = $model->isCreatedWhileAgo();
201
202
        $this->assertFalse($exists);
203
204
        // create fake reservation
205
        $model->storeReservation($this->getTestingReservationData());
206
        $exists = $model->isCreatedWhileAgo();
207
208
        $this->assertTrue($exists);
209
    }
210
211 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...
212
    {
213
        $nextMonday = Carbon::parse('next monday')->format('d/m/Y');
214
215
        return [
216
            'date' => $nextMonday,
217
            'time' => '11:00',
218
            'email' => '[email protected]',
219
            'phone' => '777111222',
220
            'street' => 'ABCDE',
221
            'name' => 'Vojta Svoboda',
222
            'message' => 'Hello.',
223
        ];
224
    }
225
}
226