Skip to content

Commit 09d02fc

Browse files
committed
test: add CountryResourceTest
1 parent e0b9b3e commit 09d02fc

4 files changed

Lines changed: 230 additions & 11 deletions

File tree

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<?php
2+
3+
use Eclipse\World\Filament\Clusters\World\Resources\CountryResource;
4+
use Eclipse\World\Filament\Clusters\World\Resources\CountryResource\Pages\ListCountries;
5+
use Eclipse\World\Models\Country;
6+
7+
use function Pest\Livewire\livewire;
8+
9+
beforeEach(function () {
10+
$this->setUpSuperAdmin();
11+
});
12+
13+
test('unauthorized access can be prevented', function () {
14+
// Create regular user with no permissions
15+
$this->setUpCommonUser();
16+
17+
// Create test country
18+
$country = Country::factory()->create();
19+
20+
// View table
21+
$this->get(CountryResource::getUrl())
22+
->assertForbidden();
23+
24+
// Add direct permission to view the table, since otherwise any other action below is not available even for testing
25+
$this->user->givePermissionTo('view_any_country');
26+
27+
// Create country
28+
livewire(ListCountries::class)
29+
->assertActionDisabled('create');
30+
31+
// Edit country
32+
livewire(ListCountries::class)
33+
->assertCanSeeTableRecords([$country])
34+
->assertTableActionDisabled('edit', $country);
35+
36+
// Delete country
37+
livewire(ListCountries::class)
38+
->assertTableActionDisabled('delete', $country)
39+
->assertTableBulkActionDisabled('delete');
40+
41+
// Restore and force delete
42+
$country->delete();
43+
$this->assertSoftDeleted($country);
44+
45+
livewire(ListCountries::class)
46+
->assertTableActionDisabled('restore', $country)
47+
->assertTableBulkActionDisabled('restore')
48+
->assertTableActionDisabled('forceDelete', $country)
49+
->assertTableBulkActionDisabled('forceDelete');
50+
});
51+
52+
test('countries table can be displayed', function () {
53+
$this->get(CountryResource::getUrl())
54+
->assertSuccessful();
55+
});
56+
57+
test('form validation works', function () {
58+
$component = livewire(ListCountries::class);
59+
60+
// Test required fields
61+
$component->callAction('create')
62+
->assertHasActionErrors([
63+
'id' => 'required',
64+
'a3_id' => 'required',
65+
'name' => 'required',
66+
]);
67+
68+
// Test with valid data
69+
$component->callAction('create', Country::factory()->definition())
70+
->assertHasNoActionErrors();
71+
});
72+
73+
test('new country can be created', function () {
74+
$data = Country::factory()->definition();
75+
76+
livewire(ListCountries::class)
77+
->callAction('create', $data)
78+
->assertHasNoActionErrors();
79+
80+
$country = Country::where('id', $data['id'])->first();
81+
expect($country)->toBeObject();
82+
83+
foreach ($data as $key => $val) {
84+
expect($country->$key)->toEqual($val);
85+
}
86+
});
87+
88+
test('existing country can be updated', function () {
89+
$country = Country::factory()->create([
90+
'id' => 'SI',
91+
'a3_id' => 'SVN',
92+
'num_code' => 705,
93+
'name' => 'Slovenia',
94+
'flag' => '🇸🇮',
95+
]);
96+
97+
$data = \Illuminate\Support\Arr::except(Country::factory()->definition(), ['id']);
98+
99+
livewire(ListCountries::class)
100+
->callTableAction('edit', $country, $data)
101+
->assertHasNoTableActionErrors();
102+
103+
$country->refresh();
104+
105+
foreach ($data as $key => $val) {
106+
expect($country->$key)->toEqual($val);
107+
}
108+
});
109+
110+
test('country can be deleted', function () {
111+
$country = Country::factory()->create();
112+
113+
livewire(ListCountries::class)
114+
->callTableAction('delete', $country)
115+
->assertHasNoTableActionErrors();
116+
117+
$this->assertSoftDeleted($country);
118+
});
119+
120+
test('country can be restored', function () {
121+
$country = Country::factory()->create();
122+
$country->delete();
123+
124+
$this->assertSoftDeleted($country);
125+
126+
livewire(ListCountries::class)
127+
->filterTable('trashed')
128+
->assertTableActionExists('restore')
129+
->assertTableActionEnabled('restore', $country)
130+
->assertTableActionVisible('restore', $country)
131+
->callTableAction('restore', $country)
132+
->assertHasNoTableActionErrors();
133+
134+
$this->assertNotSoftDeleted($country);
135+
});
136+
137+
test('country can be force deleted', function () {
138+
$country = Country::factory()->create();
139+
140+
$country->delete();
141+
$this->assertSoftDeleted($country);
142+
143+
livewire(ListCountries::class)
144+
->filterTable('trashed')
145+
->assertTableActionExists('forceDelete')
146+
->assertTableActionEnabled('forceDelete', $country)
147+
->assertTableActionVisible('forceDelete', $country)
148+
->callTableAction('forceDelete', $country)
149+
->assertHasNoTableActionErrors();
150+
151+
$this->assertModelMissing($country);
152+
});

tests/Feature/ExampleTest.php

Lines changed: 0 additions & 5 deletions
This file was deleted.

tests/Pest.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use Illuminate\Support\Facades\Artisan;
34
use Tests\TestCase;
45

56
/*
@@ -13,7 +14,18 @@
1314
|
1415
*/
1516

16-
uses(TestCase::class)->in(__DIR__);
17+
uses(TestCase::class)
18+
->use(Illuminate\Foundation\Testing\RefreshDatabase::class)
19+
->beforeEach(function () {
20+
// Seed roles and permissions with Filament Shield plugin
21+
Artisan::call('shield:generate', [
22+
'--all' => null,
23+
'--panel' => 'admin',
24+
'--option' => 'permissions',
25+
'--minimal' => null,
26+
]);
27+
})
28+
->in(__DIR__);
1729

1830
/*
1931
|--------------------------------------------------------------------------
@@ -26,9 +38,9 @@
2638
|
2739
*/
2840

29-
expect()->extend('toBeOne', function () {
30-
return $this->toBe(1);
31-
});
41+
// expect()->extend('toBeOne', function () {
42+
// return $this->toBe(1);
43+
// });
3244

3345
/*
3446
|--------------------------------------------------------------------------

tests/TestCase.php

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,69 @@
22

33
namespace Tests;
44

5-
use PHPUnit\Framework\TestCase as BaseTestCase;
5+
use Orchestra\Testbench\Concerns\WithWorkbench;
6+
use Orchestra\Testbench\TestCase as BaseTestCase;
7+
use Workbench\App\Models\User;
68

79
abstract class TestCase extends BaseTestCase
810
{
9-
//
11+
use WithWorkbench;
12+
13+
protected ?User $superAdmin = null;
14+
15+
protected ?User $user = null;
16+
17+
protected function setUp(): void
18+
{
19+
// Always show errors when testing
20+
ini_set('display_errors', 1);
21+
error_reporting(E_ALL);
22+
23+
parent::setUp();
24+
25+
$this->withoutVite();
26+
}
27+
28+
/**
29+
* Run database migrations
30+
*/
31+
protected function migrate(): self
32+
{
33+
$this->artisan('migrate');
34+
35+
return $this;
36+
}
37+
38+
/**
39+
* Set up default "super admin" user
40+
*/
41+
protected function setUpSuperAdmin(): self
42+
{
43+
$this->superAdmin = User::factory()->make();
44+
$this->superAdmin->assignRole('super_admin')->save();
45+
46+
$this->actingAs($this->superAdmin);
47+
48+
return $this;
49+
}
50+
51+
/**
52+
* Set up a common user with no roles or permissions
53+
*/
54+
protected function setUpCommonUser(): self
55+
{
56+
$this->user = User::factory()->create();
57+
58+
$this->actingAs($this->user);
59+
60+
return $this;
61+
}
62+
63+
public function ignorePackageDiscoveriesFrom()
64+
{
65+
return [
66+
// A list of packages that should not be auto-discovered when running tests
67+
'laravel/telescope',
68+
];
69+
}
1070
}

0 commit comments

Comments
 (0)