|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests; |
| 4 | + |
| 5 | +use App\Policy; |
| 6 | +use App\PolicyAcceptance; |
| 7 | +use App\User; |
| 8 | +use Carbon\CarbonImmutable; |
| 9 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 10 | +use RuntimeException; |
| 11 | + |
| 12 | +class PolicyAcceptanceTest extends TestCase { |
| 13 | + use RefreshDatabase; |
| 14 | + |
| 15 | + protected int $userId; |
| 16 | + |
| 17 | + protected int $policyId; |
| 18 | + |
| 19 | + protected function setUp(): void { |
| 20 | + parent::setUp(); |
| 21 | + $user = User::factory()->create(); |
| 22 | + $this->userId = $user->id; |
| 23 | + $policy = Policy::create([ |
| 24 | + 'policy_type' => 'terms-of-use', |
| 25 | + 'active_from' => CarbonImmutable::yesterday(), |
| 26 | + 'content_vue_file' => 'terms-of-use/example.vue', |
| 27 | + ]); |
| 28 | + $this->policyId = $policy->id; |
| 29 | + } |
| 30 | + |
| 31 | + public function testCreatesAndSavesSuccessfully(): void { |
| 32 | + $policyAcceptance = new PolicyAcceptance([ |
| 33 | + 'user_id' => $this->userId, |
| 34 | + 'policy_id' => $this->policyId, |
| 35 | + 'accepted_at' => CarbonImmutable::now(), |
| 36 | + ]); |
| 37 | + $policyAcceptance->save(); |
| 38 | + $policyAcceptance->refresh(); |
| 39 | + |
| 40 | + $this->assertDatabaseHas('policy_acceptances', [ |
| 41 | + 'user_id' => $this->userId, |
| 42 | + 'policy_id' => $this->policyId, |
| 43 | + ]); |
| 44 | + |
| 45 | + $this->assertNotEmpty($policyAcceptance->accepted_at); |
| 46 | + $this->assertInstanceOf(CarbonImmutable::class, $policyAcceptance->accepted_at); |
| 47 | + } |
| 48 | + |
| 49 | + public function testCreateFailsIfAcceptedAtIsMissing() { |
| 50 | + $this->expectException(RuntimeException::class); |
| 51 | + PolicyAcceptance::create([ |
| 52 | + 'user_id' => $this->userId, |
| 53 | + 'policy_id' => $this->policyId, |
| 54 | + ]); |
| 55 | + } |
| 56 | + |
| 57 | + public function testCreateFailsIfAcceptedAtIsNull() { |
| 58 | + $this->expectException(RuntimeException::class); |
| 59 | + PolicyAcceptance::create([ |
| 60 | + 'user_id' => $this->userId, |
| 61 | + 'policy_id' => $this->policyId, |
| 62 | + 'accepted_at' => null, |
| 63 | + ]); |
| 64 | + } |
| 65 | + |
| 66 | + public function testUserAcceptingSamePolicyTwiceFails() { |
| 67 | + PolicyAcceptance::create([ |
| 68 | + 'user_id' => $this->userId, |
| 69 | + 'policy_id' => $this->policyId, |
| 70 | + 'accepted_at' => CarbonImmutable::now()->subSeconds(2), |
| 71 | + ]); |
| 72 | + $this->expectException(RuntimeException::class); |
| 73 | + PolicyAcceptance::create([ |
| 74 | + 'user_id' => $this->userId, |
| 75 | + 'policy_id' => $this->policyId, |
| 76 | + 'accepted_at' => CarbonImmutable::now(), |
| 77 | + ]); |
| 78 | + } |
| 79 | +} |
0 commit comments