Skip to content

Commit 0817655

Browse files
committed
Add tests
1 parent 360bc06 commit 0817655

File tree

4 files changed

+123
-7
lines changed

4 files changed

+123
-7
lines changed

phpstan-baseline.neon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ parameters:
1515
count: 1
1616
path: src/FilamentDevelopersLogin.php
1717

18+
-
19+
message: "#^Call to an undefined method Livewire\\\\Features\\\\SupportTesting\\\\Testable\\:\\:test\\(\\)\\.$#"
20+
count: 3
21+
path: tests/Feature/MenuLoginsTest.php
22+
1823
-
1924
message: "#^Access to an undefined property DutchCodingCompany\\\\FilamentDeveloperLogins\\\\Tests\\\\Fixtures\\\\TestUser\\:\\:\\$is_admin\\.$#"
2025
count: 1

src/FilamentDeveloperLoginsServiceProvider.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,21 @@ protected static function registerRenderHooks(): void
5252
/** @var FilamentDeveloperLoginsPlugin $plugin */
5353
$plugin = $panel->getPlugin('filament-developer-logins');
5454

55-
// Check if the plugin is enabled.
56-
if (! $plugin->getEnabled()) {
57-
return;
58-
}
59-
6055
FilamentView::registerRenderHook(
6156
PanelsRenderHook::AUTH_LOGIN_FORM_AFTER,
62-
static fn (): string => Blade::render('<x-filament-developer-logins::developer-logins />'),
57+
static function () use ($plugin) : ?string {
58+
if (! $plugin->getEnabled()) {
59+
return null;
60+
}
61+
62+
return Blade::render('<x-filament-developer-logins::developer-logins />');
63+
},
6364
);
6465

6566
FilamentView::registerRenderHook(
6667
PanelsRenderHook::GLOBAL_SEARCH_AFTER,
6768
static function () use ($plugin) : ?string {
68-
if (! $plugin->getSwitchable()) {
69+
if (! $plugin->getEnabled() || ! $plugin->getSwitchable()) {
6970
return null;
7071
}
7172

tests/Feature/MenuLoginsTest.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace DutchCodingCompany\FilamentDeveloperLogins\Tests\Feature;
4+
5+
use DutchCodingCompany\FilamentDeveloperLogins\FilamentDeveloperLoginsPlugin;
6+
use DutchCodingCompany\FilamentDeveloperLogins\Livewire\MenuLogins;
7+
use DutchCodingCompany\FilamentDeveloperLogins\Tests\Fixtures\TestUser;
8+
use DutchCodingCompany\FilamentDeveloperLogins\Tests\TestCase;
9+
use Filament\Facades\Filament;
10+
use Filament\Pages\Dashboard;
11+
use Livewire\Livewire;
12+
13+
final class MenuLoginsTest extends TestCase
14+
{
15+
public function test_component_is_rendered(): void
16+
{
17+
$user = TestUser::factory()->create();
18+
19+
$this->actingAs($user)
20+
->get(Dashboard::getUrl())
21+
->assertSeeLivewire(MenuLogins::class);
22+
}
23+
24+
public function test_component_is_not_rendered_when_switchable_is_false(): void
25+
{
26+
FilamentDeveloperLoginsPlugin::current()
27+
->switchable(false);
28+
29+
$user = TestUser::factory()->create();
30+
31+
$this->actingAs($user)
32+
->get(Dashboard::getUrl())
33+
->assertDontSeeLivewire(MenuLogins::class);
34+
}
35+
36+
public function test_component_is_not_rendered_when_plugin_is_not_enabled(): void
37+
{
38+
FilamentDeveloperLoginsPlugin::current()
39+
->enabled(false);
40+
41+
$user = TestUser::factory()->create();
42+
43+
$this->actingAs($user)
44+
->get(Dashboard::getUrl())
45+
->assertDontSeeLivewire(MenuLogins::class);
46+
}
47+
48+
public function test_livewire_displays_to_correct_amount_of_users(): void
49+
{
50+
Livewire::actingAs(TestUser::factory()->create())
51+
->test(MenuLogins::class)
52+
->assertViewHas('users', function (array $users) {
53+
return count($users) === 1;
54+
});
55+
}
56+
57+
public function test_forbidden_is_returned_when_enabled_or_switchable_is_false(): void
58+
{
59+
$authenticatedUser = TestUser::factory()->create();
60+
61+
TestUser::factory()->create([
62+
'email' => '[email protected]',
63+
'is_admin' => false,
64+
]);
65+
66+
FilamentDeveloperLoginsPlugin::current()
67+
->enabled(false);
68+
69+
Livewire::actingAs($authenticatedUser)
70+
->test(MenuLogins::class)
71+
->call('loginAs', '[email protected]')
72+
->assertForbidden();
73+
74+
FilamentDeveloperLoginsPlugin::current()
75+
->enabled()
76+
->switchable(false);
77+
78+
Livewire::actingAs($authenticatedUser)
79+
->test(MenuLogins::class)
80+
->call('loginAs', '[email protected]')
81+
->assertForbidden();
82+
}
83+
}

tests/TestCase.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,38 @@
22

33
namespace DutchCodingCompany\FilamentDeveloperLogins\Tests;
44

5+
use BladeUI\Heroicons\BladeHeroiconsServiceProvider;
6+
use BladeUI\Icons\BladeIconsServiceProvider;
57
use DutchCodingCompany\FilamentDeveloperLogins\FilamentDeveloperLoginsPlugin;
68
use DutchCodingCompany\FilamentDeveloperLogins\FilamentDeveloperLoginsServiceProvider;
79
use DutchCodingCompany\FilamentDeveloperLogins\Http\Controllers\DeveloperLoginsController;
810
use DutchCodingCompany\FilamentDeveloperLogins\Tests\Fixtures\TestUser;
11+
use Filament\Actions\ActionsServiceProvider;
912
use Filament\Facades\Filament;
1013
use Filament\FilamentServiceProvider;
14+
use Filament\Forms\FormsServiceProvider;
15+
use Filament\Http\Middleware\Authenticate;
16+
use Filament\Http\Middleware\DisableBladeIconComponents;
17+
use Filament\Http\Middleware\DispatchServingFilamentEvent;
18+
use Filament\Notifications\NotificationsServiceProvider;
1119
use Filament\Pages\Dashboard;
1220
use Filament\Panel;
21+
use Filament\Support\SupportServiceProvider;
22+
use Filament\Widgets\AccountWidget;
23+
use Filament\Widgets\FilamentInfoWidget;
24+
use Filament\Widgets\WidgetsServiceProvider;
25+
use Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse;
26+
use Illuminate\Cookie\Middleware\EncryptCookies;
1327
use Illuminate\Database\Eloquent\Factories\Factory;
1428
use Illuminate\Encryption\Encrypter;
29+
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken;
30+
use Illuminate\Routing\Middleware\SubstituteBindings;
31+
use Illuminate\Session\Middleware\AuthenticateSession;
32+
use Illuminate\Session\Middleware\StartSession;
33+
use Illuminate\View\Middleware\ShareErrorsFromSession;
1534
use Livewire\LivewireServiceProvider;
1635
use Orchestra\Testbench\TestCase as Orchestra;
36+
use RyanChandler\BladeCaptureDirective\BladeCaptureDirectiveServiceProvider;
1737

1838
class TestCase extends Orchestra
1939
{
@@ -35,6 +55,7 @@ protected function registerTestPanel(): void
3555
Filament::registerPanel(
3656
fn (): Panel => Panel::make()
3757
->default()
58+
->darkMode(false)
3859
->id($this->panelName)
3960
->path($this->panelName)
4061
->login()
@@ -77,6 +98,12 @@ protected function getPackageProviders($app): array
7798

7899
return [
79100
FilamentServiceProvider::class,
101+
BladeCaptureDirectiveServiceProvider::class,
102+
WidgetsServiceProvider::class,
103+
SupportServiceProvider::class,
104+
FormsServiceProvider::class,
105+
ActionsServiceProvider::class,
106+
NotificationsServiceProvider::class,
80107
FilamentDeveloperLoginsServiceProvider::class,
81108
LivewireServiceProvider::class,
82109
];

0 commit comments

Comments
 (0)