Skip to content

Commit

Permalink
feat:[LAR-70] Add discussions table in admin cpanel and add feature t… (
Browse files Browse the repository at this point in the history
#163)

Co-authored-by: Chri$ <[email protected]>
  • Loading branch information
cybersoldattech and Chri$ authored Oct 22, 2024
1 parent 0adffa9 commit 70bb600
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
71 changes: 71 additions & 0 deletions app/Filament/Resources/DiscussionResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace App\Filament\Resources;

use App\Filament\Resources\DiscussionResource\Pages;
use App\Models\Discussion;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;

final class DiscussionResource extends Resource
{
protected static ?string $model = Discussion::class;

protected static ?string $navigationIcon = 'heroicon-o-chat-bubble-bottom-center-text';

public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('title')
->label('Titre')
->sortable(),
TextColumn::make('locked')
->label('Vérrouillé')
->getStateUsing(fn ($record) => ($record->locked) ? 'Oui' : 'Non')
->colors([
'success' => 'Oui',
'danger' => 'Non',
])
->badge(),
TextColumn::make('is_pinned')
->label('Epinglé')
->getStateUsing(fn ($record) => ($record->is_pinned) ? 'Oui' : 'Non')
->colors([
'success' => 'Oui',
'danger' => 'Non',
])
->badge(),
TextColumn::make('created_at')
->label('Date de création')
->dateTime(),
TextColumn::make('user.name')
->label('Auteur'),
])
->filters([
Filter::make('is_pinned')->query(fn (Builder $query) => $query->where('is_pinned', true))->label('Epinglé'),
Filter::make('is_locked')->query(fn (Builder $query) => $query->where('locked', true))->label('Vérrouillé'),
])
->actions([
Tables\Actions\DeleteAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}

public static function getPages(): array
{
return [
'index' => Pages\ListDiscussions::route('/'),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace App\Filament\Resources\DiscussionResource\Pages;

use App\Filament\Resources\DiscussionResource;
use Filament\Resources\Pages\ListRecords;

final class ListDiscussions extends ListRecords
{
protected static string $resource = DiscussionResource::class;
}
42 changes: 42 additions & 0 deletions tests/Feature/Filament/DiscussionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

use App\Filament\Resources\DiscussionResource;
use App\Filament\Resources\DiscussionResource\Pages\ListDiscussions;
use App\Models\Discussion;
use Illuminate\Database\Eloquent\Factories\Sequence;
use Livewire\Livewire;

beforeEach(function (): void {
$this->user = $this->login();
$this->discussions = Discussion::factory()
->count(10)
->state(new Sequence(
['is_pinned' => true, 'locked' => false],
['is_pinned' => false, 'locked' => true],
))
->create();
});

describe(DiscussionResource::class, function (): void {

it('page can display table with records', function (): void {
Livewire::test(ListDiscussions::class)
->assertCanSeeTableRecords($this->discussions);
});

it('admin user can filter discussion by `is_pinned`', function (): void {
Livewire::test(ListDiscussions::class)
->assertCanSeeTableRecords($this->discussions)
->filterTable('is_pinned')
->assertCountTableRecords(5);
});

it('admin user can filter discussion by `locked`', function (): void {
Livewire::test(ListDiscussions::class)
->assertCanSeeTableRecords($this->discussions)
->filterTable('is_locked')
->assertCountTableRecords(5);
});
})->group('discussions');

0 comments on commit 70bb600

Please sign in to comment.