From b156aa74a59cfe474e753ad3cb8665cae2361a8e Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Tue, 27 Feb 2024 16:45:49 -0800 Subject: [PATCH 01/21] Update helper name --- tests/Feature/Notifications/AccessoryWebhookTest.php | 4 ++-- tests/Feature/Notifications/AssetWebhookTest.php | 4 ++-- tests/Feature/Notifications/ConsumableWebhookTest.php | 2 +- tests/Feature/Notifications/LicenseWebhookTest.php | 4 ++-- tests/Support/Settings.php | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/Feature/Notifications/AccessoryWebhookTest.php b/tests/Feature/Notifications/AccessoryWebhookTest.php index 2e3ef999ba..b6e74feb5b 100644 --- a/tests/Feature/Notifications/AccessoryWebhookTest.php +++ b/tests/Feature/Notifications/AccessoryWebhookTest.php @@ -44,7 +44,7 @@ class AccessoryWebhookTest extends TestCase { Notification::fake(); - $this->settings->disableWebhook(); + $this->settings->disableSlackWebhook(); event(new CheckoutableCheckedOut( Accessory::factory()->appleBtKeyboard()->create(), @@ -82,7 +82,7 @@ class AccessoryWebhookTest extends TestCase { Notification::fake(); - $this->settings->disableWebhook(); + $this->settings->disableSlackWebhook(); event(new CheckoutableCheckedIn( Accessory::factory()->appleBtKeyboard()->create(), diff --git a/tests/Feature/Notifications/AssetWebhookTest.php b/tests/Feature/Notifications/AssetWebhookTest.php index 158bced664..d693ce3fe2 100644 --- a/tests/Feature/Notifications/AssetWebhookTest.php +++ b/tests/Feature/Notifications/AssetWebhookTest.php @@ -56,7 +56,7 @@ class AssetWebhookTest extends TestCase { Notification::fake(); - $this->settings->disableWebhook(); + $this->settings->disableSlackWebhook(); event(new CheckoutableCheckedOut( $this->createAsset(), @@ -96,7 +96,7 @@ class AssetWebhookTest extends TestCase { Notification::fake(); - $this->settings->disableWebhook(); + $this->settings->disableSlackWebhook(); event(new CheckoutableCheckedIn( $this->createAsset(), diff --git a/tests/Feature/Notifications/ConsumableWebhookTest.php b/tests/Feature/Notifications/ConsumableWebhookTest.php index 2815731bd2..035573bb4f 100644 --- a/tests/Feature/Notifications/ConsumableWebhookTest.php +++ b/tests/Feature/Notifications/ConsumableWebhookTest.php @@ -42,7 +42,7 @@ class ConsumableWebhookTest extends TestCase { Notification::fake(); - $this->settings->disableWebhook(); + $this->settings->disableSlackWebhook(); event(new CheckoutableCheckedOut( Consumable::factory()->cardstock()->create(), diff --git a/tests/Feature/Notifications/LicenseWebhookTest.php b/tests/Feature/Notifications/LicenseWebhookTest.php index 24ec53a75e..cfb2fca5b8 100644 --- a/tests/Feature/Notifications/LicenseWebhookTest.php +++ b/tests/Feature/Notifications/LicenseWebhookTest.php @@ -55,7 +55,7 @@ class LicenseWebhookTest extends TestCase { Notification::fake(); - $this->settings->disableWebhook(); + $this->settings->disableSlackWebhook(); event(new CheckoutableCheckedOut( LicenseSeat::factory()->create(), @@ -95,7 +95,7 @@ class LicenseWebhookTest extends TestCase { Notification::fake(); - $this->settings->disableWebhook(); + $this->settings->disableSlackWebhook(); event(new CheckoutableCheckedIn( LicenseSeat::factory()->create(), diff --git a/tests/Support/Settings.php b/tests/Support/Settings.php index 3104851152..2d499838c2 100644 --- a/tests/Support/Settings.php +++ b/tests/Support/Settings.php @@ -49,7 +49,7 @@ class Settings ]); } - public function disableWebhook(): Settings + public function disableSlackWebhook(): Settings { return $this->update([ 'webhook_selected' => '', From c08164d864e7b92d7f813eece30c7fc050e3f3ac Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Tue, 27 Feb 2024 16:48:17 -0800 Subject: [PATCH 02/21] Update test names --- ...AccessoryWebhookTest.php => AccessorySlackTest.php} | 10 +++++----- .../{AssetWebhookTest.php => AssetSlackTest.php} | 10 +++++----- ...ComponentWebhookTest.php => ComponentSlackTest.php} | 6 +++--- ...nsumableWebhookTest.php => ConsumableSlackTest.php} | 6 +++--- .../{LicenseWebhookTest.php => LicenseSlackTest.php} | 10 +++++----- 5 files changed, 21 insertions(+), 21 deletions(-) rename tests/Feature/Notifications/{AccessoryWebhookTest.php => AccessorySlackTest.php} (86%) rename tests/Feature/Notifications/{AssetWebhookTest.php => AssetSlackTest.php} (87%) rename tests/Feature/Notifications/{ComponentWebhookTest.php => ComponentSlackTest.php} (84%) rename tests/Feature/Notifications/{ConsumableWebhookTest.php => ConsumableSlackTest.php} (86%) rename tests/Feature/Notifications/{LicenseWebhookTest.php => LicenseSlackTest.php} (86%) diff --git a/tests/Feature/Notifications/AccessoryWebhookTest.php b/tests/Feature/Notifications/AccessorySlackTest.php similarity index 86% rename from tests/Feature/Notifications/AccessoryWebhookTest.php rename to tests/Feature/Notifications/AccessorySlackTest.php index b6e74feb5b..bd54ed8cb9 100644 --- a/tests/Feature/Notifications/AccessoryWebhookTest.php +++ b/tests/Feature/Notifications/AccessorySlackTest.php @@ -14,11 +14,11 @@ use Illuminate\Support\Facades\Notification; use Tests\Support\InteractsWithSettings; use Tests\TestCase; -class AccessoryWebhookTest extends TestCase +class AccessorySlackTest extends TestCase { use InteractsWithSettings; - public function testAccessoryCheckoutSendsWebhookNotificationWhenSettingEnabled() + public function testAccessoryCheckoutSendsSlackNotificationWhenSettingEnabled() { Notification::fake(); @@ -40,7 +40,7 @@ class AccessoryWebhookTest extends TestCase ); } - public function testAccessoryCheckoutDoesNotSendWebhookNotificationWhenSettingDisabled() + public function testAccessoryCheckoutDoesNotSendSlackNotificationWhenSettingDisabled() { Notification::fake(); @@ -56,7 +56,7 @@ class AccessoryWebhookTest extends TestCase Notification::assertNotSentTo(new AnonymousNotifiable, CheckoutAccessoryNotification::class); } - public function testAccessoryCheckinSendsWebhookNotificationWhenSettingEnabled() + public function testAccessoryCheckinSendsSlackNotificationWhenSettingEnabled() { Notification::fake(); @@ -78,7 +78,7 @@ class AccessoryWebhookTest extends TestCase ); } - public function testAccessoryCheckinDoesNotSendWebhookNotificationWhenSettingDisabled() + public function testAccessoryCheckinDoesNotSendSlackNotificationWhenSettingDisabled() { Notification::fake(); diff --git a/tests/Feature/Notifications/AssetWebhookTest.php b/tests/Feature/Notifications/AssetSlackTest.php similarity index 87% rename from tests/Feature/Notifications/AssetWebhookTest.php rename to tests/Feature/Notifications/AssetSlackTest.php index d693ce3fe2..d0b376c65c 100644 --- a/tests/Feature/Notifications/AssetWebhookTest.php +++ b/tests/Feature/Notifications/AssetSlackTest.php @@ -15,7 +15,7 @@ use Illuminate\Support\Facades\Notification; use Tests\Support\InteractsWithSettings; use Tests\TestCase; -class AssetWebhookTest extends TestCase +class AssetSlackTest extends TestCase { use InteractsWithSettings; @@ -29,7 +29,7 @@ class AssetWebhookTest extends TestCase } /** @dataProvider targets */ - public function testAssetCheckoutSendsWebhookNotificationWhenSettingEnabled($checkoutTarget) + public function testAssetCheckoutSendsSlackNotificationWhenSettingEnabled($checkoutTarget) { Notification::fake(); @@ -52,7 +52,7 @@ class AssetWebhookTest extends TestCase } /** @dataProvider targets */ - public function testAssetCheckoutDoesNotSendWebhookNotificationWhenSettingDisabled($checkoutTarget) + public function testAssetCheckoutDoesNotSendSlackNotificationWhenSettingDisabled($checkoutTarget) { Notification::fake(); @@ -69,7 +69,7 @@ class AssetWebhookTest extends TestCase } /** @dataProvider targets */ - public function testAssetCheckinSendsWebhookNotificationWhenSettingEnabled($checkoutTarget) + public function testAssetCheckinSendsSlackNotificationWhenSettingEnabled($checkoutTarget) { Notification::fake(); @@ -92,7 +92,7 @@ class AssetWebhookTest extends TestCase } /** @dataProvider targets */ - public function testAssetCheckinDoesNotSendWebhookNotificationWhenSettingDisabled($checkoutTarget) + public function testAssetCheckinDoesNotSendSlackNotificationWhenSettingDisabled($checkoutTarget) { Notification::fake(); diff --git a/tests/Feature/Notifications/ComponentWebhookTest.php b/tests/Feature/Notifications/ComponentSlackTest.php similarity index 84% rename from tests/Feature/Notifications/ComponentWebhookTest.php rename to tests/Feature/Notifications/ComponentSlackTest.php index 2e2a535219..b9fba83690 100644 --- a/tests/Feature/Notifications/ComponentWebhookTest.php +++ b/tests/Feature/Notifications/ComponentSlackTest.php @@ -11,12 +11,12 @@ use Illuminate\Support\Facades\Notification; use Tests\Support\InteractsWithSettings; use Tests\TestCase; -class ComponentWebhookTest extends TestCase +class ComponentSlackTest extends TestCase { use InteractsWithSettings; - public function testComponentCheckoutDoesNotSendWebhookNotification() + public function testComponentCheckoutDoesNotSendSlackNotification() { Notification::fake(); @@ -32,7 +32,7 @@ class ComponentWebhookTest extends TestCase Notification::assertNothingSent(); } - public function testComponentCheckinDoesNotSendWebhookNotification() + public function testComponentCheckinDoesNotSendSlackNotification() { Notification::fake(); diff --git a/tests/Feature/Notifications/ConsumableWebhookTest.php b/tests/Feature/Notifications/ConsumableSlackTest.php similarity index 86% rename from tests/Feature/Notifications/ConsumableWebhookTest.php rename to tests/Feature/Notifications/ConsumableSlackTest.php index 035573bb4f..9fd5d0b82a 100644 --- a/tests/Feature/Notifications/ConsumableWebhookTest.php +++ b/tests/Feature/Notifications/ConsumableSlackTest.php @@ -12,11 +12,11 @@ use Illuminate\Support\Facades\Notification; use Tests\Support\InteractsWithSettings; use Tests\TestCase; -class ConsumableWebhookTest extends TestCase +class ConsumableSlackTest extends TestCase { use InteractsWithSettings; - public function testConsumableCheckoutSendsWebhookNotificationWhenSettingEnabled() + public function testConsumableCheckoutSendsSlackNotificationWhenSettingEnabled() { Notification::fake(); @@ -38,7 +38,7 @@ class ConsumableWebhookTest extends TestCase ); } - public function testConsumableCheckoutDoesNotSendWebhookNotificationWhenSettingDisabled() + public function testConsumableCheckoutDoesNotSendSlackNotificationWhenSettingDisabled() { Notification::fake(); diff --git a/tests/Feature/Notifications/LicenseWebhookTest.php b/tests/Feature/Notifications/LicenseSlackTest.php similarity index 86% rename from tests/Feature/Notifications/LicenseWebhookTest.php rename to tests/Feature/Notifications/LicenseSlackTest.php index cfb2fca5b8..c10fbdf5c3 100644 --- a/tests/Feature/Notifications/LicenseWebhookTest.php +++ b/tests/Feature/Notifications/LicenseSlackTest.php @@ -15,7 +15,7 @@ use Illuminate\Support\Facades\Notification; use Tests\Support\InteractsWithSettings; use Tests\TestCase; -class LicenseWebhookTest extends TestCase +class LicenseSlackTest extends TestCase { use InteractsWithSettings; @@ -28,7 +28,7 @@ class LicenseWebhookTest extends TestCase } /** @dataProvider targets */ - public function testLicenseCheckoutSendsWebhookNotificationWhenSettingEnabled($checkoutTarget) + public function testLicenseCheckoutSendsSlackNotificationWhenSettingEnabled($checkoutTarget) { Notification::fake(); @@ -51,7 +51,7 @@ class LicenseWebhookTest extends TestCase } /** @dataProvider targets */ - public function testLicenseCheckoutDoesNotSendWebhookNotificationWhenSettingDisabled($checkoutTarget) + public function testLicenseCheckoutDoesNotSendSlackNotificationWhenSettingDisabled($checkoutTarget) { Notification::fake(); @@ -68,7 +68,7 @@ class LicenseWebhookTest extends TestCase } /** @dataProvider targets */ - public function testLicenseCheckinSendsWebhookNotificationWhenSettingEnabled($checkoutTarget) + public function testLicenseCheckinSendsSlackNotificationWhenSettingEnabled($checkoutTarget) { Notification::fake(); @@ -91,7 +91,7 @@ class LicenseWebhookTest extends TestCase } /** @dataProvider targets */ - public function testLicenseCheckinDoesNotSendWebhookNotificationWhenSettingDisabled($checkoutTarget) + public function testLicenseCheckinDoesNotSendSlackNotificationWhenSettingDisabled($checkoutTarget) { Notification::fake(); From 7d3719bf705e11c4fa63649e279b4f90def1b88a Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Tue, 27 Feb 2024 17:07:40 -0800 Subject: [PATCH 03/21] Consolidate some slack notification tests --- .../Notifications/ComponentSlackTest.php | 50 ----------- .../Notifications/ConsumableSlackTest.php | 56 ------------- .../SlackNotificationsUponCheckinTest.php | 84 +++++++++++++++++++ .../SlackNotificationsUponCheckoutTest.php} | 42 +++++++--- 4 files changed, 114 insertions(+), 118 deletions(-) delete mode 100644 tests/Feature/Notifications/ComponentSlackTest.php delete mode 100644 tests/Feature/Notifications/ConsumableSlackTest.php create mode 100644 tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php rename tests/Feature/Notifications/{AccessorySlackTest.php => Webhooks/SlackNotificationsUponCheckoutTest.php} (67%) diff --git a/tests/Feature/Notifications/ComponentSlackTest.php b/tests/Feature/Notifications/ComponentSlackTest.php deleted file mode 100644 index b9fba83690..0000000000 --- a/tests/Feature/Notifications/ComponentSlackTest.php +++ /dev/null @@ -1,50 +0,0 @@ -settings->enableSlackWebhook(); - - event(new CheckoutableCheckedOut( - Component::factory()->ramCrucial8()->create(), - Asset::factory()->laptopMbp()->create(), - User::factory()->superuser()->create(), - '' - )); - - Notification::assertNothingSent(); - } - - public function testComponentCheckinDoesNotSendSlackNotification() - { - Notification::fake(); - - $this->settings->enableSlackWebhook(); - - event(new CheckoutableCheckedIn( - Component::factory()->ramCrucial8()->create(), - Asset::factory()->laptopMbp()->create(), - User::factory()->superuser()->create(), - '' - )); - - Notification::assertNothingSent(); - } -} diff --git a/tests/Feature/Notifications/ConsumableSlackTest.php b/tests/Feature/Notifications/ConsumableSlackTest.php deleted file mode 100644 index 9fd5d0b82a..0000000000 --- a/tests/Feature/Notifications/ConsumableSlackTest.php +++ /dev/null @@ -1,56 +0,0 @@ -settings->enableSlackWebhook(); - - event(new CheckoutableCheckedOut( - Consumable::factory()->cardstock()->create(), - User::factory()->create(), - User::factory()->superuser()->create(), - '' - )); - - Notification::assertSentTo( - new AnonymousNotifiable, - CheckoutConsumableNotification::class, - function ($notification, $channels, $notifiable) { - return $notifiable->routes['slack'] === Setting::getSettings()->webhook_endpoint; - } - ); - } - - public function testConsumableCheckoutDoesNotSendSlackNotificationWhenSettingDisabled() - { - Notification::fake(); - - $this->settings->disableSlackWebhook(); - - event(new CheckoutableCheckedOut( - Consumable::factory()->cardstock()->create(), - User::factory()->create(), - User::factory()->superuser()->create(), - '' - )); - - Notification::assertNotSentTo(new AnonymousNotifiable, CheckoutConsumableNotification::class); - } -} diff --git a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php new file mode 100644 index 0000000000..282417d822 --- /dev/null +++ b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php @@ -0,0 +1,84 @@ +settings->enableSlackWebhook(); + + event(new CheckoutableCheckedIn( + Accessory::factory()->appleBtKeyboard()->create(), + User::factory()->create(), + User::factory()->superuser()->create(), + '' + )); + + Notification::assertSentTo( + new AnonymousNotifiable, + CheckinAccessoryNotification::class, + function ($notification, $channels, $notifiable) { + return $notifiable->routes['slack'] === Setting::getSettings()->webhook_endpoint; + } + ); + } + + public function testAccessoryCheckinDoesNotSendSlackNotificationWhenSettingDisabled() + { + Notification::fake(); + + $this->settings->disableSlackWebhook(); + + event(new CheckoutableCheckedIn( + Accessory::factory()->appleBtKeyboard()->create(), + User::factory()->create(), + User::factory()->superuser()->create(), + '' + )); + + Notification::assertNotSentTo(new AnonymousNotifiable, CheckinAccessoryNotification::class); + } + + public function testComponentCheckinDoesNotSendSlackNotification() + { + Notification::fake(); + + $this->settings->enableSlackWebhook(); + + event(new CheckoutableCheckedIn( + Component::factory()->ramCrucial8()->create(), + Asset::factory()->laptopMbp()->create(), + User::factory()->superuser()->create(), + '' + )); + + Notification::assertNothingSent(); + } + + public function testConsumableCheckinSendSlackNotificationWhenSettingEnabled() + { + $this->markTestIncomplete(); + } + + public function testConsumableCheckinDoesNotSendSlackNotificationWhenSettingDisabled() + { + $this->markTestIncomplete(); + } +} diff --git a/tests/Feature/Notifications/AccessorySlackTest.php b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php similarity index 67% rename from tests/Feature/Notifications/AccessorySlackTest.php rename to tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php index bd54ed8cb9..3992a6c51b 100644 --- a/tests/Feature/Notifications/AccessorySlackTest.php +++ b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php @@ -1,20 +1,22 @@ settings->enableSlackWebhook(); - event(new CheckoutableCheckedIn( - Accessory::factory()->appleBtKeyboard()->create(), + event(new CheckoutableCheckedOut( + Component::factory()->ramCrucial8()->create(), + Asset::factory()->laptopMbp()->create(), + User::factory()->superuser()->create(), + '' + )); + + Notification::assertNothingSent(); + } + + public function testConsumableCheckoutSendsSlackNotificationWhenSettingEnabled() + { + Notification::fake(); + + $this->settings->enableSlackWebhook(); + + event(new CheckoutableCheckedOut( + Consumable::factory()->cardstock()->create(), User::factory()->create(), User::factory()->superuser()->create(), '' @@ -71,26 +89,26 @@ class AccessorySlackTest extends TestCase Notification::assertSentTo( new AnonymousNotifiable, - CheckinAccessoryNotification::class, + CheckoutConsumableNotification::class, function ($notification, $channels, $notifiable) { return $notifiable->routes['slack'] === Setting::getSettings()->webhook_endpoint; } ); } - public function testAccessoryCheckinDoesNotSendSlackNotificationWhenSettingDisabled() + public function testConsumableCheckoutDoesNotSendSlackNotificationWhenSettingDisabled() { Notification::fake(); $this->settings->disableSlackWebhook(); - event(new CheckoutableCheckedIn( - Accessory::factory()->appleBtKeyboard()->create(), + event(new CheckoutableCheckedOut( + Consumable::factory()->cardstock()->create(), User::factory()->create(), User::factory()->superuser()->create(), '' )); - Notification::assertNotSentTo(new AnonymousNotifiable, CheckinAccessoryNotification::class); + Notification::assertNotSentTo(new AnonymousNotifiable, CheckoutConsumableNotification::class); } } From 4c1aadd74e6aa43d13e7d96ee833abffa6de8d21 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Tue, 27 Feb 2024 17:09:22 -0800 Subject: [PATCH 04/21] Improve naming and inline helper --- .../Feature/Notifications/AssetSlackTest.php | 25 ++++++++----------- .../Notifications/LicenseSlackTest.php | 10 ++++---- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/tests/Feature/Notifications/AssetSlackTest.php b/tests/Feature/Notifications/AssetSlackTest.php index d0b376c65c..c6d03df636 100644 --- a/tests/Feature/Notifications/AssetSlackTest.php +++ b/tests/Feature/Notifications/AssetSlackTest.php @@ -19,16 +19,16 @@ class AssetSlackTest extends TestCase { use InteractsWithSettings; - public function targets(): array + public function assetCheckoutTargets(): array { return [ 'Asset checked out to user' => [fn() => User::factory()->create()], - 'Asset checked out to asset' => [fn() => $this->createAsset()], + 'Asset checked out to asset' => [fn() => Asset::factory()->laptopMbp()->create()], 'Asset checked out to location' => [fn() => Location::factory()->create()], ]; } - /** @dataProvider targets */ + /** @dataProvider assetCheckoutTargets */ public function testAssetCheckoutSendsSlackNotificationWhenSettingEnabled($checkoutTarget) { Notification::fake(); @@ -36,7 +36,7 @@ class AssetSlackTest extends TestCase $this->settings->enableSlackWebhook(); event(new CheckoutableCheckedOut( - $this->createAsset(), + Asset::factory()->laptopMbp()->create(), $checkoutTarget(), User::factory()->superuser()->create(), '' @@ -51,7 +51,7 @@ class AssetSlackTest extends TestCase ); } - /** @dataProvider targets */ + /** @dataProvider assetCheckoutTargets */ public function testAssetCheckoutDoesNotSendSlackNotificationWhenSettingDisabled($checkoutTarget) { Notification::fake(); @@ -59,7 +59,7 @@ class AssetSlackTest extends TestCase $this->settings->disableSlackWebhook(); event(new CheckoutableCheckedOut( - $this->createAsset(), + Asset::factory()->laptopMbp()->create(), $checkoutTarget(), User::factory()->superuser()->create(), '' @@ -68,7 +68,7 @@ class AssetSlackTest extends TestCase Notification::assertNotSentTo(new AnonymousNotifiable, CheckoutAssetNotification::class); } - /** @dataProvider targets */ + /** @dataProvider assetCheckoutTargets */ public function testAssetCheckinSendsSlackNotificationWhenSettingEnabled($checkoutTarget) { Notification::fake(); @@ -76,7 +76,7 @@ class AssetSlackTest extends TestCase $this->settings->enableSlackWebhook(); event(new CheckoutableCheckedIn( - $this->createAsset(), + Asset::factory()->laptopMbp()->create(), $checkoutTarget(), User::factory()->superuser()->create(), '' @@ -91,7 +91,7 @@ class AssetSlackTest extends TestCase ); } - /** @dataProvider targets */ + /** @dataProvider assetCheckoutTargets */ public function testAssetCheckinDoesNotSendSlackNotificationWhenSettingDisabled($checkoutTarget) { Notification::fake(); @@ -99,7 +99,7 @@ class AssetSlackTest extends TestCase $this->settings->disableSlackWebhook(); event(new CheckoutableCheckedIn( - $this->createAsset(), + Asset::factory()->laptopMbp()->create(), $checkoutTarget(), User::factory()->superuser()->create(), '' @@ -107,9 +107,4 @@ class AssetSlackTest extends TestCase Notification::assertNotSentTo(new AnonymousNotifiable, CheckinAssetNotification::class); } - - private function createAsset() - { - return Asset::factory()->laptopMbp()->create(); - } } diff --git a/tests/Feature/Notifications/LicenseSlackTest.php b/tests/Feature/Notifications/LicenseSlackTest.php index c10fbdf5c3..8ed38c2788 100644 --- a/tests/Feature/Notifications/LicenseSlackTest.php +++ b/tests/Feature/Notifications/LicenseSlackTest.php @@ -19,7 +19,7 @@ class LicenseSlackTest extends TestCase { use InteractsWithSettings; - public function targets(): array + public function licenseCheckoutTargets(): array { return [ 'License checked out to user' => [fn() => User::factory()->create()], @@ -27,7 +27,7 @@ class LicenseSlackTest extends TestCase ]; } - /** @dataProvider targets */ + /** @dataProvider licenseCheckoutTargets */ public function testLicenseCheckoutSendsSlackNotificationWhenSettingEnabled($checkoutTarget) { Notification::fake(); @@ -50,7 +50,7 @@ class LicenseSlackTest extends TestCase ); } - /** @dataProvider targets */ + /** @dataProvider licenseCheckoutTargets */ public function testLicenseCheckoutDoesNotSendSlackNotificationWhenSettingDisabled($checkoutTarget) { Notification::fake(); @@ -67,7 +67,7 @@ class LicenseSlackTest extends TestCase Notification::assertNotSentTo(new AnonymousNotifiable, CheckoutLicenseSeatNotification::class); } - /** @dataProvider targets */ + /** @dataProvider licenseCheckoutTargets */ public function testLicenseCheckinSendsSlackNotificationWhenSettingEnabled($checkoutTarget) { Notification::fake(); @@ -90,7 +90,7 @@ class LicenseSlackTest extends TestCase ); } - /** @dataProvider targets */ + /** @dataProvider licenseCheckoutTargets */ public function testLicenseCheckinDoesNotSendSlackNotificationWhenSettingDisabled($checkoutTarget) { Notification::fake(); From 43cc296582b84b2385c1f8615285074ea8a1139a Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Tue, 27 Feb 2024 17:14:35 -0800 Subject: [PATCH 05/21] Consolidate additional tests --- .../Feature/Notifications/AssetSlackTest.php | 110 ------------------ .../Notifications/LicenseSlackTest.php | 109 ----------------- .../SlackNotificationsUponCheckinTest.php | 101 ++++++++++++++++ .../SlackNotificationsUponCheckoutTest.php | 101 ++++++++++++++++ 4 files changed, 202 insertions(+), 219 deletions(-) delete mode 100644 tests/Feature/Notifications/AssetSlackTest.php delete mode 100644 tests/Feature/Notifications/LicenseSlackTest.php diff --git a/tests/Feature/Notifications/AssetSlackTest.php b/tests/Feature/Notifications/AssetSlackTest.php deleted file mode 100644 index c6d03df636..0000000000 --- a/tests/Feature/Notifications/AssetSlackTest.php +++ /dev/null @@ -1,110 +0,0 @@ - [fn() => User::factory()->create()], - 'Asset checked out to asset' => [fn() => Asset::factory()->laptopMbp()->create()], - 'Asset checked out to location' => [fn() => Location::factory()->create()], - ]; - } - - /** @dataProvider assetCheckoutTargets */ - public function testAssetCheckoutSendsSlackNotificationWhenSettingEnabled($checkoutTarget) - { - Notification::fake(); - - $this->settings->enableSlackWebhook(); - - event(new CheckoutableCheckedOut( - Asset::factory()->laptopMbp()->create(), - $checkoutTarget(), - User::factory()->superuser()->create(), - '' - )); - - Notification::assertSentTo( - new AnonymousNotifiable, - CheckoutAssetNotification::class, - function ($notification, $channels, $notifiable) { - return $notifiable->routes['slack'] === Setting::getSettings()->webhook_endpoint; - } - ); - } - - /** @dataProvider assetCheckoutTargets */ - public function testAssetCheckoutDoesNotSendSlackNotificationWhenSettingDisabled($checkoutTarget) - { - Notification::fake(); - - $this->settings->disableSlackWebhook(); - - event(new CheckoutableCheckedOut( - Asset::factory()->laptopMbp()->create(), - $checkoutTarget(), - User::factory()->superuser()->create(), - '' - )); - - Notification::assertNotSentTo(new AnonymousNotifiable, CheckoutAssetNotification::class); - } - - /** @dataProvider assetCheckoutTargets */ - public function testAssetCheckinSendsSlackNotificationWhenSettingEnabled($checkoutTarget) - { - Notification::fake(); - - $this->settings->enableSlackWebhook(); - - event(new CheckoutableCheckedIn( - Asset::factory()->laptopMbp()->create(), - $checkoutTarget(), - User::factory()->superuser()->create(), - '' - )); - - Notification::assertSentTo( - new AnonymousNotifiable, - CheckinAssetNotification::class, - function ($notification, $channels, $notifiable) { - return $notifiable->routes['slack'] === Setting::getSettings()->webhook_endpoint; - } - ); - } - - /** @dataProvider assetCheckoutTargets */ - public function testAssetCheckinDoesNotSendSlackNotificationWhenSettingDisabled($checkoutTarget) - { - Notification::fake(); - - $this->settings->disableSlackWebhook(); - - event(new CheckoutableCheckedIn( - Asset::factory()->laptopMbp()->create(), - $checkoutTarget(), - User::factory()->superuser()->create(), - '' - )); - - Notification::assertNotSentTo(new AnonymousNotifiable, CheckinAssetNotification::class); - } -} diff --git a/tests/Feature/Notifications/LicenseSlackTest.php b/tests/Feature/Notifications/LicenseSlackTest.php deleted file mode 100644 index 8ed38c2788..0000000000 --- a/tests/Feature/Notifications/LicenseSlackTest.php +++ /dev/null @@ -1,109 +0,0 @@ - [fn() => User::factory()->create()], - 'License checked out to asset' => [fn() => Asset::factory()->laptopMbp()->create()], - ]; - } - - /** @dataProvider licenseCheckoutTargets */ - public function testLicenseCheckoutSendsSlackNotificationWhenSettingEnabled($checkoutTarget) - { - Notification::fake(); - - $this->settings->enableSlackWebhook(); - - event(new CheckoutableCheckedOut( - LicenseSeat::factory()->create(), - $checkoutTarget(), - User::factory()->superuser()->create(), - '' - )); - - Notification::assertSentTo( - new AnonymousNotifiable, - CheckoutLicenseSeatNotification::class, - function ($notification, $channels, $notifiable) { - return $notifiable->routes['slack'] === Setting::getSettings()->webhook_endpoint; - } - ); - } - - /** @dataProvider licenseCheckoutTargets */ - public function testLicenseCheckoutDoesNotSendSlackNotificationWhenSettingDisabled($checkoutTarget) - { - Notification::fake(); - - $this->settings->disableSlackWebhook(); - - event(new CheckoutableCheckedOut( - LicenseSeat::factory()->create(), - $checkoutTarget(), - User::factory()->superuser()->create(), - '' - )); - - Notification::assertNotSentTo(new AnonymousNotifiable, CheckoutLicenseSeatNotification::class); - } - - /** @dataProvider licenseCheckoutTargets */ - public function testLicenseCheckinSendsSlackNotificationWhenSettingEnabled($checkoutTarget) - { - Notification::fake(); - - $this->settings->enableSlackWebhook(); - - event(new CheckoutableCheckedIn( - LicenseSeat::factory()->create(), - $checkoutTarget(), - User::factory()->superuser()->create(), - '' - )); - - Notification::assertSentTo( - new AnonymousNotifiable, - CheckinLicenseSeatNotification::class, - function ($notification, $channels, $notifiable) { - return $notifiable->routes['slack'] === Setting::getSettings()->webhook_endpoint; - } - ); - } - - /** @dataProvider licenseCheckoutTargets */ - public function testLicenseCheckinDoesNotSendSlackNotificationWhenSettingDisabled($checkoutTarget) - { - Notification::fake(); - - $this->settings->disableSlackWebhook(); - - event(new CheckoutableCheckedIn( - LicenseSeat::factory()->create(), - $checkoutTarget(), - User::factory()->superuser()->create(), - '' - )); - - Notification::assertNotSentTo(new AnonymousNotifiable, CheckinLicenseSeatNotification::class); - } -} diff --git a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php index 282417d822..2cf697e93f 100644 --- a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php +++ b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php @@ -6,9 +6,13 @@ use App\Events\CheckoutableCheckedIn; use App\Models\Accessory; use App\Models\Asset; use App\Models\Component; +use App\Models\LicenseSeat; +use App\Models\Location; use App\Models\Setting; use App\Models\User; use App\Notifications\CheckinAccessoryNotification; +use App\Notifications\CheckinAssetNotification; +use App\Notifications\CheckinLicenseSeatNotification; use Illuminate\Notifications\AnonymousNotifiable; use Illuminate\Support\Facades\Notification; use Tests\Support\InteractsWithSettings; @@ -18,6 +22,23 @@ class SlackNotificationsUponCheckinTest extends TestCase { use InteractsWithSettings; + public function assetCheckoutTargets(): array + { + 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()], + ]; + } + + public function licenseCheckoutTargets(): array + { + return [ + 'License checked out to user' => [fn() => User::factory()->create()], + 'License checked out to asset' => [fn() => Asset::factory()->laptopMbp()->create()], + ]; + } + public function testAccessoryCheckinSendsSlackNotificationWhenSettingEnabled() { Notification::fake(); @@ -56,6 +77,46 @@ class SlackNotificationsUponCheckinTest extends TestCase Notification::assertNotSentTo(new AnonymousNotifiable, CheckinAccessoryNotification::class); } + /** @dataProvider assetCheckoutTargets */ + public function testAssetCheckinSendsSlackNotificationWhenSettingEnabled($checkoutTarget) + { + Notification::fake(); + + $this->settings->enableSlackWebhook(); + + event(new CheckoutableCheckedIn( + Asset::factory()->laptopMbp()->create(), + $checkoutTarget(), + User::factory()->superuser()->create(), + '' + )); + + Notification::assertSentTo( + new AnonymousNotifiable, + CheckinAssetNotification::class, + function ($notification, $channels, $notifiable) { + return $notifiable->routes['slack'] === Setting::getSettings()->webhook_endpoint; + } + ); + } + + /** @dataProvider assetCheckoutTargets */ + public function testAssetCheckinDoesNotSendSlackNotificationWhenSettingDisabled($checkoutTarget) + { + Notification::fake(); + + $this->settings->disableSlackWebhook(); + + event(new CheckoutableCheckedIn( + Asset::factory()->laptopMbp()->create(), + $checkoutTarget(), + User::factory()->superuser()->create(), + '' + )); + + Notification::assertNotSentTo(new AnonymousNotifiable, CheckinAssetNotification::class); + } + public function testComponentCheckinDoesNotSendSlackNotification() { Notification::fake(); @@ -81,4 +142,44 @@ class SlackNotificationsUponCheckinTest extends TestCase { $this->markTestIncomplete(); } + + /** @dataProvider licenseCheckoutTargets */ + public function testLicenseCheckinSendsSlackNotificationWhenSettingEnabled($checkoutTarget) + { + Notification::fake(); + + $this->settings->enableSlackWebhook(); + + event(new CheckoutableCheckedIn( + LicenseSeat::factory()->create(), + $checkoutTarget(), + User::factory()->superuser()->create(), + '' + )); + + Notification::assertSentTo( + new AnonymousNotifiable, + CheckinLicenseSeatNotification::class, + function ($notification, $channels, $notifiable) { + return $notifiable->routes['slack'] === Setting::getSettings()->webhook_endpoint; + } + ); + } + + /** @dataProvider licenseCheckoutTargets */ + public function testLicenseCheckinDoesNotSendSlackNotificationWhenSettingDisabled($checkoutTarget) + { + Notification::fake(); + + $this->settings->disableSlackWebhook(); + + event(new CheckoutableCheckedIn( + LicenseSeat::factory()->create(), + $checkoutTarget(), + User::factory()->superuser()->create(), + '' + )); + + Notification::assertNotSentTo(new AnonymousNotifiable, CheckinLicenseSeatNotification::class); + } } diff --git a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php index 3992a6c51b..aeee70c9b4 100644 --- a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php +++ b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php @@ -7,10 +7,14 @@ use App\Models\Accessory; use App\Models\Asset; use App\Models\Component; use App\Models\Consumable; +use App\Models\LicenseSeat; +use App\Models\Location; use App\Models\Setting; use App\Models\User; use App\Notifications\CheckoutAccessoryNotification; +use App\Notifications\CheckoutAssetNotification; use App\Notifications\CheckoutConsumableNotification; +use App\Notifications\CheckoutLicenseSeatNotification; use Illuminate\Notifications\AnonymousNotifiable; use Illuminate\Support\Facades\Notification; use Tests\Support\InteractsWithSettings; @@ -20,6 +24,23 @@ class SlackNotificationsUponCheckoutTest extends TestCase { use InteractsWithSettings; + public function assetCheckoutTargets(): array + { + 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()], + ]; + } + + public function licenseCheckoutTargets(): array + { + return [ + 'License checked out to user' => [fn() => User::factory()->create()], + 'License checked out to asset' => [fn() => Asset::factory()->laptopMbp()->create()], + ]; + } + public function testAccessoryCheckoutSendsSlackNotificationWhenSettingEnabled() { Notification::fake(); @@ -58,6 +79,46 @@ class SlackNotificationsUponCheckoutTest extends TestCase Notification::assertNotSentTo(new AnonymousNotifiable, CheckoutAccessoryNotification::class); } + /** @dataProvider assetCheckoutTargets */ + public function testAssetCheckoutSendsSlackNotificationWhenSettingEnabled($checkoutTarget) + { + Notification::fake(); + + $this->settings->enableSlackWebhook(); + + event(new CheckoutableCheckedOut( + Asset::factory()->laptopMbp()->create(), + $checkoutTarget(), + User::factory()->superuser()->create(), + '' + )); + + Notification::assertSentTo( + new AnonymousNotifiable, + CheckoutAssetNotification::class, + function ($notification, $channels, $notifiable) { + return $notifiable->routes['slack'] === Setting::getSettings()->webhook_endpoint; + } + ); + } + + /** @dataProvider assetCheckoutTargets */ + public function testAssetCheckoutDoesNotSendSlackNotificationWhenSettingDisabled($checkoutTarget) + { + Notification::fake(); + + $this->settings->disableSlackWebhook(); + + event(new CheckoutableCheckedOut( + Asset::factory()->laptopMbp()->create(), + $checkoutTarget(), + User::factory()->superuser()->create(), + '' + )); + + Notification::assertNotSentTo(new AnonymousNotifiable, CheckoutAssetNotification::class); + } + public function testComponentCheckoutDoesNotSendSlackNotification() { Notification::fake(); @@ -111,4 +172,44 @@ class SlackNotificationsUponCheckoutTest extends TestCase Notification::assertNotSentTo(new AnonymousNotifiable, CheckoutConsumableNotification::class); } + + /** @dataProvider licenseCheckoutTargets */ + public function testLicenseCheckoutSendsSlackNotificationWhenSettingEnabled($checkoutTarget) + { + Notification::fake(); + + $this->settings->enableSlackWebhook(); + + event(new CheckoutableCheckedOut( + LicenseSeat::factory()->create(), + $checkoutTarget(), + User::factory()->superuser()->create(), + '' + )); + + Notification::assertSentTo( + new AnonymousNotifiable, + CheckoutLicenseSeatNotification::class, + function ($notification, $channels, $notifiable) { + return $notifiable->routes['slack'] === Setting::getSettings()->webhook_endpoint; + } + ); + } + + /** @dataProvider licenseCheckoutTargets */ + public function testLicenseCheckoutDoesNotSendSlackNotificationWhenSettingDisabled($checkoutTarget) + { + Notification::fake(); + + $this->settings->disableSlackWebhook(); + + event(new CheckoutableCheckedOut( + LicenseSeat::factory()->create(), + $checkoutTarget(), + User::factory()->superuser()->create(), + '' + )); + + Notification::assertNotSentTo(new AnonymousNotifiable, CheckoutLicenseSeatNotification::class); + } } From 2ea883aa1575bb09951f12b4c9ff1a82c70b9ea2 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Tue, 27 Feb 2024 17:23:30 -0800 Subject: [PATCH 06/21] Move Notification::fake() to setUp method --- .../SlackNotificationsUponCheckinTest.php | 21 ++++++---------- .../SlackNotificationsUponCheckoutTest.php | 25 ++++++------------- 2 files changed, 14 insertions(+), 32 deletions(-) diff --git a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php index 2cf697e93f..feb4cd26d9 100644 --- a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php +++ b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php @@ -22,6 +22,13 @@ class SlackNotificationsUponCheckinTest extends TestCase { use InteractsWithSettings; + protected function setUp(): void + { + parent::setUp(); + + Notification::fake(); + } + public function assetCheckoutTargets(): array { return [ @@ -41,8 +48,6 @@ class SlackNotificationsUponCheckinTest extends TestCase public function testAccessoryCheckinSendsSlackNotificationWhenSettingEnabled() { - Notification::fake(); - $this->settings->enableSlackWebhook(); event(new CheckoutableCheckedIn( @@ -63,8 +68,6 @@ class SlackNotificationsUponCheckinTest extends TestCase public function testAccessoryCheckinDoesNotSendSlackNotificationWhenSettingDisabled() { - Notification::fake(); - $this->settings->disableSlackWebhook(); event(new CheckoutableCheckedIn( @@ -80,8 +83,6 @@ class SlackNotificationsUponCheckinTest extends TestCase /** @dataProvider assetCheckoutTargets */ public function testAssetCheckinSendsSlackNotificationWhenSettingEnabled($checkoutTarget) { - Notification::fake(); - $this->settings->enableSlackWebhook(); event(new CheckoutableCheckedIn( @@ -103,8 +104,6 @@ class SlackNotificationsUponCheckinTest extends TestCase /** @dataProvider assetCheckoutTargets */ public function testAssetCheckinDoesNotSendSlackNotificationWhenSettingDisabled($checkoutTarget) { - Notification::fake(); - $this->settings->disableSlackWebhook(); event(new CheckoutableCheckedIn( @@ -119,8 +118,6 @@ class SlackNotificationsUponCheckinTest extends TestCase public function testComponentCheckinDoesNotSendSlackNotification() { - Notification::fake(); - $this->settings->enableSlackWebhook(); event(new CheckoutableCheckedIn( @@ -146,8 +143,6 @@ class SlackNotificationsUponCheckinTest extends TestCase /** @dataProvider licenseCheckoutTargets */ public function testLicenseCheckinSendsSlackNotificationWhenSettingEnabled($checkoutTarget) { - Notification::fake(); - $this->settings->enableSlackWebhook(); event(new CheckoutableCheckedIn( @@ -169,8 +164,6 @@ class SlackNotificationsUponCheckinTest extends TestCase /** @dataProvider licenseCheckoutTargets */ public function testLicenseCheckinDoesNotSendSlackNotificationWhenSettingDisabled($checkoutTarget) { - Notification::fake(); - $this->settings->disableSlackWebhook(); event(new CheckoutableCheckedIn( diff --git a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php index aeee70c9b4..4f8b1da223 100644 --- a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php +++ b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php @@ -24,6 +24,13 @@ class SlackNotificationsUponCheckoutTest extends TestCase { use InteractsWithSettings; + protected function setUp(): void + { + parent::setUp(); + + Notification::fake(); + } + public function assetCheckoutTargets(): array { return [ @@ -43,8 +50,6 @@ class SlackNotificationsUponCheckoutTest extends TestCase public function testAccessoryCheckoutSendsSlackNotificationWhenSettingEnabled() { - Notification::fake(); - $this->settings->enableSlackWebhook(); event(new CheckoutableCheckedOut( @@ -65,8 +70,6 @@ class SlackNotificationsUponCheckoutTest extends TestCase public function testAccessoryCheckoutDoesNotSendSlackNotificationWhenSettingDisabled() { - Notification::fake(); - $this->settings->disableSlackWebhook(); event(new CheckoutableCheckedOut( @@ -82,8 +85,6 @@ class SlackNotificationsUponCheckoutTest extends TestCase /** @dataProvider assetCheckoutTargets */ public function testAssetCheckoutSendsSlackNotificationWhenSettingEnabled($checkoutTarget) { - Notification::fake(); - $this->settings->enableSlackWebhook(); event(new CheckoutableCheckedOut( @@ -105,8 +106,6 @@ class SlackNotificationsUponCheckoutTest extends TestCase /** @dataProvider assetCheckoutTargets */ public function testAssetCheckoutDoesNotSendSlackNotificationWhenSettingDisabled($checkoutTarget) { - Notification::fake(); - $this->settings->disableSlackWebhook(); event(new CheckoutableCheckedOut( @@ -121,8 +120,6 @@ class SlackNotificationsUponCheckoutTest extends TestCase public function testComponentCheckoutDoesNotSendSlackNotification() { - Notification::fake(); - $this->settings->enableSlackWebhook(); event(new CheckoutableCheckedOut( @@ -137,8 +134,6 @@ class SlackNotificationsUponCheckoutTest extends TestCase public function testConsumableCheckoutSendsSlackNotificationWhenSettingEnabled() { - Notification::fake(); - $this->settings->enableSlackWebhook(); event(new CheckoutableCheckedOut( @@ -159,8 +154,6 @@ class SlackNotificationsUponCheckoutTest extends TestCase public function testConsumableCheckoutDoesNotSendSlackNotificationWhenSettingDisabled() { - Notification::fake(); - $this->settings->disableSlackWebhook(); event(new CheckoutableCheckedOut( @@ -176,8 +169,6 @@ class SlackNotificationsUponCheckoutTest extends TestCase /** @dataProvider licenseCheckoutTargets */ public function testLicenseCheckoutSendsSlackNotificationWhenSettingEnabled($checkoutTarget) { - Notification::fake(); - $this->settings->enableSlackWebhook(); event(new CheckoutableCheckedOut( @@ -199,8 +190,6 @@ class SlackNotificationsUponCheckoutTest extends TestCase /** @dataProvider licenseCheckoutTargets */ public function testLicenseCheckoutDoesNotSendSlackNotificationWhenSettingDisabled($checkoutTarget) { - Notification::fake(); - $this->settings->disableSlackWebhook(); event(new CheckoutableCheckedOut( From bf32ab177ff68b2bbe4acbbabc923fe7ed850c76 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Tue, 27 Feb 2024 17:37:07 -0800 Subject: [PATCH 07/21] Improve readability by extracting fireCheckInEvent method --- .../SlackNotificationsUponCheckinTest.php | 53 +++++++++---------- 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php index feb4cd26d9..e73d62a1cb 100644 --- a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php +++ b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php @@ -13,6 +13,7 @@ use App\Models\User; use App\Notifications\CheckinAccessoryNotification; use App\Notifications\CheckinAssetNotification; use App\Notifications\CheckinLicenseSeatNotification; +use Illuminate\Database\Eloquent\Model; use Illuminate\Notifications\AnonymousNotifiable; use Illuminate\Support\Facades\Notification; use Tests\Support\InteractsWithSettings; @@ -50,12 +51,10 @@ class SlackNotificationsUponCheckinTest extends TestCase { $this->settings->enableSlackWebhook(); - event(new CheckoutableCheckedIn( + $this->fireCheckInEvent( Accessory::factory()->appleBtKeyboard()->create(), User::factory()->create(), - User::factory()->superuser()->create(), - '' - )); + ); Notification::assertSentTo( new AnonymousNotifiable, @@ -70,12 +69,10 @@ class SlackNotificationsUponCheckinTest extends TestCase { $this->settings->disableSlackWebhook(); - event(new CheckoutableCheckedIn( + $this->fireCheckInEvent( Accessory::factory()->appleBtKeyboard()->create(), User::factory()->create(), - User::factory()->superuser()->create(), - '' - )); + ); Notification::assertNotSentTo(new AnonymousNotifiable, CheckinAccessoryNotification::class); } @@ -85,12 +82,10 @@ class SlackNotificationsUponCheckinTest extends TestCase { $this->settings->enableSlackWebhook(); - event(new CheckoutableCheckedIn( + $this->fireCheckInEvent( Asset::factory()->laptopMbp()->create(), $checkoutTarget(), - User::factory()->superuser()->create(), - '' - )); + ); Notification::assertSentTo( new AnonymousNotifiable, @@ -106,12 +101,10 @@ class SlackNotificationsUponCheckinTest extends TestCase { $this->settings->disableSlackWebhook(); - event(new CheckoutableCheckedIn( + $this->fireCheckInEvent( Asset::factory()->laptopMbp()->create(), $checkoutTarget(), - User::factory()->superuser()->create(), - '' - )); + ); Notification::assertNotSentTo(new AnonymousNotifiable, CheckinAssetNotification::class); } @@ -120,12 +113,10 @@ class SlackNotificationsUponCheckinTest extends TestCase { $this->settings->enableSlackWebhook(); - event(new CheckoutableCheckedIn( + $this->fireCheckInEvent( Component::factory()->ramCrucial8()->create(), Asset::factory()->laptopMbp()->create(), - User::factory()->superuser()->create(), - '' - )); + ); Notification::assertNothingSent(); } @@ -145,12 +136,10 @@ class SlackNotificationsUponCheckinTest extends TestCase { $this->settings->enableSlackWebhook(); - event(new CheckoutableCheckedIn( + $this->fireCheckInEvent( LicenseSeat::factory()->create(), $checkoutTarget(), - User::factory()->superuser()->create(), - '' - )); + ); Notification::assertSentTo( new AnonymousNotifiable, @@ -166,13 +155,21 @@ class SlackNotificationsUponCheckinTest extends TestCase { $this->settings->disableSlackWebhook(); - event(new CheckoutableCheckedIn( + $this->fireCheckInEvent( LicenseSeat::factory()->create(), $checkoutTarget(), - User::factory()->superuser()->create(), - '' - )); + ); Notification::assertNotSentTo(new AnonymousNotifiable, CheckinLicenseSeatNotification::class); } + + private function fireCheckInEvent(Model $checkoutable, Model $target) + { + event(new CheckoutableCheckedIn( + $checkoutable, + $target, + User::factory()->superuser()->create(), + '' + )); + } } From bd4d3aa52bae2d4f92e60abfbf12b459b8c92f97 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Tue, 27 Feb 2024 17:44:19 -0800 Subject: [PATCH 08/21] Improve readability by extracting additional helpers --- .../SlackNotificationsUponCheckinTest.php | 46 +++++++++---------- 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php index e73d62a1cb..8d8bf3a6e0 100644 --- a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php +++ b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php @@ -56,13 +56,7 @@ class SlackNotificationsUponCheckinTest extends TestCase User::factory()->create(), ); - Notification::assertSentTo( - new AnonymousNotifiable, - CheckinAccessoryNotification::class, - function ($notification, $channels, $notifiable) { - return $notifiable->routes['slack'] === Setting::getSettings()->webhook_endpoint; - } - ); + $this->assertSlackNotificationSent(CheckinAccessoryNotification::class); } public function testAccessoryCheckinDoesNotSendSlackNotificationWhenSettingDisabled() @@ -74,7 +68,7 @@ class SlackNotificationsUponCheckinTest extends TestCase User::factory()->create(), ); - Notification::assertNotSentTo(new AnonymousNotifiable, CheckinAccessoryNotification::class); + $this->assertNoSlackNotificationSent(CheckinAccessoryNotification::class); } /** @dataProvider assetCheckoutTargets */ @@ -87,13 +81,7 @@ class SlackNotificationsUponCheckinTest extends TestCase $checkoutTarget(), ); - Notification::assertSentTo( - new AnonymousNotifiable, - CheckinAssetNotification::class, - function ($notification, $channels, $notifiable) { - return $notifiable->routes['slack'] === Setting::getSettings()->webhook_endpoint; - } - ); + $this->assertSlackNotificationSent(CheckinAssetNotification::class); } /** @dataProvider assetCheckoutTargets */ @@ -106,7 +94,7 @@ class SlackNotificationsUponCheckinTest extends TestCase $checkoutTarget(), ); - Notification::assertNotSentTo(new AnonymousNotifiable, CheckinAssetNotification::class); + $this->assertNoSlackNotificationSent(CheckinAssetNotification::class); } public function testComponentCheckinDoesNotSendSlackNotification() @@ -141,13 +129,7 @@ class SlackNotificationsUponCheckinTest extends TestCase $checkoutTarget(), ); - Notification::assertSentTo( - new AnonymousNotifiable, - CheckinLicenseSeatNotification::class, - function ($notification, $channels, $notifiable) { - return $notifiable->routes['slack'] === Setting::getSettings()->webhook_endpoint; - } - ); + $this->assertSlackNotificationSent(CheckinLicenseSeatNotification::class); } /** @dataProvider licenseCheckoutTargets */ @@ -160,7 +142,7 @@ class SlackNotificationsUponCheckinTest extends TestCase $checkoutTarget(), ); - Notification::assertNotSentTo(new AnonymousNotifiable, CheckinLicenseSeatNotification::class); + $this->assertNoSlackNotificationSent(CheckinLicenseSeatNotification::class); } private function fireCheckInEvent(Model $checkoutable, Model $target) @@ -172,4 +154,20 @@ class SlackNotificationsUponCheckinTest extends TestCase '' )); } + + private function assertSlackNotificationSent(string $notificationClass) + { + Notification::assertSentTo( + new AnonymousNotifiable, + $notificationClass, + function ($notification, $channels, $notifiable) { + return $notifiable->routes['slack'] === Setting::getSettings()->webhook_endpoint; + } + ); + } + + private function assertNoSlackNotificationSent(string $notificationClass) + { + Notification::assertNotSentTo(new AnonymousNotifiable, CheckinAccessoryNotification::class); + } } From 9a93ad2e06efc2020224c50b15df63dd632303f8 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Tue, 27 Feb 2024 17:46:27 -0800 Subject: [PATCH 09/21] Remove unneeded factory state --- .../Webhooks/SlackNotificationsUponCheckinTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php index 8d8bf3a6e0..be6b9f8113 100644 --- a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php +++ b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php @@ -52,7 +52,7 @@ class SlackNotificationsUponCheckinTest extends TestCase $this->settings->enableSlackWebhook(); $this->fireCheckInEvent( - Accessory::factory()->appleBtKeyboard()->create(), + Accessory::factory()->create(), User::factory()->create(), ); @@ -64,7 +64,7 @@ class SlackNotificationsUponCheckinTest extends TestCase $this->settings->disableSlackWebhook(); $this->fireCheckInEvent( - Accessory::factory()->appleBtKeyboard()->create(), + Accessory::factory()->create(), User::factory()->create(), ); From 15b8140bffc5dbd113ebaac26bcf74f0da1a448a Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Tue, 27 Feb 2024 17:48:17 -0800 Subject: [PATCH 10/21] Fix test helper --- .../Webhooks/SlackNotificationsUponCheckinTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php index be6b9f8113..c03d9a30aa 100644 --- a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php +++ b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php @@ -168,6 +168,6 @@ class SlackNotificationsUponCheckinTest extends TestCase private function assertNoSlackNotificationSent(string $notificationClass) { - Notification::assertNotSentTo(new AnonymousNotifiable, CheckinAccessoryNotification::class); + Notification::assertNotSentTo(new AnonymousNotifiable, $notificationClass); } } From cd579a04dd3add300e5edcd7c4e26ffebe5b4e6e Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Tue, 27 Feb 2024 17:52:18 -0800 Subject: [PATCH 11/21] Improve readability by extracting fireCheckOutEvent method --- .../SlackNotificationsUponCheckoutTest.php | 65 +++++++++---------- 1 file changed, 29 insertions(+), 36 deletions(-) diff --git a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php index 4f8b1da223..7e3097f6d6 100644 --- a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php +++ b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php @@ -15,6 +15,7 @@ use App\Notifications\CheckoutAccessoryNotification; use App\Notifications\CheckoutAssetNotification; use App\Notifications\CheckoutConsumableNotification; use App\Notifications\CheckoutLicenseSeatNotification; +use Illuminate\Database\Eloquent\Model; use Illuminate\Notifications\AnonymousNotifiable; use Illuminate\Support\Facades\Notification; use Tests\Support\InteractsWithSettings; @@ -52,12 +53,10 @@ class SlackNotificationsUponCheckoutTest extends TestCase { $this->settings->enableSlackWebhook(); - event(new CheckoutableCheckedOut( + $this->fireCheckOutEvent( Accessory::factory()->appleBtKeyboard()->create(), User::factory()->create(), - User::factory()->superuser()->create(), - '' - )); + ); Notification::assertSentTo( new AnonymousNotifiable, @@ -72,12 +71,10 @@ class SlackNotificationsUponCheckoutTest extends TestCase { $this->settings->disableSlackWebhook(); - event(new CheckoutableCheckedOut( + $this->fireCheckOutEvent( Accessory::factory()->appleBtKeyboard()->create(), User::factory()->create(), - User::factory()->superuser()->create(), - '' - )); + ); Notification::assertNotSentTo(new AnonymousNotifiable, CheckoutAccessoryNotification::class); } @@ -87,12 +84,10 @@ class SlackNotificationsUponCheckoutTest extends TestCase { $this->settings->enableSlackWebhook(); - event(new CheckoutableCheckedOut( + $this->fireCheckOutEvent( Asset::factory()->laptopMbp()->create(), $checkoutTarget(), - User::factory()->superuser()->create(), - '' - )); + ); Notification::assertSentTo( new AnonymousNotifiable, @@ -108,12 +103,10 @@ class SlackNotificationsUponCheckoutTest extends TestCase { $this->settings->disableSlackWebhook(); - event(new CheckoutableCheckedOut( + $this->fireCheckOutEvent( Asset::factory()->laptopMbp()->create(), $checkoutTarget(), - User::factory()->superuser()->create(), - '' - )); + ); Notification::assertNotSentTo(new AnonymousNotifiable, CheckoutAssetNotification::class); } @@ -122,12 +115,10 @@ class SlackNotificationsUponCheckoutTest extends TestCase { $this->settings->enableSlackWebhook(); - event(new CheckoutableCheckedOut( + $this->fireCheckOutEvent( Component::factory()->ramCrucial8()->create(), Asset::factory()->laptopMbp()->create(), - User::factory()->superuser()->create(), - '' - )); + ); Notification::assertNothingSent(); } @@ -136,12 +127,10 @@ class SlackNotificationsUponCheckoutTest extends TestCase { $this->settings->enableSlackWebhook(); - event(new CheckoutableCheckedOut( + $this->fireCheckOutEvent( Consumable::factory()->cardstock()->create(), User::factory()->create(), - User::factory()->superuser()->create(), - '' - )); + ); Notification::assertSentTo( new AnonymousNotifiable, @@ -156,12 +145,10 @@ class SlackNotificationsUponCheckoutTest extends TestCase { $this->settings->disableSlackWebhook(); - event(new CheckoutableCheckedOut( + $this->fireCheckOutEvent( Consumable::factory()->cardstock()->create(), User::factory()->create(), - User::factory()->superuser()->create(), - '' - )); + ); Notification::assertNotSentTo(new AnonymousNotifiable, CheckoutConsumableNotification::class); } @@ -171,12 +158,10 @@ class SlackNotificationsUponCheckoutTest extends TestCase { $this->settings->enableSlackWebhook(); - event(new CheckoutableCheckedOut( + $this->fireCheckOutEvent( LicenseSeat::factory()->create(), $checkoutTarget(), - User::factory()->superuser()->create(), - '' - )); + ); Notification::assertSentTo( new AnonymousNotifiable, @@ -192,13 +177,21 @@ class SlackNotificationsUponCheckoutTest extends TestCase { $this->settings->disableSlackWebhook(); - event(new CheckoutableCheckedOut( + $this->fireCheckOutEvent( LicenseSeat::factory()->create(), $checkoutTarget(), - User::factory()->superuser()->create(), - '' - )); + ); Notification::assertNotSentTo(new AnonymousNotifiable, CheckoutLicenseSeatNotification::class); } + + private function fireCheckOutEvent(Model $checkoutable, Model $target) + { + event(new CheckoutableCheckedOut( + $checkoutable, + $target, + User::factory()->superuser()->create(), + '', + )); + } } From d20844fefa68cf324dab9579949e886552b7ce4f Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Tue, 27 Feb 2024 17:57:12 -0800 Subject: [PATCH 12/21] Improve readability by extracting additional helpers --- .../SlackNotificationsUponCheckoutTest.php | 56 ++++++++----------- 1 file changed, 24 insertions(+), 32 deletions(-) diff --git a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php index 7e3097f6d6..739c7e96b8 100644 --- a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php +++ b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php @@ -58,13 +58,7 @@ class SlackNotificationsUponCheckoutTest extends TestCase User::factory()->create(), ); - Notification::assertSentTo( - new AnonymousNotifiable, - CheckoutAccessoryNotification::class, - function ($notification, $channels, $notifiable) { - return $notifiable->routes['slack'] === Setting::getSettings()->webhook_endpoint; - } - ); + $this->assertSlackNotificationSent(CheckoutAccessoryNotification::class); } public function testAccessoryCheckoutDoesNotSendSlackNotificationWhenSettingDisabled() @@ -76,7 +70,7 @@ class SlackNotificationsUponCheckoutTest extends TestCase User::factory()->create(), ); - Notification::assertNotSentTo(new AnonymousNotifiable, CheckoutAccessoryNotification::class); + $this->assertNoSlackNotificationSent(CheckoutAccessoryNotification::class); } /** @dataProvider assetCheckoutTargets */ @@ -89,13 +83,7 @@ class SlackNotificationsUponCheckoutTest extends TestCase $checkoutTarget(), ); - Notification::assertSentTo( - new AnonymousNotifiable, - CheckoutAssetNotification::class, - function ($notification, $channels, $notifiable) { - return $notifiable->routes['slack'] === Setting::getSettings()->webhook_endpoint; - } - ); + $this->assertSlackNotificationSent(CheckoutAssetNotification::class); } /** @dataProvider assetCheckoutTargets */ @@ -108,7 +96,7 @@ class SlackNotificationsUponCheckoutTest extends TestCase $checkoutTarget(), ); - Notification::assertNotSentTo(new AnonymousNotifiable, CheckoutAssetNotification::class); + $this->assertNoSlackNotificationSent(CheckoutAssetNotification::class); } public function testComponentCheckoutDoesNotSendSlackNotification() @@ -132,13 +120,7 @@ class SlackNotificationsUponCheckoutTest extends TestCase User::factory()->create(), ); - Notification::assertSentTo( - new AnonymousNotifiable, - CheckoutConsumableNotification::class, - function ($notification, $channels, $notifiable) { - return $notifiable->routes['slack'] === Setting::getSettings()->webhook_endpoint; - } - ); + $this->assertSlackNotificationSent(CheckoutConsumableNotification::class); } public function testConsumableCheckoutDoesNotSendSlackNotificationWhenSettingDisabled() @@ -150,7 +132,7 @@ class SlackNotificationsUponCheckoutTest extends TestCase User::factory()->create(), ); - Notification::assertNotSentTo(new AnonymousNotifiable, CheckoutConsumableNotification::class); + $this->assertNoSlackNotificationSent(CheckoutConsumableNotification::class); } /** @dataProvider licenseCheckoutTargets */ @@ -163,13 +145,7 @@ class SlackNotificationsUponCheckoutTest extends TestCase $checkoutTarget(), ); - Notification::assertSentTo( - new AnonymousNotifiable, - CheckoutLicenseSeatNotification::class, - function ($notification, $channels, $notifiable) { - return $notifiable->routes['slack'] === Setting::getSettings()->webhook_endpoint; - } - ); + $this->assertSlackNotificationSent(CheckoutLicenseSeatNotification::class); } /** @dataProvider licenseCheckoutTargets */ @@ -182,7 +158,7 @@ class SlackNotificationsUponCheckoutTest extends TestCase $checkoutTarget(), ); - Notification::assertNotSentTo(new AnonymousNotifiable, CheckoutLicenseSeatNotification::class); + $this->assertNoSlackNotificationSent(CheckoutLicenseSeatNotification::class); } private function fireCheckOutEvent(Model $checkoutable, Model $target) @@ -194,4 +170,20 @@ class SlackNotificationsUponCheckoutTest extends TestCase '', )); } + + private function assertSlackNotificationSent(string $notificationClass) + { + Notification::assertSentTo( + new AnonymousNotifiable, + $notificationClass, + function ($notification, $channels, $notifiable) { + return $notifiable->routes['slack'] === Setting::getSettings()->webhook_endpoint; + } + ); + } + + private function assertNoSlackNotificationSent(string $notificationClass) + { + Notification::assertNotSentTo(new AnonymousNotifiable, $notificationClass); + } } From 8978dff054ac0d32913b5d7e73249d8016785579 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Tue, 27 Feb 2024 18:01:08 -0800 Subject: [PATCH 13/21] Consolidate helpers into trait --- .../SlackNotificationsUponCheckinTest.php | 20 ++------------ .../SlackNotificationsUponCheckoutTest.php | 20 ++------------ .../AssertsAgainstSlackNotifications.php | 26 +++++++++++++++++++ 3 files changed, 30 insertions(+), 36 deletions(-) create mode 100644 tests/Support/AssertsAgainstSlackNotifications.php diff --git a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php index c03d9a30aa..372f212c03 100644 --- a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php +++ b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php @@ -8,19 +8,19 @@ use App\Models\Asset; use App\Models\Component; use App\Models\LicenseSeat; use App\Models\Location; -use App\Models\Setting; use App\Models\User; use App\Notifications\CheckinAccessoryNotification; use App\Notifications\CheckinAssetNotification; use App\Notifications\CheckinLicenseSeatNotification; use Illuminate\Database\Eloquent\Model; -use Illuminate\Notifications\AnonymousNotifiable; use Illuminate\Support\Facades\Notification; +use Tests\Support\AssertsAgainstSlackNotifications; use Tests\Support\InteractsWithSettings; use Tests\TestCase; class SlackNotificationsUponCheckinTest extends TestCase { + use AssertsAgainstSlackNotifications; use InteractsWithSettings; protected function setUp(): void @@ -154,20 +154,4 @@ class SlackNotificationsUponCheckinTest extends TestCase '' )); } - - private function assertSlackNotificationSent(string $notificationClass) - { - Notification::assertSentTo( - new AnonymousNotifiable, - $notificationClass, - function ($notification, $channels, $notifiable) { - return $notifiable->routes['slack'] === Setting::getSettings()->webhook_endpoint; - } - ); - } - - private function assertNoSlackNotificationSent(string $notificationClass) - { - Notification::assertNotSentTo(new AnonymousNotifiable, $notificationClass); - } } diff --git a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php index 739c7e96b8..58570f90e7 100644 --- a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php +++ b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php @@ -9,20 +9,20 @@ use App\Models\Component; use App\Models\Consumable; use App\Models\LicenseSeat; use App\Models\Location; -use App\Models\Setting; use App\Models\User; use App\Notifications\CheckoutAccessoryNotification; use App\Notifications\CheckoutAssetNotification; use App\Notifications\CheckoutConsumableNotification; use App\Notifications\CheckoutLicenseSeatNotification; use Illuminate\Database\Eloquent\Model; -use Illuminate\Notifications\AnonymousNotifiable; use Illuminate\Support\Facades\Notification; +use Tests\Support\AssertsAgainstSlackNotifications; use Tests\Support\InteractsWithSettings; use Tests\TestCase; class SlackNotificationsUponCheckoutTest extends TestCase { + use AssertsAgainstSlackNotifications; use InteractsWithSettings; protected function setUp(): void @@ -170,20 +170,4 @@ class SlackNotificationsUponCheckoutTest extends TestCase '', )); } - - private function assertSlackNotificationSent(string $notificationClass) - { - Notification::assertSentTo( - new AnonymousNotifiable, - $notificationClass, - function ($notification, $channels, $notifiable) { - return $notifiable->routes['slack'] === Setting::getSettings()->webhook_endpoint; - } - ); - } - - private function assertNoSlackNotificationSent(string $notificationClass) - { - Notification::assertNotSentTo(new AnonymousNotifiable, $notificationClass); - } } diff --git a/tests/Support/AssertsAgainstSlackNotifications.php b/tests/Support/AssertsAgainstSlackNotifications.php new file mode 100644 index 0000000000..11e6beea2d --- /dev/null +++ b/tests/Support/AssertsAgainstSlackNotifications.php @@ -0,0 +1,26 @@ +routes['slack'] === Setting::getSettings()->webhook_endpoint; + } + ); + } + + public function assertNoSlackNotificationSent(string $notificationClass) + { + Notification::assertNotSentTo(new AnonymousNotifiable, $notificationClass); + } +} From 03b7891edc6d9af615240f6405c6248970280613 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Tue, 27 Feb 2024 18:03:53 -0800 Subject: [PATCH 14/21] Remove unneeded factory states --- .../Webhooks/SlackNotificationsUponCheckinTest.php | 6 +++--- .../SlackNotificationsUponCheckoutTest.php | 14 +++++++------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php index 372f212c03..b352574ff6 100644 --- a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php +++ b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php @@ -77,7 +77,7 @@ class SlackNotificationsUponCheckinTest extends TestCase $this->settings->enableSlackWebhook(); $this->fireCheckInEvent( - Asset::factory()->laptopMbp()->create(), + Asset::factory()->create(), $checkoutTarget(), ); @@ -90,7 +90,7 @@ class SlackNotificationsUponCheckinTest extends TestCase $this->settings->disableSlackWebhook(); $this->fireCheckInEvent( - Asset::factory()->laptopMbp()->create(), + Asset::factory()->create(), $checkoutTarget(), ); @@ -102,7 +102,7 @@ class SlackNotificationsUponCheckinTest extends TestCase $this->settings->enableSlackWebhook(); $this->fireCheckInEvent( - Component::factory()->ramCrucial8()->create(), + Component::factory()->create(), Asset::factory()->laptopMbp()->create(), ); diff --git a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php index 58570f90e7..91d2917844 100644 --- a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php +++ b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php @@ -54,7 +54,7 @@ class SlackNotificationsUponCheckoutTest extends TestCase $this->settings->enableSlackWebhook(); $this->fireCheckOutEvent( - Accessory::factory()->appleBtKeyboard()->create(), + Accessory::factory()->create(), User::factory()->create(), ); @@ -66,7 +66,7 @@ class SlackNotificationsUponCheckoutTest extends TestCase $this->settings->disableSlackWebhook(); $this->fireCheckOutEvent( - Accessory::factory()->appleBtKeyboard()->create(), + Accessory::factory()->create(), User::factory()->create(), ); @@ -79,7 +79,7 @@ class SlackNotificationsUponCheckoutTest extends TestCase $this->settings->enableSlackWebhook(); $this->fireCheckOutEvent( - Asset::factory()->laptopMbp()->create(), + Asset::factory()->create(), $checkoutTarget(), ); @@ -92,7 +92,7 @@ class SlackNotificationsUponCheckoutTest extends TestCase $this->settings->disableSlackWebhook(); $this->fireCheckOutEvent( - Asset::factory()->laptopMbp()->create(), + Asset::factory()->create(), $checkoutTarget(), ); @@ -104,7 +104,7 @@ class SlackNotificationsUponCheckoutTest extends TestCase $this->settings->enableSlackWebhook(); $this->fireCheckOutEvent( - Component::factory()->ramCrucial8()->create(), + Component::factory()->create(), Asset::factory()->laptopMbp()->create(), ); @@ -116,7 +116,7 @@ class SlackNotificationsUponCheckoutTest extends TestCase $this->settings->enableSlackWebhook(); $this->fireCheckOutEvent( - Consumable::factory()->cardstock()->create(), + Consumable::factory()->create(), User::factory()->create(), ); @@ -128,7 +128,7 @@ class SlackNotificationsUponCheckoutTest extends TestCase $this->settings->disableSlackWebhook(); $this->fireCheckOutEvent( - Consumable::factory()->cardstock()->create(), + Consumable::factory()->create(), User::factory()->create(), ); From 551354b1bbc91737d12f40249723e39369ead2ae Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Tue, 27 Feb 2024 18:05:18 -0800 Subject: [PATCH 15/21] Add group annotation for tests --- .../Webhooks/SlackNotificationsUponCheckinTest.php | 3 +++ .../Webhooks/SlackNotificationsUponCheckoutTest.php | 3 +++ 2 files changed, 6 insertions(+) diff --git a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php index b352574ff6..324beda752 100644 --- a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php +++ b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php @@ -18,6 +18,9 @@ use Tests\Support\AssertsAgainstSlackNotifications; use Tests\Support\InteractsWithSettings; use Tests\TestCase; +/** + * @group notifications + */ class SlackNotificationsUponCheckinTest extends TestCase { use AssertsAgainstSlackNotifications; diff --git a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php index 91d2917844..fd628d35ed 100644 --- a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php +++ b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php @@ -20,6 +20,9 @@ use Tests\Support\AssertsAgainstSlackNotifications; use Tests\Support\InteractsWithSettings; use Tests\TestCase; +/** + * @group notifications + */ class SlackNotificationsUponCheckoutTest extends TestCase { use AssertsAgainstSlackNotifications; From 01d5d4c2c8c03283e58ab8f55277d0f0c9edadfd Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Wed, 13 Mar 2024 16:37:50 -0700 Subject: [PATCH 16/21] Improve data provider name --- .../Webhooks/SlackNotificationsUponCheckinTest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php index 324beda752..2ccbb47e72 100644 --- a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php +++ b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php @@ -33,7 +33,7 @@ class SlackNotificationsUponCheckinTest extends TestCase Notification::fake(); } - public function assetCheckoutTargets(): array + public function assetCheckInTargets(): array { return [ 'Asset checked out to user' => [fn() => User::factory()->create()], @@ -42,7 +42,7 @@ class SlackNotificationsUponCheckinTest extends TestCase ]; } - public function licenseCheckoutTargets(): array + public function licenseCheckInTargets(): array { return [ 'License checked out to user' => [fn() => User::factory()->create()], @@ -74,7 +74,7 @@ class SlackNotificationsUponCheckinTest extends TestCase $this->assertNoSlackNotificationSent(CheckinAccessoryNotification::class); } - /** @dataProvider assetCheckoutTargets */ + /** @dataProvider assetCheckInTargets */ public function testAssetCheckinSendsSlackNotificationWhenSettingEnabled($checkoutTarget) { $this->settings->enableSlackWebhook(); @@ -87,7 +87,7 @@ class SlackNotificationsUponCheckinTest extends TestCase $this->assertSlackNotificationSent(CheckinAssetNotification::class); } - /** @dataProvider assetCheckoutTargets */ + /** @dataProvider assetCheckInTargets */ public function testAssetCheckinDoesNotSendSlackNotificationWhenSettingDisabled($checkoutTarget) { $this->settings->disableSlackWebhook(); @@ -122,7 +122,7 @@ class SlackNotificationsUponCheckinTest extends TestCase $this->markTestIncomplete(); } - /** @dataProvider licenseCheckoutTargets */ + /** @dataProvider licenseCheckInTargets */ public function testLicenseCheckinSendsSlackNotificationWhenSettingEnabled($checkoutTarget) { $this->settings->enableSlackWebhook(); @@ -135,7 +135,7 @@ class SlackNotificationsUponCheckinTest extends TestCase $this->assertSlackNotificationSent(CheckinLicenseSeatNotification::class); } - /** @dataProvider licenseCheckoutTargets */ + /** @dataProvider licenseCheckInTargets */ public function testLicenseCheckinDoesNotSendSlackNotificationWhenSettingDisabled($checkoutTarget) { $this->settings->disableSlackWebhook(); From 8aed26aab112bcc6401b04a7aba61253de735d48 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Wed, 13 Mar 2024 16:56:54 -0700 Subject: [PATCH 17/21] Remove unneeded test cases --- .../Webhooks/SlackNotificationsUponCheckinTest.php | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php index 2ccbb47e72..5397aae196 100644 --- a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php +++ b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php @@ -112,16 +112,6 @@ class SlackNotificationsUponCheckinTest extends TestCase Notification::assertNothingSent(); } - public function testConsumableCheckinSendSlackNotificationWhenSettingEnabled() - { - $this->markTestIncomplete(); - } - - public function testConsumableCheckinDoesNotSendSlackNotificationWhenSettingDisabled() - { - $this->markTestIncomplete(); - } - /** @dataProvider licenseCheckInTargets */ public function testLicenseCheckinSendsSlackNotificationWhenSettingEnabled($checkoutTarget) { From 07c5264b4143a9c1b6a8c72976f719bf288c04c0 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 14 Mar 2024 11:17:47 -0700 Subject: [PATCH 18/21] Organize email check in notifications test --- .../Notifications/AssetWebhookTest.php | 163 ------------------ .../EmailNotificationsUponCheckinTest.php | 64 +++++++ 2 files changed, 64 insertions(+), 163 deletions(-) delete mode 100644 tests/Feature/Notifications/AssetWebhookTest.php create mode 100644 tests/Feature/Notifications/Email/EmailNotificationsUponCheckinTest.php diff --git a/tests/Feature/Notifications/AssetWebhookTest.php b/tests/Feature/Notifications/AssetWebhookTest.php deleted file mode 100644 index 95218f98e7..0000000000 --- a/tests/Feature/Notifications/AssetWebhookTest.php +++ /dev/null @@ -1,163 +0,0 @@ - [fn() => User::factory()->create()], - 'Asset checked out to asset' => [fn() => $this->createAsset()], - 'Asset checked out to location' => [fn() => Location::factory()->create()], - ]; - } - - /** @dataProvider targets */ - public function testAssetCheckoutSendsWebhookNotificationWhenSettingEnabled($checkoutTarget) - { - Notification::fake(); - - $this->settings->enableSlackWebhook(); - - event(new CheckoutableCheckedOut( - $this->createAsset(), - $checkoutTarget(), - User::factory()->superuser()->create(), - '' - )); - - Notification::assertSentTo( - new AnonymousNotifiable, - CheckoutAssetNotification::class, - function ($notification, $channels, $notifiable) { - return $notifiable->routes['slack'] === Setting::getSettings()->webhook_endpoint; - } - ); - } - - /** @dataProvider targets */ - public function testAssetCheckoutDoesNotSendWebhookNotificationWhenSettingDisabled($checkoutTarget) - { - Notification::fake(); - - $this->settings->disableWebhook(); - - event(new CheckoutableCheckedOut( - $this->createAsset(), - $checkoutTarget(), - User::factory()->superuser()->create(), - '' - )); - - Notification::assertNotSentTo(new AnonymousNotifiable, CheckoutAssetNotification::class); - } - - /** @dataProvider targets */ - public function testAssetCheckinSendsWebhookNotificationWhenSettingEnabled($checkoutTarget) - { - Notification::fake(); - - $this->settings->enableSlackWebhook(); - - event(new CheckoutableCheckedIn( - $this->createAsset(), - $checkoutTarget(), - User::factory()->superuser()->create(), - '' - )); - - Notification::assertSentTo( - new AnonymousNotifiable, - CheckinAssetNotification::class, - function ($notification, $channels, $notifiable) { - return $notifiable->routes['slack'] === Setting::getSettings()->webhook_endpoint; - } - ); - } - - /** @dataProvider targets */ - public function testAssetCheckinDoesNotSendWebhookNotificationWhenSettingDisabled($checkoutTarget) - { - Notification::fake(); - - $this->settings->disableWebhook(); - - event(new CheckoutableCheckedIn( - $this->createAsset(), - $checkoutTarget(), - User::factory()->superuser()->create(), - '' - )); - - Notification::assertNotSentTo(new AnonymousNotifiable, CheckinAssetNotification::class); - } - - public function testCheckInEmailSentToUserIfSettingEnabled() - { - Notification::fake(); - - $user = User::factory()->create(); - $asset = Asset::factory()->assignedToUser($user)->create(); - - $asset->model->category->update(['checkin_email' => true]); - - event(new CheckoutableCheckedIn( - $asset, - $user, - User::factory()->checkinAssets()->create(), - '' - )); - - Notification::assertSentTo( - [$user], - function (CheckinAssetNotification $notification, $channels) { - return in_array('mail', $channels); - }, - ); - } - - public function testCheckInEmailNotSentToUserIfSettingDisabled() - { - Notification::fake(); - - $user = User::factory()->create(); - $asset = Asset::factory()->assignedToUser($user)->create(); - - $asset->model->category->update(['checkin_email' => false]); - - event(new CheckoutableCheckedIn( - $asset, - $user, - User::factory()->checkinAssets()->create(), - '' - )); - - Notification::assertNotSentTo( - [$user], - function (CheckinAssetNotification $notification, $channels) { - return in_array('mail', $channels); - } - ); - } - - private function createAsset() - { - return Asset::factory()->laptopMbp()->create(); - } -} diff --git a/tests/Feature/Notifications/Email/EmailNotificationsUponCheckinTest.php b/tests/Feature/Notifications/Email/EmailNotificationsUponCheckinTest.php new file mode 100644 index 0000000000..1892e2cdc5 --- /dev/null +++ b/tests/Feature/Notifications/Email/EmailNotificationsUponCheckinTest.php @@ -0,0 +1,64 @@ +create(); + $asset = Asset::factory()->assignedToUser($user)->create(); + + $asset->model->category->update(['checkin_email' => true]); + + event(new CheckoutableCheckedIn( + $asset, + $user, + User::factory()->checkinAssets()->create(), + '' + )); + + Notification::assertSentTo( + [$user], + function (CheckinAssetNotification $notification, $channels) { + return in_array('mail', $channels); + }, + ); + } + + public function testCheckInEmailNotSentToUserIfSettingDisabled() + { + Notification::fake(); + + $user = User::factory()->create(); + $asset = Asset::factory()->assignedToUser($user)->create(); + + $asset->model->category->update(['checkin_email' => false]); + + event(new CheckoutableCheckedIn( + $asset, + $user, + User::factory()->checkinAssets()->create(), + '' + )); + + Notification::assertNotSentTo( + [$user], + function (CheckinAssetNotification $notification, $channels) { + return in_array('mail', $channels); + } + ); + } +} From 02f6aa6161c116576519a562c70023969792dfab Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 14 Mar 2024 11:19:36 -0700 Subject: [PATCH 19/21] Add group tag for new test case --- .../Notifications/Email/EmailNotificationsUponCheckinTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/Feature/Notifications/Email/EmailNotificationsUponCheckinTest.php b/tests/Feature/Notifications/Email/EmailNotificationsUponCheckinTest.php index 1892e2cdc5..35345fac24 100644 --- a/tests/Feature/Notifications/Email/EmailNotificationsUponCheckinTest.php +++ b/tests/Feature/Notifications/Email/EmailNotificationsUponCheckinTest.php @@ -10,6 +10,9 @@ use Illuminate\Support\Facades\Notification; use Tests\Support\InteractsWithSettings; use Tests\TestCase; +/** + * @group notifications + */ class EmailNotificationsUponCheckinTest extends TestCase { use InteractsWithSettings; From dbc79655b00719d5ee178ca5cd9158695945e6d3 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 14 Mar 2024 12:02:56 -0700 Subject: [PATCH 20/21] Cleanup --- .../EmailNotificationsUponCheckinTest.php | 39 ++++++++++--------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/tests/Feature/Notifications/Email/EmailNotificationsUponCheckinTest.php b/tests/Feature/Notifications/Email/EmailNotificationsUponCheckinTest.php index 35345fac24..dbe79c5727 100644 --- a/tests/Feature/Notifications/Email/EmailNotificationsUponCheckinTest.php +++ b/tests/Feature/Notifications/Email/EmailNotificationsUponCheckinTest.php @@ -17,24 +17,24 @@ class EmailNotificationsUponCheckinTest extends TestCase { use InteractsWithSettings; + protected function setUp(): void + { + parent::setUp(); + + Notification::fake(); + } + public function testCheckInEmailSentToUserIfSettingEnabled() { - Notification::fake(); - $user = User::factory()->create(); $asset = Asset::factory()->assignedToUser($user)->create(); $asset->model->category->update(['checkin_email' => true]); - event(new CheckoutableCheckedIn( - $asset, - $user, - User::factory()->checkinAssets()->create(), - '' - )); + $this->fireCheckInEvent($asset, $user); Notification::assertSentTo( - [$user], + $user, function (CheckinAssetNotification $notification, $channels) { return in_array('mail', $channels); }, @@ -43,25 +43,28 @@ class EmailNotificationsUponCheckinTest extends TestCase public function testCheckInEmailNotSentToUserIfSettingDisabled() { - Notification::fake(); - $user = User::factory()->create(); $asset = Asset::factory()->assignedToUser($user)->create(); $asset->model->category->update(['checkin_email' => false]); + $this->fireCheckInEvent($asset, $user); + + Notification::assertNotSentTo( + $user, + function (CheckinAssetNotification $notification, $channels) { + return in_array('mail', $channels); + } + ); + } + + private function fireCheckInEvent($asset, $user): void + { event(new CheckoutableCheckedIn( $asset, $user, User::factory()->checkinAssets()->create(), '' )); - - Notification::assertNotSentTo( - [$user], - function (CheckinAssetNotification $notification, $channels) { - return in_array('mail', $channels); - } - ); } } From 485f11c9455ba6345c785dac691e7d16298baaa1 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 14 Mar 2024 12:56:49 -0700 Subject: [PATCH 21/21] Move trait usage to parent TestCase --- .../Webhooks/SlackNotificationsUponCheckinTest.php | 2 -- .../Webhooks/SlackNotificationsUponCheckoutTest.php | 2 -- tests/TestCase.php | 2 ++ 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php index 5397aae196..b6bb7801a7 100644 --- a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php +++ b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php @@ -14,7 +14,6 @@ use App\Notifications\CheckinAssetNotification; use App\Notifications\CheckinLicenseSeatNotification; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Notification; -use Tests\Support\AssertsAgainstSlackNotifications; use Tests\Support\InteractsWithSettings; use Tests\TestCase; @@ -23,7 +22,6 @@ use Tests\TestCase; */ class SlackNotificationsUponCheckinTest extends TestCase { - use AssertsAgainstSlackNotifications; use InteractsWithSettings; protected function setUp(): void diff --git a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php index fd628d35ed..550f7c5b18 100644 --- a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php +++ b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php @@ -16,7 +16,6 @@ use App\Notifications\CheckoutConsumableNotification; use App\Notifications\CheckoutLicenseSeatNotification; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Notification; -use Tests\Support\AssertsAgainstSlackNotifications; use Tests\Support\InteractsWithSettings; use Tests\TestCase; @@ -25,7 +24,6 @@ use Tests\TestCase; */ class SlackNotificationsUponCheckoutTest extends TestCase { - use AssertsAgainstSlackNotifications; use InteractsWithSettings; protected function setUp(): void diff --git a/tests/TestCase.php b/tests/TestCase.php index 03f273ad6f..535f9a3e21 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -6,12 +6,14 @@ use App\Http\Middleware\SecurityHeaders; use Illuminate\Foundation\Testing\LazilyRefreshDatabase; use Illuminate\Foundation\Testing\TestCase as BaseTestCase; use RuntimeException; +use Tests\Support\AssertsAgainstSlackNotifications; use Tests\Support\CustomTestMacros; use Tests\Support\InteractsWithAuthentication; use Tests\Support\InteractsWithSettings; abstract class TestCase extends BaseTestCase { + use AssertsAgainstSlackNotifications; use CreatesApplication; use CustomTestMacros; use InteractsWithAuthentication;