Passed
Push — master ( 513688...efac60 )
by Adrien
15:52
created

BookingRepositoryTest::modelToIds()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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