Skip to content

Commit 8eb6e8a

Browse files
committed
Add tests to check client and animal integrity
1 parent 4c4c116 commit 8eb6e8a

File tree

1 file changed

+66
-21
lines changed

1 file changed

+66
-21
lines changed
Lines changed: 66 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
<?php
22

3+
use App\Models\Client;
34
use App\TimeOfDay;
45
use Database\Factories\AnimalFactory;
56

6-
// TODO Update client name
7+
$VALID_APPOINTMENT = [
8+
'client' => [
9+
'name' => fake()->name,
10+
'email' => fake()->email,
11+
],
12+
'animal' => [
13+
'name' => fake()->randomElement(AnimalFactory::NAMES),
14+
'type' => fake()->randomElement(AnimalFactory::TYPES),
15+
'age_years' => fake()->numberBetween(1, 20),
16+
'age_months' => fake()->numberBetween(0, 12),
17+
],
18+
'appointment' => [
19+
'preferred_date' => fake()->dateTimeInInterval(now(), '+30 days')->format('Y-m-d'),
20+
'preferred_time' => [fake()->randomElement(TimeOfDay::selectable())->value],
21+
'symptoms' => fake()->realText(),
22+
],
23+
];
724

825
test('can not empty schedule appointment', function () {
926
$response = $this
@@ -16,38 +33,66 @@
1633
$this->assertDatabaseEmpty('appointments');
1734
});
1835

19-
test('can schedule appointment', function () {
20-
$data = [
21-
'client' => [
22-
'name' => fake()->name,
23-
'email' => fake()->email,
24-
],
25-
'animal' => [
26-
'name' => fake()->randomElement(AnimalFactory::NAMES),
27-
'type' => fake()->randomElement(AnimalFactory::TYPES),
28-
'age_years' => fake()->numberBetween(1, 20),
29-
'age_months' => fake()->numberBetween(0, 12),
30-
],
31-
'appointment' => [
32-
'preferred_date' => fake()->dateTimeInInterval(now(), '+30 days')->format('Y-m-d'),
33-
'preferred_time' => [fake()->randomElement(TimeOfDay::selectable())->value],
34-
'symptoms' => fake()->realText(),
35-
],
36-
];
36+
test('can schedule appointment', function () use ($VALID_APPOINTMENT) {
37+
$data = $VALID_APPOINTMENT;
3738

39+
// Try to schedule an appointment
3840
$response = $this
3941
->post(route('public.schedule-appointment'), $data);
4042

43+
// Check for redirect. Since we're using Inertia, it redirects the user to the homepage on success
4144
$response->assertRedirect(route('public.schedule-appointment'));
4245

43-
$ageInMonths = ($data['animal']['age_years'] * 12) + $data['animal']['age_months'];
46+
// Check if data was saved
4447
$this->assertDatabaseHas('clients', $data['client']);
4548
$this->assertDatabaseHas(
4649
'animals',
4750
Arr::except($data['animal'], ['age_years', 'age_months']),
4851
);
4952
$this->assertDatabaseHas('appointments', [
5053
...$data['appointment'],
51-
'animal_age_months' => $ageInMonths,
54+
'animal_age_months' => ($data['animal']['age_years'] * 12) + $data['animal']['age_months'],
5255
]);
5356
});
57+
58+
test('new appointment updates client name', function () use ($VALID_APPOINTMENT) {
59+
$data = $VALID_APPOINTMENT;
60+
61+
// Create the client
62+
$clientData = ['name' => 'old name', 'email' => $data['client']['email']];
63+
Client::factory()
64+
->state($clientData)
65+
->create();
66+
67+
// Ensure it exists
68+
$this->assertDatabaseHas('clients', $clientData);
69+
70+
// Try to schedule an appointment
71+
$response = $this
72+
->post(route('public.schedule-appointment'), $data);
73+
74+
// Check for redirect. Since we're using Inertia, it redirects the user to the homepage on success
75+
$response->assertRedirect(route('public.schedule-appointment'));
76+
77+
// Check if the client was updated
78+
$this->assertDatabaseHas('clients', $data['client']);
79+
$this->assertDatabaseCount('clients', 1);
80+
});
81+
82+
test('new appointment does not recreate existing client and animal', function () use ($VALID_APPOINTMENT) {
83+
$data = $VALID_APPOINTMENT;
84+
85+
// Try to schedule an appointment, twice, with the same data
86+
for ($i = 0; $i < 2; $i++) {
87+
$response = $this
88+
->post(route('public.schedule-appointment'), $data);
89+
90+
// Check for redirect. Since we're using Inertia, it redirects the user to the homepage on success
91+
$response->assertRedirect(route('public.schedule-appointment'));
92+
}
93+
94+
// Check if the client was updated
95+
$this->assertDatabaseCount('clients', 1);
96+
$this->assertDatabaseCount('animals', 1);
97+
$this->assertDatabaseCount('appointments', 2);
98+
});

0 commit comments

Comments
 (0)