Passed
Pull Request — master (#20)
by
unknown
08:32
created

ReservationsFacadeTest::testStoreReservation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
rs 9.4285
cc 1
eloc 11
nc 1
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()
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' => (new \DateTime('next monday'))->format('d/m/Y'),
51
        ]);
52
    }
53
54 View Code Duplication
    public function testStoreReservationDaysOff()
1 ignored issue
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...
55
    {
56
        $model = $this->getModel();
57
58
        $this->setExpectedException('October\Rain\Exception\ApplicationException');
59
        $data = $this->getTestingReservationData();
60
        $data['date'] = (new \DateTime('next sunday'))->format('d/m/Y');
61
62
        $model->storeReservation($data);
63
    }
64
65 View Code Duplication
    public function testStoreReservationOutOfHours()
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...
66
    {
67
        $model = $this->getModel();
68
69
        $data = $this->getTestingReservationData();
70
        $data['time'] = '19:00';
71
72
        $this->setExpectedException('October\Rain\Exception\ApplicationException');
73
        $model->storeReservation($data);
74
    }
75
76
    public function testStoreReservation()
77
    {
78
        $model = $this->getModel();
79
        $reservation = $model->storeReservation($this->getTestingReservationData());
80
81
        // check status
82
        $defaultStatusIdent = Config::get('vojtasvoboda.reservations::config.statuses.received', 'received');
83
        $this->assertEquals($defaultStatusIdent, $reservation->status->ident);
84
85
        // check locale
86
        $locale = App::getLocale();
87
        $this->assertEquals($locale, $reservation->locale);
88
89
        // check date and time
90
        $testingData = $this->getTestingReservationData();
91
        $inputDate = $testingData['date'] . ' ' . $testingData['time'];
92
        $dateTime = Carbon::createFromFormat('d/m/Y H:i', $inputDate);
93
        $this->assertEquals($dateTime, $reservation->date);
94
    }
95
96
    public function testDoubleStoreReservationUnder30Seconds()
97
    {
98
        $model = $this->getModel();
99
        $testingData = $this->getTestingReservationData();
100
        $model->storeReservation($testingData);
101
102
        $this->setExpectedException('October\Rain\Exception\ApplicationException');
103
        $model->storeReservation($testingData);
104
    }
105
106
    public function testTransformDateTime()
107
    {
108
        $model = $this->getModel();
109
110
        $dateTime = new \DateTime('next monday');
111
        $data = [
112
            'date' => $dateTime->format('d/m/Y'),
113
            'time' => '15:45',
114
        ];
115
        $date = $model->transformDateTime($data);
116
117
        $this->assertInstanceOf('Carbon\Carbon', $date);
118
        $this->assertEquals($dateTime->format('Y-m-d').' 15:45:00', $date->format('Y-m-d H:i:s'));
119
    }
120
121
    public function testGetReservationsCountByMail()
122
    {
123
        $model = $this->getModel();
124
125
        // create one reservation with [email protected] email
126
        $model->storeReservation($this->getTestingReservationData());
127
128
        $count = $model->getReservationsCountByMail('[email protected]');
129
        $this->assertEquals(0, $count);
130
131
        $count = $model->getReservationsCountByMail('[email protected]');
132
        $this->assertEquals(1, $count);
133
    }
134
135
    public function testIsUserReturning()
136
    {
137
        $model = $this->getModel();
138
139
        // enable Returning Customers function
140
        Settings::set('returning_mark', 1);
141
142
        // is returning without any reservation?
143
        $isReturning = $model->isUserReturning('[email protected]');
144
        $this->assertEquals(false, $isReturning, 'There is no reservation, so customer cant be returning.');
145
146
        // create one reservation with [email protected] email
147
        $model->storeReservation($this->getTestingReservationData());
148
149
        // is returning without any reservation?
150
        $isReturning = $model->isUserReturning('[email protected]');
151
        $this->assertEquals(false, $isReturning, 'Email [email protected] does not has any reservation, so it should not be marked as returning customer.');
152
153
        // is returning with one reservation?
154
        $isReturning = $model->isUserReturning('[email protected]');
155
        $this->assertEquals(true, $isReturning, 'Email [email protected] has one reservation, so it should be marked as returning customer.');
156
    }
157
158
    public function testIsCreatedWhileAgo()
159
    {
160
        $model = $this->getModel();
161
        $exists = $model->isCreatedWhileAgo();
162
163
        $this->assertFalse($exists);
164
165
        // create fake reservation
166
        $model->storeReservation($this->getTestingReservationData());
167
        $exists = $model->isCreatedWhileAgo();
168
169
        $this->assertTrue($exists);
170
    }
171
172 View Code Duplication
    private function getTestingReservationData()
173
    {
174
        return [
175
            'date' => (new \DateTime('next monday'))->format('d/m/Y'),
176
            'time' => '11:00',
177
            'email' => '[email protected]',
178
            'phone' => '777111222',
179
            'street' => 'ABCDE',
180
            'name' => 'Vojta Svoboda',
181
            'message' => 'Hello.',
182
        ];
183
    }
184
}
185