mirror of
https://github.com/laravel/valet.git
synced 2026-02-04 16:10:08 +01:00
Write tests for Server.php
This commit is contained in:
@@ -188,7 +188,7 @@ public function serveStaticFile(string $staticFilePath, string $sitePath, string
|
||||
* @param string $path
|
||||
* @return bool
|
||||
*/
|
||||
protected function isActualFile($path)
|
||||
protected function isActualFile(string $path): bool
|
||||
{
|
||||
return ! is_dir($path) && file_exists($path);
|
||||
}
|
||||
|
||||
@@ -19,13 +19,67 @@ public function __construct(public array $config)
|
||||
* @param string $requestUri $_SERVER['REQUEST_URI']
|
||||
* @return string
|
||||
*/
|
||||
public function uriFromRequestUri(string $requestUri): string
|
||||
public static function uriFromRequestUri(string $requestUri): string
|
||||
{
|
||||
return rawurldecode(
|
||||
explode('?', $requestUri)[0]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract the domain from the site name.
|
||||
*
|
||||
* @param string $siteName
|
||||
* @return string
|
||||
*/
|
||||
public static function domainFromSiteName(string $siteName): string
|
||||
{
|
||||
return array_slice(explode('.', $siteName), -1)[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the Valet 404 "Not Found" page.
|
||||
*/
|
||||
public static function show404()
|
||||
{
|
||||
http_response_code(404);
|
||||
require __DIR__ . '/cli/templates/404.html';
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Show directory listing or 404 if directory doesn't exist.
|
||||
*
|
||||
* @param string $valetSitePath
|
||||
* @param string $uri
|
||||
*/
|
||||
public static function showDirectoryListing(string $valetSitePath, string $uri)
|
||||
{
|
||||
$is_root = ($uri == '/');
|
||||
$directory = ($is_root) ? $valetSitePath : $valetSitePath . $uri;
|
||||
|
||||
if (!file_exists($directory)) {
|
||||
static::show404();
|
||||
}
|
||||
|
||||
// Sort directories at the top
|
||||
$paths = glob("$directory/*");
|
||||
usort($paths, function ($a, $b) {
|
||||
return (is_dir($a) == is_dir($b)) ? strnatcasecmp($a, $b) : (is_dir($a) ? -1 : 1);
|
||||
});
|
||||
|
||||
// Output the HTML for the directory listing
|
||||
echo "<h1>Index of $uri</h1>";
|
||||
echo '<hr>';
|
||||
echo implode("<br>\n", array_map(function ($path) use ($uri, $is_root) {
|
||||
$file = basename($path);
|
||||
|
||||
return ($is_root) ? "<a href='/$file'>/$file</a>" : "<a href='$uri/$file'>$uri/$file/</a>";
|
||||
}, $paths));
|
||||
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract site name from HTTP host, stripping www. and supporting wildcard DNS.
|
||||
*
|
||||
@@ -86,28 +140,17 @@ public function allowWildcardDnsDomains(string $domain): string
|
||||
return $domain;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract the domain from the site name.
|
||||
*
|
||||
* @param string $siteName
|
||||
* @return string
|
||||
*/
|
||||
public function domainFromSiteName(string $siteName): string
|
||||
{
|
||||
return array_slice(explode('.', $siteName), -1)[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine the fully qualified path to the site.
|
||||
* Inspects registered path directories, case-sensitive.
|
||||
*
|
||||
* @param string $siteName
|
||||
* @return string
|
||||
* @return string|null
|
||||
*/
|
||||
public function sitePath(string $siteName): string
|
||||
public function sitePath(string $siteName): ?string
|
||||
{
|
||||
$valetSitePath = null;
|
||||
$domain = $this->domainFromSiteName($siteName);
|
||||
$domain = static::domainFromSiteName($siteName);
|
||||
|
||||
foreach ($this->config['paths'] as $path) {
|
||||
$handle = opendir($path);
|
||||
@@ -143,58 +186,21 @@ public function sitePath(string $siteName): string
|
||||
return $valetSitePath;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the Valet 404 "Not Found" page.
|
||||
*/
|
||||
public function show404()
|
||||
{
|
||||
http_response_code(404);
|
||||
require __DIR__.'/cli/templates/404.html';
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null If set, default site path for uncaught urls
|
||||
* Return the default site path for uncaught URLs, if it's set.
|
||||
*
|
||||
* @return string|null
|
||||
**/
|
||||
public function defaultSitePath(): ?string
|
||||
{
|
||||
if (isset($this->config['default']) && is_string($this->config['default']) && is_dir($this->config['default'])) {
|
||||
return $this->config['default'];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Show directory listing or 404 if directory doesn't exist.
|
||||
*
|
||||
* @param string $valetSitePath
|
||||
* @param string $uri
|
||||
*/
|
||||
public function showDirectoryListing(string $valetSitePath, string $uri)
|
||||
{
|
||||
$is_root = ($uri == '/');
|
||||
$directory = ($is_root) ? $valetSitePath : $valetSitePath.$uri;
|
||||
|
||||
if (! file_exists($directory)) {
|
||||
show_valet_404();
|
||||
}
|
||||
|
||||
// Sort directories at the top
|
||||
$paths = glob("$directory/*");
|
||||
usort($paths, function ($a, $b) {
|
||||
return (is_dir($a) == is_dir($b)) ? strnatcasecmp($a, $b) : (is_dir($a) ? -1 : 1);
|
||||
});
|
||||
|
||||
// Output the HTML for the directory listing
|
||||
echo "<h1>Index of $uri</h1>";
|
||||
echo '<hr>';
|
||||
echo implode("<br>\n", array_map(function ($path) use ($uri, $is_root) {
|
||||
$file = basename($path);
|
||||
|
||||
return ($is_root) ? "<a href='/$file'>/$file</a>" : "<a href='$uri/$file'>$uri/$file/</a>";
|
||||
}, $paths));
|
||||
|
||||
exit;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,7 +66,7 @@ private function getLinkNameByCurrentDir(): ?string
|
||||
public function host(string $path): ?string
|
||||
{
|
||||
foreach ($this->files->scandir($this->sitesPath()) as $link) {
|
||||
if ($resolved = realpath($this->sitesPath($link)) === $path) {
|
||||
if (realpath($this->sitesPath($link)) === $path) {
|
||||
return $link;
|
||||
}
|
||||
}
|
||||
|
||||
10
server.php
10
server.php
@@ -24,12 +24,12 @@
|
||||
/**
|
||||
* Parse the URI and site / host for the incoming request.
|
||||
*/
|
||||
$uri = $server->uriFromRequestUri($_SERVER['REQUEST_URI']);
|
||||
$uri = Server::uriFromRequestUri($_SERVER['REQUEST_URI']);
|
||||
$siteName = $server->siteNameFromHttpHost($_SERVER['HTTP_HOST']);
|
||||
$valetSitePath = $server->sitePath($siteName);
|
||||
|
||||
if (is_null($valetSitePath) && is_null($valetSitePath = $server->defaultSitePath())) {
|
||||
$server->show404();
|
||||
Server::show404();
|
||||
}
|
||||
|
||||
$valetSitePath = realpath($valetSitePath);
|
||||
@@ -40,7 +40,7 @@
|
||||
$valetDriver = ValetDriver::assign($valetSitePath, $siteName, $uri);
|
||||
|
||||
if (! $valetDriver) {
|
||||
$server->show404();
|
||||
Server::show404();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -85,10 +85,10 @@
|
||||
|
||||
if (! $frontControllerPath) {
|
||||
if (isset($valetConfig['directory-listing']) && $valetConfig['directory-listing'] == 'on') {
|
||||
$server->showDirectoryListing($valetSitePath, $uri);
|
||||
Server::showDirectoryListing($valetSitePath, $uri);
|
||||
}
|
||||
|
||||
$server->show404();
|
||||
Server::show404();
|
||||
}
|
||||
|
||||
chdir(dirname($frontControllerPath));
|
||||
|
||||
92
tests/ServerTest.php
Normal file
92
tests/ServerTest.php
Normal file
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Container\Container;
|
||||
use Valet\Server;
|
||||
|
||||
use function Valet\user;
|
||||
|
||||
class ServerTest extends Yoast\PHPUnitPolyfills\TestCases\TestCase
|
||||
{
|
||||
use UsesNullWriter;
|
||||
|
||||
public function set_up()
|
||||
{
|
||||
$_SERVER['SUDO_USER'] = user();
|
||||
|
||||
Container::setInstance(new Container);
|
||||
$this->setNullWriter();
|
||||
}
|
||||
|
||||
public function tear_down()
|
||||
{
|
||||
Mockery::close();
|
||||
}
|
||||
|
||||
public function test_it_extracts_uri_from_server_request_uri()
|
||||
{
|
||||
$this->assertEquals('/about/index.php', Server::uriFromRequestUri('/about/index.php?abc=def&qrs=tuv'));
|
||||
$this->assertEquals('/', Server::uriFromRequestUri('/?abc=def&qrs=tuv'));
|
||||
}
|
||||
|
||||
public function test_it_extracts_domain_from_site_name()
|
||||
{
|
||||
$this->assertEquals('tighten', Server::domainFromSiteName('subdomain.tighten'));
|
||||
}
|
||||
|
||||
public function test_it_gets_site_name_from_http_host()
|
||||
{
|
||||
$server = new Server(['tld' => 'test']);
|
||||
|
||||
$httpHost = 'tighten.test';
|
||||
$this->assertEquals('tighten', $server->siteNameFromHttpHost($httpHost));
|
||||
}
|
||||
|
||||
public function test_it_gets_site_name_from_http_host_using_wildcard()
|
||||
{
|
||||
$server = new Server(['tld' => 'test']);
|
||||
|
||||
$httpHost = 'tighten.192.168.0.10.nip.io';
|
||||
$this->assertEquals('tighten', $server->siteNameFromHttpHost($httpHost));
|
||||
$httpHost = 'tighten-192-168-0-10.nip.io';
|
||||
$this->assertEquals('tighten', $server->siteNameFromHttpHost($httpHost));
|
||||
}
|
||||
|
||||
public function test_it_strips_www_dot_from_http_host()
|
||||
{
|
||||
$server = new Server(['tld' => 'test']);
|
||||
|
||||
$httpHost = 'www.tighten.test';
|
||||
$this->assertEquals('tighten', $server->siteNameFromHttpHost($httpHost));
|
||||
}
|
||||
|
||||
public function test_it_gets_site_path_from_site_name()
|
||||
{
|
||||
$server = new Server(['paths' => [__DIR__.'/files/sites']]);
|
||||
|
||||
$realPath = __DIR__.'/files/sites/tighten';
|
||||
$this->assertEquals($realPath, $server->sitePath('tighten'));
|
||||
$realPath = __DIR__ . '/files/sites/tighten';
|
||||
$this->assertEquals($realPath, $server->sitePath('subdomain.tighten'));
|
||||
}
|
||||
|
||||
public function test_it_returns_null_if_site_does_not_match()
|
||||
{
|
||||
$server = new Server(['paths' => []]);
|
||||
|
||||
$this->assertNull($server->sitePath('tighten'));
|
||||
}
|
||||
|
||||
public function test_it_gets_default_site_path()
|
||||
{
|
||||
$server = new Server(['default' => __DIR__.'/files/sites/tighten']);
|
||||
|
||||
$this->assertEquals(__DIR__.'/files/sites/tighten', $server->defaultSitePath());
|
||||
}
|
||||
|
||||
public function test_it_returns_null_default_site_path_if_not_set()
|
||||
{
|
||||
$server = new Server([]);
|
||||
|
||||
$this->assertNull($server->defaultSitePath());
|
||||
}
|
||||
}
|
||||
0
tests/files/sites/tighten/.gitkeep
Normal file
0
tests/files/sites/tighten/.gitkeep
Normal file
Reference in New Issue
Block a user