Skip to content

Commit

Permalink
progress testing
Browse files Browse the repository at this point in the history
  • Loading branch information
reinvanoyen committed Nov 11, 2022
1 parent f18c744 commit 74dea22
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 44 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules
vendor
.env
composer.lock
.phpunit.result.cache
52 changes: 28 additions & 24 deletions src/Cmf.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Facades\Event;
use ReinVanOyen\Cmf\Events\ServingCmf;
use ReinVanOyen\Cmf\Factories\ModuleFactory;

/**
* Class Cmf
Expand All @@ -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;
}
Expand All @@ -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);
}
}

Expand All @@ -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();
}

/**
Expand Down
19 changes: 19 additions & 0 deletions src/Factories/ModuleFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace ReinVanOyen\Cmf\Factories;

use ReinVanOyen\Cmf\Module;

class ModuleFactory
{
public static function make($module)
{
if (is_string($module)) {
return app($module);
}
if ($module instanceof Module) {
return $module;
}
return null;
}
}
22 changes: 4 additions & 18 deletions src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Str;
use ReinVanOyen\Cmf\Action\Action;
use ReinVanOyen\Cmf\Factories\ModuleFactory;
use ReinVanOyen\Cmf\Traits\CanExport;

abstract class Module implements \JsonSerializable
Expand Down Expand Up @@ -52,23 +53,6 @@ public function submodules(): array
return [];
}

/**
* @param array $modules
* @return array
*/
final public static function makeModules(array $modules): array
{
return array_map(function ($module) {
if (is_string($module)) {
return app($module);
}
if ($module instanceof Module) {
return $module;
}
return null;
}, $modules);
}

/**
* @return array
*/
Expand All @@ -82,7 +66,9 @@ public function exportAll(): array
'module' => $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;
}
Expand Down
60 changes: 60 additions & 0 deletions src/ModuleRegistry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace ReinVanOyen\Cmf;

use ReinVanOyen\Cmf\Factories\ModuleFactory;

class ModuleRegistry
{
/**
* @var array $modules
*/
private $modules;

/**
* @var array $modulesMap
*/
private $modulesMap;

/**
* @param $module
* @param bool $isRoot
* @return \Illuminate\Contracts\Foundation\Application|mixed|null
*/
public function add($module, $isRoot = true)
{
$module = ModuleFactory::make($module);
$this->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);
}
}
32 changes: 31 additions & 1 deletion tests/CmfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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())
);
}
}
}
3 changes: 2 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,7 +29,7 @@ public function setUp(): void
protected function getPackageProviders($app)
{
return [
'ReinVanOyen\Cmf\CmfServiceProvider',
CmfServiceProvider::class,
];
}

Expand Down

0 comments on commit 74dea22

Please sign in to comment.