Skip to content

Commit 793b3d8

Browse files
authored
Test Improvements (#1380)
* Test Improvements Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> --------- Signed-off-by: Mior Muhammad Zaki <[email protected]>
1 parent 83056da commit 793b3d8

16 files changed

+75
-207
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"laravel/sanctum": "^3.0",
2727
"livewire/livewire": "^3.0",
2828
"mockery/mockery": "^1.0",
29-
"orchestra/testbench": "^8.0",
29+
"orchestra/testbench": "^8.11",
3030
"phpstan/phpstan": "^1.10",
3131
"phpunit/phpunit": "^9.3"
3232
},

testbench.yaml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
providers:
2+
- Laravel\Fortify\FortifyServiceProvider
3+
- Laravel\Jetstream\JetstreamServiceProvider
4+
- Livewire\LivewireServiceProvider
5+
6+
migrations:
7+
- database/migrations
8+
- vendor/laravel/fortify/database/migrations
9+
10+
workbench:
11+
install: false

tests/AddTeamMemberTest.php

+5-13
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Actions\Jetstream\AddTeamMember;
66
use App\Actions\Jetstream\CreateTeam;
77
use App\Models\Team;
8+
use Illuminate\Foundation\Testing\RefreshDatabase;
89
use Illuminate\Support\Facades\Gate;
910
use Illuminate\Validation\ValidationException;
1011
use Laravel\Jetstream\Jetstream;
@@ -15,9 +16,11 @@
1516

1617
class AddTeamMemberTest extends OrchestraTestCase
1718
{
18-
public function setUp(): void
19+
use RefreshDatabase;
20+
21+
protected function defineEnvironment($app)
1922
{
20-
parent::setUp();
23+
parent::defineEnvironment($app);
2124

2225
Gate::policy(Team::class, TeamPolicy::class);
2326

@@ -28,8 +31,6 @@ public function test_team_members_can_be_added()
2831
{
2932
Jetstream::role('admin', 'Admin', ['foo']);
3033

31-
$this->migrate();
32-
3334
$team = $this->createTeam();
3435

3536
$otherUser = User::forceCreate([
@@ -62,8 +63,6 @@ public function test_user_email_address_must_exist()
6263
{
6364
$this->expectException(ValidationException::class);
6465

65-
$this->migrate();
66-
6766
$team = $this->createTeam();
6867

6968
$action = new AddTeamMember;
@@ -77,8 +76,6 @@ public function test_user_cant_already_be_on_team()
7776
{
7877
$this->expectException(ValidationException::class);
7978

80-
$this->migrate();
81-
8279
$team = $this->createTeam();
8380

8481
$otherUser = User::forceCreate([
@@ -106,9 +103,4 @@ protected function createTeam()
106103

107104
return $action->create($user, ['name' => 'Test Team']);
108105
}
109-
110-
protected function migrate()
111-
{
112-
$this->artisan('migrate', ['--database' => 'testbench'])->run();
113-
}
114106
}

tests/CreateTeamTest.php

+2-13
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,16 @@
1212

1313
class CreateTeamTest extends OrchestraTestCase
1414
{
15-
public function setUp(): void
15+
protected function defineEnvironment($app)
1616
{
17-
parent::setUp();
17+
parent::defineEnvironment($app);
1818

1919
Gate::policy(Team::class, TeamPolicy::class);
2020
Jetstream::useUserModel(User::class);
2121
}
2222

2323
public function test_team_name_can_be_updated()
2424
{
25-
$this->migrate();
26-
2725
$action = new CreateTeam;
2826

2927
$user = User::forceCreate([
@@ -41,8 +39,6 @@ public function test_name_is_required()
4139
{
4240
$this->expectException(ValidationException::class);
4341

44-
$this->migrate();
45-
4642
$action = new CreateTeam;
4743

4844
$user = User::forceCreate([
@@ -53,11 +49,4 @@ public function test_name_is_required()
5349

5450
$action->create($user, ['name' => '']);
5551
}
56-
57-
protected function migrate()
58-
{
59-
// $this->loadLaravelMigrations(['--database' => 'testbench']);
60-
61-
$this->artisan('migrate', ['--database' => 'testbench'])->run();
62-
}
6352
}

tests/CurrentTeamControllerTest.php

+7-19
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,21 @@
1111

1212
class CurrentTeamControllerTest extends OrchestraTestCase
1313
{
14-
public function setUp(): void
14+
protected function defineEnvironment($app)
1515
{
16-
parent::setUp();
16+
parent::defineEnvironment($app);
17+
18+
$app['config']->set([
19+
'jetstream.stack' => 'livewire',
20+
'jetstream.features' => ['teams'],
21+
]);
1722

1823
Gate::policy(Team::class, TeamPolicy::class);
1924
Jetstream::useUserModel(User::class);
2025
}
2126

2227
public function test_can_switch_to_team_the_user_belongs_to()
2328
{
24-
$this->migrate();
25-
2629
$action = new CreateTeam;
2730

2831
$user = User::forceCreate([
@@ -43,8 +46,6 @@ public function test_can_switch_to_team_the_user_belongs_to()
4346

4447
public function test_cant_switch_to_team_the_user_does_not_belong_to()
4548
{
46-
$this->migrate();
47-
4849
$action = new CreateTeam;
4950

5051
$user = User::forceCreate([
@@ -65,17 +66,4 @@ public function test_cant_switch_to_team_the_user_does_not_belong_to()
6566

6667
$response->assertStatus(403);
6768
}
68-
69-
protected function migrate()
70-
{
71-
$this->artisan('migrate', ['--database' => 'testbench'])->run();
72-
}
73-
74-
protected function getEnvironmentSetUp($app)
75-
{
76-
parent::getEnvironmentSetUp($app);
77-
78-
$app['config']->set('jetstream.stack', 'livewire');
79-
$app['config']->set('jetstream.features', ['teams']);
80-
}
8169
}

tests/DeleteTeamTest.php

+2-15
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,16 @@
1515

1616
class DeleteTeamTest extends OrchestraTestCase
1717
{
18-
public function setUp(): void
18+
protected function defineEnvironment($app)
1919
{
20-
parent::setUp();
20+
parent::defineEnvironment($app);
2121

2222
Gate::policy(Team::class, TeamPolicy::class);
2323
Jetstream::useUserModel(User::class);
2424
}
2525

2626
public function test_team_can_be_deleted()
2727
{
28-
$this->migrate();
29-
3028
$team = $this->createTeam();
3129

3230
$action = new DeleteTeam;
@@ -40,8 +38,6 @@ public function test_team_deletion_can_be_validated()
4038
{
4139
Jetstream::useUserModel(User::class);
4240

43-
$this->migrate();
44-
4541
$team = $this->createTeam();
4642

4743
$action = new ValidateTeamDeletion;
@@ -57,8 +53,6 @@ public function test_personal_team_cant_be_deleted()
5753

5854
Jetstream::useUserModel(User::class);
5955

60-
$this->migrate();
61-
6256
$team = $this->createTeam();
6357

6458
$team->forceFill(['personal_team' => true])->save();
@@ -74,8 +68,6 @@ public function test_non_owner_cant_delete_team()
7468

7569
Jetstream::useUserModel(User::class);
7670

77-
$this->migrate();
78-
7971
$team = $this->createTeam();
8072

8173
$action = new ValidateTeamDeletion;
@@ -99,9 +91,4 @@ protected function createTeam()
9991

10092
return $action->create($user, ['name' => 'Test Team']);
10193
}
102-
103-
protected function migrate()
104-
{
105-
$this->artisan('migrate', ['--database' => 'testbench'])->run();
106-
}
10794
}

tests/DeleteUserWithTeamsTest.php

+6-7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use App\Actions\Jetstream\DeleteTeam;
77
use App\Actions\Jetstream\DeleteUser;
88
use App\Models\Team;
9+
use Illuminate\Foundation\Testing\RefreshDatabase;
910
use Illuminate\Support\Facades\DB;
1011
use Illuminate\Support\Facades\Gate;
1112
use Illuminate\Support\Facades\Schema;
@@ -16,18 +17,18 @@
1617

1718
class DeleteUserWithTeamsTest extends OrchestraTestCase
1819
{
19-
public function setUp(): void
20+
use RefreshDatabase;
21+
22+
protected function defineEnvironment($app)
2023
{
21-
parent::setUp();
24+
parent::defineEnvironment($app);
2225

2326
Gate::policy(Team::class, TeamPolicy::class);
2427
Jetstream::useUserModel(User::class);
2528
}
2629

2730
public function test_user_can_be_deleted()
2831
{
29-
$this->migrate();
30-
3132
$team = $this->createTeam();
3233
$otherTeam = $this->createTeam();
3334

@@ -64,10 +65,8 @@ protected function createTeam()
6465
return $action->create($user, ['name' => 'Test Team']);
6566
}
6667

67-
protected function migrate()
68+
protected function afterRefreshingDatabase()
6869
{
69-
$this->artisan('migrate', ['--database' => 'testbench'])->run();
70-
7170
Schema::create('personal_access_tokens', function ($table) {
7271
$table->id();
7372
$table->foreignId('tokenable_id');

tests/HasTeamsTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ class HasTeamsTest extends OrchestraTestCase
1414
{
1515
use RefreshDatabase;
1616

17-
public function setUp(): void
17+
protected function defineEnvironment($app)
1818
{
19-
parent::setUp();
19+
parent::defineEnvironment($app);
2020

2121
Jetstream::$permissions = [];
2222
Jetstream::$roles = [];

tests/InviteTeamMemberTest.php

+2-11
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414

1515
class InviteTeamMemberTest extends OrchestraTestCase
1616
{
17-
public function setUp(): void
17+
protected function defineEnvironment($app)
1818
{
19-
parent::setUp();
19+
parent::defineEnvironment($app);
2020

2121
Gate::policy(Team::class, TeamPolicy::class);
2222

@@ -29,8 +29,6 @@ public function test_team_members_can_be_invited()
2929

3030
Jetstream::role('admin', 'Admin', ['foo']);
3131

32-
$this->migrate();
33-
3432
$team = $this->createTeam();
3533

3634
$otherUser = User::forceCreate([
@@ -57,8 +55,6 @@ public function test_user_cant_already_be_on_team()
5755

5856
$this->expectException(ValidationException::class);
5957

60-
$this->migrate();
61-
6258
$team = $this->createTeam();
6359

6460
$otherUser = User::forceCreate([
@@ -86,9 +82,4 @@ protected function createTeam()
8682

8783
return $action->create($user, ['name' => 'Test Team']);
8884
}
89-
90-
protected function migrate()
91-
{
92-
$this->artisan('migrate', ['--database' => 'testbench'])->run();
93-
}
9485
}

tests/OrchestraTestCase.php

+4-31
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,21 @@
22

33
namespace Laravel\Jetstream\Tests;
44

5+
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
56
use Laravel\Fortify\FortifyServiceProvider;
67
use Laravel\Jetstream\Features;
78
use Laravel\Jetstream\JetstreamServiceProvider;
89
use Livewire\LivewireServiceProvider;
10+
use Orchestra\Testbench\Concerns\WithWorkbench;
911
use Orchestra\Testbench\TestCase;
1012

1113
abstract class OrchestraTestCase extends TestCase
1214
{
13-
public function setUp(): void
14-
{
15-
parent::setUp();
16-
}
17-
18-
public function tearDown(): void
19-
{
20-
parent::tearDown();
21-
}
22-
23-
protected function getPackageProviders($app)
24-
{
25-
return [
26-
LivewireServiceProvider::class,
27-
JetstreamServiceProvider::class,
28-
FortifyServiceProvider::class,
29-
];
30-
}
15+
use LazilyRefreshDatabase, WithWorkbench;
3116

3217
protected function defineEnvironment($app)
3318
{
34-
$app['config']->set('database.default', 'testbench');
35-
36-
$app['config']->set('database.connections.testbench', [
37-
'driver' => 'sqlite',
38-
'database' => ':memory:',
39-
'prefix' => '',
40-
]);
41-
}
42-
43-
protected function defineDatabaseMigrations()
44-
{
45-
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
46-
$this->loadMigrationsFrom(__DIR__.'/../vendor/laravel/fortify/database/migrations');
19+
$app['config']->set('database.default', 'testing');
4720
}
4821

4922
protected function defineHasTeamEnvironment($app)

0 commit comments

Comments
 (0)