Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
root = true

[*]
charset = utf-8
indent_style = space
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.php]
[*.twig]
indent_size = 4

[*.twig]
[*.md]
insert_final_newline = false

[*.md]
trim_trailing_whitespace = false
8 changes: 8 additions & 0 deletions .env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ENVIRONMENT=test

APP_DETERMINE_ROUTE_BEFORE_APP_MIDDLEWARE=true
APP_DISPLAY_ERROR_DETAILS=true
APP_DEBUG_MODE=true

URL=server
PORT=8080
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
composer.phar
/vendor/

.env
# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
# composer.lock
10 changes: 10 additions & 0 deletions .phpunit-watcher.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
watch:
directories:
- src
- test
fileMask: '*.php'
phpunit:
arguments: '--configuration ./Test/phpunit.xml --testdox'
notifications:
passingTests: false
failingTests: false
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM php:7.1-alpine

RUN apk update
RUN apk add --update git unzip zlib-dev

RUN docker-php-ext-install -j$(getconf _NPROCESSORS_ONLN) opcache

RUN rm -rf /tmp/*

COPY . /var/www/html
WORKDIR /var/www/html

ENTRYPOINT php ./build/public/server.php
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
# website-server
A webserver ran by ReactPHP

# Running

Install packages
```shell
docker-compose -f docker-compose.build.yml run --rm composer
```

Run the app
```shell
docker-compose up
```

Testing the app (Requires the previous command for this to be successful)
```shell
docker-compose -f docker-compose.test.yml run --rm test
```
55 changes: 55 additions & 0 deletions Test/Integration/WebTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php declare(strict_types=1);

namespace Test\Integration;

use function App\Utility\setupEnv;
use GuzzleHttp\Client;
use Psr\Http\Message\ResponseInterface;
use Slim\App;
use PHPUnit\Framework\TestCase;

class WebTestCase extends TestCase
{
/** @var App $app */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I don't like this one liners, I'd prefer:

/**
 * @var App $app
 */

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm of the same preference tbh

protected $app;
/** @var Client $client */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add new line between properties.

protected $client;

/**
* Set up the app and client instance
* @inheritdoc
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove.

*/
public function setUp() :void
{
setupEnv(__DIR__ . '/../../', '.env.test');

$this->client = new Client();
}

/**
* @param string $uri
*
* @return ResponseInterface
*/
final public function getResponse(string $uri) :ResponseInterface
{
return $this->client->get(
$this->makeURL($uri)
);
}

/**
* @param string $uri
*
* @return string
*/
final public function makeURL(string $uri) :string
{
return sprintf(
'%s:%d/%s',
getenv('URL'),
getenv('PORT'),
$uri
);
}
}
24 changes: 24 additions & 0 deletions Test/Integration/Welcome/WelcomeActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types=1);

namespace Test\Integration\Welcome;

use Test\Integration\WebTestCase;

final class WelcomeActionTest extends WebTestCase
{
public function test_HTTP_status_code_is_200()
{
$this->assertEquals(
200,
$this->getResponse('')->getStatusCode()
);
}

public function test_body_is_correct()
{
$this->assertContains(
'<h1>You shouldn\'t be seeing this!</h1>',
$this->getResponse('')->getBody()->getContents()
);
}
}
15 changes: 15 additions & 0 deletions Test/Unit/Utility/EnvironmentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php declare(strict_types=1);

namespace Test\Unit\Utility;

use function App\Utility\setupEnv;
use PHPUnit\Framework\TestCase;

final class EnvironmentTest extends TestCase
{
public function testEnvironmentIsCreated()
{
setupEnv(__DIR__.'/Fixtures/', '.env.foo');
$this->assertSame(getenv('FOO'), 'bar');
}
}
37 changes: 37 additions & 0 deletions Test/Unit/Utility/ExtensionToContentTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php namespace Test\Unit\Utility;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Namespace on a new line.

use function App\Utility\extensionToContentType;
use PHPUnit\Framework\TestCase;

final class ExtensionToContentTypeTest extends TestCase
{

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove.

/**
* @dataProvider files
*
* @param string $file
* @param string $expect
*/
public function test_various_file_extensions_give_the_correct_mime_type($file, $expect)
{
$this->assertSame($expect, extensionToContentType($file));
}

/**
* @return array
*/
public function files()
{
return [
[__DIR__ . '/Fixtures/MimeTypes/foo.css', 'text/css'],
[__DIR__ . '/Fixtures/MimeTypes/foo.html', 'text/html'],
[__DIR__ . '/Fixtures/MimeTypes/foo.js', 'application/javascript'],
[__DIR__ . '/Fixtures/MimeTypes/foo.json', 'application/json'],
[__DIR__ . '/Fixtures/MimeTypes/foo.pdf', 'application/pdf'],
[__DIR__ . '/Fixtures/MimeTypes/foo.xml', 'application/xml'],
[__DIR__ . '/Fixtures/MimeTypes/foo.jpg', 'image/jpeg'],
[__DIR__ . '/Fixtures/MimeTypes/foo.jpeg', 'image/jpeg'],
[__DIR__ . '/Fixtures/MimeTypes/foo.png', 'image/png'],
[__DIR__ . '/Fixtures/MimeTypes/foo.gif', 'image/gif'],
];
}
}
1 change: 1 addition & 0 deletions Test/Unit/Utility/Fixtures/.env.foo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FOO=bar
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
Empty file.
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
35 changes: 35 additions & 0 deletions Test/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="../vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorToExceptions="true"
convertNoticeToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="true">

<testsuites>
<testsuite name="tests">
<directory>./Integration</directory>
<directory>./Unit</directory>
</testsuite>

<testsuite name="unit">
<directory>./Unit</directory>
</testsuite>

<testsuite name="integration">
<directory>./Integration</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>../src</directory>
</whitelist>
</filter>

</phpunit>
70 changes: 70 additions & 0 deletions build/public/server.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php declare(strict_types=1);

use function App\Utility\setupEnv;
use function App\Utility\extensionToContentType;

use React\Http\Server as HttpServer;
use Psr\Http\Message\ServerRequestInterface;
use React\Http\Response as ReactResponse;
use Slim\App as Slim;
use Slim\Container;
use Slim\Http\Response as SlimResponse;
use Slim\Http\Headers;


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove.

const APP_ROUTE = __DIR__ . '/../../';

require_once APP_ROUTE . 'vendor/autoload.php';

setupEnv();

$server = new HttpServer(function (ServerRequestInterface $request) {
$_SERVER['HTTP_COOKIE'] = $request->getHeader('Cookie');

$whitelist = 'css|css.map|png|jpg|jpeg|gif|js|js.map';
$fileTypes = sprintf('/\.(?:%s)$/', $whitelist);

if (preg_match($fileTypes, $request->getUri()->getPath())) {
$assetFilePath = sprintf('%s%s', __DIR__, $request->getUri()->getPath());
$contentType = extensionToContentType($assetFilePath);

try {
if (is_file($assetFilePath) === true) {
return new ReactResponse(
200,
['Content-Type' => $contentType],
file_get_contents($assetFilePath)
);
}
} catch (\Exception $exception) {
}

return new ReactResponse(
404,
['Content-Type' => 'text/plain'],
sprintf('%s not found', basename($assetFilePath))
);
}

$configuration = require APP_ROUTE . 'src/Config/main.php';

$app = new Slim(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Online would read better here

$app = new Slim(new Contianer($configuration));

new Container($configuration)
);

include APP_ROUTE . 'src/Dependencies/main.php';
include APP_ROUTE . 'src/Middlewares/main.php';
include APP_ROUTE . 'src/Routes/main.php';

$response = new SlimResponse(
200,
new Headers($request->getHeaders())
);

return $app->process($request, $response);
});

$loop = React\EventLoop\Factory::create();
$socket = new React\Socket\Server('0.0.0.0:8080', $loop);
$server->listen($socket);
$loop->run();
53 changes: 53 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"name": "phpwarks/website-server",
"description": "A webserver ran by ReactPHP",
"type": "project",
"license": "MIT",
"authors": [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is necessary, as we have this information in git.

{
"name": "Nigel Greenway",
"email": "github@futurepixels.co.uk"
}
],
"minimum-stability": "stable",
"require": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Must require php: ^7.1

"monolog/monolog": "^1.23",
"slim/slim": "^3.8",
"slim/flash": "^0.2.0",
"slim/twig-view": "^2.3",
"vlucas/phpdotenv": "^2.4",
"react/http": "^0.7.4",
"guzzlehttp/guzzle": "^6.3"
},
"require-dev": {
"phpunit/phpunit": "^6.4",
"sebastianfeldmann/captainhook": "^2.0",
"spatie/phpunit-watcher": "^1.3"
},
"autoload": {
"psr-4": {
"App\\": "\\src\\App",
"Test\\": "\\Test"
},
"files": [
"src/App/Utility/Environment.php",
"src/App/Utility/ExtensionToContentType.php"
]
},
"scripts": {
"test": "phpunit -c ./Test/phpunit.xml",
"test:unit": "phpunit -c ./Test/phpunit.xml --testsuite unit",
"test:integration": "phpunit -c ./Test/phpunit.xml --testsuite integration",
"test:watch": [
"./bin/phpunit-watcher watch"
],
"clean": [
"rm -rf ./{bin,build/public/assets,vendor,var}",
"docker kill PHPWARKS__server",
"docker rm PHPWARKS__server"
]
},
"config": {
"process-timeout": 0
}
}
Loading