Skip to content

Commit

Permalink
adds testing to unaccepted reminder command
Browse files Browse the repository at this point in the history
  • Loading branch information
Godmartinz committed Dec 5, 2024
1 parent 281ff6a commit 97398f1
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 8 deletions.
14 changes: 6 additions & 8 deletions app/Console/Commands/SendAcceptanceReminder.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,15 @@ public function handle()
return $item['acceptance']->assignedTo ? $item['acceptance']->assignedTo->id : '';
});

$no_mail_address = [];

foreach($unacceptedAssetGroups as $unacceptedAssetGroup) {
// The [0] is weird, but it allows for the item_count to work and grabs the appropriate info for each user.
// Collapsing and flattening the collection doesn't work above.
$acceptance = $unacceptedAssetGroup[0]['acceptance'];
$locale = $acceptance->assignedTo?->locale;
$email = $acceptance->assignedTo?->email;
if(!$email){
$this->info($acceptance->assignedTo->present()->fullName().' has no email address.');
}
$item_count = $unacceptedAssetGroup->count();

if ($locale && $email) {
Expand All @@ -84,12 +85,9 @@ public function handle()
$count++;
}

if (!empty($no_mail_address)) {
foreach($no_mail_address as $user) {
return $user.' has no email.';
}
}

$this->info($count.' users notified.');

return 0;
}

}
53 changes: 53 additions & 0 deletions tests/Feature/Console/SendAcceptanceReminderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Tests\Feature\Console;

use App\Mail\UnacceptedAssetReminderMail;
use App\Models\CheckoutAcceptance;
use App\Models\User;
use Illuminate\Support\Facades\Mail;
use Tests\TestCase;
class SendAcceptanceReminderTest extends TestCase
{
public function testAcceptanceReminderCommand()
{
Mail::fake();
$userA = User::factory()->create(['email' => 'userA@test.com']);
$userB = User::factory()->create(['email' => 'userB@test.com']);

CheckoutAcceptance::factory()->pending()->count(2)->create([
'assigned_to_id' => $userA->id,
]);
CheckoutAcceptance::factory()->pending()->create([
'assigned_to_id' => $userB->id,
]);

$this->artisan('snipeit:acceptance-reminder')->assertExitCode(0);

Mail::assertSent(UnacceptedAssetReminderMail::class, function ($mail) {
return $mail->hasTo('userA@test.com');
});

Mail::assertSent(UnacceptedAssetReminderMail::class, function ($mail) {
return $mail->hasTo('userB@test.com');
});

Mail::assertSent(UnacceptedAssetReminderMail::class,2);
}

public function testAcceptanceReminderCommandHandlesUserWithoutEmail()
{
Mail::fake();
$userA = User::factory()->create(['email' => '']);

CheckoutAcceptance::factory()->pending()->create([
'assigned_to_id' => $userA->id,
]);

$this->artisan('snipeit:acceptance-reminder')
->expectsOutput($userA->present()->fullName().' has no email address.')
->assertExitCode(0);

Mail::assertNotSent(UnacceptedAssetReminderMail::class);
}
}

0 comments on commit 97398f1

Please sign in to comment.