Skip to content

Commit

Permalink
Fix "about" tests
Browse files Browse the repository at this point in the history
  • Loading branch information
drbyte committed Apr 19, 2024
1 parent 13ff85b commit 63b29ad
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/PermissionServiceProvider.php
Expand Up @@ -180,16 +180,19 @@ protected function registerAbout(): void
return;
}

// array format: 'Display Text' => 'boolean-config-key name'
$features = [
'Teams' => 'teams',
'Wildcard-Permissions' => 'enable_wildcard_permission',
'Octane-Listener' => 'register_octane_reset_listener',
'Passport' => 'use_passport_client_credentials',
];

AboutCommand::add('Spatie Permissions', fn () => [
$config = $this->app['config'];

AboutCommand::add('Spatie Permissions', static fn () => [
'Features Enabled' => collect($features)
->filter(fn (string $feature, string $name): bool => $this->app['config']->get("permission.{$feature}"))
->filter(fn(string $feature, string $name): bool => $config->get("permission.{$feature}"))
->keys()
->whenEmpty(fn (Collection $collection) => $collection->push('Default'))
->join(', '),
Expand Down
36 changes: 34 additions & 2 deletions tests/CommandTest.php
Expand Up @@ -2,6 +2,8 @@

namespace Spatie\Permission\Tests;

use Composer\InstalledVersions;
use Illuminate\Foundation\Console\AboutCommand;
use Illuminate\Support\Facades\Artisan;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
Expand Down Expand Up @@ -186,13 +188,43 @@ public function it_can_show_roles_by_teams()
}

/** @test */
public function it_can_respond_to_about_command()
public function it_can_respond_to_about_command_with_default()
{
config()->set('permission.teams', true);
if (! class_exists(InstalledVersions::class) || ! class_exists(AboutCommand::class)) {
$this->markTestSkipped();
}
if (! method_exists(AboutCommand::class, 'flushState')) {
$this->markTestSkipped();
}

app(\Spatie\Permission\PermissionRegistrar::class)->initializeCache();

Artisan::call('about');
$output = Artisan::output();

$pattern = '/Spatie Permissions[ .\n]*Features Enabled[ .]*Default[ .\n]*Version/';
if (method_exists($this, 'assertMatchesRegularExpression')) {
$this->assertMatchesRegularExpression($pattern, $output);
} else { // phpUnit 9/8
$this->assertRegExp($pattern, $output);
}
}

/** @test */
public function it_can_respond_to_about_command_with_teams()
{
if (! class_exists(InstalledVersions::class) || ! class_exists(AboutCommand::class)) {
$this->markTestSkipped();
}
if (! method_exists(AboutCommand::class, 'flushState')) {
$this->markTestSkipped();
}

app(\Spatie\Permission\PermissionRegistrar::class)->initializeCache();

config()->set('permission.teams', true);

Artisan::call('about');
$output = Artisan::output();

$pattern = '/Spatie Permissions[ .\n]*Features Enabled[ .]*Teams[ .\n]*Version/';
Expand Down
10 changes: 10 additions & 0 deletions tests/TestCase.php
Expand Up @@ -3,6 +3,7 @@
namespace Spatie\Permission\Tests;

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Foundation\Console\AboutCommand;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Cache;
Expand Down Expand Up @@ -79,6 +80,15 @@ protected function setUp(): void
$this->setUpRoutes();
}

protected function tearDown(): void
{
parent::tearDown();

if (method_exists(AboutCommand::class, 'flushState')) {
AboutCommand::flushState();
}
}

/**
* @param \Illuminate\Foundation\Application $app
* @return array
Expand Down

0 comments on commit 63b29ad

Please sign in to comment.