|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests; |
| 4 | + |
| 5 | +use Illuminate\Database\Eloquent\Builder; |
| 6 | +use Illuminate\Support\Carbon; |
| 7 | +use Stackkit\LaravelDatabaseEmails\Email; |
| 8 | + |
| 9 | +class PruneTest extends TestCase |
| 10 | +{ |
| 11 | + /** @test */ |
| 12 | + public function by_default_mails_are_pruned_after_6_months() |
| 13 | + { |
| 14 | + $email = $this->sendEmail(); |
| 15 | + |
| 16 | + Carbon::setTestNow($email->created_at . ' + 6 months'); |
| 17 | + $this->artisan('model:prune', ['--model' => [Email::class]]); |
| 18 | + $this->assertInstanceOf(Email::class, $email->fresh()); |
| 19 | + |
| 20 | + Carbon::setTestNow($email->created_at . ' + 6 months + 1 day'); |
| 21 | + |
| 22 | + // Ensure the email object has to be passed manually, otherwise we are acidentally |
| 23 | + // deleting everyone's e-mails... |
| 24 | + $this->artisan('model:prune'); |
| 25 | + $this->assertInstanceOf(Email::class, $email->fresh()); |
| 26 | + |
| 27 | + // Now test with it passed... then it should definitely be deleted. |
| 28 | + $this->artisan('model:prune', ['--model' => [Email::class]]); |
| 29 | + $this->assertNull($email->fresh()); |
| 30 | + } |
| 31 | + |
| 32 | + /** @test */ |
| 33 | + public function can_change_when_emails_are_pruned() |
| 34 | + { |
| 35 | + Email::pruneWhen(function (Email $email) { |
| 36 | + return $email->where('created_at', '<', now()->subMonths(3)); |
| 37 | + }); |
| 38 | + |
| 39 | + $email = $this->sendEmail(); |
| 40 | + |
| 41 | + Carbon::setTestNow($email->created_at . ' + 3 months'); |
| 42 | + $this->artisan('model:prune', ['--model' => [Email::class]]); |
| 43 | + $this->assertInstanceOf(Email::class, $email->fresh()); |
| 44 | + |
| 45 | + Carbon::setTestNow($email->created_at . ' + 3 months + 1 day'); |
| 46 | + $this->artisan('model:prune', ['--model' => [Email::class]]); |
| 47 | + $this->assertNull($email->fresh()); |
| 48 | + } |
| 49 | +} |
0 commit comments