Skip to content

Commit 7d2d2be

Browse files
author
Oleksii Korshenko
authored
Merge pull request #1675 from magento-engcom/develop-prs
Public Pull Requests #10500 Improvements to the phpserver router by @aredridel
2 parents 4ee8196 + ada09f8 commit 7d2d2be

File tree

3 files changed

+101
-10
lines changed

3 files changed

+101
-10
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Phpserver;
7+
8+
/**
9+
* @magentoAppIsolation enabled
10+
*
11+
* @magentoConfigFixture current_store web/secure/base_url http://127.0.0.1:8082/
12+
* @magentoConfigFixture current_store web/unsecure/base_link_url http://127.0.0.1:8082/
13+
* @magentoConfigFixture current_store web/secure/base_link_url http://127.0.0.1:8082/
14+
* @magentoConfigFixture current_store web/secure/use_in_frontend 0
15+
*
16+
* @magentoAppArea frontend
17+
*/
18+
class PhpserverTest extends \PHPUnit\Framework\TestCase
19+
{
20+
const BASE_URL = '127.0.0.1:8082';
21+
22+
private static $serverPid;
23+
24+
/**
25+
* @var \Zend\Http\Client
26+
*/
27+
private $httpClient;
28+
29+
/**
30+
* Instantiate phpserver in the pub folder
31+
*/
32+
public static function setUpBeforeClass()
33+
{
34+
if (!(defined('TRAVIS') && TRAVIS === true)) {
35+
self::markTestSkipped('Travis environment test');
36+
}
37+
$return = [];
38+
39+
$baseDir = __DIR__ . '/../../../../../../';
40+
$command = sprintf(
41+
'cd %s && php -S %s -t ./pub/ ./phpserver/router.php >/dev/null 2>&1 & echo $!',
42+
$baseDir,
43+
static::BASE_URL
44+
);
45+
exec($command, $return);
46+
static::$serverPid = (int) $return[0];
47+
}
48+
49+
private function getUrl($url)
50+
{
51+
return sprintf('http://%s/%s', self::BASE_URL, ltrim($url, '/'));
52+
}
53+
54+
public function setUp()
55+
{
56+
$this->httpClient = new \Zend\Http\Client(null, ['timeout' => 10]);
57+
}
58+
59+
public function testServerHasPid()
60+
{
61+
$this->assertTrue(static::$serverPid > 0);
62+
}
63+
64+
public function testServerResponds()
65+
{
66+
$this->httpClient->setUri($this->getUrl('/'));
67+
$response = $this->httpClient->send();
68+
$this->assertFalse($response->isClientError());
69+
}
70+
71+
public function testStaticCssFile()
72+
{
73+
$this->httpClient->setUri($this->getUrl('/errors/default/css/styles.css'));
74+
$response = $this->httpClient->send();
75+
76+
$this->assertFalse($response->isClientError());
77+
$this->assertStringStartsWith('text/css', $response->getHeaders()->get('Content-Type')->getMediaType());
78+
}
79+
80+
public function testStaticImageFile()
81+
{
82+
$this->httpClient->setUri($this->getUrl('/errors/default/images/logo.gif'));
83+
$response = $this->httpClient->send();
84+
85+
$this->assertFalse($response->isClientError());
86+
$this->assertStringStartsWith('image/gif', $response->getHeaders()->get('Content-Type')->getMediaType());
87+
}
88+
89+
public static function tearDownAfterClass()
90+
{
91+
posix_kill(static::$serverPid, SIGKILL);
92+
}
93+
}

phpserver/README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,10 @@ php bin/magento setup:install --base-url=http://127.0.0.1:8082
2121
--db-host=localhost --db-name=magento --db-user=magento --db-password=magento
2222
--admin-firstname=Magento --admin-lastname=User --admin-email=user@example.com
2323
--admin-user=admin --admin-password=admin123 --language=en_US
24-
--currency=USD --timezone=America/Chicago --use-rewrites=0
24+
--currency=USD --timezone=America/Chicago --use-rewrites=1
2525
```
2626

27-
It's important to note that the router is not able to work with rewrite urls, that's why the flag `use-rewrites` is set to `0`.
28-
2927
Notes:
30-
- You must use `--use-rewrites=0` because the web server cannot rewrite URLs
3128
- By default, Magento creates a random Admin URI for you. Make sure to write this value down because it's how you access the Magento Admin later. For example : ```http://127.0.0.1:8082/index.php/admin_1vpn01```.
3229

3330
For more informations about the installation process using the CLI, you can consult the dedicated documentation that can found in [the developer documentation](https://github.com/magento/devdocs/blob/develop/guides/v2.0/install-gde/install/cli/install-cli-install.md).

phpserver/router.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,12 @@
7676

7777
$debug("file: $file");
7878

79-
if (file_exists($origFile)) {
79+
if (file_exists($origFile) || file_exists($file)) {
80+
if (file_exists($origFile)) {
81+
$file = $origFile;
82+
}
83+
8084
$debug('file exists');
81-
return false;
82-
} else if (file_exists($file)) {
8385
$mimeTypes = [
8486
'css' => 'text/css',
8587
'js' => 'application/javascript',
@@ -93,9 +95,8 @@
9395
'html' => 'text/html',
9496
];
9597

96-
$type = isset($mimeTypes[$ext]) && $mimeTypes[$ext];
97-
if ($type) {
98-
header("Content-Type: $type");
98+
if (isset($mimeTypes[$ext])) {
99+
header("Content-Type: $mimeTypes[$ext]");
99100
}
100101
readfile($file);
101102
return;

0 commit comments

Comments
 (0)