-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/initial structure #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 |
| 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 |
| 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 |
| 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 |
| 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 |
| 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 | ||
| ``` |
| 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 */ | ||
| protected $app; | ||
| /** @var Client $client */ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| ); | ||
| } | ||
| } | ||
| 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() | ||
| ); | ||
| } | ||
| } |
| 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'); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| <?php namespace Test\Unit\Utility; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| { | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'], | ||
| ]; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| FOO=bar |
| 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> |
| 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; | ||
|
|
||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Online would read better here |
||
| 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(); | ||
| 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": [ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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": { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Must require |
||
| "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 | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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:
There was a problem hiding this comment.
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