Skip to content

Commit

Permalink
Test Improvements (#1380)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
crynobone authored Sep 19, 2023
1 parent 83056da commit 793b3d8
Show file tree
Hide file tree
Showing 16 changed files with 75 additions and 207 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"laravel/sanctum": "^3.0",
"livewire/livewire": "^3.0",
"mockery/mockery": "^1.0",
"orchestra/testbench": "^8.0",
"orchestra/testbench": "^8.11",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^9.3"
},
Expand Down
11 changes: 11 additions & 0 deletions testbench.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
providers:
- Laravel\Fortify\FortifyServiceProvider
- Laravel\Jetstream\JetstreamServiceProvider
- Livewire\LivewireServiceProvider

migrations:
- database/migrations
- vendor/laravel/fortify/database/migrations

workbench:
install: false
18 changes: 5 additions & 13 deletions tests/AddTeamMemberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Actions\Jetstream\AddTeamMember;
use App\Actions\Jetstream\CreateTeam;
use App\Models\Team;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Gate;
use Illuminate\Validation\ValidationException;
use Laravel\Jetstream\Jetstream;
Expand All @@ -15,9 +16,11 @@

class AddTeamMemberTest extends OrchestraTestCase
{
public function setUp(): void
use RefreshDatabase;

protected function defineEnvironment($app)
{
parent::setUp();
parent::defineEnvironment($app);

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

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

$this->migrate();

$team = $this->createTeam();

$otherUser = User::forceCreate([
Expand Down Expand Up @@ -62,8 +63,6 @@ public function test_user_email_address_must_exist()
{
$this->expectException(ValidationException::class);

$this->migrate();

$team = $this->createTeam();

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

$this->migrate();

$team = $this->createTeam();

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

return $action->create($user, ['name' => 'Test Team']);
}

protected function migrate()
{
$this->artisan('migrate', ['--database' => 'testbench'])->run();
}
}
15 changes: 2 additions & 13 deletions tests/CreateTeamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,16 @@

class CreateTeamTest extends OrchestraTestCase
{
public function setUp(): void
protected function defineEnvironment($app)
{
parent::setUp();
parent::defineEnvironment($app);

Gate::policy(Team::class, TeamPolicy::class);
Jetstream::useUserModel(User::class);
}

public function test_team_name_can_be_updated()
{
$this->migrate();

$action = new CreateTeam;

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

$this->migrate();

$action = new CreateTeam;

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

$action->create($user, ['name' => '']);
}

protected function migrate()
{
// $this->loadLaravelMigrations(['--database' => 'testbench']);

$this->artisan('migrate', ['--database' => 'testbench'])->run();
}
}
26 changes: 7 additions & 19 deletions tests/CurrentTeamControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@

class CurrentTeamControllerTest extends OrchestraTestCase
{
public function setUp(): void
protected function defineEnvironment($app)
{
parent::setUp();
parent::defineEnvironment($app);

$app['config']->set([
'jetstream.stack' => 'livewire',
'jetstream.features' => ['teams'],
]);

Gate::policy(Team::class, TeamPolicy::class);
Jetstream::useUserModel(User::class);
}

public function test_can_switch_to_team_the_user_belongs_to()
{
$this->migrate();

$action = new CreateTeam;

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

public function test_cant_switch_to_team_the_user_does_not_belong_to()
{
$this->migrate();

$action = new CreateTeam;

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

$response->assertStatus(403);
}

protected function migrate()
{
$this->artisan('migrate', ['--database' => 'testbench'])->run();
}

protected function getEnvironmentSetUp($app)
{
parent::getEnvironmentSetUp($app);

$app['config']->set('jetstream.stack', 'livewire');
$app['config']->set('jetstream.features', ['teams']);
}
}
17 changes: 2 additions & 15 deletions tests/DeleteTeamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,16 @@

class DeleteTeamTest extends OrchestraTestCase
{
public function setUp(): void
protected function defineEnvironment($app)
{
parent::setUp();
parent::defineEnvironment($app);

Gate::policy(Team::class, TeamPolicy::class);
Jetstream::useUserModel(User::class);
}

public function test_team_can_be_deleted()
{
$this->migrate();

$team = $this->createTeam();

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

$this->migrate();

$team = $this->createTeam();

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

Jetstream::useUserModel(User::class);

$this->migrate();

$team = $this->createTeam();

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

Jetstream::useUserModel(User::class);

$this->migrate();

$team = $this->createTeam();

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

return $action->create($user, ['name' => 'Test Team']);
}

protected function migrate()
{
$this->artisan('migrate', ['--database' => 'testbench'])->run();
}
}
13 changes: 6 additions & 7 deletions tests/DeleteUserWithTeamsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Actions\Jetstream\DeleteTeam;
use App\Actions\Jetstream\DeleteUser;
use App\Models\Team;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Schema;
Expand All @@ -16,18 +17,18 @@

class DeleteUserWithTeamsTest extends OrchestraTestCase
{
public function setUp(): void
use RefreshDatabase;

protected function defineEnvironment($app)
{
parent::setUp();
parent::defineEnvironment($app);

Gate::policy(Team::class, TeamPolicy::class);
Jetstream::useUserModel(User::class);
}

public function test_user_can_be_deleted()
{
$this->migrate();

$team = $this->createTeam();
$otherTeam = $this->createTeam();

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

protected function migrate()
protected function afterRefreshingDatabase()
{
$this->artisan('migrate', ['--database' => 'testbench'])->run();

Schema::create('personal_access_tokens', function ($table) {
$table->id();
$table->foreignId('tokenable_id');
Expand Down
4 changes: 2 additions & 2 deletions tests/HasTeamsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ class HasTeamsTest extends OrchestraTestCase
{
use RefreshDatabase;

public function setUp(): void
protected function defineEnvironment($app)
{
parent::setUp();
parent::defineEnvironment($app);

Jetstream::$permissions = [];
Jetstream::$roles = [];
Expand Down
13 changes: 2 additions & 11 deletions tests/InviteTeamMemberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

class InviteTeamMemberTest extends OrchestraTestCase
{
public function setUp(): void
protected function defineEnvironment($app)
{
parent::setUp();
parent::defineEnvironment($app);

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

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

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

$this->migrate();

$team = $this->createTeam();

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

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

$this->migrate();

$team = $this->createTeam();

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

return $action->create($user, ['name' => 'Test Team']);
}

protected function migrate()
{
$this->artisan('migrate', ['--database' => 'testbench'])->run();
}
}
35 changes: 4 additions & 31 deletions tests/OrchestraTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,21 @@

namespace Laravel\Jetstream\Tests;

use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
use Laravel\Fortify\FortifyServiceProvider;
use Laravel\Jetstream\Features;
use Laravel\Jetstream\JetstreamServiceProvider;
use Livewire\LivewireServiceProvider;
use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\TestCase;

abstract class OrchestraTestCase extends TestCase
{
public function setUp(): void
{
parent::setUp();
}

public function tearDown(): void
{
parent::tearDown();
}

protected function getPackageProviders($app)
{
return [
LivewireServiceProvider::class,
JetstreamServiceProvider::class,
FortifyServiceProvider::class,
];
}
use LazilyRefreshDatabase, WithWorkbench;

protected function defineEnvironment($app)
{
$app['config']->set('database.default', 'testbench');

$app['config']->set('database.connections.testbench', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);
}

protected function defineDatabaseMigrations()
{
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
$this->loadMigrationsFrom(__DIR__.'/../vendor/laravel/fortify/database/migrations');
$app['config']->set('database.default', 'testing');
}

protected function defineHasTeamEnvironment($app)
Expand Down
Loading

0 comments on commit 793b3d8

Please sign in to comment.