Skip to content

Commit 19f3cb4

Browse files
committed
Cleanup tests
1 parent e10dabe commit 19f3cb4

7 files changed

Lines changed: 34 additions & 40 deletions

File tree

tests/Http/Auth/AuthenticationTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
use Code16\Sharp\Tests\Fixtures\Entities\PersonEntity;
44
use Code16\Sharp\Tests\Fixtures\TestAuthGuard;
55
use Code16\Sharp\Tests\Fixtures\User;
6+
use Illuminate\Contracts\Auth\Authenticatable;
7+
use Illuminate\Contracts\Auth\Guard;
68

79
beforeEach(function () {
810
sharp()->config()->declareEntity(PersonEntity::class);
9-
sharp()->config()->disableImpersonation();
1011
});
1112

1213
function setTestAuthGuard(): void
@@ -117,7 +118,7 @@ function setTestAuthGuard(): void
117118

118119
it('allows custom auth guard', function () {
119120
auth()->extend('test', function () {
120-
return new class() implements \Illuminate\Contracts\Auth\Guard
121+
return new class() implements Guard
121122
{
122123
protected $user;
123124

@@ -148,7 +149,7 @@ public function hasUser()
148149
return $this->user !== null;
149150
}
150151

151-
public function setUser(\Illuminate\Contracts\Auth\Authenticatable $user)
152+
public function setUser(Authenticatable $user)
152153
{
153154
$this->user = $user;
154155
}

tests/Http/Auth/AuthorizationsTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
beforeEach(function () {
1818
login();
1919
sharp()->config()->declareEntity(PersonEntity::class);
20-
sharp()->config()->disableImpersonation();
2120
});
2221

2322
it('allows to configure prohibited actions on entities', function () {

tests/Http/Auth/ImpersonateTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,16 @@
44
use Code16\Sharp\Auth\Impersonate\SharpImpersonationHandler;
55
use Code16\Sharp\Tests\Fixtures\User;
66
use Illuminate\Database\Schema\Blueprint;
7+
use Illuminate\Support\Facades\Gate;
78
use Illuminate\Support\Facades\Schema;
89
use Inertia\Testing\AssertableInertia as Assert;
910

11+
use function Orchestra\Testbench\Pest\defineEnvironment;
12+
13+
defineEnvironment(function () {
14+
sharp()->config()->enableImpersonation();
15+
});
16+
1017
function migrateUsersTable()
1118
{
1219
Schema::create('users', function (Blueprint $table) {
@@ -38,7 +45,7 @@ public function getUsers(): array
3845

3946
it('redirects to impersonation page if authenticated as non admin user', function () {
4047
migrateUsersTable();
41-
\Illuminate\Support\Facades\Gate::define('viewSharp', fn ($user) => false);
48+
Gate::define('viewSharp', fn ($user) => false);
4249

4350
sharp()->config()->enableImpersonation(new class() extends SharpImpersonationHandler
4451
{
@@ -137,6 +144,7 @@ public function getUsers(): array
137144
});
138145

139146
sharp()->config()->disableImpersonation();
147+
140148
$this->get(route('code16.sharp.impersonate'))
141149
->assertInertia(fn (Assert $page) => $page
142150
->where('impersonateUsers', null)

tests/Http/Auth/PasswordResetEloquentTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,14 @@
99
use Illuminate\Support\Facades\Notification;
1010
use Illuminate\Support\Facades\Schema;
1111

12+
use function Orchestra\Testbench\Pest\defineEnvironment;
13+
1214
uses(LazilyRefreshDatabase::class);
1315

16+
defineEnvironment(function () {
17+
sharp()->config()->enableForgottenPassword();
18+
});
19+
1420
beforeEach(function () {
1521
$this->withoutExceptionHandling();
1622
Notification::fake();

tests/Http/Auth/PasswordResetTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
use Code16\Sharp\Tests\Fixtures\TestPasswordBroker;
66

7-
beforeEach(function () {
7+
use function Orchestra\Testbench\Pest\defineEnvironment;
8+
9+
defineEnvironment(function () {
810
sharp()->config()->enableForgottenPassword(broker: TestPasswordBroker::class);
911
});
1012

tests/TestCase.php

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66
use BladeUI\Icons\Factory;
77
use Code16\ContentRenderer\ContentRendererServiceProvider;
88
use Code16\Sharp\SharpInternalServiceProvider;
9-
use Illuminate\Testing\Fluent\AssertableJson;
109
use Orchestra\Testbench\Pest\WithPest;
1110
use Orchestra\Testbench\TestCase as Orchestra;
12-
use PHPUnit\Framework\Assert as PHPUnit;
1311

1412
class TestCase extends Orchestra
1513
{
@@ -20,30 +18,9 @@ protected function setUp(): void
2018
parent::setUp();
2119

2220
$this->withoutVite();
23-
24-
config()->set('app.key', 'base64:'.base64_encode(random_bytes(32)));
25-
config()->set('view.cache', false);
26-
config()->set('inertia.testing.page_paths', [__DIR__.'/../resources/js/Pages']);
27-
28-
// laravel 11 polyfill, TODO to remove when laravel 12+ only
29-
AssertableJson::macro('whereNull', function ($key) {
30-
$this->has($key);
31-
32-
$actual = $this->prop($key);
33-
34-
PHPUnit::assertNull(
35-
$actual,
36-
sprintf(
37-
'Property [%s] should be null.',
38-
$this->dotPath($key),
39-
)
40-
);
41-
42-
return $this;
43-
});
4421
}
4522

46-
protected function getPackageProviders($app)
23+
protected function getPackageProviders($app): array
4724
{
4825
return [
4926
SharpInternalServiceProvider::class,
@@ -52,20 +29,18 @@ protected function getPackageProviders($app)
5229
];
5330
}
5431

55-
public function getEnvironmentSetUp($app)
32+
public function defineEnvironment($app): void
5633
{
57-
config()->set('database.default', 'testing');
34+
$app['config']->set('app.key', 'base64:'.base64_encode(random_bytes(32)));
35+
$app['config']->set('view.cache', false);
36+
$app['config']->set('inertia.testing.page_paths', [__DIR__.'/../resources/js/Pages']);
37+
$app['config']->set('database.default', 'testing');
38+
39+
$app['view']->addNamespace('fixtures', __DIR__.'/Fixtures/resources/views');
5840

5941
$app->make(Factory::class)->add('testicon', [
6042
'path' => __DIR__.'/Fixtures/resources/svg',
6143
'prefix' => 'testicon',
6244
]);
63-
64-
$app->make('view')->addNamespace('fixtures', __DIR__.'/Fixtures/resources/views');
65-
66-
// We have to set these two because otherwise corresponding routes won't be loaded at all
67-
sharp()->config()
68-
->enableForgottenPassword()
69-
->enableImpersonation();
7045
}
7146
}

tests/Unit/Console/GeneratorTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
beforeEach(function () {
99
login();
10+
});
11+
12+
afterEach(function () {
1013
File::deleteDirectory(base_path('app/Sharp'));
1114
});
1215

@@ -112,7 +115,7 @@
112115
->assertExitCode(0);
113116

114117
// Manually add this new Entity to the Sharp config
115-
app(\Code16\Sharp\Config\SharpConfigBuilder::class)
118+
app(SharpConfigBuilder::class)
116119
->addEntity('settings', '\App\Sharp\Entities\SettingsEntity');
117120

118121
$this->get(

0 commit comments

Comments
 (0)