Skip to content

Commit

Permalink
Add SettingsServiceProvider (#8)
Browse files Browse the repository at this point in the history
* Move config to SettingsServiceProvider

* WIP

* WIP

* WIP
  • Loading branch information
mako321 authored Sep 27, 2022
1 parent 178cced commit c122367
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 10 deletions.
3 changes: 3 additions & 0 deletions env/codecov/testbench.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ providers:
- Spatie\Permission\PermissionServiceProvider
- EscolaLms\Jitsi\EscolaLmsJitsiServiceProvider
- EscolaLms\Settings\EscolaLmsSettingsServiceProvider
- EscolaLms\Core\EscolaLmsServiceProvider
- EscolaLms\Auth\EscolaLmsAuthServiceProvider
- EscolaLms\Categories\EscolaLmsCategoriesServiceProvider
3 changes: 3 additions & 0 deletions env/mysql/testbench.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ providers:
- Spatie\Permission\PermissionServiceProvider
- EscolaLms\Jitsi\EscolaLmsJitsiServiceProvider
- EscolaLms\Settings\EscolaLmsSettingsServiceProvider
- EscolaLms\Core\EscolaLmsServiceProvider
- EscolaLms\Auth\EscolaLmsAuthServiceProvider
- EscolaLms\Categories\EscolaLmsCategoriesServiceProvider
3 changes: 3 additions & 0 deletions env/postgres/testbench.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ providers:
- Spatie\Permission\PermissionServiceProvider
- EscolaLms\Jitsi\EscolaLmsJitsiServiceProvider
- EscolaLms\Settings\EscolaLmsSettingsServiceProvider
- EscolaLms\Core\EscolaLmsServiceProvider
- EscolaLms\Auth\EscolaLmsAuthServiceProvider
- EscolaLms\Categories\EscolaLmsCategoriesServiceProvider
13 changes: 3 additions & 10 deletions src/EscolaLmsJitsiServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,17 @@

namespace EscolaLms\Jitsi;

use EscolaLms\Jitsi\Providers\SettingsServiceProvider;
use Illuminate\Support\ServiceProvider;
use EscolaLms\Jitsi\Services\Contracts\JitsiServiceContract;
use EscolaLms\Jitsi\Services\JitsiService;
use EscolaLms\Settings\Facades\AdministrableConfig;
use EscolaLms\Jitsi\Enum\PackageStatusEnum;

/**
* SWAGGER_VERSION
*/

class EscolaLmsJitsiServiceProvider extends ServiceProvider
{

const CONFIG_KEY = 'jitsi';

public $singletons = [
JitsiServiceContract::class => JitsiService::class,
];
Expand All @@ -28,18 +24,15 @@ class EscolaLmsJitsiServiceProvider extends ServiceProvider
*/
public function boot()
{
AdministrableConfig::registerConfig(self::CONFIG_KEY . '.package_status', ['required', 'string', 'in:' . implode(',', PackageStatusEnum::getValues())], false);
AdministrableConfig::registerConfig(self::CONFIG_KEY . '.host', ['required', 'string'], true);
AdministrableConfig::registerConfig(self::CONFIG_KEY . '.app_id', ['nullable', 'string'], false);
AdministrableConfig::registerConfig(self::CONFIG_KEY . '.secret', ['nullable', 'string'], false);
}


public function register()
{
$this->mergeConfigFrom(
__DIR__ . '/../config/jitsi.php',
'jitsi'
);

$this->app->register(SettingsServiceProvider::class);
}
}
27 changes: 27 additions & 0 deletions src/Providers/SettingsServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace EscolaLms\Jitsi\Providers;

use EscolaLms\Jitsi\Enum\PackageStatusEnum;
use EscolaLms\Settings\Facades\AdministrableConfig;
use Illuminate\Support\ServiceProvider;
use EscolaLms\Settings\EscolaLmsSettingsServiceProvider;

class SettingsServiceProvider extends ServiceProvider
{
const CONFIG_KEY = 'jitsi';

public function register()
{
if (class_exists(\EscolaLms\Settings\EscolaLmsSettingsServiceProvider::class)) {
if (!$this->app->getProviders(EscolaLmsSettingsServiceProvider::class)) {
$this->app->register(EscolaLmsSettingsServiceProvider::class);
}

AdministrableConfig::registerConfig(self::CONFIG_KEY . '.package_status', ['required', 'string', 'in:' . implode(',', PackageStatusEnum::getValues())], false);
AdministrableConfig::registerConfig(self::CONFIG_KEY . '.host', ['required', 'string']);
AdministrableConfig::registerConfig(self::CONFIG_KEY . '.app_id', ['nullable', 'string'], false);
AdministrableConfig::registerConfig(self::CONFIG_KEY . '.secret', ['nullable', 'string'], false);
}
}
}
3 changes: 3 additions & 0 deletions testbench.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ providers:
- Spatie\Permission\PermissionServiceProvider
- EscolaLms\Jitsi\EscolaLmsJitsiServiceProvider
- EscolaLms\Settings\EscolaLmsSettingsServiceProvider
- EscolaLms\Core\EscolaLmsServiceProvider
- EscolaLms\Auth\EscolaLmsAuthServiceProvider
- EscolaLms\Categories\EscolaLmsCategoriesServiceProvider
140 changes: 140 additions & 0 deletions tests/Api/SettingsApiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php

namespace EscolaLms\Jitsi\Tests\Api;

use EscolaLms\Core\Tests\ApiTestTrait;
use EscolaLms\Core\Tests\CreatesUsers;
use EscolaLms\Jitsi\Enum\PackageStatusEnum;
use EscolaLms\Jitsi\Providers\SettingsServiceProvider;
use EscolaLms\Jitsi\Tests\TestCase;
use EscolaLms\Settings\Database\Seeders\PermissionTableSeeder;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Config;

class SettingsApiTest extends TestCase
{
use CreatesUsers, ApiTestTrait, DatabaseTransactions, WithFaker;

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

if (!class_exists(\EscolaLms\Settings\EscolaLmsSettingsServiceProvider::class)) {
$this->markTestSkipped('Settings package not installed');
}

$this->seed(PermissionTableSeeder::class);
Config::set('escola_settings.use_database', true);

$this->user = config('auth.providers.users.model')::factory()->create();
$this->user->guard_name = 'api';
$this->user->assignRole('admin');
}

public function testAdministrableConfigApi(): void
{
$configKey = SettingsServiceProvider::CONFIG_KEY;

$packageStatus = $this->faker->randomElement(PackageStatusEnum::getValues());
$host = $this->faker->domainWord;
$appId = $this->faker->uuid;
$secret = $this->faker->uuid;

$this->response = $this->actingAs($this->user, 'api')->json(
'POST',
'/api/admin/config',
[
'config' => [
[
'key' => "$configKey.package_status",
'value' => $packageStatus,
],
[
'key' => "$configKey.host",
'value' => $host,
],
[
'key' => "$configKey.app_id",
'value' => $appId,
],
[
'key' => "$configKey.secret",
'value' => $secret,
],
]
]
);
$this->response->assertOk();

$this->response = $this->actingAs($this->user, 'api')->json(
'GET',
'/api/admin/config'
);

$this->response->assertOk();

$this->response->assertJsonFragment([
$configKey => [
'package_status' => [
'full_key' => "$configKey.package_status",
'key' => 'package_status',
'rules' => [
'required',
'string',
'in:' . implode(',', PackageStatusEnum::getValues()),
],
'public' => false,
'readonly' => false,
'value' => $packageStatus,
],
'host' => [
'full_key' => "$configKey.host",
'key' => 'host',
'rules' => [
'required',
'string'
],
'public' => true,
'readonly' => false,
'value' => $host,
],
'app_id' => [
'full_key' => "$configKey.app_id",
'key' => 'app_id',
'rules' => [
'nullable',
'string',
],
'public' => false,
'value' => $appId,
'readonly' => false,
],
'secret' => [
'full_key' => "$configKey.secret",
'key' => 'secret',
'rules' => [
'nullable',
'string'
],
'public' => false,
'readonly' => false,
'value' => $secret,
],
],
]);

$this->response = $this->json(
'GET',
'/api/config'
);

$this->response->assertOk();

$this->response->assertJsonFragment([
$configKey => [
'host' => $host,
],
]);
}
}

0 comments on commit c122367

Please sign in to comment.