Skip to content

Commit 1b7c2cd

Browse files
committed
refactor: Introduced new Way to Test CLI Commands
1 parent a16a89e commit 1b7c2cd

File tree

4 files changed

+167
-148
lines changed

4 files changed

+167
-148
lines changed

phpunit.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
<file>./webfiori/framework/cli/commands/UpdateTableCommand.php</file>
4646
<file>./webfiori/framework/cli/commands/VersionCommand.php</file>
4747
<file>./webfiori/framework/cli/commands/WHelpCommand.php</file>
48+
<file>./webfiori/framework/cli/CLITestCase.php</file>
49+
<file>./webfiori/framework/cli/CLIUtils.php</file>
4850

4951
<file>./webfiori/framework/cli/helpers/ClassInfoReader.php</file>
5052
<file>./webfiori/framework/cli/helpers/CreateBackgroundTask.php</file>

tests/webfiori/framework/test/cli/CreateAPITestCaseTest.php

Lines changed: 88 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,32 @@
11
<?php
22
namespace webfiori\framework\test\cli;
33

4-
use webfiori\framework\App;
4+
use webfiori\framework\cli\CLITestCase;
5+
use webfiori\framework\cli\commands\CreateCommand;
56
use webfiori\framework\scheduler\webServices\TasksServicesManager;
67
use webfiori\http\WebServicesManager;
78
/**
89
* @author Ibrahim
910
*/
10-
class CreateAPITestCaseTest extends CreateTestCase {
11+
class CreateAPITestCaseTest extends CLITestCase {
1112
/**
1213
* @test
1314
*/
1415
public function testCreateAPITestCase00() {
15-
$runner = App::getRunner();
16-
$runner->setArgsVector([
17-
'webfiori',
18-
'create',
16+
$this->assertEquals([
17+
"Error: The argument --manager has invalid value.\n",
18+
], $this->executeMultiCommand([
19+
CreateCommand::class,
1920
'--c' => 'api-test',
2021
'--manager' => 'A',
2122
'--service' => 'c'
22-
]);
23-
$runner->setInputs([
24-
25-
]);
26-
$exitCode = $runner->start();
27-
$this->assertEquals(-1, $exitCode);
28-
$this->assertEquals([
29-
"Error: The argument --manager has invalid value.\n",
30-
31-
], $runner->getOutput());
32-
// $this->assertTrue(class_exists('\\app\\commands\\NewCLICommand'));
33-
// $this->removeClass('\\app\\commands\\NewCLICommand');
23+
]));
24+
$this->assertEquals(-1, $this->getExitCode());
3425
}
3526
/**
3627
* @test
3728
*/
3829
public function testCreateAPITestCase01() {
39-
$runner = App::getRunner();
40-
$runner->setArgsVector([
41-
'webfiori',
42-
'create',
43-
'--c' => 'api-test',
44-
'--manager' => TasksServicesManager::class,
45-
'--service' => 'c'
46-
]);
47-
$runner->setInputs([
48-
"0",
49-
"y"
50-
]);
51-
$exitCode = $runner->start();
52-
$this->assertEquals(0, $exitCode);
5330
$path = ROOT_PATH.DS."tests".DS."webfiori".DS."framework".DS."scheduler".DS."webServices";
5431
$this->assertEquals([
5532
"Info: Selected services manager has no service with name 'c'.\n",
@@ -65,57 +42,44 @@ public function testCreateAPITestCase01() {
6542
"Path: ".$path."\n",
6643
"Would you like to use default parameters?(Y/n)\n",
6744
"Info: New class was created at \"".$path."\".\n"
68-
], $runner->getOutput());
69-
$clazz = '\tests\webfiori\\framework\scheduler\webServices\\TasksLoginServiceTest';
45+
], $this->executeMultiCommand([
46+
CreateCommand::class,
47+
'--c' => 'api-test',
48+
'--manager' => TasksServicesManager::class,
49+
'--service' => 'c'
50+
], [
51+
"0",
52+
"y"
53+
]));
54+
55+
$this->assertEquals(0, $this->getExitCode());
56+
$clazz = '\\tests\webfiori\\framework\scheduler\webServices\\TasksLoginServiceTest';
7057
$this->assertTrue(class_exists($clazz));
7158
$this->removeClass($clazz);
7259
}
7360
/**
7461
* @test
7562
*/
7663
public function testCreateAPITestCase02() {
77-
$runner = App::getRunner();
78-
$runner->setArgsVector([
79-
'webfiori',
80-
'create',
64+
$path = ROOT_PATH.DS."tests".DS."webfiori".DS."framework".DS."scheduler".DS."webServices";
65+
$this->assertEquals([
66+
"Info: New class was created at \"".$path."\".\n"
67+
], $this->executeMultiCommand([
68+
CreateCommand::class,
8169
'--c' => 'api-test',
8270
'--manager' => TasksServicesManager::class,
8371
'--service' => 'get-tasks',
8472
'--defaults'
85-
]);
86-
$runner->setInputs([
87-
]);
88-
$exitCode = $runner->start();
89-
$this->assertEquals(0, $exitCode);
90-
$path = ROOT_PATH.DS."tests".DS."webfiori".DS."framework".DS."scheduler".DS."webServices";
91-
$this->assertEquals([
92-
"Info: New class was created at \"".$path."\".\n"
93-
], $runner->getOutput());
94-
$clazz = '\tests\webfiori\\framework\scheduler\webServices\\GetTasksServiceTest';
73+
]));
74+
$this->assertEquals(0, $this->getExitCode());
75+
$clazz = '\\tests\webfiori\\framework\scheduler\webServices\\GetTasksServiceTest';
9576
$this->assertTrue(class_exists($clazz));
9677
$this->removeClass($clazz);
9778
}
9879
/**
9980
* @test
10081
*/
10182
public function testCreateAPITestCase03() {
102-
$runner = App::getRunner();
103-
$runner->setArgsVector([
104-
'webfiori',
105-
'create',
106-
'--c' => 'api-test',
107-
'--service' => 'get-tasks',
108-
]);
109-
$runner->setInputs([
110-
'\webfiori\\framework\scheduler\webServices\\TasksServicesManager',
111-
'n',
112-
'10',
113-
'',
114-
'',
115-
116-
]);
117-
$exitCode = $runner->start();
118-
$this->assertEquals(0, $exitCode);
11983
$path = ROOT_PATH.DS."tests".DS."webfiori".DS."framework".DS."scheduler".DS."webServices";
12084
$this->assertEquals([
12185
"Please enter services manager information:\n",
@@ -128,34 +92,27 @@ public function testCreateAPITestCase03() {
12892
"Enter a name for the new class:\n",
12993
"Enter an optional namespace for the class: Enter = 'tests\webfiori\\framework\scheduler\webServices'\n",
13094
"Info: New class was created at \"".$path."\".\n"
131-
], $runner->getOutput());
132-
$clazz = '\tests\webfiori\\framework\scheduler\webServices\\GetTasksServiceTest';
95+
], $this->executeMultiCommand([
96+
CreateCommand::class,
97+
'--c' => 'api-test',
98+
'--service' => 'get-tasks',
99+
], [
100+
'\webfiori\\framework\scheduler\webServices\\TasksServicesManager',
101+
'n',
102+
'10',
103+
'',
104+
'',
105+
]));
106+
$this->assertEquals(0, $this->getExitCode());
107+
108+
$clazz = '\\tests\webfiori\\framework\scheduler\webServices\\GetTasksServiceTest';
133109
$this->assertTrue(class_exists($clazz));
134110
$this->removeClass($clazz);
135111
}
136112
/**
137113
* @test
138114
*/
139115
public function testCreateAPITestCase04() {
140-
$runner = App::getRunner();
141-
142-
143-
$runner->setArgsVector([
144-
'webfiori',
145-
'create',
146-
'--c' => 'api-test',
147-
'--service' => 'say-hi-service',
148-
]);
149-
$runner->setInputs([
150-
'\\tests\\apis\\multiple\\ServicesManager00',
151-
'n',
152-
'10',
153-
'',
154-
'',
155-
156-
]);
157-
$exitCode = $runner->start();
158-
$this->assertEquals(0, $exitCode);
159116
$path = ROOT_PATH.DS."tests".DS."tests".DS."apis".DS."multiple";
160117
$this->assertEquals([
161118
"Please enter services manager information:\n",
@@ -168,7 +125,19 @@ public function testCreateAPITestCase04() {
168125
"Enter a name for the new class:\n",
169126
"Enter an optional namespace for the class: Enter = 'tests\\tests\apis\multiple'\n",
170127
"Info: New class was created at \"".$path."\".\n"
171-
], $runner->getOutput());
128+
], $this->executeMultiCommand([
129+
CreateCommand::class,
130+
'--c' => 'api-test',
131+
'--service' => 'say-hi-service',
132+
], [
133+
'\\tests\\apis\\multiple\\ServicesManager00',
134+
'n',
135+
'10',
136+
'',
137+
'',
138+
]));
139+
$this->assertEquals(0, $this->getExitCode());
140+
172141
$clazz = '\\tests\\tests\\apis\\multiple\\WebService00Test';
173142
$this->assertTrue(class_exists($clazz));
174143
$this->removeClass($clazz);
@@ -178,46 +147,19 @@ public function testCreateAPITestCase04() {
178147
* @test
179148
*/
180149
public function testCreateAPITestCase05() {
181-
$runner = App::getRunner();
182-
$runner->setArgsVector([
183-
'webfiori',
184-
'create',
185-
'--c' => 'api-test',
186-
'--manager' => '\\tests\\apis\\emptyService\\EmptyServicesManager',
187-
]);
188-
$runner->setInputs();
189-
190-
$exitCode = $runner->start();
191-
$this->assertEquals(-1, $exitCode);
192-
193150
$this->assertEquals([
194151
"Info: Provided services manager has 0 registered services.\n",
195-
], $runner->getOutput());
152+
], $this->executeMultiCommand([
153+
CreateCommand::class,
154+
'--c' => 'api-test',
155+
'--manager' => '\\tests\\apis\\emptyService\\EmptyServicesManager',
156+
]));
157+
$this->assertEquals(-1, $this->getExitCode());
196158
}
197159
/**
198160
* @test
199161
*/
200162
public function testCreateAPITestCase06() {
201-
$runner = App::getRunner();
202-
203-
204-
$runner->setArgsVector([
205-
'webfiori',
206-
'create',
207-
'--c' => 'api-test',
208-
'--service' => 'say-hi-service',
209-
]);
210-
$runner->setInputs([
211-
'\\tests\\apis\\multiple\\WebService00',
212-
'\\tests\\apis\\multiple\\ServicesManager00',
213-
'n',
214-
'10',
215-
'',
216-
'',
217-
218-
]);
219-
$exitCode = $runner->start();
220-
$this->assertEquals(0, $exitCode);
221163
$path = ROOT_PATH.DS."tests".DS."tests".DS."apis".DS."multiple";
222164
$this->assertEquals([
223165
"Please enter services manager information:\n",
@@ -232,7 +174,19 @@ public function testCreateAPITestCase06() {
232174
"Enter a name for the new class:\n",
233175
"Enter an optional namespace for the class: Enter = 'tests\\tests\apis\multiple'\n",
234176
"Info: New class was created at \"".$path."\".\n"
235-
], $runner->getOutput());
177+
], $this->executeMultiCommand([
178+
CreateCommand::class,
179+
'--c' => 'api-test',
180+
'--service' => 'say-hi-service',
181+
], [
182+
'\\tests\\apis\\multiple\\WebService00',
183+
'\\tests\\apis\\multiple\\ServicesManager00',
184+
'n',
185+
'10',
186+
'',
187+
'',
188+
]));
189+
$this->assertEquals(0, $this->getExitCode());
236190
$clazz = '\\tests\\tests\\apis\\multiple\\WebService00Test';
237191
$this->assertTrue(class_exists($clazz));
238192
$this->removeClass($clazz);
@@ -241,42 +195,30 @@ public function testCreateAPITestCase06() {
241195
* @test
242196
*/
243197
public function testCreateAPITestCase07() {
244-
$runner = App::getRunner();
245-
$runner->setArgsVector([
246-
'webfiori',
247-
'create',
248-
'--c' => 'api-test',
249-
'--manager' => '\\tests\\apis\\emptyService\\Xyz',
250-
]);
251-
$runner->setInputs();
252-
253-
$exitCode = $runner->start();
254-
$this->assertEquals(-1, $exitCode);
255-
256198
$this->assertEquals([
257199
"Error: The argument --manager has invalid value.\n",
258-
], $runner->getOutput());
200+
], $this->executeMultiCommand([
201+
CreateCommand::class,
202+
'--c' => 'api-test',
203+
'--manager' => '\\tests\\apis\\emptyService\\Xyz',
204+
]));
205+
$this->assertEquals(-1, $this->getExitCode());
259206
}
260207
/**
261208
* @test
262209
*/
263210
public function testCreateAPITestCase08() {
264-
$runner = App::getRunner();
265-
$runner->setArgsVector([
266-
'webfiori',
267-
'create',
211+
$path = ROOT_PATH.DS."tests".DS."tests".DS."apis".DS."multiple";
212+
$this->assertEquals([
213+
"Info: New class was created at \"".$path."\".\n"
214+
], $this->executeMultiCommand([
215+
CreateCommand::class,
268216
'--c' => 'api-test',
269217
'--service' => 'say-hi-service-2',
270218
'--manager' => '\\tests\\apis\\multiple\\ServicesManager00',
271219
'--defaults'
272-
]);
273-
$runner->setInputs();
274-
$exitCode = $runner->start();
275-
$this->assertEquals(0, $exitCode);
276-
$path = ROOT_PATH.DS."tests".DS."tests".DS."apis".DS."multiple";
277-
$this->assertEquals([
278-
"Info: New class was created at \"".$path."\".\n"
279-
], $runner->getOutput());
220+
]));
221+
$this->assertEquals(0, $this->getExitCode());
280222
$clazz = '\\tests\\tests\\apis\\multiple\\WebService01Test';
281223
$this->assertTrue(class_exists($clazz));
282224
$this->removeClass($clazz);

tests/webfiori/framework/test/session/SessionsManagerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,8 @@ public function testInitSessionsDb() {
314314
SessionsManager::reset();
315315
$sto = new DatabaseSessionStorage();
316316
$sto->getController()->createTables()->execute();
317-
$sto->getController()->table('session_data')->select()->execute();
318-
$sto->getController()->table('sessions')->select()->execute();
317+
// $sto->getController()->table('session_data')->select()->execute();
318+
// $sto->getController()->table('sessions')->select()->execute();
319319
$this->assertTrue(true);
320320
}
321321
/**

0 commit comments

Comments
 (0)