Completed
Push — master ( dc6e13...340cc5 )
by Adrien
07:39
created

BookingRepositoryTest::testGetAllToInvoice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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