From 74dea2230056f6abe16e3a87b7bf2c5208e5e923 Mon Sep 17 00:00:00 2001 From: Rein Van Oyen Date: Fri, 11 Nov 2022 18:23:32 +0100 Subject: [PATCH] progress testing --- .gitignore | 1 + src/Cmf.php | 52 +++++++++++++++------------- src/Factories/ModuleFactory.php | 19 +++++++++++ src/Module.php | 22 +++--------- src/ModuleRegistry.php | 60 +++++++++++++++++++++++++++++++++ tests/CmfTest.php | 32 +++++++++++++++++- tests/TestCase.php | 3 +- 7 files changed, 145 insertions(+), 44 deletions(-) create mode 100644 src/Factories/ModuleFactory.php create mode 100644 src/ModuleRegistry.php diff --git a/.gitignore b/.gitignore index 1f971b0..6d25dae 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ node_modules vendor .env composer.lock +.phpunit.result.cache diff --git a/src/Cmf.php b/src/Cmf.php index edff6db..43774c9 100644 --- a/src/Cmf.php +++ b/src/Cmf.php @@ -4,6 +4,7 @@ use Illuminate\Support\Facades\Event; use ReinVanOyen\Cmf\Events\ServingCmf; +use ReinVanOyen\Cmf\Factories\ModuleFactory; /** * Class Cmf @@ -19,24 +20,27 @@ class Cmf private $title; /** - * The modules of the CMF application - * - * @var array $modules + * @var ModuleRegistry $modules */ private $modules; /** - * An associative array holding the modules, for easier access - * - * @var array $modulesMap + * Cmf constructor. + * @param ModuleRegistry $modules + * @param string $title */ - private $modulesMap = []; + public function __construct(ModuleRegistry $modules, string $title) + { + $this->title = $title; + $this->modules = $modules; + } /** - * Cmf constructor. + * Set the title of the CMF application + * * @param string $title */ - public function __construct(string $title) + public function setTitle(string $title) { $this->title = $title; } @@ -53,23 +57,23 @@ public function getTitle(): string /** * @param array $modules - * @throws \Psr\Container\ContainerExceptionInterface - * @throws \Psr\Container\NotFoundExceptionInterface */ public function registerModules(array $modules) { - $this->modules = Module::makeModules($modules); - - // Store all modules by id in the modules map - foreach ($this->modules as $module) { + foreach ($modules as $module) { + $this->registerModule($module); + } + } - $this->modulesMap[$module->id()] = $module; - - $submodules = Module::makeModules($module->submodules()); + /** + * @param $module + */ + public function registerModule($module) + { + $module = $this->modules->add($module); - foreach ($submodules as $submodule) { - $this->modulesMap[$submodule->id()] = $submodule; - } + foreach ($module->submodules() as $submodule) { + $this->modules->add($submodule, false); } } @@ -81,17 +85,17 @@ public function registerModules(array $modules) */ public function getModule(string $id): ?Module { - return ($this->modulesMap[$id] ?? null); + return $this->modules->get($id); } /** - * Get all modules + * Get all root modules * * @return array */ public function getModules(): array { - return $this->modules; + return $this->modules->root(); } /** diff --git a/src/Factories/ModuleFactory.php b/src/Factories/ModuleFactory.php new file mode 100644 index 0000000..3aa1d6a --- /dev/null +++ b/src/Factories/ModuleFactory.php @@ -0,0 +1,19 @@ + $this->id(), ]; $this->exports['url'] = url('admin/'.$this->id()); - $this->exports['submodules'] = self::makeModules($this->submodules()); + $this->exports['submodules'] = array_map(function($module) { + return ModuleFactory::make($module); + }, $this->submodules()); return $this->exports; } diff --git a/src/ModuleRegistry.php b/src/ModuleRegistry.php new file mode 100644 index 0000000..ebe109d --- /dev/null +++ b/src/ModuleRegistry.php @@ -0,0 +1,60 @@ +modulesMap[$module->id()] = $module; + + if ($isRoot) { + $this->modules[] = $module; + } + + return $module; + } + + /** + * @param string $id + * @return mixed|null + */ + public function get(string $id): ?Module + { + return ($this->modulesMap[$id] ?? null); + } + + /** + * @return array + */ + public function root(): array + { + return $this->modules; + } + + /** + * @return array + */ + public function all(): array + { + return array_keys($this->modulesMap); + } +} diff --git a/tests/CmfTest.php b/tests/CmfTest.php index 0ab89a5..6157c87 100644 --- a/tests/CmfTest.php +++ b/tests/CmfTest.php @@ -3,13 +3,21 @@ namespace ReinVanOyen\Cmf\Tests\Component; use ReinVanOyen\Cmf\Cmf; +use ReinVanOyen\Cmf\Module; use ReinVanOyen\Cmf\Tests\TestCase; class CmfTest extends TestCase { - public function testTitleConfiguation() + public function testConfiguation() { + // Assert if the title is injected to the CMF instance $this->assertEquals('Test cmf', app(Cmf::class)->getTitle()); + + // Change the title + app(Cmf::class)->setTitle('New title'); + + // Assert the title corresponds to the new title + $this->assertEquals('New title', app(Cmf::class)->getTitle()); } public function testIfApiIsUnauthorized() @@ -24,4 +32,26 @@ public function testModulesCount() { $this->assertCount(1, app(Cmf::class)->getModules()); } + + public function testIfAllModulesAreInstantiated() + { + $modules = app(Cmf::class)->getModules(); + + foreach ($modules as $module) { + $this->assertInstanceOf(Module::class, $module); + $this->assertIsString($module->id()); + } + } + + public function testIfModulesAreCorrectlyStored() + { + $modules = app(Cmf::class)->getModules(); + + foreach ($modules as $module) { + $this->assertEquals( + $module, + app(Cmf::class)->getModule($module->id()) + ); + } + } } diff --git a/tests/TestCase.php b/tests/TestCase.php index cb1a792..f197283 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -3,6 +3,7 @@ namespace ReinVanOyen\Cmf\Tests; use ReinVanOyen\Cmf\Cmf; +use ReinVanOyen\Cmf\CmfServiceProvider; use ReinVanOyen\Cmf\Tests\Fixtures\Modules\UserModule; class TestCase extends \Orchestra\Testbench\TestCase @@ -28,7 +29,7 @@ public function setUp(): void protected function getPackageProviders($app) { return [ - 'ReinVanOyen\Cmf\CmfServiceProvider', + CmfServiceProvider::class, ]; }