Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Renamed File app.php to main.php #23

Merged
merged 14 commits into from
Apr 29, 2024
9 changes: 5 additions & 4 deletions bin/InitAppCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function exec(): int {
}

try {
$this->println('Creating new app at "'.$appPath.'" ...');
$this->createAppClass($appPath, $dirName);
$this->createEntryPoint($appPath, $dirName, $entry);
$this->success('App created successfully.');
Expand All @@ -44,8 +45,8 @@ public function exec(): int {
}
}
private function createAppClass(string $appPath, string $dirName) {
$this->println('Creating "'.$dirName.'/app.php"...');
$file = new File($appPath.DIRECTORY_SEPARATOR.'app.php');
$this->println('Creating "'.$dirName.'/main.php"...');
$file = new File($appPath.DIRECTORY_SEPARATOR.'main.php');

if (!$file->isExist()) {
$file->append("<?php\n\n");
Expand All @@ -67,7 +68,7 @@ private function createAppClass(string $appPath, string $dirName) {

return true;
}
$this->warning('File app.php already exist!');
$this->warning('File main.php already exist!');
}
private function createEntryPoint(string $appPath, string $dir, string $eName) {
$this->println('Creating "'.$dir.'/'.$eName.'"...');
Expand All @@ -76,7 +77,7 @@ private function createEntryPoint(string $appPath, string $dir, string $eName) {
if (!$file->isExist()) {
$data = "#!/usr/bin/env php\n"
."<?php\n"
."require \"app.php\";\n\n";
."require \"main.php\";\n\n";
$file->create(true);
file_put_contents($file->getDir().DIRECTORY_SEPARATOR.$eName, $data);

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion bin/wfc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env php
<?php
require "app.php";
require "main.php";
75 changes: 10 additions & 65 deletions example/tests/HelloCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,83 +4,28 @@
require_once(realpath(dirname(__FILE__).'\\..\\..\\vendor\\autoload.php'));
require_once(realpath(dirname(__FILE__).'\\..\\app\\HelloWorldCommand.php'));

use webfiori\cli\CommandTestCase;

use webfiori\cli\Runner;
use PHPUnit\Framework\TestCase;
use webfiori\cli\commands\HelpCommand;

class HelloCommandTest extends TestCase {
class HelloCommandTest extends CommandTestCase {
/**
* @test
*/
public function test00() {
$runner = new Runner();

//Register the command that will be tested.
$runner->register(new HelloWorldCommand());

//Set arguments vector
$runner->setArgsVector([
'app.php',//First argument is always name of entry point.
//Can be set to anything since its testing env.
'hello'
]);

//Set user inputs.
//Must be called to use Array as input and output stream even if there are no inputs.
$runner->setInputs();

//Start the process
$exitStatus = $runner->start();

//Verify test results
$this->assertEquals(0, $exitStatus);
//A basic test case without using arg vector or user inputs
$this->assertEquals([
"Hello World!\n"
], $runner->getOutput());
"Hello World!".self::NL
], $this->executeSingleCommand([new HelloWorldCommand()]));
}
/**
* @test
*/
public function test01() {
$runner = new Runner();
$runner->register(new HelloWorldCommand());

$runner->setArgsVector([
'app.php',
'hello',
'--person-name' => 'Ibrahim BinAlshikh'
]);
$runner->setInputs();
$exitStatus = $runner->start();
$this->assertEquals(0, $exitStatus);
$this->assertEquals([
"Hello Ibrahim BinAlshikh!\n"
], $runner->getOutput());
}
/**
* @test
*/
public function test03() {
$runner = new Runner();
$runner->register(new HelpCommand());
$runner->register(new HelloWorldCommand());
$runner->setDefaultCommand('help');
$runner->setArgsVector([
'app.php',
]);
$runner->setInputs();
$exitStatus = $runner->start();
$this->assertEquals(0, $exitStatus);
//A test case that uses arg vector
$this->assertEquals([
"Usage:\n",
" command [arg1 arg2=\"val\" arg3...]\n\n",
"Global Arguments:\n",
" --ansi:[Optional] Force the use of ANSI output.\n",
"Available Commands:\n",
" help: Display CLI Help. To display help for specific command, use the argument \"--command-name\" with this command.\n",
" hello: A command to show greetings.\n"
], $runner->getOutput());
"Hello Ibrahim BinAlshikh!\n".self::NL
], $this->executeSingleCommand(new HelloWorldCommand(), [
'--person-name' => 'Ibrahim BinAlshikh'
]));
}
}

1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<file>./webfiori/cli/Formatter.php</file>
<file>./webfiori/cli/KeysMap.php</file>
<file>./webfiori/cli/Runner.php</file>
<file>./webfiori/cli/CommandTestCase.php</file>
<file>./webfiori/cli/InputValidator.php</file>
<file>./webfiori/cli/streams/ArrayInputStream.php</file>
<file>./webfiori/cli/streams/ArrayOutputStream.php</file>
Expand Down
1 change: 1 addition & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
require_once $baseDir.DS.'Runner.php';
require_once $baseDir.DS.'Option.php';
require_once $baseDir.DS.'InputValidator.php';
require_once $baseDir.DS.'CommandTestCase.php';
require_once $baseDir.DS.'streams'.DS.'ArrayInputStream.php';
require_once $baseDir.DS.'streams'.DS.'ArrayOutputStream.php';
require_once $baseDir.DS.'streams'.DS.'FileInputStream.php';
Expand Down
2 changes: 1 addition & 1 deletion tests/webfiori/tests/cli/CLICommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use webfiori\tests\cli\TestCommand;
use webfiori\tests\TestStudent;

class CLICommandTest extends TestCase {
class CLICommandTestCase extends TestCase {
/**
* @test
*/
Expand Down
32 changes: 19 additions & 13 deletions tests/webfiori/tests/cli/InitAppCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function test00() {
$r->setDefaultCommand('init');
$r->setInputs([]);
$r->setArgsVector([
'app.php',
'main.php',
'init'
]);
$this->assertEquals(-1, $r->start());
Expand All @@ -37,7 +37,7 @@ public function test01() {
$r->setDefaultCommand('init');
$r->setInputs([]);
$r->setArgsVector([
'app.php',
'main.php',
'init',
'--dir' => "test\0a"
]);
Expand All @@ -53,13 +53,15 @@ public function test02() {
->setDefaultCommand('init')
->setInputs([])
->setArgsVector([
'app.php',
'main.php',
'init',
'--dir' => 'test'
]);
$appPath = ROOT_DIR.DS.'test';
$this->assertEquals(0, $r->start());
$this->assertEquals([
"Creating \"test/app.php\"...\n",
"Creating new app at \"$appPath\" ...\n",
"Creating \"test/main.php\"...\n",
"Creating \"test/test\"...\n",
"Success: App created successfully.\n"
], $r->getOutput());
Expand All @@ -74,19 +76,21 @@ public function test03() {
$r->setDefaultCommand('init');
$r->setInputs([]);
$r->setArgsVector([
'app.php',
'main.php',
'init',
'--dir' => 'test'
]);
$this->assertEquals(0, $r->start());
$appPath = ROOT_DIR.DS.'test';
$this->assertEquals([
"Creating \"test/app.php\"...\n",
"Warning: File app.php already exist!\n",
"Creating new app at \"$appPath\" ...\n",
"Creating \"test/main.php\"...\n",
"Warning: File main.php already exist!\n",
"Creating \"test/test\"...\n",
"Warning: File test already exist!\n",
"Success: App created successfully.\n"
], $r->getOutput());
unlink(ROOT_DIR.DS.'test'.DS.'app.php');
unlink(ROOT_DIR.DS.'test'.DS.'main.php');
unlink(ROOT_DIR.DS.'test'.DS.'test');
rmdir(ROOT_DIR.DS.'test');
}
Expand All @@ -99,20 +103,22 @@ public function test04() {
$r->setDefaultCommand('init');
$r->setInputs([]);
$r->setArgsVector([
'app.php',
'main.php',
'init',
'--dir' => 'test2',
'--entry' => 'bang'
]);
$this->assertEquals(0, $r->start());
$appPath = ROOT_DIR.DS.'test2';
$this->assertEquals([
"Creating \"test2/app.php\"...\n",
"Creating new app at \"$appPath\" ...\n",
"Creating \"test2/main.php\"...\n",
"Creating \"test2/bang\"...\n",
"Success: App created successfully.\n"
], $r->getOutput());
unlink(ROOT_DIR.DS.'test2'.DS.'app.php');
unlink(ROOT_DIR.DS.'test2'.DS.'bang');
rmdir(ROOT_DIR.DS.'test2');
unlink($appPath.DS.'main.php');
unlink($appPath.DS.'bang');
rmdir($appPath);
}
}

Expand Down
54 changes: 23 additions & 31 deletions tests/webfiori/tests/cli/RunnerTest.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
<?php
namespace webfiori\tests\cli;

use webfiori\cli\Argument;
use webfiori\cli\commands\HelpCommand;
use webfiori\cli\CommandTestCase;
use webfiori\cli\Runner;
use webfiori\cli\streams\ArrayInputStream;
use webfiori\cli\streams\ArrayOutputStream;
use webfiori\cli\streams\StdIn;
use webfiori\cli\streams\StdOut;
use webfiori\cli\Runner;
use PHPUnit\Framework\TestCase;
use webfiori\tests\cli\testCommands\Command00;
use webfiori\cli\commands\HelpCommand;
use webfiori\tests\cli\testCommands\WithExceptionCommand;
use webfiori\tests\cli\testCommands\Command01;
use webfiori\cli\Argument;
use webfiori\tests\cli\testCommands\WithExceptionCommand;
/**
* Description of RunnerTest
*
* @author Ibrahim
*/
class RunnerTest extends TestCase {
class RunnerTest extends CommandTestCase {
/**
* @test
*/
Expand Down Expand Up @@ -107,40 +107,31 @@ public function testRunner02() {
* @test
*/
public function testRunner03() {
$runner = new Runner();
$runner->register(new Command00());
$runner->setInputs([]);
$this->assertEquals(-1, $runner->runCommand(null, [
'super-hero',
'name' => 'Ok'
]));
$this->assertEquals(-1, $runner->getLastCommandExitStatus());
$this->assertEquals([
"Error: The following argument(s) have invalid values: 'name'\n",
"Info: Allowed values for the argument 'name':\n",
"Ibrahim\n",
"Ali\n"
], $runner->getOutput());
], $this->executeSingleCommand(new Command00(), [
'super-hero',
'name' => 'Ok'
]));
$this->assertEquals(-1, $this->getExitCode());
}
/**
* @test
*/
public function testRunner04() {
$runner = new Runner();
$runner->register(new Command00());
$runner->setInputs([]);
$this->assertEquals(-1, $runner->runCommand(null, [
'super-hero',
'name' => 'Ok',
'--ansi'
]));
$this->assertEquals(-1, $runner->getLastCommandExitStatus());
$this->assertEquals([
"\e[1;91mError: \e[0mThe following argument(s) have invalid values: 'name'\n",
"\e[1;34mInfo: \e[0mAllowed values for the argument 'name':\n",
"Ibrahim\n",
"Ali\n"
], $runner->getOutput());
], $this->executeSingleCommand(new Command00(), [
'name' => 'Ok',
'--ansi'
]));
$this->assertEquals(-1, $this->getExitCode());
}
/**
* @test
Expand All @@ -166,19 +157,20 @@ public function testRunner05() {
* @test
*/
public function testRunner06() {
$runner = new Runner();
$runner->register(new Command00());
$runner->setDefaultCommand('help');
$runner->setInputs([]);
$this->assertEquals(0, $runner->runCommand(new HelpCommand(), []));

$this->assertEquals([
"Usage:\n",
" command [arg1 arg2=\"val\" arg3...]\n\n",
"Global Arguments:\n",
" --ansi:[Optional] Force the use of ANSI output.\n",
"Available Commands:\n",
" super-hero: A command to display hero's name.\n",
], $runner->getOutput());
" help: Display CLI Help. To display help for specific command, use the argument \"--command-name\" with this command.\n"
], $this->executeMultiCommand([
new Command00(),
new HelpCommand()
], 'help'));
$this->assertEquals(0, $this->getExitCode());
}
/**
* @test
Expand Down
Loading
Loading