Skip to content

Commit

Permalink
test: Added More Test Cases
Browse files Browse the repository at this point in the history
  • Loading branch information
usernane committed Jul 1, 2024
1 parent afd7e2c commit 2f872fe
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace webfiori\framework\test\writers;

use PHPUnit\Framework\TestCase;
use webfiori\cli\CLICommand;
use webfiori\framework\writers\CLICommandClassWriter;

class CLICommandClassWriterTest extends TestCase {
Expand Down Expand Up @@ -41,11 +42,29 @@ public function test01() {
$this->assertEquals('Lets-Do-It', $writer->getCommandName());
$this->assertEquals('', $writer->getDescription());
$this->assertEquals([], $writer->getArgs());
$writer->setArgs([
new \webfiori\cli\Argument('--do', 'A do arg', true)
]);
$this->assertEquals('', $writer->getDescription());
$writer->setCommandDescription('Random desc');
$this->assertEquals('Random desc', $writer->getDescription());
$this->assertEquals([
'webfiori\cli\CLICommand'
], $writer->getUseStatements());
$writer->writeClass();
$this->assertTrue(class_exists($writer->getNamespace().'\\'.$writer->getName()));
$clazz = $writer->getNamespace().'\\'.$writer->getName();
$this->assertTrue(class_exists($clazz));
$writer->removeClass();
$clazzObj = new $clazz();
$this->assertTrue($clazzObj instanceof CLICommand);
$this->assertEquals('Lets-Do-It', $clazzObj->getName());
$this->assertEquals('Random desc', $clazzObj->getDescription());
$this->assertEquals([
'--do'
], $clazzObj->getArgsNames());
$arg = $clazzObj->getArg('--do');
$this->assertTrue($arg instanceof \webfiori\cli\Argument);
$this->assertEquals('A do arg', $arg->getDescription());
$this->assertTrue($arg->isOptional());
}
}
23 changes: 12 additions & 11 deletions webfiori/framework/writers/CLICommandClassWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ public function writeClassComment() {
$topArr[] = ' * In addition, the command have the following args:';
$topArr[] = ' * <ul>';

foreach ($this->args as $argArr) {
$topArr[] = " * <li>".$argArr['name']."</li>";
foreach ($this->args as $argObj) {
$topArr[] = " * <li>".$argObj->getName()."</li>";
}
$topArr[] = ' * </ul>';
}
Expand All @@ -180,18 +180,19 @@ private function writeConstructor() {
if (count($this->args) > 0) {
$this->append(["parent::__construct('$this->name', ["], 2);

foreach ($this->args as $argArr) {
$this->append("'".$argArr['name']."' => [", 3);

if (strlen($argArr['description']) != 0) {
$this->append("'description' => '".str_replace("'", "\'", $argArr['description'])."',", 4);
foreach ($this->args as $argObj) {

$this->append("'".$argObj->getName()."' => [", 3);

if (strlen($argObj->getDescription()) != 0) {
$this->append("'description' => '".str_replace("'", "\'", $argObj->getDescription())."',", 4);
}
$this->append("'optional' => ".($argArr['optional'] === true ? 'true' : 'false').",", 4);
$this->append("'optional' => ".($argObj->isOptional() ? 'true' : 'false').",", 4);

if (count($argArr['values']) != 0) {
if (count($argObj->getAllowedValues()) != 0) {
$this->append("'values' => [", 4);

foreach ($argArr['values'] as $val) {
foreach ($argObj->getAllowedValues() as $val) {
$this->append("'".str_replace("'", "\'", $val)."',", 5);
}
$this->append("]", 4);
Expand All @@ -200,7 +201,7 @@ private function writeConstructor() {
}
$this->append("], '".str_replace("'", "\'", $this->desc)."');", 2);
} else {
$this->append("parent::__construct('$this->name', '".str_replace("'", "\'", $this->desc)."');", 2);
$this->append("parent::__construct('$this->name', [], '".str_replace("'", "\'", $this->desc)."');", 2);
}

$this->append('}', 1);
Expand Down

0 comments on commit 2f872fe

Please sign in to comment.