Skip to content

Commit 9366788

Browse files
author
ankitcodes4u
committed
refactor: remove redundant code and standardize test naming
1 parent 40cc2fe commit 9366788

6 files changed

Lines changed: 99 additions & 168 deletions

File tree

src/CmsPlugin.php

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,13 @@
22

33
namespace Eclipse\Cms;
44

5-
use Eclipse\Cms\Admin\Filament\Resources\PageResource;
6-
use Eclipse\Cms\Admin\Filament\Resources\SectionResource;
75
use Eclipse\Common\Foundation\Plugins\Plugin;
8-
use Filament\Navigation\NavigationGroup;
96
use Filament\Panel;
107

118
class CmsPlugin extends Plugin
129
{
13-
public function getId(): string
14-
{
15-
return 'eclipse-cms';
16-
}
17-
1810
public function register(Panel $panel): void
1911
{
20-
$panel
21-
->resources([
22-
SectionResource::class,
23-
PageResource::class,
24-
])
25-
->navigationGroups([
26-
NavigationGroup::make('CMS')
27-
->label('CMS')
28-
->collapsible(),
29-
]);
30-
}
31-
32-
public function boot(Panel $panel): void
33-
{
34-
//
35-
}
36-
37-
public static function make(): static
38-
{
39-
return app(static::class);
40-
}
41-
42-
public static function get(): static
43-
{
44-
/** @var static $plugin */
45-
$plugin = filament(app(static::class)->getId());
46-
47-
return $plugin;
12+
parent::register($panel);
4813
}
4914
}

tests/Feature/Filament/Resources/PageResourceTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Livewire\Livewire;
88

99
beforeEach(function () {
10-
$this->set_up_super_admin_and_tenant();
10+
$this->setUpSuperAdmin();
1111
});
1212

1313
test('authorized access can view pages list', function () {
@@ -118,21 +118,21 @@
118118
});
119119

120120
test('unauthorized access can be prevented', function () {
121-
$this->set_up_user_without_permissions();
121+
$this->setUpUserWithoutPermissions();
122122

123123
Livewire::test(PageResource\Pages\ListPages::class)
124124
->assertForbidden();
125125
});
126126

127127
test('user with create permission can create pages', function () {
128-
$this->set_up_user_with_permissions(['view_any_page', 'create_page']);
128+
$this->setUpUserWithPermissions(['view_any_page', 'create_page']);
129129

130130
Livewire::test(PageResource\Pages\CreatePage::class)
131131
->assertSuccessful();
132132
});
133133

134134
test('user with update permission can edit pages', function () {
135-
$this->set_up_user_with_permissions(['view_any_page', 'view_page', 'update_page']);
135+
$this->setUpUserWithPermissions(['view_any_page', 'view_page', 'update_page']);
136136
$page = Page::factory()->create();
137137

138138
Livewire::test(PageResource\Pages\EditPage::class, [
@@ -142,7 +142,7 @@
142142
});
143143

144144
test('user with delete permission can delete pages', function () {
145-
$this->set_up_user_with_permissions(['view_any_page', 'view_page', 'delete_page']);
145+
$this->setUpUserWithPermissions(['view_any_page', 'view_page', 'delete_page']);
146146
$page = Page::factory()->create();
147147

148148
$pageExists = Page::where('id', $page->id)->exists();
Lines changed: 86 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,154 +1,118 @@
11
<?php
22

3-
namespace Tests\Feature\Filament\Resources;
4-
53
use Eclipse\Cms\Admin\Filament\Resources\SectionResource;
64
use Eclipse\Cms\Models\Section;
75
use Filament\Actions\DeleteAction;
86
use Livewire\Livewire;
9-
use Tests\TestCase;
10-
11-
class SectionResourceTest extends TestCase
12-
{
13-
public function test_authorized_access_can_view_sections_list(): void
14-
{
15-
$this->migrate()
16-
->set_up_super_admin_and_tenant();
17-
18-
$response = $this->get(SectionResource::getUrl('index'));
19-
20-
$response->assertSuccessful();
21-
}
22-
23-
public function test_create_section_screen_can_be_rendered(): void
24-
{
25-
$this->migrate()
26-
->set_up_super_admin_and_tenant();
27-
28-
$response = $this->get(SectionResource::getUrl('create'));
29-
30-
$response->assertSuccessful();
31-
}
32-
33-
public function test_section_form_validation_works(): void
34-
{
35-
$this->migrate()
36-
->set_up_super_admin_and_tenant();
37-
38-
Livewire::test(SectionResource\Pages\CreateSection::class)
39-
->fillForm([
40-
'name' => '',
41-
'type' => 'pages',
42-
])
43-
->call('create')
44-
->assertHasFormErrors(['name']);
45-
}
46-
47-
public function test_section_can_be_created_through_form(): void
48-
{
49-
$this->migrate()
50-
->set_up_super_admin_and_tenant();
51-
52-
$newData = [
53-
'name.en' => 'Test Section',
54-
'name.sl' => 'Test Sekcija',
55-
'type' => 'pages',
56-
];
577

58-
Livewire::test(SectionResource\Pages\CreateSection::class)
59-
->fillForm($newData)
60-
->call('create')
61-
->assertHasNoFormErrors();
8+
beforeEach(function () {
9+
$this->setUpSuperAdmin();
10+
});
6211

63-
$this->assertDatabaseHas('cms_sections', [
64-
'type' => 'pages',
65-
]);
66-
}
12+
test('authorized access can view sections list', function () {
13+
$response = $this->get(SectionResource::getUrl('index'));
6714

68-
public function test_section_can_be_updated(): void
69-
{
70-
$this->migrate()
71-
->set_up_super_admin_and_tenant();
15+
$response->assertSuccessful();
16+
});
7217

73-
$section = Section::factory()->create();
18+
test('create section screen can be rendered', function () {
19+
$response = $this->get(SectionResource::getUrl('create'));
7420

75-
$newData = [
76-
'name.en' => 'Updated Section',
77-
'name.sl' => 'Posodobljena Sekcija',
78-
'type' => 'pages',
79-
];
21+
$response->assertSuccessful();
22+
});
8023

81-
Livewire::test(SectionResource\Pages\EditSection::class, [
82-
'record' => $section->getRouteKey(),
24+
test('section form validation works', function () {
25+
Livewire::test(SectionResource\Pages\CreateSection::class)
26+
->fillForm([
27+
'name' => '',
28+
'type' => 'pages',
8329
])
84-
->fillForm($newData)
85-
->call('save')
86-
->assertHasNoFormErrors();
30+
->call('create')
31+
->assertHasFormErrors(['name']);
32+
});
8733

88-
$this->assertTrue(true);
89-
}
34+
test('section can be created through form', function () {
35+
$newData = [
36+
'name.en' => 'Test Section',
37+
'name.sl' => 'Test Sekcija',
38+
'type' => 'pages',
39+
];
9040

91-
public function test_section_can_be_deleted(): void
92-
{
93-
$this->migrate()
94-
->set_up_super_admin_and_tenant();
41+
Livewire::test(SectionResource\Pages\CreateSection::class)
42+
->fillForm($newData)
43+
->call('create')
44+
->assertHasNoFormErrors();
9545

96-
$section = Section::factory()->create();
46+
expect(Section::where('type', 'pages')->exists())->toBeTrue();
47+
});
9748

98-
Livewire::test(SectionResource\Pages\EditSection::class, [
99-
'record' => $section->getRouteKey(),
100-
])
101-
->callAction(DeleteAction::class);
49+
test('section can be updated', function () {
50+
$section = Section::factory()->create();
10251

103-
$this->assertSoftDeleted($section);
104-
}
52+
$newData = [
53+
'name.en' => 'Updated Section',
54+
'name.sl' => 'Posodobljena Sekcija',
55+
'type' => 'pages',
56+
];
10557

106-
public function test_unauthorized_access_can_be_prevented(): void
107-
{
108-
$this->migrate()
109-
->set_up_user_without_permissions();
58+
Livewire::test(SectionResource\Pages\EditSection::class, [
59+
'record' => $section->getRouteKey(),
60+
])
61+
->fillForm($newData)
62+
->call('save')
63+
->assertHasNoFormErrors();
11064

111-
$response = $this->get(SectionResource::getUrl('index'));
65+
expect(true)->toBeTrue();
66+
});
11267

113-
$response->assertForbidden();
114-
}
68+
test('section can be deleted', function () {
69+
$section = Section::factory()->create();
11570

116-
public function test_user_with_create_permission_can_create_sections(): void
117-
{
118-
$this->migrate()
119-
->set_up_user_with_permissions(['view_any_section', 'create_section']);
71+
Livewire::test(SectionResource\Pages\EditSection::class, [
72+
'record' => $section->getRouteKey(),
73+
])
74+
->callAction(DeleteAction::class);
12075

121-
$response = $this->get(SectionResource::getUrl('create'));
76+
expect($section->fresh()->trashed())->toBeTrue();
77+
});
12278

123-
$response->assertSuccessful();
124-
}
79+
test('unauthorized access can be prevented', function () {
80+
$this->setUpUserWithoutPermissions();
12581

126-
public function test_user_with_update_permission_can_edit_sections(): void
127-
{
128-
$this->migrate()
129-
->set_up_user_with_permissions(['view_any_section', 'view_section', 'update_section']);
82+
$response = $this->get(SectionResource::getUrl('index'));
13083

131-
$section = Section::factory()->create();
84+
$response->assertForbidden();
85+
});
13286

133-
$response = $this->get(SectionResource::getUrl('edit', [
134-
'record' => $section,
135-
]));
87+
test('user with create permission can create sections', function () {
88+
$this->setUpUserWithPermissions(['view_any_section', 'create_section']);
13689

137-
$response->assertSuccessful();
138-
}
90+
$response = $this->get(SectionResource::getUrl('create'));
13991

140-
public function test_user_with_delete_permission_can_delete_sections(): void
141-
{
142-
$this->migrate()
143-
->set_up_user_with_permissions(['view_any_section', 'view_section', 'update_section', 'delete_section']);
92+
$response->assertSuccessful();
93+
});
14494

145-
$section = Section::factory()->create();
95+
test('user with update permission can edit sections', function () {
96+
$this->setUpUserWithPermissions(['view_any_section', 'view_section', 'update_section']);
14697

147-
Livewire::test(SectionResource\Pages\EditSection::class, [
148-
'record' => $section->getRouteKey(),
149-
])
150-
->callAction('delete');
98+
$section = Section::factory()->create();
99+
100+
$response = $this->get(SectionResource::getUrl('edit', [
101+
'record' => $section,
102+
]));
103+
104+
$response->assertSuccessful();
105+
});
106+
107+
test('user with delete permission can delete sections', function () {
108+
$this->setUpUserWithPermissions(['view_any_section', 'view_section', 'update_section', 'delete_section']);
109+
110+
$section = Section::factory()->create();
111+
112+
Livewire::test(SectionResource\Pages\EditSection::class, [
113+
'record' => $section->getRouteKey(),
114+
])
115+
->callAction('delete');
151116

152-
$this->assertSoftDeleted($section);
153-
}
154-
}
117+
expect($section->fresh()->trashed())->toBeTrue();
118+
});

tests/Feature/Models/PageTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Illuminate\Validation\ValidationException;
77

88
beforeEach(function () {
9-
$this->set_up_super_admin_and_tenant();
9+
$this->setUpSuperAdmin();
1010
});
1111

1212
test('page can be created with valid data', function () {

tests/TestCase.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ protected function migrate(): self
3939
return $this;
4040
}
4141

42-
protected function set_up_super_admin_and_tenant(): self
42+
protected function setUpSuperAdmin(): self
4343
{
4444
$this->migrate();
4545
$this->superAdmin = User::factory()->create();
@@ -61,7 +61,7 @@ protected function set_up_super_admin_and_tenant(): self
6161
return $this;
6262
}
6363

64-
protected function set_up_common_user_and_tenant(): self
64+
protected function setUpCommonUserAndTenant(): self
6565
{
6666
$this->migrate();
6767
$this->user = User::factory()->create();
@@ -70,7 +70,7 @@ protected function set_up_common_user_and_tenant(): self
7070
return $this;
7171
}
7272

73-
protected function set_up_user_without_permissions(): self
73+
protected function setUpUserWithoutPermissions(): self
7474
{
7575
$this->migrate();
7676
$this->user = User::factory()->create();
@@ -79,7 +79,7 @@ protected function set_up_user_without_permissions(): self
7979
return $this;
8080
}
8181

82-
protected function set_up_user_with_permissions(array $permissions): self
82+
protected function setUpUserWithPermissions(array $permissions): self
8383
{
8484
$this->migrate();
8585
$this->user = User::factory()->create();

workbench/app/Providers/AdminPanelProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Workbench\App\Providers;
44

5+
use BezhanSalleh\FilamentShield\FilamentShieldPlugin;
56
use Eclipse\Cms\CmsPlugin;
67
use Filament\Http\Middleware\Authenticate;
78
use Filament\Http\Middleware\DisableBladeIconComponents;
@@ -44,6 +45,7 @@ public function panel(Panel $panel): Panel
4445
Authenticate::class,
4546
])
4647
->plugins([
48+
FilamentShieldPlugin::make(),
4749
CmsPlugin::make(),
4850
SpatieLaravelTranslatablePlugin::make()
4951
->defaultLocales(['en']),

0 commit comments

Comments
 (0)