Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/Filament/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ public static function getRelations(): array
{
return [
RelationManagers\PluginLicensesRelationManager::class,
RelationManagers\ProductLicensesRelationManager::class,
RelationManagers\LicensesRelationManager::class,
RelationManagers\SubscriptionsRelationManager::class,
];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace App\Filament\Resources\UserResource\RelationManagers;

use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Table;

class ProductLicensesRelationManager extends RelationManager
{
protected static string $relationship = 'productLicenses';

protected static ?string $title = 'Products';

public function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Select::make('product_id')
->relationship('product', 'name')
->searchable()
->preload()
->required(),
Forms\Components\Toggle::make('is_comped')
->label('Comped')
->default(true),
Forms\Components\DateTimePicker::make('purchased_at')
->default(now()),
]);
}

public function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('product.name')
->label('Product')
->searchable()
->sortable()
->fontFamily('mono'),
Tables\Columns\TextColumn::make('price_paid')
->label('Price Paid')
->money('usd', divideBy: 100)
->sortable(),
Tables\Columns\IconColumn::make('is_comped')
->label('Comped')
->boolean(),
Tables\Columns\TextColumn::make('purchased_at')
->dateTime()
->sortable(),
])
->defaultSort('purchased_at', 'desc')
->filters([
Tables\Filters\TernaryFilter::make('is_comped')
->label('Comped'),
])
->headerActions([
Tables\Actions\CreateAction::make()
->mutateFormDataUsing(function (array $data): array {
$data['price_paid'] = 0;
$data['currency'] = 'USD';

return $data;
}),
])
->actions([
Tables\Actions\DeleteAction::make(),
]);
}
}
1 change: 1 addition & 0 deletions app/Models/ProductLicense.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ protected function casts(): array
{
return [
'price_paid' => 'integer',
'is_comped' => 'boolean',
'purchased_at' => 'datetime',
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function up(): void
NULL AS bundle_name,
pdl.price_paid,
pdl.currency,
0 AS is_comped,
pdl.is_comped,
pdl.purchased_at,
pdl.created_at,
pdl.updated_at
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('product_licenses', function (Blueprint $table) {
$table->boolean('is_comped')->default(false)->after('currency')->index();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('product_licenses', function (Blueprint $table) {
$table->dropIndex(['is_comped']);
$table->dropColumn('is_comped');
});
}
};
147 changes: 147 additions & 0 deletions tests/Feature/Filament/ProductLicensesRelationManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php

namespace Tests\Feature\Filament;

use App\Filament\Resources\UserResource\Pages\EditUser;
use App\Filament\Resources\UserResource\RelationManagers\ProductLicensesRelationManager;
use App\Models\Product;
use App\Models\ProductLicense;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;

class ProductLicensesRelationManagerTest extends TestCase
{
use RefreshDatabase;

private User $admin;

private User $user;

protected function setUp(): void
{
parent::setUp();

$this->admin = User::factory()->create(['email' => 'admin@test.com']);
config(['filament.users' => ['admin@test.com']]);

$this->user = User::factory()->create();
}

public function test_it_lists_product_licenses_for_user(): void
{
$licenses = ProductLicense::factory()->count(3)->create([
'user_id' => $this->user->id,
]);

Livewire::actingAs($this->admin)
->test(ProductLicensesRelationManager::class, [
'ownerRecord' => $this->user,
'pageClass' => EditUser::class,
])
->assertCanSeeTableRecords($licenses)
->assertCountTableRecords(3);
}

public function test_it_does_not_show_other_users_licenses(): void
{
$otherUser = User::factory()->create();

ProductLicense::factory()->create([
'user_id' => $this->user->id,
]);

ProductLicense::factory()->create([
'user_id' => $otherUser->id,
]);

Livewire::actingAs($this->admin)
->test(ProductLicensesRelationManager::class, [
'ownerRecord' => $this->user,
'pageClass' => EditUser::class,
])
->assertCountTableRecords(1);
}

public function test_it_shows_comped_status(): void
{
ProductLicense::factory()->create([
'user_id' => $this->user->id,
'is_comped' => true,
'price_paid' => 0,
]);

Livewire::actingAs($this->admin)
->test(ProductLicensesRelationManager::class, [
'ownerRecord' => $this->user,
'pageClass' => EditUser::class,
])
->assertCountTableRecords(1);
}

public function test_it_can_create_a_comped_product_license(): void
{
$product = Product::factory()->create();

Livewire::actingAs($this->admin)
->test(ProductLicensesRelationManager::class, [
'ownerRecord' => $this->user,
'pageClass' => EditUser::class,
])
->callTableAction('create', data: [
'product_id' => $product->id,
'is_comped' => true,
'purchased_at' => now()->toDateTimeString(),
])
->assertHasNoTableActionErrors();

$this->assertDatabaseHas('product_licenses', [
'user_id' => $this->user->id,
'product_id' => $product->id,
'is_comped' => true,
'price_paid' => 0,
'currency' => 'USD',
]);
}

public function test_it_can_delete_a_product_license(): void
{
$license = ProductLicense::factory()->create([
'user_id' => $this->user->id,
]);

Livewire::actingAs($this->admin)
->test(ProductLicensesRelationManager::class, [
'ownerRecord' => $this->user,
'pageClass' => EditUser::class,
])
->callTableAction('delete', $license)
->assertHasNoTableActionErrors();

$this->assertDatabaseMissing('product_licenses', [
'id' => $license->id,
]);
}

public function test_it_can_filter_by_comped_status(): void
{
ProductLicense::factory()->create([
'user_id' => $this->user->id,
'is_comped' => true,
]);

ProductLicense::factory()->create([
'user_id' => $this->user->id,
'is_comped' => false,
]);

Livewire::actingAs($this->admin)
->test(ProductLicensesRelationManager::class, [
'ownerRecord' => $this->user,
'pageClass' => EditUser::class,
])
->filterTable('is_comped', true)
->assertCountTableRecords(1);
}
}
Loading