Implement package limit notification system
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Events\Packages\TenantPackageEventLimitReached;
|
||||
use App\Events\Packages\TenantPackageEventThresholdReached;
|
||||
use App\Models\Event;
|
||||
use App\Models\Package;
|
||||
use App\Models\PackagePurchase;
|
||||
@@ -9,13 +11,15 @@ use App\Models\Photo;
|
||||
use App\Models\Tenant;
|
||||
use App\Models\TenantPackage;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use Illuminate\Support\Facades\Event as EventFacade;
|
||||
use Tests\TestCase;
|
||||
|
||||
class TenantModelTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function testTenantHasManyEvents(): void
|
||||
public function test_tenant_has_many_events(): void
|
||||
{
|
||||
$tenant = Tenant::factory()->create();
|
||||
Event::factory()->count(3)->create(['tenant_id' => $tenant->id]);
|
||||
@@ -23,7 +27,7 @@ class TenantModelTest extends TestCase
|
||||
$this->assertCount(3, $tenant->events()->get());
|
||||
}
|
||||
|
||||
public function testTenantHasPhotosThroughEvents(): void
|
||||
public function test_tenant_has_photos_through_events(): void
|
||||
{
|
||||
$tenant = Tenant::factory()->create();
|
||||
$event = Event::factory()->create(['tenant_id' => $tenant->id]);
|
||||
@@ -32,7 +36,7 @@ class TenantModelTest extends TestCase
|
||||
$this->assertCount(2, $tenant->photos()->get());
|
||||
}
|
||||
|
||||
public function testTenantHasManyPackagePurchases(): void
|
||||
public function test_tenant_has_many_package_purchases(): void
|
||||
{
|
||||
$tenant = Tenant::factory()->create();
|
||||
$package = Package::factory()->create();
|
||||
@@ -44,7 +48,7 @@ class TenantModelTest extends TestCase
|
||||
$this->assertCount(2, $tenant->purchases()->get());
|
||||
}
|
||||
|
||||
public function testActiveSubscriptionAccessorReturnsTrueWhenActivePackageExists(): void
|
||||
public function test_active_subscription_accessor_returns_true_when_active_package_exists(): void
|
||||
{
|
||||
$tenant = Tenant::factory()->create();
|
||||
$package = Package::factory()->create(['type' => 'reseller']);
|
||||
@@ -58,21 +62,21 @@ class TenantModelTest extends TestCase
|
||||
$this->assertTrue($tenant->fresh()->active_subscription);
|
||||
}
|
||||
|
||||
public function testActiveSubscriptionAccessorReturnsFalseWithoutActivePackage(): void
|
||||
public function test_active_subscription_accessor_returns_false_without_active_package(): void
|
||||
{
|
||||
$tenant = Tenant::factory()->create();
|
||||
|
||||
$this->assertFalse($tenant->fresh()->active_subscription);
|
||||
}
|
||||
|
||||
public function testIncrementUsedEventsReturnsFalseWithoutActivePackage(): void
|
||||
public function test_increment_used_events_returns_false_without_active_package(): void
|
||||
{
|
||||
$tenant = Tenant::factory()->create();
|
||||
|
||||
$this->assertFalse($tenant->incrementUsedEvents());
|
||||
}
|
||||
|
||||
public function testIncrementUsedEventsUpdatesActivePackage(): void
|
||||
public function test_increment_used_events_updates_active_package(): void
|
||||
{
|
||||
$tenant = Tenant::factory()->create();
|
||||
$package = Package::factory()->create(['type' => 'reseller']);
|
||||
@@ -87,7 +91,41 @@ class TenantModelTest extends TestCase
|
||||
$this->assertEquals(3, $tenantPackage->fresh()->used_events);
|
||||
}
|
||||
|
||||
public function testSettingsCastToArray(): void
|
||||
public function test_consume_event_allowance_dispatches_notifications_and_updates_usage(): void
|
||||
{
|
||||
EventFacade::fake([
|
||||
TenantPackageEventThresholdReached::class,
|
||||
TenantPackageEventLimitReached::class,
|
||||
]);
|
||||
|
||||
Config::set('package-limits.event_thresholds', [0.5]);
|
||||
|
||||
$tenant = Tenant::factory()->create();
|
||||
$package = Package::factory()->create([
|
||||
'type' => 'reseller',
|
||||
'max_events_per_year' => 4,
|
||||
]);
|
||||
|
||||
$tenantPackage = TenantPackage::factory()->create([
|
||||
'tenant_id' => $tenant->id,
|
||||
'package_id' => $package->id,
|
||||
'active' => true,
|
||||
'used_events' => 1,
|
||||
]);
|
||||
|
||||
$this->assertTrue($tenant->consumeEventAllowance());
|
||||
|
||||
EventFacade::assertDispatched(TenantPackageEventThresholdReached::class);
|
||||
EventFacade::assertNotDispatched(TenantPackageEventLimitReached::class);
|
||||
|
||||
$tenantPackage->refresh();
|
||||
|
||||
$this->assertSame(2, $tenantPackage->used_events);
|
||||
$this->assertNotNull($tenantPackage->event_warning_sent_at);
|
||||
$this->assertSame(0.5, (float) $tenantPackage->event_warning_threshold);
|
||||
}
|
||||
|
||||
public function test_settings_cast_to_array(): void
|
||||
{
|
||||
$tenant = Tenant::factory()->create([
|
||||
'settings' => ['theme' => 'dark', 'logo' => 'logo.png'],
|
||||
@@ -97,7 +135,7 @@ class TenantModelTest extends TestCase
|
||||
$this->assertSame('dark', $tenant->settings['theme']);
|
||||
}
|
||||
|
||||
public function testFeaturesCastToArray(): void
|
||||
public function test_features_cast_to_array(): void
|
||||
{
|
||||
$tenant = Tenant::factory()->create([
|
||||
'features' => ['photo_likes' => true, 'analytics' => false],
|
||||
@@ -107,4 +145,23 @@ class TenantModelTest extends TestCase
|
||||
$this->assertTrue($tenant->features['photo_likes']);
|
||||
$this->assertFalse($tenant->features['analytics']);
|
||||
}
|
||||
|
||||
public function test_increment_credits_clears_warning_when_balance_above_threshold(): void
|
||||
{
|
||||
Config::set('package-limits.credit_thresholds', [5, 1]);
|
||||
|
||||
$tenant = Tenant::factory()->create([
|
||||
'event_credits_balance' => 1,
|
||||
'credit_warning_sent_at' => now()->subDay(),
|
||||
'credit_warning_threshold' => 1,
|
||||
]);
|
||||
|
||||
$tenant->incrementCredits(10);
|
||||
|
||||
$tenant->refresh();
|
||||
|
||||
$this->assertNull($tenant->credit_warning_sent_at);
|
||||
$this->assertNull($tenant->credit_warning_threshold);
|
||||
$this->assertSame(11, (int) $tenant->event_credits_balance);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user