Passed
Pull Request — master (#13)
by Vojta
09:53 queued 03:21
created

testDoubleStoreReservationUnder30Seconds()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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