|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tests\Feature; |
| 6 | + |
| 7 | +use App\Models\Appliance; |
| 8 | +use App\Models\AppliancePerson; |
| 9 | +use App\Models\ApplianceRate; |
| 10 | +use App\Models\Device; |
| 11 | +use App\Models\Log; |
| 12 | +use Database\Factories\AppliancePersonFactory; |
| 13 | +use Database\Factories\ApplianceTypeFactory; |
| 14 | +use Database\Factories\Person\PersonFactory; |
| 15 | +use Tests\CreateEnvironments; |
| 16 | +use Tests\TestCase; |
| 17 | + |
| 18 | +class AppliancePersonDeleteTest extends TestCase { |
| 19 | + use CreateEnvironments; |
| 20 | + |
| 21 | + public function testSoftDeletesAppliancePerson(): void { |
| 22 | + $this->createTestData(); |
| 23 | + $appliancePerson = $this->seedAppliance(); |
| 24 | + |
| 25 | + $response = $this->actingAs($this->user)->delete( |
| 26 | + "/api/appliances/person/{$appliancePerson->id}", |
| 27 | + ['admin_id' => $this->user->id] |
| 28 | + ); |
| 29 | + |
| 30 | + $response->assertStatus(200); |
| 31 | + $this->assertNull(AppliancePerson::query()->find($appliancePerson->id)); |
| 32 | + $this->assertNotNull(AppliancePerson::query()->withTrashed()->find($appliancePerson->id)); |
| 33 | + } |
| 34 | + |
| 35 | + public function testReleasesBoundDevice(): void { |
| 36 | + $this->createTestData(); |
| 37 | + $appliancePerson = $this->seedAppliance(deviceSerial: 'SN-TEST-1'); |
| 38 | + Device::query()->create([ |
| 39 | + 'device_serial' => 'SN-TEST-1', |
| 40 | + 'person_id' => $appliancePerson->person_id, |
| 41 | + 'device_type' => Device::class, |
| 42 | + 'device_id' => 0, |
| 43 | + ]); |
| 44 | + |
| 45 | + $this->actingAs($this->user)->delete( |
| 46 | + "/api/appliances/person/{$appliancePerson->id}", |
| 47 | + ['admin_id' => $this->user->id] |
| 48 | + )->assertStatus(200); |
| 49 | + |
| 50 | + $device = Device::query()->where('device_serial', 'SN-TEST-1')->first(); |
| 51 | + $this->assertNotNull($device); |
| 52 | + $this->assertNull($device->person_id); |
| 53 | + } |
| 54 | + |
| 55 | + public function testAllowsDeleteWhenRatesPaid(): void { |
| 56 | + $this->createTestData(); |
| 57 | + $appliancePerson = $this->seedAppliance(); |
| 58 | + $appliancePerson->rates()->oldest('due_date')->first()->update(['remaining' => 0]); |
| 59 | + |
| 60 | + $response = $this->actingAs($this->user)->delete( |
| 61 | + "/api/appliances/person/{$appliancePerson->id}", |
| 62 | + ['admin_id' => $this->user->id] |
| 63 | + ); |
| 64 | + |
| 65 | + $response->assertStatus(200); |
| 66 | + $this->assertNotNull(AppliancePerson::query()->withTrashed()->find($appliancePerson->id)->deleted_at); |
| 67 | + } |
| 68 | + |
| 69 | + public function testWritesAuditLog(): void { |
| 70 | + $this->createTestData(); |
| 71 | + $appliancePerson = $this->seedAppliance(); |
| 72 | + |
| 73 | + $this->actingAs($this->user)->delete( |
| 74 | + "/api/appliances/person/{$appliancePerson->id}", |
| 75 | + ['admin_id' => $this->user->id] |
| 76 | + )->assertStatus(200); |
| 77 | + |
| 78 | + $log = Log::query() |
| 79 | + ->where('affected_type', AppliancePerson::class) |
| 80 | + ->where('affected_id', $appliancePerson->id) |
| 81 | + ->latest('id') |
| 82 | + ->first(); |
| 83 | + $this->assertNotNull($log); |
| 84 | + $this->assertSame($this->user->id, $log->user_id); |
| 85 | + $this->assertStringContainsString("User {$this->user->name} deleted the sold appliance", $log->action); |
| 86 | + } |
| 87 | + |
| 88 | + public function testShowReturnsTrashedAppliancePersonWithDeletedAt(): void { |
| 89 | + $this->createTestData(); |
| 90 | + $appliancePerson = $this->seedAppliance(); |
| 91 | + |
| 92 | + $this->actingAs($this->user)->delete( |
| 93 | + "/api/appliances/person/{$appliancePerson->id}", |
| 94 | + ['admin_id' => $this->user->id] |
| 95 | + )->assertStatus(200); |
| 96 | + |
| 97 | + $response = $this->actingAs($this->user)->get( |
| 98 | + "/api/appliances/person/people/detail/{$appliancePerson->id}" |
| 99 | + ); |
| 100 | + |
| 101 | + $response->assertStatus(200); |
| 102 | + $response->assertJsonPath('data.id', $appliancePerson->id); |
| 103 | + $this->assertNotNull($response->json('data.deleted_at')); |
| 104 | + } |
| 105 | + |
| 106 | + private function seedAppliance(?string $deviceSerial = null): AppliancePerson { |
| 107 | + $person = PersonFactory::new()->create(); |
| 108 | + $applianceType = ApplianceTypeFactory::new()->create(); |
| 109 | + $appliance = Appliance::query()->create([ |
| 110 | + 'name' => 'Test Appliance', |
| 111 | + 'price' => 1000, |
| 112 | + 'appliance_type_id' => $applianceType->id, |
| 113 | + ]); |
| 114 | + |
| 115 | + /** @var AppliancePerson $appliancePerson */ |
| 116 | + $appliancePerson = AppliancePersonFactory::new()->create([ |
| 117 | + 'appliance_id' => $appliance->id, |
| 118 | + 'person_id' => $person->id, |
| 119 | + 'total_cost' => 1000, |
| 120 | + 'rate_count' => 5, |
| 121 | + 'down_payment' => 0, |
| 122 | + 'device_serial' => $deviceSerial, |
| 123 | + ]); |
| 124 | + |
| 125 | + foreach ([200, 200, 200, 200, 200] as $i => $cost) { |
| 126 | + ApplianceRate::query()->create([ |
| 127 | + 'appliance_person_id' => $appliancePerson->id, |
| 128 | + 'rate_cost' => $cost, |
| 129 | + 'remaining' => $cost, |
| 130 | + 'remind' => 0, |
| 131 | + 'due_date' => now()->addMonths($i + 1), |
| 132 | + ]); |
| 133 | + } |
| 134 | + |
| 135 | + return $appliancePerson->fresh(); |
| 136 | + } |
| 137 | +} |
0 commit comments