2023-03-30 16:58:16 -07:00
|
|
|
<?php
|
|
|
|
|
2024-02-27 17:07:40 -08:00
|
|
|
namespace Tests\Feature\Notifications\Webhooks;
|
2023-03-30 16:58:16 -07:00
|
|
|
|
2024-08-06 13:25:21 -07:00
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
|
|
use PHPUnit\Framework\Attributes\Group;
|
2023-03-30 16:58:16 -07:00
|
|
|
use App\Events\CheckoutableCheckedOut;
|
|
|
|
use App\Models\Accessory;
|
2024-02-27 17:07:40 -08:00
|
|
|
use App\Models\Asset;
|
|
|
|
use App\Models\Component;
|
|
|
|
use App\Models\Consumable;
|
2024-02-27 17:14:35 -08:00
|
|
|
use App\Models\LicenseSeat;
|
|
|
|
use App\Models\Location;
|
2023-03-30 16:58:16 -07:00
|
|
|
use App\Models\User;
|
|
|
|
use App\Notifications\CheckoutAccessoryNotification;
|
2024-02-27 17:14:35 -08:00
|
|
|
use App\Notifications\CheckoutAssetNotification;
|
2024-02-27 17:07:40 -08:00
|
|
|
use App\Notifications\CheckoutConsumableNotification;
|
2024-02-27 17:14:35 -08:00
|
|
|
use App\Notifications\CheckoutLicenseSeatNotification;
|
2024-02-27 17:52:18 -08:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2023-03-30 16:58:16 -07:00
|
|
|
use Illuminate\Support\Facades\Notification;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
2024-08-06 13:25:21 -07:00
|
|
|
#[Group('notifications')]
|
2024-02-27 17:07:40 -08:00
|
|
|
class SlackNotificationsUponCheckoutTest extends TestCase
|
2023-03-30 16:58:16 -07:00
|
|
|
{
|
2024-02-27 17:23:30 -08:00
|
|
|
protected function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
Notification::fake();
|
|
|
|
}
|
|
|
|
|
2024-08-06 13:25:21 -07:00
|
|
|
public static function assetCheckoutTargets(): array
|
2024-02-27 17:14:35 -08:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'Asset checked out to user' => [fn() => User::factory()->create()],
|
|
|
|
'Asset checked out to asset' => [fn() => Asset::factory()->laptopMbp()->create()],
|
|
|
|
'Asset checked out to location' => [fn() => Location::factory()->create()],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2024-08-06 13:25:21 -07:00
|
|
|
public static function licenseCheckoutTargets(): array
|
2024-02-27 17:14:35 -08:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'License checked out to user' => [fn() => User::factory()->create()],
|
|
|
|
'License checked out to asset' => [fn() => Asset::factory()->laptopMbp()->create()],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2024-08-06 13:25:22 -07:00
|
|
|
public function testAccessoryCheckoutSendsSlackNotificationWhenSettingEnabled(): void
|
2023-03-30 16:58:16 -07:00
|
|
|
{
|
2024-02-14 18:17:34 -08:00
|
|
|
$this->settings->enableSlackWebhook();
|
2023-03-30 16:58:16 -07:00
|
|
|
|
2024-02-27 17:52:18 -08:00
|
|
|
$this->fireCheckOutEvent(
|
2024-02-27 18:03:53 -08:00
|
|
|
Accessory::factory()->create(),
|
2023-03-30 16:58:16 -07:00
|
|
|
User::factory()->create(),
|
2024-02-27 17:52:18 -08:00
|
|
|
);
|
2023-03-30 16:58:16 -07:00
|
|
|
|
2024-02-27 17:57:12 -08:00
|
|
|
$this->assertSlackNotificationSent(CheckoutAccessoryNotification::class);
|
2023-03-30 16:58:16 -07:00
|
|
|
}
|
|
|
|
|
2024-08-06 13:25:22 -07:00
|
|
|
public function testAccessoryCheckoutDoesNotSendSlackNotificationWhenSettingDisabled(): void
|
2023-03-30 16:58:16 -07:00
|
|
|
{
|
2024-02-27 16:45:49 -08:00
|
|
|
$this->settings->disableSlackWebhook();
|
2023-03-30 16:58:16 -07:00
|
|
|
|
2024-02-27 17:52:18 -08:00
|
|
|
$this->fireCheckOutEvent(
|
2024-02-27 18:03:53 -08:00
|
|
|
Accessory::factory()->create(),
|
2023-03-30 16:58:16 -07:00
|
|
|
User::factory()->create(),
|
2024-02-27 17:52:18 -08:00
|
|
|
);
|
2023-03-30 16:58:16 -07:00
|
|
|
|
2024-02-27 17:57:12 -08:00
|
|
|
$this->assertNoSlackNotificationSent(CheckoutAccessoryNotification::class);
|
2023-03-30 16:58:16 -07:00
|
|
|
}
|
2023-04-05 12:36:24 -07:00
|
|
|
|
2024-08-06 13:25:21 -07:00
|
|
|
#[DataProvider('assetCheckoutTargets')]
|
2024-08-06 13:25:22 -07:00
|
|
|
public function testAssetCheckoutSendsSlackNotificationWhenSettingEnabled($checkoutTarget): void
|
2024-02-27 17:14:35 -08:00
|
|
|
{
|
|
|
|
$this->settings->enableSlackWebhook();
|
|
|
|
|
2024-02-27 17:52:18 -08:00
|
|
|
$this->fireCheckOutEvent(
|
2024-02-27 18:03:53 -08:00
|
|
|
Asset::factory()->create(),
|
2024-02-27 17:14:35 -08:00
|
|
|
$checkoutTarget(),
|
2024-02-27 17:52:18 -08:00
|
|
|
);
|
2024-02-27 17:14:35 -08:00
|
|
|
|
2024-02-27 17:57:12 -08:00
|
|
|
$this->assertSlackNotificationSent(CheckoutAssetNotification::class);
|
2024-02-27 17:14:35 -08:00
|
|
|
}
|
|
|
|
|
2024-08-06 13:25:21 -07:00
|
|
|
#[DataProvider('assetCheckoutTargets')]
|
2024-08-06 13:25:22 -07:00
|
|
|
public function testAssetCheckoutDoesNotSendSlackNotificationWhenSettingDisabled($checkoutTarget): void
|
2024-02-27 17:14:35 -08:00
|
|
|
{
|
|
|
|
$this->settings->disableSlackWebhook();
|
|
|
|
|
2024-02-27 17:52:18 -08:00
|
|
|
$this->fireCheckOutEvent(
|
2024-02-27 18:03:53 -08:00
|
|
|
Asset::factory()->create(),
|
2024-02-27 17:14:35 -08:00
|
|
|
$checkoutTarget(),
|
2024-02-27 17:52:18 -08:00
|
|
|
);
|
2024-02-27 17:14:35 -08:00
|
|
|
|
2024-02-27 17:57:12 -08:00
|
|
|
$this->assertNoSlackNotificationSent(CheckoutAssetNotification::class);
|
2024-02-27 17:14:35 -08:00
|
|
|
}
|
|
|
|
|
2024-08-06 13:25:22 -07:00
|
|
|
public function testComponentCheckoutDoesNotSendSlackNotification(): void
|
2023-04-05 12:36:24 -07:00
|
|
|
{
|
2024-02-14 18:17:34 -08:00
|
|
|
$this->settings->enableSlackWebhook();
|
2023-04-05 12:36:24 -07:00
|
|
|
|
2024-02-27 17:52:18 -08:00
|
|
|
$this->fireCheckOutEvent(
|
2024-02-27 18:03:53 -08:00
|
|
|
Component::factory()->create(),
|
2024-02-27 17:07:40 -08:00
|
|
|
Asset::factory()->laptopMbp()->create(),
|
2024-02-27 17:52:18 -08:00
|
|
|
);
|
2024-02-27 17:07:40 -08:00
|
|
|
|
|
|
|
Notification::assertNothingSent();
|
|
|
|
}
|
|
|
|
|
2024-08-06 13:25:22 -07:00
|
|
|
public function testConsumableCheckoutSendsSlackNotificationWhenSettingEnabled(): void
|
2024-02-27 17:07:40 -08:00
|
|
|
{
|
|
|
|
$this->settings->enableSlackWebhook();
|
|
|
|
|
2024-02-27 17:52:18 -08:00
|
|
|
$this->fireCheckOutEvent(
|
2024-02-27 18:03:53 -08:00
|
|
|
Consumable::factory()->create(),
|
2023-04-05 12:36:24 -07:00
|
|
|
User::factory()->create(),
|
2024-02-27 17:52:18 -08:00
|
|
|
);
|
2023-04-05 12:36:24 -07:00
|
|
|
|
2024-02-27 17:57:12 -08:00
|
|
|
$this->assertSlackNotificationSent(CheckoutConsumableNotification::class);
|
2023-04-05 12:36:24 -07:00
|
|
|
}
|
|
|
|
|
2024-08-06 13:25:22 -07:00
|
|
|
public function testConsumableCheckoutDoesNotSendSlackNotificationWhenSettingDisabled(): void
|
2023-04-05 12:36:24 -07:00
|
|
|
{
|
2024-02-27 16:45:49 -08:00
|
|
|
$this->settings->disableSlackWebhook();
|
2023-04-05 12:36:24 -07:00
|
|
|
|
2024-02-27 17:52:18 -08:00
|
|
|
$this->fireCheckOutEvent(
|
2024-02-27 18:03:53 -08:00
|
|
|
Consumable::factory()->create(),
|
2023-04-05 12:36:24 -07:00
|
|
|
User::factory()->create(),
|
2024-02-27 17:52:18 -08:00
|
|
|
);
|
2023-04-05 12:36:24 -07:00
|
|
|
|
2024-02-27 17:57:12 -08:00
|
|
|
$this->assertNoSlackNotificationSent(CheckoutConsumableNotification::class);
|
2023-04-05 12:36:24 -07:00
|
|
|
}
|
2024-02-27 17:14:35 -08:00
|
|
|
|
2024-08-06 13:25:21 -07:00
|
|
|
#[DataProvider('licenseCheckoutTargets')]
|
2024-08-06 13:25:22 -07:00
|
|
|
public function testLicenseCheckoutSendsSlackNotificationWhenSettingEnabled($checkoutTarget): void
|
2024-02-27 17:14:35 -08:00
|
|
|
{
|
|
|
|
$this->settings->enableSlackWebhook();
|
|
|
|
|
2024-02-27 17:52:18 -08:00
|
|
|
$this->fireCheckOutEvent(
|
2024-02-27 17:14:35 -08:00
|
|
|
LicenseSeat::factory()->create(),
|
|
|
|
$checkoutTarget(),
|
2024-02-27 17:52:18 -08:00
|
|
|
);
|
2024-02-27 17:14:35 -08:00
|
|
|
|
2024-02-27 17:57:12 -08:00
|
|
|
$this->assertSlackNotificationSent(CheckoutLicenseSeatNotification::class);
|
2024-02-27 17:14:35 -08:00
|
|
|
}
|
|
|
|
|
2024-08-06 13:25:21 -07:00
|
|
|
#[DataProvider('licenseCheckoutTargets')]
|
2024-08-06 13:25:22 -07:00
|
|
|
public function testLicenseCheckoutDoesNotSendSlackNotificationWhenSettingDisabled($checkoutTarget): void
|
2024-02-27 17:14:35 -08:00
|
|
|
{
|
|
|
|
$this->settings->disableSlackWebhook();
|
|
|
|
|
2024-02-27 17:52:18 -08:00
|
|
|
$this->fireCheckOutEvent(
|
2024-02-27 17:14:35 -08:00
|
|
|
LicenseSeat::factory()->create(),
|
|
|
|
$checkoutTarget(),
|
2024-02-27 17:52:18 -08:00
|
|
|
);
|
2024-02-27 17:14:35 -08:00
|
|
|
|
2024-02-27 17:57:12 -08:00
|
|
|
$this->assertNoSlackNotificationSent(CheckoutLicenseSeatNotification::class);
|
2024-02-27 17:14:35 -08:00
|
|
|
}
|
2024-02-27 17:52:18 -08:00
|
|
|
|
|
|
|
private function fireCheckOutEvent(Model $checkoutable, Model $target)
|
|
|
|
{
|
|
|
|
event(new CheckoutableCheckedOut(
|
|
|
|
$checkoutable,
|
|
|
|
$target,
|
|
|
|
User::factory()->superuser()->create(),
|
|
|
|
'',
|
|
|
|
));
|
|
|
|
}
|
2023-03-30 16:58:16 -07:00
|
|
|
}
|