Skip to content

Commit

Permalink
feat: config command with build and clear cache config
Browse files Browse the repository at this point in the history
  • Loading branch information
SonyPradana committed May 25, 2024
1 parent 8a0aea6 commit f81b64e
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 0 deletions.
77 changes: 77 additions & 0 deletions src/System/Integrate/Console/ConfigCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

namespace System\Integrate\Console;

use System\Console\Command;
use System\Integrate\Application;
use System\Integrate\Bootstrap\ConfigProviders;
use System\Integrate\ConfigRepository;

use function System\Console\fail;
use function System\Console\ok;

class ConfigCommand extends Command
{
/**
* Register command.
*
* @var array<int, array<string, mixed>>
*/
public static array $command = [
[
'pattern' => 'config:cache',
'fn' => [self::class, 'main'],
], [
'pattern' => 'config:clear',
'fn' => [self::class, 'clear'],
],
];

/**
* @return array<string, array<string, string|string[]>>
*/
public function printHelp()
{
return [
'commands' => [
'cron' => 'Run cron job (all shadule)',
'cron:work' => 'Run virtual cron job in terminal (ansync)',
'cron:list' => 'Get list of shadule',
],
'options' => [],
'relation' => [],
];
}

public function main(): int
{
$app = Application::getIntance();
(new ConfigProviders())->bootstrap($app);

$this->clear();
$config = $app->get(ConfigRepository::class)->toArray();
$cached_config = '<?php return ' . var_export($config, true) . ';' . PHP_EOL;
if (file_put_contents($app->getApplicationCachePath() . 'config.php', $cached_config)) {
ok('Config file has successfully created.')->out();

return 0;
}
fail('Cant build config cache.')->out();

return 1;
}

public function clear(): int
{
if (file_exists($file = Application::getIntance()->getApplicationCachePath() . 'config.php')) {
@unlink($file);
ok('Clear config file has successfully.')->out();

return 0;
}

return 1;
}
}
64 changes: 64 additions & 0 deletions tests/Integrate/Commands/ConfigCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace System\Integrate\Commands;

use PHPUnit\Framework\TestCase;
use System\Integrate\Application;
use System\Integrate\Console\ConfigCommand;

class ConfigCommandTest extends TestCase
{
protected function tearDown(): void
{
// tests\Integrate\bootsrap\cache\config.php
if (file_exists($file = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'bootsrap' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . 'config.php')) {
@unlink($file);
}
}

/**
* @test
*/
public function itCanCreateConfigFile()
{
$app = new Application(dirname(__DIR__) . DIRECTORY_SEPARATOR);

$app->setConfigPath(DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'Config' . DIRECTORY_SEPARATOR);

$command = new ConfigCommand([]);

ob_start();
$status = $command->main();
$out = ob_get_clean();

$this->assertEquals(0, $status);
// $this->assertStringContainsString('Clear config file has successfully', $out);
$this->assertStringContainsString('Config file has successfully created.', $out);

$app->flush();
}

/**
* @test
*/
public function itCanRemoveConfigFile()
{
$app = new Application(dirname(__DIR__) . DIRECTORY_SEPARATOR);

$app->setConfigPath(DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'Config' . DIRECTORY_SEPARATOR);

$command = new ConfigCommand([]);

ob_start();
$command->main();
$status = $command->clear();
$out = ob_get_clean();

$this->assertEquals(0, $status);
$this->assertStringContainsString('Config file has successfully created.', $out);

$app->flush();
}
}
1 change: 1 addition & 0 deletions tests/Integrate/bootsrap/cache/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config.php

0 comments on commit f81b64e

Please sign in to comment.