Skip to content

Commit

Permalink
refactor: Introduced new Way to Test CLI Commands
Browse files Browse the repository at this point in the history
  • Loading branch information
usernane committed Oct 16, 2024
1 parent a16a89e commit 1b7c2cd
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 148 deletions.
2 changes: 2 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
<file>./webfiori/framework/cli/commands/UpdateTableCommand.php</file>
<file>./webfiori/framework/cli/commands/VersionCommand.php</file>
<file>./webfiori/framework/cli/commands/WHelpCommand.php</file>
<file>./webfiori/framework/cli/CLITestCase.php</file>
<file>./webfiori/framework/cli/CLIUtils.php</file>

<file>./webfiori/framework/cli/helpers/ClassInfoReader.php</file>
<file>./webfiori/framework/cli/helpers/CreateBackgroundTask.php</file>
Expand Down
234 changes: 88 additions & 146 deletions tests/webfiori/framework/test/cli/CreateAPITestCaseTest.php
Original file line number Diff line number Diff line change
@@ -1,55 +1,32 @@
<?php
namespace webfiori\framework\test\cli;

use webfiori\framework\App;
use webfiori\framework\cli\CLITestCase;
use webfiori\framework\cli\commands\CreateCommand;
use webfiori\framework\scheduler\webServices\TasksServicesManager;
use webfiori\http\WebServicesManager;
/**
* @author Ibrahim
*/
class CreateAPITestCaseTest extends CreateTestCase {
class CreateAPITestCaseTest extends CLITestCase {
/**
* @test
*/
public function testCreateAPITestCase00() {
$runner = App::getRunner();
$runner->setArgsVector([
'webfiori',
'create',
$this->assertEquals([
"Error: The argument --manager has invalid value.\n",
], $this->executeMultiCommand([
CreateCommand::class,
'--c' => 'api-test',
'--manager' => 'A',
'--service' => 'c'
]);
$runner->setInputs([

]);
$exitCode = $runner->start();
$this->assertEquals(-1, $exitCode);
$this->assertEquals([
"Error: The argument --manager has invalid value.\n",

], $runner->getOutput());
// $this->assertTrue(class_exists('\\app\\commands\\NewCLICommand'));
// $this->removeClass('\\app\\commands\\NewCLICommand');
]));
$this->assertEquals(-1, $this->getExitCode());
}
/**
* @test
*/
public function testCreateAPITestCase01() {
$runner = App::getRunner();
$runner->setArgsVector([
'webfiori',
'create',
'--c' => 'api-test',
'--manager' => TasksServicesManager::class,
'--service' => 'c'
]);
$runner->setInputs([
"0",
"y"
]);
$exitCode = $runner->start();
$this->assertEquals(0, $exitCode);
$path = ROOT_PATH.DS."tests".DS."webfiori".DS."framework".DS."scheduler".DS."webServices";
$this->assertEquals([
"Info: Selected services manager has no service with name 'c'.\n",
Expand All @@ -65,57 +42,44 @@ public function testCreateAPITestCase01() {
"Path: ".$path."\n",
"Would you like to use default parameters?(Y/n)\n",
"Info: New class was created at \"".$path."\".\n"
], $runner->getOutput());
$clazz = '\tests\webfiori\\framework\scheduler\webServices\\TasksLoginServiceTest';
], $this->executeMultiCommand([
CreateCommand::class,
'--c' => 'api-test',
'--manager' => TasksServicesManager::class,
'--service' => 'c'
], [
"0",
"y"
]));

$this->assertEquals(0, $this->getExitCode());
$clazz = '\\tests\webfiori\\framework\scheduler\webServices\\TasksLoginServiceTest';
$this->assertTrue(class_exists($clazz));
$this->removeClass($clazz);
}
/**
* @test
*/
public function testCreateAPITestCase02() {
$runner = App::getRunner();
$runner->setArgsVector([
'webfiori',
'create',
$path = ROOT_PATH.DS."tests".DS."webfiori".DS."framework".DS."scheduler".DS."webServices";
$this->assertEquals([
"Info: New class was created at \"".$path."\".\n"
], $this->executeMultiCommand([
CreateCommand::class,
'--c' => 'api-test',
'--manager' => TasksServicesManager::class,
'--service' => 'get-tasks',
'--defaults'
]);
$runner->setInputs([
]);
$exitCode = $runner->start();
$this->assertEquals(0, $exitCode);
$path = ROOT_PATH.DS."tests".DS."webfiori".DS."framework".DS."scheduler".DS."webServices";
$this->assertEquals([
"Info: New class was created at \"".$path."\".\n"
], $runner->getOutput());
$clazz = '\tests\webfiori\\framework\scheduler\webServices\\GetTasksServiceTest';
]));
$this->assertEquals(0, $this->getExitCode());
$clazz = '\\tests\webfiori\\framework\scheduler\webServices\\GetTasksServiceTest';
$this->assertTrue(class_exists($clazz));
$this->removeClass($clazz);
}
/**
* @test
*/
public function testCreateAPITestCase03() {
$runner = App::getRunner();
$runner->setArgsVector([
'webfiori',
'create',
'--c' => 'api-test',
'--service' => 'get-tasks',
]);
$runner->setInputs([
'\webfiori\\framework\scheduler\webServices\\TasksServicesManager',
'n',
'10',
'',
'',

]);
$exitCode = $runner->start();
$this->assertEquals(0, $exitCode);
$path = ROOT_PATH.DS."tests".DS."webfiori".DS."framework".DS."scheduler".DS."webServices";
$this->assertEquals([
"Please enter services manager information:\n",
Expand All @@ -128,34 +92,27 @@ public function testCreateAPITestCase03() {
"Enter a name for the new class:\n",
"Enter an optional namespace for the class: Enter = 'tests\webfiori\\framework\scheduler\webServices'\n",
"Info: New class was created at \"".$path."\".\n"
], $runner->getOutput());
$clazz = '\tests\webfiori\\framework\scheduler\webServices\\GetTasksServiceTest';
], $this->executeMultiCommand([
CreateCommand::class,
'--c' => 'api-test',
'--service' => 'get-tasks',
], [
'\webfiori\\framework\scheduler\webServices\\TasksServicesManager',
'n',
'10',
'',
'',
]));
$this->assertEquals(0, $this->getExitCode());

$clazz = '\\tests\webfiori\\framework\scheduler\webServices\\GetTasksServiceTest';
$this->assertTrue(class_exists($clazz));
$this->removeClass($clazz);
}
/**
* @test
*/
public function testCreateAPITestCase04() {
$runner = App::getRunner();


$runner->setArgsVector([
'webfiori',
'create',
'--c' => 'api-test',
'--service' => 'say-hi-service',
]);
$runner->setInputs([
'\\tests\\apis\\multiple\\ServicesManager00',
'n',
'10',
'',
'',

]);
$exitCode = $runner->start();
$this->assertEquals(0, $exitCode);
$path = ROOT_PATH.DS."tests".DS."tests".DS."apis".DS."multiple";
$this->assertEquals([
"Please enter services manager information:\n",
Expand All @@ -168,7 +125,19 @@ public function testCreateAPITestCase04() {
"Enter a name for the new class:\n",
"Enter an optional namespace for the class: Enter = 'tests\\tests\apis\multiple'\n",
"Info: New class was created at \"".$path."\".\n"
], $runner->getOutput());
], $this->executeMultiCommand([
CreateCommand::class,
'--c' => 'api-test',
'--service' => 'say-hi-service',
], [
'\\tests\\apis\\multiple\\ServicesManager00',
'n',
'10',
'',
'',
]));
$this->assertEquals(0, $this->getExitCode());

$clazz = '\\tests\\tests\\apis\\multiple\\WebService00Test';
$this->assertTrue(class_exists($clazz));
$this->removeClass($clazz);
Expand All @@ -178,46 +147,19 @@ public function testCreateAPITestCase04() {
* @test
*/
public function testCreateAPITestCase05() {
$runner = App::getRunner();
$runner->setArgsVector([
'webfiori',
'create',
'--c' => 'api-test',
'--manager' => '\\tests\\apis\\emptyService\\EmptyServicesManager',
]);
$runner->setInputs();

$exitCode = $runner->start();
$this->assertEquals(-1, $exitCode);

$this->assertEquals([
"Info: Provided services manager has 0 registered services.\n",
], $runner->getOutput());
], $this->executeMultiCommand([
CreateCommand::class,
'--c' => 'api-test',
'--manager' => '\\tests\\apis\\emptyService\\EmptyServicesManager',
]));
$this->assertEquals(-1, $this->getExitCode());
}
/**
* @test
*/
public function testCreateAPITestCase06() {
$runner = App::getRunner();


$runner->setArgsVector([
'webfiori',
'create',
'--c' => 'api-test',
'--service' => 'say-hi-service',
]);
$runner->setInputs([
'\\tests\\apis\\multiple\\WebService00',
'\\tests\\apis\\multiple\\ServicesManager00',
'n',
'10',
'',
'',

]);
$exitCode = $runner->start();
$this->assertEquals(0, $exitCode);
$path = ROOT_PATH.DS."tests".DS."tests".DS."apis".DS."multiple";
$this->assertEquals([
"Please enter services manager information:\n",
Expand All @@ -232,7 +174,19 @@ public function testCreateAPITestCase06() {
"Enter a name for the new class:\n",
"Enter an optional namespace for the class: Enter = 'tests\\tests\apis\multiple'\n",
"Info: New class was created at \"".$path."\".\n"
], $runner->getOutput());
], $this->executeMultiCommand([
CreateCommand::class,
'--c' => 'api-test',
'--service' => 'say-hi-service',
], [
'\\tests\\apis\\multiple\\WebService00',
'\\tests\\apis\\multiple\\ServicesManager00',
'n',
'10',
'',
'',
]));
$this->assertEquals(0, $this->getExitCode());
$clazz = '\\tests\\tests\\apis\\multiple\\WebService00Test';
$this->assertTrue(class_exists($clazz));
$this->removeClass($clazz);
Expand All @@ -241,42 +195,30 @@ public function testCreateAPITestCase06() {
* @test
*/
public function testCreateAPITestCase07() {
$runner = App::getRunner();
$runner->setArgsVector([
'webfiori',
'create',
'--c' => 'api-test',
'--manager' => '\\tests\\apis\\emptyService\\Xyz',
]);
$runner->setInputs();

$exitCode = $runner->start();
$this->assertEquals(-1, $exitCode);

$this->assertEquals([
"Error: The argument --manager has invalid value.\n",
], $runner->getOutput());
], $this->executeMultiCommand([
CreateCommand::class,
'--c' => 'api-test',
'--manager' => '\\tests\\apis\\emptyService\\Xyz',
]));
$this->assertEquals(-1, $this->getExitCode());
}
/**
* @test
*/
public function testCreateAPITestCase08() {
$runner = App::getRunner();
$runner->setArgsVector([
'webfiori',
'create',
$path = ROOT_PATH.DS."tests".DS."tests".DS."apis".DS."multiple";
$this->assertEquals([
"Info: New class was created at \"".$path."\".\n"
], $this->executeMultiCommand([
CreateCommand::class,
'--c' => 'api-test',
'--service' => 'say-hi-service-2',
'--manager' => '\\tests\\apis\\multiple\\ServicesManager00',
'--defaults'
]);
$runner->setInputs();
$exitCode = $runner->start();
$this->assertEquals(0, $exitCode);
$path = ROOT_PATH.DS."tests".DS."tests".DS."apis".DS."multiple";
$this->assertEquals([
"Info: New class was created at \"".$path."\".\n"
], $runner->getOutput());
]));
$this->assertEquals(0, $this->getExitCode());
$clazz = '\\tests\\tests\\apis\\multiple\\WebService01Test';
$this->assertTrue(class_exists($clazz));
$this->removeClass($clazz);
Expand Down
4 changes: 2 additions & 2 deletions tests/webfiori/framework/test/session/SessionsManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,8 @@ public function testInitSessionsDb() {
SessionsManager::reset();
$sto = new DatabaseSessionStorage();
$sto->getController()->createTables()->execute();
$sto->getController()->table('session_data')->select()->execute();
$sto->getController()->table('sessions')->select()->execute();
// $sto->getController()->table('session_data')->select()->execute();
// $sto->getController()->table('sessions')->select()->execute();
$this->assertTrue(true);
}
/**
Expand Down
Loading

0 comments on commit 1b7c2cd

Please sign in to comment.