Skip to content

Commit 4554cca

Browse files
committed
feat: Added Support For Args in Add SMTP Conn
1 parent 8223428 commit 4554cca

2 files changed

Lines changed: 134 additions & 9 deletions

File tree

WebFiori/Framework/Cli/Commands/AddSmtpConnectionCommand.php

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111
namespace WebFiori\Framework\Cli\Commands;
1212

13+
use WebFiori\Cli\Argument;
1314
use WebFiori\Cli\Command;
1415
use WebFiori\Mail\Exceptions\SMTPException;
1516
use WebFiori\Mail\SMTPAccount;
@@ -24,7 +25,17 @@
2425
*/
2526
class AddSmtpConnectionCommand extends Command {
2627
public function __construct() {
27-
parent::__construct('add:smtp-connection', [], 'Add an SMTP account.');
28+
parent::__construct('add:smtp-connection', [
29+
new Argument('--host', 'The address of SMTP server host.', true),
30+
new Argument('--port', 'Port number of the SMTP server.', true),
31+
new Argument('--user', 'The username to use when connecting to the server.', true),
32+
new Argument('--password', 'The password to use when connecting to the server.', true),
33+
new Argument('--sender-address', 'The email address that will appear as sender.', true),
34+
new Argument('--sender-name', 'The name that will appear as sender.', true),
35+
new Argument('--name', 'A friendly name to identify the connection.', true),
36+
new Argument('--oauth-token', 'OAuth access token to use for authentication instead of password.', true),
37+
new Argument('--no-check', 'If provided, the connection will be added without the attempt to check if provided credentials are valid.', true),
38+
], 'Add an SMTP account.');
2839
}
2940
/**
3041
* Execute the command.
@@ -33,9 +44,12 @@ public function __construct() {
3344
*/
3445
public function exec() : int {
3546
$smtpConn = new SMTPAccount();
36-
$smtpConn->setServerAddress($this->getInput('SMTP Server address:', '127.0.0.1'));
47+
48+
$hostArg = $this->getArgValue('--host');
49+
$smtpConn->setServerAddress($hostArg !== null ? $hostArg : $this->getInput('SMTP Server address:', '127.0.0.1'));
50+
3751
$smtpConn->setPort(25);
38-
$addr = $smtpConn->getAddress();
52+
$addr = $smtpConn->getServerAddress();
3953

4054
if ($addr == 'smtp.outlook.com'
4155
|| $addr == 'outlook.office365.com'
@@ -45,12 +59,39 @@ public function exec() : int {
4559
|| $addr == 'smtp.mail.yahoo.com') {
4660
$smtpConn->setPort(465);
4761
}
48-
$smtpConn->setPort($this->getInput('Port number:', $smtpConn->getPort()));
49-
$smtpConn->setUsername($this->getInput('Username:'));
50-
$smtpConn->setPassword($this->getMaskedInput('Password:'));
51-
$smtpConn->setAddress($this->getInput('Sender email address:', $smtpConn->getUsername()));
52-
$smtpConn->setSenderName($this->getInput('Sender name:', $smtpConn->getAddress()));
53-
$smtpConn->setAccountName($this->getInput('Give your connection a friendly name:', 'smtp-connection-'.count(App::getConfig()->getSMTPConnections())));
62+
63+
$portArg = $this->getArgValue('--port');
64+
$smtpConn->setPort($portArg !== null ? (int) $portArg : $this->getInput('Port number:', $smtpConn->getPort()));
65+
66+
$userArg = $this->getArgValue('--user');
67+
$smtpConn->setUsername($userArg !== null ? $userArg : $this->getInput('Username:'));
68+
69+
$passArg = $this->getArgValue('--password');
70+
$smtpConn->setPassword($passArg !== null ? $passArg : $this->getMaskedInput('Password:'));
71+
72+
$senderAddrArg = $this->getArgValue('--sender-address');
73+
$smtpConn->setAddress($senderAddrArg !== null ? $senderAddrArg : $this->getInput('Sender email address:', $smtpConn->getUsername()));
74+
75+
$senderNameArg = $this->getArgValue('--sender-name');
76+
$smtpConn->setSenderName($senderNameArg !== null ? $senderNameArg : $this->getInput('Sender name:', $smtpConn->getAddress()));
77+
78+
$defaultName = 'smtp-connection-'.count(App::getConfig()->getSMTPConnections());
79+
$nameArg = $this->getArgValue('--name');
80+
$smtpConn->setAccountName($nameArg !== null ? $nameArg : $this->getInput('Give your connection a friendly name:', $defaultName));
81+
82+
$tokenArg = $this->getArgValue('--oauth-token');
83+
84+
if ($tokenArg !== null) {
85+
$smtpConn->setAccessToken($tokenArg);
86+
}
87+
88+
if ($this->isArgProvided('--no-check')) {
89+
App::getConfig()->addOrUpdateSMTPAccount($smtpConn);
90+
$this->success('Connection information was stored in application configuration.');
91+
92+
return 0;
93+
}
94+
5495
$this->println('Trying to connect. This can take up to 1 minute...');
5596
$server = new SMTPServer($smtpConn->getServerAddress(), $smtpConn->getPort());
5697

tests/WebFiori/Framework/Tests/Cli/AddSmtpConnectionCommandTest.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,88 @@ public function testAddSMTPConnection00() {
4848
"Would you like to store connection information anyway?(y/N)\n",
4949
], $output);
5050
}
51+
/**
52+
* @test
53+
* Tests that all args bypass interactive prompts and --no-check skips connection attempt.
54+
*/
55+
public function testAddSMTPConnection01() {
56+
$connName = 'my-smtp-conn-'.time();
57+
$countBefore = count(App::getConfig()->getSMTPConnections());
58+
59+
$output = $this->executeSingleCommand(new AddSmtpConnectionCommand(), [
60+
'--host=smtp.example.com',
61+
'--port=587',
62+
'--user=user@example.com',
63+
'--password=secret',
64+
'--sender-address=user@example.com',
65+
'--sender-name=Test User',
66+
'--name='.$connName,
67+
'--no-check',
68+
], []);
69+
70+
$this->assertEquals(0, $this->getExitCode());
71+
$this->assertEquals([
72+
"Success: Connection information was stored in application configuration.\n"
73+
], $output);
74+
75+
$connections = App::getConfig()->getSMTPConnections();
76+
$this->assertCount($countBefore + 1, $connections);
77+
$this->assertArrayHasKey($connName, $connections);
78+
$conn = $connections[$connName];
79+
$this->assertEquals('smtp.example.com', $conn->getServerAddress());
80+
$this->assertEquals(587, $conn->getPort());
81+
$this->assertEquals('user@example.com', $conn->getUsername());
82+
$this->assertEquals('user@example.com', $conn->getAddress());
83+
$this->assertEquals('Test User', $conn->getSenderName());
84+
}
85+
/**
86+
* @test
87+
* Tests that --oauth-token is stored on the connection.
88+
*/
89+
public function testAddSMTPConnection02() {
90+
$connName = 'oauth-smtp-conn-'.time();
91+
$token = 'my-oauth-token-xyz';
92+
93+
$output = $this->executeSingleCommand(new AddSmtpConnectionCommand(), [
94+
'--host=smtp.example.com',
95+
'--port=587',
96+
'--user=user@example.com',
97+
'--password=secret',
98+
'--sender-address=user@example.com',
99+
'--sender-name=Test User',
100+
'--name='.$connName,
101+
'--oauth-token='.$token,
102+
'--no-check',
103+
], []);
104+
105+
$this->assertEquals(0, $this->getExitCode());
106+
$connections = App::getConfig()->getSMTPConnections();
107+
$this->assertArrayHasKey($connName, $connections);
108+
$this->assertEquals($token, $connections[$connName]->getAccessToken());
109+
}
110+
/**
111+
* @test
112+
* Tests that providing some args still prompts for the missing ones.
113+
*/
114+
public function testAddSMTPConnection03() {
115+
$connName = 'smtp-connection-'.(count(App::getConfig()->getSMTPConnections()));
116+
117+
$output = $this->executeSingleCommand(new AddSmtpConnectionCommand(), [
118+
'--host=127.0.0.1',
119+
'--port=25',
120+
'--user=test@example.com',
121+
'--password=secret',
122+
'--sender-address=test@example.com',
123+
'--sender-name=Test',
124+
], [
125+
"\n", // Hit Enter to pick default connection name
126+
'n'
127+
]);
128+
129+
$this->assertEquals(0, $this->getExitCode());
130+
$output = $this->getOutput();
131+
132+
$this->assertEquals("Give your connection a friendly name: Enter = '$connName'\n", $output[0]);
133+
$this->assertEquals("Trying to connect. This can take up to 1 minute...\n", $output[1]);
134+
}
51135
}

0 commit comments

Comments
 (0)