132 lines
3.6 KiB
PHP
132 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit;
|
|
|
|
use App\Models\Event;
|
|
use App\Models\Photo;
|
|
use App\Models\PurchaseHistory;
|
|
use App\Models\Tenant;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\TestCase;
|
|
|
|
class TenantModelTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
/** @test */
|
|
public function tenant_has_many_events()
|
|
{
|
|
$tenant = Tenant::factory()->create();
|
|
Event::factory(3)->create(['tenant_id' => $tenant->id]);
|
|
|
|
$this->assertCount(3, $tenant->events);
|
|
}
|
|
|
|
/** @test */
|
|
public function tenant_has_photos_through_events()
|
|
{
|
|
$tenant = Tenant::factory()->create();
|
|
$event = Event::factory()->create(['tenant_id' => $tenant->id]);
|
|
Photo::factory(2)->create(['event_id' => $event->id]);
|
|
|
|
$this->assertCount(2, $tenant->photos);
|
|
}
|
|
|
|
/** @test */
|
|
public function tenant_has_many_purchases()
|
|
{
|
|
$tenant = Tenant::factory()->create();
|
|
PurchaseHistory::factory(2)->create(['tenant_id' => $tenant->id]);
|
|
|
|
$this->assertCount(2, $tenant->purchases);
|
|
}
|
|
|
|
/** @test */
|
|
public function active_subscription_returns_true_if_not_expired()
|
|
{
|
|
$tenant = Tenant::factory()->create([
|
|
'subscription_tier' => 'pro',
|
|
'subscription_expires_at' => now()->addDays(30),
|
|
]);
|
|
|
|
$this->assertTrue($tenant->active_subscription);
|
|
}
|
|
|
|
/** @test */
|
|
public function active_subscription_returns_false_if_expired()
|
|
{
|
|
$tenant = Tenant::factory()->create([
|
|
'subscription_tier' => 'pro',
|
|
'subscription_expires_at' => now()->subDays(1),
|
|
]);
|
|
|
|
$this->assertFalse($tenant->active_subscription);
|
|
}
|
|
|
|
/** @test */
|
|
public function active_subscription_returns_false_if_no_subscription()
|
|
{
|
|
$tenant = Tenant::factory()->create([
|
|
'subscription_tier' => 'free',
|
|
'subscription_expires_at' => null,
|
|
]);
|
|
|
|
$this->assertFalse($tenant->active_subscription);
|
|
}
|
|
|
|
/** @test */
|
|
public function can_decrement_credits()
|
|
{
|
|
$tenant = Tenant::factory()->create(['event_credits_balance' => 10]);
|
|
|
|
$result = $tenant->decrementCredits(3);
|
|
|
|
$this->assertTrue($result);
|
|
$this->assertEquals(7, $tenant->fresh()->event_credits_balance);
|
|
}
|
|
|
|
/** @test */
|
|
public function can_increment_credits()
|
|
{
|
|
$tenant = Tenant::factory()->create(['event_credits_balance' => 10]);
|
|
|
|
$result = $tenant->incrementCredits(5);
|
|
|
|
$this->assertTrue($result);
|
|
$this->assertEquals(15, $tenant->fresh()->event_credits_balance);
|
|
}
|
|
|
|
/** @test */
|
|
public function decrementing_credits_does_not_go_negative()
|
|
{
|
|
$tenant = Tenant::factory()->create(['event_credits_balance' => 2]);
|
|
|
|
$result = $tenant->decrementCredits(5);
|
|
|
|
$this->assertFalse($result);
|
|
$this->assertEquals(2, $tenant->fresh()->event_credits_balance);
|
|
}
|
|
|
|
/** @test */
|
|
public function settings_are_properly_cast_as_json()
|
|
{
|
|
$tenant = Tenant::factory()->create([
|
|
'settings' => json_encode(['theme' => 'dark', 'logo' => 'logo.png'])
|
|
]);
|
|
|
|
$this->assertIsArray($tenant->settings);
|
|
$this->assertEquals('dark', $tenant->settings['theme']);
|
|
}
|
|
|
|
/** @test */
|
|
public function features_are_cast_as_array()
|
|
{
|
|
$tenant = Tenant::factory()->create([
|
|
'features' => ['photo_likes' => true, 'analytics' => false]
|
|
]);
|
|
|
|
$this->assertIsArray($tenant->features);
|
|
$this->assertTrue($tenant->features['photo_likes']);
|
|
$this->assertFalse($tenant->features['analytics']);
|
|
}
|
|
} |