Skip to content

Commit

Permalink
test: refactored tests to be compatible with the new lunar admin perm…
Browse files Browse the repository at this point in the history
…issions
  • Loading branch information
armezit committed Dec 18, 2023
1 parent 8285150 commit 6569804
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 29 deletions.
10 changes: 5 additions & 5 deletions tests/Feature/Http/Livewire/Pages/CodePool/ImportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public function cant_view_page_as_guest()
/** @test */
public function cant_view_page_without_permission()
{
$this->setupRolesPermissions();

$staff = Staff::factory()->create([
'admin' => false,
]);
Expand All @@ -36,15 +38,13 @@ public function cant_view_page_without_permission()
/** @test */
public function can_view_page_with_correct_permission()
{
$this->setupRolesPermissions();

$staff = Staff::factory()->create([
'admin' => false,
]);

$staff->permissions()->createMany([
[
'handle' => 'catalogue:manage-products',
],
]);
$staff->givePermissionTo('catalogue:manage-products');

$this->actingAs($staff, 'staff');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public function cant_view_page_as_guest()
/** @test */
public function cant_view_page_without_permission()
{
$this->setupRolesPermissions();

$staff = Staff::factory()->create([
'admin' => false,
]);
Expand All @@ -36,15 +38,13 @@ public function cant_view_page_without_permission()
/** @test */
public function can_view_page_with_correct_permission()
{
$this->setupRolesPermissions();

$staff = Staff::factory()->create([
'admin' => false,
]);

$staff->permissions()->createMany([
[
'handle' => 'catalogue:manage-products',
],
]);
$staff->givePermissionTo('catalogue:manage-products');

$this->actingAs($staff, 'staff');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public function cant_view_page_as_guest()
/** @test */
public function cant_view_page_without_permission()
{
$this->setupRolesPermissions();

$staff = Staff::factory()->create([
'admin' => false,
]);
Expand All @@ -37,15 +39,13 @@ public function cant_view_page_without_permission()
/** @test */
public function can_view_page_with_correct_permission()
{
$this->setupRolesPermissions();

$staff = Staff::factory()->create([
'admin' => false,
]);

$staff->permissions()->createMany([
[
'handle' => 'catalogue:manage-products',
],
]);
$staff->givePermissionTo('catalogue:manage-products');

$this->actingAs($staff, 'staff');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public function cant_view_page_as_guest()
/** @test */
public function cant_view_page_without_permission()
{
$this->setupRolesPermissions();

$staff = Staff::factory()->create([
'admin' => false,
]);
Expand All @@ -36,15 +38,13 @@ public function cant_view_page_without_permission()
/** @test */
public function can_view_page_with_correct_permission()
{
$this->setupRolesPermissions();

$staff = Staff::factory()->create([
'admin' => false,
]);

$staff->permissions()->createMany([
[
'handle' => 'catalogue:manage-products',
],
]);
$staff->givePermissionTo('catalogue:manage-products');

$this->actingAs($staff, 'staff');

Expand Down
26 changes: 17 additions & 9 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Support\Facades\Config;
use Kalnoy\Nestedset\NestedSetServiceProvider;
use Livewire\LivewireServiceProvider;
use Lunar\Hub\Actions\Permission\SyncRolesPermissions;
use Lunar\Hub\AdminHubServiceProvider;
use Lunar\LivewireTables\LivewireTablesServiceProvider;
use Lunar\LunarServiceProvider;
Expand All @@ -19,6 +20,7 @@
use Spatie\LaravelBlink\BlinkServiceProvider;
use Spatie\LaravelData\LaravelDataServiceProvider;
use Spatie\MediaLibrary\MediaLibraryServiceProvider;
use Spatie\Permission\PermissionServiceProvider;

class TestCase extends \Orchestra\Testbench\TestCase
{
Expand All @@ -30,13 +32,13 @@ public function __construct(string $name = null, array $data = [], string $dataN
parent::__construct($name, $data, $dataName);
}

protected function setUp(): void
public function setUp(): void
{
parent::setUp();

Factory::guessFactoryNamesUsing(
fn (string $modelName) => 'Armezit\\Lunar\\VirtualProduct\\Database\\Factories\\'.class_basename($modelName).'Factory'
);
Factory::guessFactoryNamesUsing(function (string $modelName) {
return 'Armezit\\Lunar\\VirtualProduct\\Database\\Factories\\' . class_basename($modelName) . 'Factory';
});

// additional setup
Config::set('auth.providers.users.model', User::class);
Expand All @@ -47,7 +49,7 @@ protected function setUp(): void
protected function defineDatabaseMigrations()
{
$this->loadLaravelMigrations();
$this->loadMigrationsFrom(__DIR__.'/../vendor/lunar/core/database/migrations');
$this->loadMigrationsFrom(__DIR__ . '/../vendor/lunar/core/database/migrations');
}

protected function getPackageProviders($app)
Expand All @@ -56,6 +58,7 @@ protected function getPackageProviders($app)
LunarServiceProvider::class,
LivewireServiceProvider::class,
LivewireTablesServiceProvider::class,
PermissionServiceProvider::class,
AdminHubServiceProvider::class,
ActivitylogServiceProvider::class,
MediaLibraryServiceProvider::class,
Expand All @@ -71,7 +74,7 @@ protected function getPackageProviders($app)
/**
* Define environment setup.
*
* @param \Illuminate\Foundation\Application $app
* @param \Illuminate\Foundation\Application $app
* @return void
*/
protected function defineEnvironment($app)
Expand All @@ -82,17 +85,22 @@ protected function defineEnvironment($app)
/**
* Define environment setup.
*
* @param \Illuminate\Foundation\Application $app
* @param \Illuminate\Foundation\Application $app
* @return void
*/
public function getEnvironmentSetUp($app)
{
config()->set('database.default', 'testing');
Config::set('database.default', 'testing');

$migrationFiles = glob(__DIR__.'/../database/migrations/*.php.stub');
$migrationFiles = glob(__DIR__ . '/../database/migrations/*.php.stub');
foreach ($migrationFiles as $migrationFile) {
$migration = include $migrationFile;
$migration->up();
}
}

protected function setupRolesPermissions()
{
app(SyncRolesPermissions::class)();
}
}

0 comments on commit 6569804

Please sign in to comment.