Passed
Push — master ( b3e1ed...202dba )
by Adrien
07:28
created

BookingRepositoryTest::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ApplicationTest\Repository;
6
7
use Application\DBAL\Types\BookingStatusType;
8
use Application\DBAL\Types\BookingTypeType;
9
use Application\Model\Booking;
10
use Application\Model\User;
11
use Application\Repository\BookingRepository;
12
use Cake\Chronos\Date;
13
14
class BookingRepositoryTest extends AbstractRepositoryTest
15
{
16
    /**
17
     * @var BookingRepository
18
     */
19
    private $repository;
20
21
    public function setUp(): void
22
    {
23
        parent::setUp();
24
        $this->repository = _em()->getRepository(Booking::class);
25
        Date::setTestNow(new Date('2020-01-01'));
26
    }
27
28
    public function tearDown(): void
29
    {
30
        Date::setTestNow(null);
31
        parent::tearDown();
32
    }
33
34
    public function testGetAllToInvoice(): void
35
    {
36
        $user = _em()->getRepository(User::class)->getByLogin('administrator');
37
        User::setCurrent($user);
38
39
        $bookings = $this->repository->getAllToInvoice();
40
        $actual = [];
41
        foreach ($bookings as $a) {
42
            $actual[] = $a->getId();
43
        }
44
45
        $expected = [
46
            4004,
47
            4005,
48
        ];
49
50
        self::assertSame($expected, $actual);
51
    }
52
53
    private function insertTestData(array $data): void
54
    {
55
        $connection = $this->getEntityManager()->getConnection();
56
        $connection->delete('booking', [1 => 1]);
57
58
        foreach ($data as $user) {
59
            $bookings = $user['bookings'] ?? [];
60
            $account = $user['account'] ?? null;
61
62
            unset($user['bookings'], $user['account']);
63
            $connection->insert('user', $user);
64
            $userId = $connection->lastInsertId();
65
66
            foreach ($bookings as $booking) {
67
                $booking['owner_id'] = $userId;
68
69
                $bookable = $booking['bookable'] ?? null;
70
                unset($booking['bookable']);
71
72
                if ($bookable) {
73
                    $connection->insert('bookable', $bookable);
74
                    $bookableId = $connection->lastInsertId();
75
                    $booking['bookable_id'] = $bookableId;
76
                }
77
78
                $connection->insert('booking', $booking);
79
            }
80
81
            if ($account) {
82
                $connection->insert('account', [
83
                    'owner_id' => $userId,
84
                ]);
85
                $accountId = $connection->lastInsertId();
86
87
                $connection->insert('transaction', []);
88
                $transactionId = $connection->lastInsertId();
89
90
                foreach ($account['transaction_lines'] as $line) {
91
                    $line['transaction_id'] = $transactionId;
92
                    $line['debit_id'] = $accountId;
93
94
                    // Automatically link to last bookable created
95
                    $line['bookable_id'] = $bookableId ?? null;
96
97
                    $connection->insert('transaction_line', $line);
98
                }
99
            }
100
        }
101
    }
102
103
    /**
104
     * @dataProvider providerGetAllToInvoice
105
     */
106
    public function testGetAllToInvoiceAllCases(array $data, array $expected): void
107
    {
108
        $this->insertTestData($data);
109
110
        $user = _em()->getRepository(User::class)->getByLogin('administrator');
111
        User::setCurrent($user);
112
113
        $bookings = $this->repository->getAllToInvoice();
114
115
        $actual = [];
116
        foreach ($bookings as $a) {
117
            $actual[] = $a->getBookable()->getName();
118
        }
119
120
        self::assertSame($expected, $actual);
121
    }
122
123
    public function providerGetAllToInvoice(): array
124
    {
125
        $normal = [
126
            [
127
                'role' => User::ROLE_MEMBER,
128
                'status' => User::STATUS_ACTIVE,
129
                'bookings' => [
130
                    [
131
                        'start_date' => '2019-02-25',
132
                        'end_date' => null,
133
                        'status' => BookingStatusType::BOOKED,
134
                        'bookable' => [
135
                            'name' => 'cotisation',
136
                            'booking_type' => BookingTypeType::MANDATORY,
137
                            'is_active' => true,
138
                            'periodic_price' => '25.00',
139
                        ],
140
                    ],
141
                    [
142
                        'start_date' => '2020-01-01',
143
                        'end_date' => null,
144
                        'status' => BookingStatusType::BOOKED,
145
                        'bookable' => [
146
                            'name' => 'casier',
147
                            'booking_type' => BookingTypeType::ADMIN_ONLY,
148
                            'is_active' => true,
149
                            'periodic_price' => '25.00',
150
                        ],
151
                    ],
152
                ],
153
                'account' => [
154
                    'transaction_lines' => [
155
                        [
156
                            'balance' => '5.00',
157
                            'transactionDate' => '2019-02-25',
158
                        ],
159
                    ],
160
                ],
161
            ],
162
        ];
163
164
        $inactiveUser = $normal;
165
        $inactiveUser[0]['status'] = User::STATUS_INACTIVE;
166
167
        $archivedUser = $normal;
168
        $archivedUser[0]['status'] = User::STATUS_ARCHIVED;
169
170
        $newUser = $normal;
171
        $newUser[0]['status'] = User::STATUS_NEW;
172
173
        $anonymousUser = $normal;
174
        $anonymousUser[0]['role'] = User::ROLE_ANONYMOUS;
175
176
        $bookingOnlyUser = $normal;
177
        $bookingOnlyUser[0]['role'] = User::ROLE_BOOKING_ONLY;
178
179
        $individualUser = $normal;
180
        $individualUser[0]['role'] = User::ROLE_INDIVIDUAL;
181
182
        $responsibleUser = $normal;
183
        $responsibleUser[0]['role'] = User::ROLE_RESPONSIBLE;
184
185
        $administratorUser = $normal;
186
        $administratorUser[0]['role'] = User::ROLE_ADMINISTRATOR;
187
188
        $futureBookingThisYear = $normal;
189
        $futureBookingThisYear[0]['bookings'][0]['start_date'] = '2019-12-31';
190
191
        $futureBookingNextYear = $normal;
192
        $futureBookingNextYear[0]['bookings'][0]['start_date'] = '2021-01-01';
193
194
        $terminatedBooking = $normal;
195
        $terminatedBooking[0]['bookings'][0]['end_date'] = '2019-01-01';
196
197
        $terminatedBooking = $normal;
198
        $terminatedBooking[0]['bookings'][0]['end_date'] = '2019-01-01';
199
200
        $terminatedBookingNextYear = $normal;
201
        $terminatedBookingNextYear[0]['bookings'][0]['end_date'] = '2021-01-01';
202
203
        $applicationBooking = $normal;
204
        $applicationBooking[0]['bookings'][0]['status'] = BookingStatusType::APPLICATION;
205
206
        $processedBooking = $normal;
207
        $processedBooking[0]['bookings'][0]['status'] = BookingStatusType::PROCESSED;
208
209
        $selfApprovedBookable = $normal;
210
        $selfApprovedBookable[0]['bookings'][0]['bookable']['booking_type'] = BookingTypeType::SELF_APPROVED;
211
212
        $adminApprovedBookable = $normal;
213
        $adminApprovedBookable[0]['bookings'][0]['bookable']['booking_type'] = BookingTypeType::ADMIN_APPROVED;
214
215
        $inactiveBookable = $normal;
216
        $inactiveBookable[0]['bookings'][0]['bookable']['is_active'] = false;
217
218
        $freeBookable = $normal;
219
        $freeBookable[0]['bookings'][0]['bookable']['periodic_price'] = 0;
220
221
        $negativeBookable = $normal;
222
        $negativeBookable[0]['bookings'][0]['bookable']['periodic_price'] = -10;
223
224
        $existingTransactionThisYear = $normal;
225
        $existingTransactionThisYear[0]['account']['transaction_lines'][0]['transactionDate'] = '2020-02-01';
226
227
        $existingTransactionNextYear = $normal;
228
        $existingTransactionNextYear[0]['account']['transaction_lines'][0]['transactionDate'] = '2021-02-01';
229
230
        return [
231
            'normal user get cotisation and storage' => [
232
                $normal,
233
                ['casier', 'cotisation'],
234
            ],
235
            'inactive user get only storage' => [
236
                $inactiveUser,
237
                ['casier'],
238
            ],
239
            'archived user get nothing' => [
240
                $archivedUser,
241
                [],
242
            ],
243
            'new user get nothing' => [
244
                $newUser,
245
                [],
246
            ],
247
            'anonymous user get nothing' => [
248
                $anonymousUser,
249
                [],
250
            ],
251
            'bookingOnly user get nothing' => [
252
                $bookingOnlyUser,
253
                [],
254
            ],
255
            'individual user get nothing' => [
256
                $individualUser,
257
                [],
258
            ],
259
            'responsible user get cotisation and storage' => [
260
                $responsibleUser,
261
                ['casier', 'cotisation'],
262
            ],
263
            'administrator user get cotisation and storage' => [
264
                $administratorUser,
265
                ['casier', 'cotisation'],
266
            ],
267
            'future booking this year are still counted' => [
268
                $futureBookingThisYear,
269
                ['casier', 'cotisation'],
270
            ],
271
            'future booking next year are not yet counted' => [
272
                $futureBookingNextYear,
273
                ['casier'],
274
            ],
275
            'terminated booking are not counted anymore' => [
276
                $terminatedBooking,
277
                ['casier'],
278
            ],
279
            'terminated booking next year are not yet terminated so must be counted' => [
280
                $terminatedBookingNextYear,
281
                ['casier', 'cotisation'],
282
            ],
283
            'application booking is ignored' => [
284
                $applicationBooking,
285
                ['casier'],
286
            ],
287
            'processsed booking is ignored' => [
288
                $processedBooking,
289
                ['casier'],
290
            ],
291
            'self approvable bookable is not counted' => [
292
                $selfApprovedBookable,
293
                ['casier'],
294
            ],
295
            'admin approvable bookable is not counted' => [
296
                $adminApprovedBookable,
297
                ['casier'],
298
            ],
299
            'inactive bookable is not counted' => [
300
                $inactiveBookable,
301
                ['casier'],
302
            ],
303
            'free bookable is not counted' => [
304
                $freeBookable,
305
                ['casier'],
306
            ],
307
            'negative bookable is counted' => [
308
                $negativeBookable,
309
                ['casier', 'cotisation'],
310
            ],
311
            'existing transaction for this year should not be re-created' => [
312
                $existingTransactionThisYear,
313
                ['cotisation'],
314
            ],
315
            'existing transaction for next year have no impact' => [
316
                $existingTransactionNextYear,
317
                ['casier', 'cotisation'],
318
            ],
319
        ];
320
    }
321
}
322