|
1 | 1 | <?php |
2 | 2 |
|
| 3 | +use App\Models\Client; |
3 | 4 | use App\TimeOfDay; |
4 | 5 | use Database\Factories\AnimalFactory; |
5 | 6 |
|
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 | +]; |
7 | 24 |
|
8 | 25 | test('can not empty schedule appointment', function () { |
9 | 26 | $response = $this |
|
16 | 33 | $this->assertDatabaseEmpty('appointments'); |
17 | 34 | }); |
18 | 35 |
|
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; |
37 | 38 |
|
| 39 | + // Try to schedule an appointment |
38 | 40 | $response = $this |
39 | 41 | ->post(route('public.schedule-appointment'), $data); |
40 | 42 |
|
| 43 | + // Check for redirect. Since we're using Inertia, it redirects the user to the homepage on success |
41 | 44 | $response->assertRedirect(route('public.schedule-appointment')); |
42 | 45 |
|
43 | | - $ageInMonths = ($data['animal']['age_years'] * 12) + $data['animal']['age_months']; |
| 46 | + // Check if data was saved |
44 | 47 | $this->assertDatabaseHas('clients', $data['client']); |
45 | 48 | $this->assertDatabaseHas( |
46 | 49 | 'animals', |
47 | 50 | Arr::except($data['animal'], ['age_years', 'age_months']), |
48 | 51 | ); |
49 | 52 | $this->assertDatabaseHas('appointments', [ |
50 | 53 | ...$data['appointment'], |
51 | | - 'animal_age_months' => $ageInMonths, |
| 54 | + 'animal_age_months' => ($data['animal']['age_years'] * 12) + $data['animal']['age_months'], |
52 | 55 | ]); |
53 | 56 | }); |
| 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