feat: add TextWidget
add TextWidget CRUD api add basic tests for API
This commit is contained in:
parent
13cfd2961f
commit
c66645cbdd
18 changed files with 536 additions and 1 deletions
92
tests/Unit/TextWidgetApiTest.php
Normal file
92
tests/Unit/TextWidgetApiTest.php
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
<?php
|
||||
|
||||
use App\Models\TextWidget;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
test('can list text widgets', function () {
|
||||
$user = User::factory()->create();
|
||||
TextWidget::factory()->count(3)->create();
|
||||
|
||||
$response = $this->actingAs($user)->getJson('/api/text-widgets');
|
||||
|
||||
$response->assertOk()
|
||||
->assertJsonCount(3);
|
||||
});
|
||||
|
||||
test('can create a text widget', function () {
|
||||
$admin = User::factory()->create(['email' => config('app.admin_email')]);
|
||||
|
||||
$this->actingAs($admin);
|
||||
|
||||
$data = [
|
||||
'title' => 'Sample Title',
|
||||
'content' => 'Sample Content',
|
||||
];
|
||||
|
||||
$response = $this->postJson('/api/text-widgets', $data);
|
||||
|
||||
$response->assertCreated()
|
||||
->assertJsonFragment($data);
|
||||
});
|
||||
|
||||
test('can show a text widget', function () {
|
||||
$user = User::factory()->create();
|
||||
$textWidget = TextWidget::factory()->create();
|
||||
|
||||
$response = $this->actingAs($user)->getJson("/api/text-widgets/{$textWidget->id}");
|
||||
|
||||
$response->assertOk()
|
||||
->assertJsonFragment(['id' => $textWidget->id]);
|
||||
});
|
||||
|
||||
test('can update a text widget', function () {
|
||||
$user = User::factory()->create();
|
||||
$textWidget = TextWidget::factory()->create();
|
||||
$data = ['title' => 'Updated Title'];
|
||||
|
||||
$response = $this->actingAs($user)->putJson("/api/text-widgets/{$textWidget->id}", $data);
|
||||
|
||||
$response->assertOk()
|
||||
->assertJsonFragment($data);
|
||||
});
|
||||
|
||||
test('can delete a text widget', function () {
|
||||
$user = User::factory()->create();
|
||||
$textWidget = TextWidget::factory()->create();
|
||||
|
||||
$response = $this->actingAs($user)->deleteJson("/api/text-widgets/{$textWidget->id}");
|
||||
|
||||
$response->assertNoContent();
|
||||
|
||||
$this->assertDatabaseMissing('text_widgets', ['id' => $textWidget->id]);
|
||||
});
|
||||
|
||||
test('only admin can create text widgets', function () {
|
||||
$adminEmail = Config::get('app.admin_email');
|
||||
$user = User::factory()->create(['email' => $adminEmail]);
|
||||
|
||||
$this->actingAs($user)
|
||||
->postJson('/api/text-widgets', ['title' => 'Test', 'content' => 'Test content'])
|
||||
->assertCreated();
|
||||
|
||||
$nonAdmin = User::factory()->create(['email' => 'nonadmin@example.com']);
|
||||
|
||||
$this->actingAs($nonAdmin)
|
||||
->postJson('/api/text-widgets', ['title' => 'Test', 'content' => 'Test content'])
|
||||
->assertForbidden();
|
||||
});
|
||||
|
||||
test('authenticated users can read text widgets', function () {
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->actingAs($user)
|
||||
->getJson('/api/text-widgets')
|
||||
->assertOk();
|
||||
|
||||
$this->getJson('/api/text-widgets')
|
||||
->assertOk();
|
||||
});
|
||||
15
tests/Unit/TextWidgetModelTest.php
Normal file
15
tests/Unit/TextWidgetModelTest.php
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
use App\Models\TextWidget;
|
||||
|
||||
it('has correct fillable attributes', function () {
|
||||
$expected = [
|
||||
'title',
|
||||
'description',
|
||||
'content',
|
||||
];
|
||||
|
||||
$entry = new TextWidget;
|
||||
|
||||
expect($entry->getFillable())->toEqual($expected);
|
||||
});
|
||||
47
tests/Unit/TextWidgetResourceTest.php
Normal file
47
tests/Unit/TextWidgetResourceTest.php
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
<?php
|
||||
|
||||
use App\Filament\Resources\Entries\EntryResource;
|
||||
use App\Filament\Resources\TextWidgets\TextWidgetResource;
|
||||
use App\Models\TextWidget;
|
||||
use ReflectionClass;
|
||||
|
||||
it('references the correct model and record title attribute', function () {
|
||||
$defaults = (new ReflectionClass(TextWidgetResource::class))->getDefaultProperties();
|
||||
|
||||
expect($defaults['model'] ?? null)->toBe(TextWidget::class);
|
||||
expect($defaults['recordTitleAttribute'] ?? null)->toBe('title');
|
||||
});
|
||||
|
||||
it('defines the expected pages', function () {
|
||||
$pages = EntryResource::getPages();
|
||||
|
||||
expect(array_keys($pages))->toEqual(['index', 'create', 'view', 'edit']);
|
||||
});
|
||||
|
||||
it('accepts new record when entered via the form schema', function () {
|
||||
|
||||
$data = [
|
||||
'title' => 'Test Entry',
|
||||
'description' => 'This is a test entry.',
|
||||
'content' => '<p>This is the content of the test entry.</p>',
|
||||
];
|
||||
|
||||
$entry = new TextWidget;
|
||||
$entry->fill($data);
|
||||
|
||||
expect($entry->title)->toBe('Test Entry');
|
||||
});
|
||||
|
||||
it('deletes a record correctly', function () {
|
||||
$entry = TextWidget::create([
|
||||
'title' => 'Test Entry to Delete',
|
||||
'description' => 'This is a test entry.',
|
||||
'content' => '<p>This is the content of the test entry.</p>',
|
||||
]);
|
||||
|
||||
$entryId = $entry->id;
|
||||
$entry->delete();
|
||||
|
||||
$deletedEntry = TextWidget::find($entryId);
|
||||
expect($deletedEntry)->toBeNull();
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue