Skip to content

Commit 6e5b0b8

Browse files
committed
Update command unit tests to use helper functions
1 parent 8df5d5b commit 6e5b0b8

File tree

13 files changed

+615
-100
lines changed

13 files changed

+615
-100
lines changed
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
<?php
2+
/*
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace Pterodactyl\Console\Commands\Environment;
26+
27+
use Illuminate\Console\Command;
28+
use Pterodactyl\Traits\Commands\EnvironmentWriterTrait;
29+
use Illuminate\Contracts\Config\Repository as ConfigRepository;
30+
31+
class EmailSettingsCommand extends Command
32+
{
33+
use EnvironmentWriterTrait;
34+
35+
/**
36+
* @var \Illuminate\Contracts\Config\Repository
37+
*/
38+
protected $config;
39+
40+
/**
41+
* @var string
42+
*/
43+
protected $description = 'Set or update the email sending configuration for the Panel.';
44+
45+
/**
46+
* @var string
47+
*/
48+
protected $signature = 'p:environment:mail
49+
{--driver= : The mail driver to use.}
50+
{--email= : Email address that messages from the Panel will originate from.}
51+
{--from= : The name emails from the Panel will appear to be from.}
52+
{--encryption=}
53+
{--host=}
54+
{--port=}
55+
{--username=}
56+
{--password=}';
57+
58+
/**
59+
* @var array
60+
*/
61+
protected $variables = [];
62+
63+
/**
64+
* EmailSettingsCommand constructor.
65+
*
66+
* @param \Illuminate\Contracts\Config\Repository $config
67+
*/
68+
public function __construct(ConfigRepository $config)
69+
{
70+
parent::__construct();
71+
72+
$this->config = $config;
73+
}
74+
75+
/**
76+
* Handle command execution.
77+
*/
78+
public function handle()
79+
{
80+
$this->variables['MAIL_DRIVER'] = $this->option('driver') ?? $this->choice(
81+
trans('command/messages.environment.mail.ask_driver'), [
82+
'smtp' => 'SMTP Server',
83+
'mail' => 'PHP\'s Internal Mail Function',
84+
'mailgun' => 'Mailgun Transactional Email',
85+
'mandrill' => 'Mandrill Transactional Email',
86+
'postmark' => 'Postmarkapp Transactional Email',
87+
], $this->config->get('mail.driver', 'smtp')
88+
);
89+
90+
$method = 'setup' . studly_case($this->variables['MAIL_DRIVER']) . 'DriverVariables';
91+
if (method_exists($this, $method)) {
92+
$this->{$method}();
93+
}
94+
95+
$this->variables['MAIL_FROM'] = $this->option('email') ?? $this->ask(
96+
trans('command/messages.environment.mail.ask_mail_from'), $this->config->get('mail.from.address')
97+
);
98+
99+
$this->variables['MAIL_FROM_NAME'] = $this->option('from') ?? $this->ask(
100+
trans('command/messages.environment.mail.ask_mail_name'), $this->config->get('mail.from.name')
101+
);
102+
103+
$this->variables['MAIL_ENCRYPTION'] = $this->option('encryption') ?? $this->choice(
104+
trans('command/messages.environment.mail.ask_encryption'), ['tls' => 'TLS', 'ssl' => 'SSL', '' => 'None'], $this->config->get('mail.encryption', 'tls')
105+
);
106+
107+
$this->writeToEnvironment($this->variables);
108+
109+
$this->line('Updating stored environment configuration file.');
110+
$this->call('config:cache');
111+
$this->line('');
112+
}
113+
114+
/**
115+
* Handle variables for SMTP driver.
116+
*/
117+
private function setupSmtpDriverVariables()
118+
{
119+
$this->variables['MAIL_HOST'] = $this->option('host') ?? $this->ask(
120+
trans('command/messages.environment.mail.ask_smtp_host'), $this->config->get('mail.host')
121+
);
122+
123+
$this->variables['MAIL_PORT'] = $this->option('port') ?? $this->ask(
124+
trans('command/messages.environment.mail.ask_smtp_port'), $this->config->get('mail.port')
125+
);
126+
127+
$this->variables['MAIL_USERNAME'] = $this->option('username') ?? $this->ask(
128+
trans('command/messages.environment.mail.ask_smtp_username'), $this->config->get('mail.username')
129+
);
130+
131+
$this->variables['MAIL_PASSWORD'] = $this->option('password') ?? $this->secret(
132+
trans('command/messages.environment.mail.ask_smtp_password')
133+
);
134+
}
135+
136+
/**
137+
* Handle variables for mailgun driver.
138+
*/
139+
private function setupMailgunDriverVariables()
140+
{
141+
$this->variables['MAILGUN_DOMAIN'] = $this->option('host') ?? $this->ask(
142+
trans('command/messages.environment.mail.ask_mailgun_domain'), $this->config->get('services.mailgun.domain')
143+
);
144+
145+
$this->variables['MAILGUN_KEY'] = $this->option('password') ?? $this->ask(
146+
trans('command/messages.environment.mail.ask_mailgun_secret'), $this->config->get('services.mailgun.secret')
147+
);
148+
}
149+
150+
/**
151+
* Handle variables for mandrill driver.
152+
*/
153+
private function setupMandrillDriverVariables()
154+
{
155+
$this->variables['MANDRILL_SECRET'] = $this->option('password') ?? $this->ask(
156+
trans('command/messages.environment.mail.ask_mandrill_secret'), $this->config->get('services.mandrill.secret')
157+
);
158+
}
159+
160+
/**
161+
* Handle variables for postmark driver.
162+
*/
163+
private function setupPostmarkDriverVariables()
164+
{
165+
$this->variables['MAIL_DRIVER'] = 'smtp';
166+
$this->variables['MAIL_HOST'] = 'smtp.postmarkapp.com';
167+
$this->variables['MAIL_PORT'] = 587;
168+
$this->variables['MAIL_USERNAME'] = $this->variables['MAIL_PASSWORD'] = $this->option('username') ?? $this->ask(
169+
trans('command/messages.environment.mail.ask_postmark_username'), $this->config->get('mail.username')
170+
);
171+
}
172+
}

app/Console/Kernel.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Pterodactyl\Console\Commands\User\DisableTwoFactorCommand;
1313
use Pterodactyl\Console\Commands\Location\DeleteLocationCommand;
1414
use Pterodactyl\Console\Commands\Schedule\ProcessRunnableCommand;
15+
use Pterodactyl\Console\Commands\Environment\EmailSettingsCommand;
1516
use Pterodactyl\Console\Commands\Maintenance\CleanServiceBackupFilesCommand;
1617

1718
class Kernel extends ConsoleKernel
@@ -26,6 +27,7 @@ class Kernel extends ConsoleKernel
2627
DeleteLocationCommand::class,
2728
DeleteUserCommand::class,
2829
DisableTwoFactorCommand::class,
30+
EmailSettingsCommand::class,
2931
InfoCommand::class,
3032
MakeLocationCommand::class,
3133
MakeUserCommand::class,
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/*
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace Pterodactyl\Traits\Commands;
26+
27+
use Pterodactyl\Exceptions\PterodactylException;
28+
29+
trait EnvironmentWriterTrait
30+
{
31+
/**
32+
* Update the .env file for the application using the passed in values.
33+
*
34+
* @param array $values
35+
*
36+
* @throws \Pterodactyl\Exceptions\PterodactylException
37+
*/
38+
public function writeToEnvironment(array $values = [])
39+
{
40+
$path = base_path('.env');
41+
if (! file_exists($path)) {
42+
throw new PterodactylException('Cannot locate .env file, was this software installed correctly?');
43+
}
44+
45+
$saveContents = file_get_contents($path);
46+
collect($values)->each(function ($value, $key) use (&$saveContents) {
47+
$key = strtoupper($key);
48+
if (str_contains($value, ' ') && ! preg_match('/\"(.*)\"/', $value)) {
49+
$value = sprintf('"%s"', addslashes($value));
50+
}
51+
52+
$saveValue = sprintf('%s=%s', $key, $value);
53+
54+
if (preg_match_all('/^' . $key . '=(.*)$/m', $saveContents) < 1) {
55+
$saveContents = $saveContents . PHP_EOL . $saveValue;
56+
} else {
57+
$saveContents = preg_replace('/^' . $key . '=(.*)$/m', $saveValue, $saveContents);
58+
}
59+
});
60+
61+
file_put_contents($path, $saveContents);
62+
}
63+
}

resources/lang/en/command/messages.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,20 @@
6060
'server' => [
6161
'rebuild_failed' => 'Rebuild request for ":name" (#:id) on node ":node" failed with error: :message',
6262
],
63+
'environment' => [
64+
'mail' => [
65+
'ask_smtp_host' => 'SMTP Host (e.g. smtp.google.com)',
66+
'ask_smtp_port' => 'SMTP Port',
67+
'ask_smtp_username' => 'SMTP Username',
68+
'ask_smtp_password' => 'SMTP Password',
69+
'ask_mailgun_domain' => 'Mailgun Domain',
70+
'ask_mailgun_secret' => 'Mailgun Secret',
71+
'ask_mandrill_secret' => 'Mandrill Secret',
72+
'ask_postmark_username' => 'Postmark API Key',
73+
'ask_driver' => 'Which driver should be used for sending emails?',
74+
'ask_mail_from' => 'Email address emails should originate from',
75+
'ask_mail_name' => 'Name that emails should appear from',
76+
'ask_encryption' => 'Encryption method to use',
77+
],
78+
],
6379
];

tests/Unit/Commands/CommandTestCase.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,23 @@
3131

3232
abstract class CommandTestCase extends TestCase
3333
{
34+
/**
35+
* @var bool
36+
*/
37+
protected $commandIsInteractive = true;
38+
39+
/**
40+
* Set a command to be non-interactive for testing purposes.
41+
*
42+
* @return $this
43+
*/
44+
public function withoutInteraction()
45+
{
46+
$this->commandIsInteractive = false;
47+
48+
return $this;
49+
}
50+
3451
/**
3552
* Return the display from running a command.
3653
*
@@ -48,6 +65,8 @@ protected function runCommand(Command $command, array $args = [], array $inputs
4865

4966
$response = new CommandTester($command);
5067
$response->setInputs($inputs);
68+
69+
$opts = array_merge($opts, ['interactive' => $this->commandIsInteractive]);
5170
$response->execute($args, $opts);
5271

5372
return $response->getDisplay();

0 commit comments

Comments
 (0)