Cleanup code from #1876

- Extract a method for email address generation
- Consistent types
- No docblocks for types where superfluous
- Tweak console output
- Don't inherit from integration test's base class in unit test
This commit is contained in:
Franz Liedke 2019-09-24 01:00:22 +02:00
parent 522d3356ca
commit 5a9c1a91b2
6 changed files with 66 additions and 51 deletions

View File

@ -15,60 +15,49 @@ use Psr\Http\Message\UriInterface;
final class BaseUrl final class BaseUrl
{ {
/** @var UriInterface|string */ /** @var string */
private $baseUrl; private $normalized;
/** private function __construct(string $baseUrl)
* @param UriInterface|string $baseUrl
*/
private function __construct($baseUrl)
{ {
$this->baseUrl = $this->normalise($baseUrl); $this->normalized = $this->normalize($baseUrl);
} }
/**
* @param string $baseUrl
* @return \Flarum\Install\BaseUrl
*/
public static function fromString(string $baseUrl): self public static function fromString(string $baseUrl): self
{ {
return new self($baseUrl); return new self($baseUrl);
} }
/**
* @param \Psr\Http\Message\UriInterface $baseUrl
* @return \Flarum\Install\BaseUrl
*/
public static function fromUri(UriInterface $baseUrl): self public static function fromUri(UriInterface $baseUrl): self
{ {
return self::fromString((string) $baseUrl); return new self((string) $baseUrl);
} }
/**
* @return string
*/
public function __toString(): string public function __toString(): string
{ {
return $this->baseUrl; return $this->normalized;
} }
/** public function toEmail(string $mailbox): string
* @param UriInterface|string $baseUrl {
* @return string $host = preg_replace('/^www\./i', '', parse_url($this->normalized, PHP_URL_HOST));
*/
private function normalise($baseUrl): string return "$mailbox@$host";
}
private function normalize(string $baseUrl): string
{ {
// Empty base url is still valid // Empty base url is still valid
if (empty($baseUrl)) { if (empty($baseUrl)) {
return ''; return '';
} }
$normalisedBaseUrl = trim($baseUrl, '/'); $normalizedBaseUrl = trim($baseUrl, '/');
if (! preg_match('#^https?://#i', $normalisedBaseUrl)) { if (! preg_match('#^https?://#i', $normalizedBaseUrl)) {
$normalisedBaseUrl = sprintf('http://%s', $normalisedBaseUrl); $normalizedBaseUrl = sprintf('http://%s', $normalizedBaseUrl);
} }
$parseUrl = parse_url($normalisedBaseUrl); $parseUrl = parse_url($normalizedBaseUrl);
$path = $parseUrl['path'] ?? null; $path = $parseUrl['path'] ?? null;
if (isset($parseUrl['path']) && strrpos($parseUrl['path'], '.php') !== false) { if (isset($parseUrl['path']) && strrpos($parseUrl['path'], '.php') !== false) {

View File

@ -29,6 +29,7 @@ class UserDataProvider implements DataProviderInterface
protected $questionHelper; protected $questionHelper;
/** @var BaseUrl */
protected $baseUrl; protected $baseUrl;
public function __construct(InputInterface $input, OutputInterface $output, QuestionHelper $questionHelper) public function __construct(InputInterface $input, OutputInterface $output, QuestionHelper $questionHelper)
@ -42,7 +43,7 @@ class UserDataProvider implements DataProviderInterface
{ {
return $installation return $installation
->debugMode(false) ->debugMode(false)
->baseUrl(BaseUrl::fromString($this->getBaseUrl())) ->baseUrl($this->getBaseUrl())
->databaseConfig($this->getDatabaseConfiguration()) ->databaseConfig($this->getDatabaseConfiguration())
->adminUser($this->getAdminUser()) ->adminUser($this->getAdminUser())
->settings($this->getSettings()); ->settings($this->getSettings());
@ -68,15 +69,17 @@ class UserDataProvider implements DataProviderInterface
); );
} }
private function getBaseUrl(): string private function getBaseUrl(): BaseUrl
{ {
return $this->baseUrl = $this->ask('Base URL:(Default: http://flarum.local)', 'http://flarum.local'); $baseUrl = $this->ask('Base URL (Default: http://flarum.local):', 'http://flarum.local');
return $this->baseUrl = BaseUrl::fromString($baseUrl);
} }
private function getAdminUser(): AdminUser private function getAdminUser(): AdminUser
{ {
return new AdminUser( return new AdminUser(
$this->ask('Admin username:(Default: admin)', 'admin'), $this->ask('Admin username (Default: admin):', 'admin'),
$this->askForAdminPassword(), $this->askForAdminPassword(),
$this->ask('Admin email address (required):') $this->ask('Admin email address (required):')
); );
@ -109,7 +112,7 @@ class UserDataProvider implements DataProviderInterface
return [ return [
'forum_title' => $title, 'forum_title' => $title,
'mail_from' => 'noreply@'.preg_replace('/^www\./i', '', parse_url($this->baseUrl, PHP_URL_HOST)), 'mail_from' => $this->baseUrl->toEmail('noreply'),
'welcome_title' => 'Welcome to '.$title, 'welcome_title' => 'Welcome to '.$title,
]; ];
} }

View File

@ -55,16 +55,16 @@ class InstallController implements RequestHandlerInterface
public function handle(Request $request): ResponseInterface public function handle(Request $request): ResponseInterface
{ {
$input = $request->getParsedBody(); $input = $request->getParsedBody();
$baseUrl = $request->getUri(); $baseUrl = BaseUrl::fromUri($request->getUri());
try { try {
$pipeline = $this->installation $pipeline = $this->installation
->baseUrl(BaseUrl::fromUri($baseUrl)) ->baseUrl($baseUrl)
->databaseConfig($this->makeDatabaseConfig($input)) ->databaseConfig($this->makeDatabaseConfig($input))
->adminUser($this->makeAdminUser($input)) ->adminUser($this->makeAdminUser($input))
->settings([ ->settings([
'forum_title' => Arr::get($input, 'forumTitle'), 'forum_title' => Arr::get($input, 'forumTitle'),
'mail_from' => 'noreply@'.preg_replace('/^www\./i', '', $baseUrl->getHost()), 'mail_from' => $baseUrl->toEmail('noreply'),
'welcome_title' => 'Welcome to '.Arr::get($input, 'forumTitle'), 'welcome_title' => 'Welcome to '.Arr::get($input, 'forumTitle'),
]) ])
->build(); ->build();

View File

@ -65,13 +65,9 @@ class Installation
return $this; return $this;
} }
/**
* @param \Flarum\Install\BaseUrl $baseUrl
* @return $this
*/
public function baseUrl(BaseUrl $baseUrl) public function baseUrl(BaseUrl $baseUrl)
{ {
$this->baseUrl = (string) $baseUrl; $this->baseUrl = $baseUrl;
return $this; return $this;
} }

View File

@ -11,6 +11,7 @@
namespace Flarum\Install\Steps; namespace Flarum\Install\Steps;
use Flarum\Install\BaseUrl;
use Flarum\Install\DatabaseConfig; use Flarum\Install\DatabaseConfig;
use Flarum\Install\ReversibleStep; use Flarum\Install\ReversibleStep;
use Flarum\Install\Step; use Flarum\Install\Step;
@ -25,7 +26,7 @@ class StoreConfig implements Step, ReversibleStep
private $configFile; private $configFile;
public function __construct($debugMode, DatabaseConfig $dbConfig, $baseUrl, $configFile) public function __construct($debugMode, DatabaseConfig $dbConfig, BaseUrl $baseUrl, $configFile)
{ {
$this->debugMode = $debugMode; $this->debugMode = $debugMode;
$this->dbConfig = $dbConfig; $this->dbConfig = $dbConfig;
@ -57,7 +58,7 @@ class StoreConfig implements Step, ReversibleStep
return [ return [
'debug' => $this->debugMode, 'debug' => $this->debugMode,
'database' => $this->dbConfig->toArray(), 'database' => $this->dbConfig->toArray(),
'url' => $this->baseUrl, 'url' => (string) $this->baseUrl,
'paths' => $this->getPathsConfig(), 'paths' => $this->getPathsConfig(),
]; ];
} }

View File

@ -12,14 +12,13 @@
namespace Flarum\Tests\unit; namespace Flarum\Tests\unit;
use Flarum\Install\BaseUrl; use Flarum\Install\BaseUrl;
use Flarum\Tests\integration\TestCase; use PHPUnit\Framework\TestCase;
use Zend\Diactoros\Uri;
class BaseUrlTest extends TestCase class BaseUrlTest extends TestCase
{ {
/** /**
* @dataProvider urlProvider * @dataProvider urlProvider
* @param $uri
* @param $expected
*/ */
public function test_base_url_simulating_cli_installer($uri, $expected) public function test_base_url_simulating_cli_installer($uri, $expected)
{ {
@ -28,14 +27,23 @@ class BaseUrlTest extends TestCase
/** /**
* @dataProvider urlProvider * @dataProvider urlProvider
* @param $uri
* @param $expected
*/ */
public function test_base_url_simulating_web_installer($uri, $expected) public function test_base_url_simulating_web_installer($uri, $expected)
{ {
$request = $this->request('get', $uri); $uri = new Uri($uri);
$this->assertEquals($expected, BaseUrl::fromUri($request->getUri())); $this->assertEquals($expected, BaseUrl::fromUri($uri));
}
/**
* @dataProvider emailProvider
*/
public function test_default_email_generation($uri, $expected)
{
$this->assertEquals(
$expected,
BaseUrl::fromString($uri)->toEmail('noreply')
);
} }
public function urlProvider() public function urlProvider()
@ -56,4 +64,22 @@ class BaseUrlTest extends TestCase
['http://sub.flarum.org', 'http://sub.flarum.org'], ['http://sub.flarum.org', 'http://sub.flarum.org'],
]; ];
} }
public function emailProvider()
{
return [
['flarum.org', 'noreply@flarum.org'],
['flarum.org/', 'noreply@flarum.org'],
['http://flarum.org', 'noreply@flarum.org'],
['http://flarum.org/', 'noreply@flarum.org'],
['https://flarum.org', 'noreply@flarum.org'],
['http://flarum.org/index.php', 'noreply@flarum.org'],
['http://flarum.org/index.php/', 'noreply@flarum.org'],
['http://flarum.org/flarum', 'noreply@flarum.org'],
['http://flarum.org/flarum/index.php', 'noreply@flarum.org'],
['http://flarum.org/flarum/index.php/', 'noreply@flarum.org'],
['sub.flarum.org', 'noreply@sub.flarum.org'],
['http://sub.flarum.org', 'noreply@sub.flarum.org'],
];
}
} }