-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from paynl/feature/psr-server-request-package
Add PSR Server Request from Globals helper function
- Loading branch information
Showing
10 changed files
with
269 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
name: Static Code Analysis | ||
|
||
on: | ||
push: | ||
branches: [ "main" ] | ||
pull_request: | ||
branches: [ "main" ] | ||
|
||
permissions: | ||
contents: read | ||
|
||
jobs: | ||
build: | ||
|
||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Validate composer.json and composer.lock | ||
run: composer validate --strict | ||
|
||
- name: Cache Composer packages | ||
id: composer-cache | ||
uses: actions/cache@v3 | ||
with: | ||
path: vendor | ||
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} | ||
restore-keys: | | ||
${{ runner.os }}-php- | ||
- name: Install dependencies | ||
run: composer install --prefer-dist --no-progress | ||
|
||
- name: Run php tests | ||
run: composer run-script analyse |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
name: PHPUnit tests | ||
|
||
on: | ||
push: | ||
branches: [ "main" ] | ||
pull_request: | ||
branches: [ "main" ] | ||
|
||
permissions: | ||
contents: read | ||
|
||
jobs: | ||
build: | ||
|
||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Validate composer.json and composer.lock | ||
run: composer validate --strict | ||
|
||
- name: Cache Composer packages | ||
id: composer-cache | ||
uses: actions/cache@v3 | ||
with: | ||
path: vendor | ||
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} | ||
restore-keys: | | ||
${{ runner.os }}-php- | ||
- name: Install dependencies | ||
run: composer install --prefer-dist --no-progress | ||
|
||
- name: Run php tests | ||
run: composer run-script phpunit-clover | ||
|
||
- name: Upload coverage results to Coveralls | ||
env: | ||
COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }} | ||
run: | | ||
vendor/bin/php-coveralls --coverage_clover=build/logs/clover.xml -v |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/vendor/ | ||
.idea/ | ||
.phpunit.cache/ | ||
composer.lock | ||
.phpunit.result.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,29 @@ | ||
# PSR Server Request | ||
|
||
[![Static Code Analysis](https://github.com/paynl/psr-server-request/actions/workflows/code-analysis.yaml/badge.svg)](https://github.com/paynl/psr-server-request/actions/workflows/code-analysis.yaml) | ||
[![PHPUnit tests](https://github.com/paynl/psr-server-request/actions/workflows/phpunit.yaml/badge.svg)](https://github.com/paynl/psr-server-request/actions/workflows/phpunit.yaml) | ||
[![Coverage Status](https://coveralls.io/repos/github/paynl/psr-server-request/badge.svg?branch=feature/psr-server-request-package)](https://coveralls.io/github/paynl/psr-server-request?branch=feature/psr-server-request-package) | ||
|
||
This package adds a helper function to create a PSR Server Request from the Global PHP variables. | ||
|
||
## Requirements | ||
|
||
To install this package you need: | ||
|
||
* PHP >= 7.4; | ||
* composer. | ||
|
||
## Installation | ||
|
||
``` | ||
composer require paynl/psr-server-request | ||
``` | ||
|
||
This will install the package into your project and autoload the helper function. | ||
|
||
## Usage | ||
|
||
To create a PSR Server Request from the PHP Globals you can use the following method: | ||
```php | ||
$serverRequest = create_psr_server_request(); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
{ | ||
"name": "paynl/psr-server-request", | ||
"description": "Create a PSR Server Request from PHP Globals", | ||
"type": "library", | ||
"license": "proprietary", | ||
"minimum-stability": "stable", | ||
"support": { | ||
"email" : "[email protected]" | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "Kevin Jansen", | ||
"email": "[email protected]", | ||
"role": "Maintainer" | ||
}, | ||
{ | ||
"name": "Wesley de Kanter", | ||
"email": "[email protected]", | ||
"role": "Maintainer" | ||
} | ||
], | ||
"require": { | ||
"php": "^7.4 | ^8", | ||
"nyholm/psr7": "^1.8", | ||
"psr/http-message": "^2.0", | ||
"nyholm/psr7-server": "^1.1" | ||
}, | ||
"require-dev": { | ||
"phpstan/phpstan": "^1.10", | ||
"squizlabs/php_codesniffer": "^3.7", | ||
"phpunit/phpunit": "^9", | ||
"php-coveralls/php-coveralls": "^2.7", | ||
"phpunit/phpcov": "^8.2" | ||
}, | ||
"autoload": { | ||
"files": [ | ||
"src/PsrServerRequestHelper.php" | ||
] | ||
}, | ||
"scripts": { | ||
"phpcs": "vendor/bin/phpcs --standard=phpcs.xml", | ||
"phpcbf" : "vendor/bin/phpcbf", | ||
"phpstan": "vendor/bin/phpstan", | ||
"phpunit" : "vendor/bin/phpunit", | ||
"phpunit-clover": "XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-clover build/logs/clover.xml", | ||
"analyse": [ | ||
"@phpcs", | ||
"@phpstan" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?xml version="1.0"?> | ||
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd"> | ||
<arg name="basepath" value="."/> | ||
<arg name="extensions" value="php"/> | ||
<arg name="parallel" value="80"/> | ||
<arg name="colors"/> | ||
|
||
<!-- Show progress of the run and show sniff names --> | ||
<arg value="ps"/> | ||
|
||
<file>src</file> | ||
|
||
<rule ref="PSR12" /> | ||
</ruleset> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
parameters: | ||
level: max | ||
paths: | ||
- src |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<!-- https://phpunit.readthedocs.io/en/latest/configuration.html --> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd" | ||
colors="true" | ||
bootstrap="vendor/autoload.php" | ||
defaultTestSuite="default" | ||
> | ||
<php> | ||
<ini name="display_errors" value="1"/> | ||
<ini name="error_reporting" value="-1"/> | ||
<ini name="memory_limit" value="-1"/> | ||
<server name="APP_ENV" value="test" force="true"/> | ||
<server name="SHELL_VERBOSITY" value="-1"/> | ||
</php> | ||
|
||
<testsuites> | ||
<testsuite name="default"> | ||
<directory>tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<coverage processUncoveredFiles="true"> | ||
<include> | ||
<directory suffix=".php">src</directory> | ||
</include> | ||
</coverage> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
use Nyholm\Psr7\Factory\Psr17Factory; | ||
use Nyholm\Psr7Server\ServerRequestCreator; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
if (function_exists('create_psr_server_request') === false) { | ||
function create_psr_server_request(): ServerRequestInterface | ||
{ | ||
$psr17Factory = new Psr17Factory(); | ||
$serverRequestCreator = new ServerRequestCreator( | ||
$psr17Factory, | ||
$psr17Factory, | ||
$psr17Factory, | ||
$psr17Factory, | ||
); | ||
|
||
$serverRequest = $serverRequestCreator->fromGlobals(); | ||
|
||
// This is to mitigate a bug in the ServerRequestCreator where it sets the Host header twice. | ||
// Once this is fixed within the ServerRequestCreator this mitigation becomes obsolete | ||
// @see https://github.com/Nyholm/psr7-server/issues/48 | ||
// @see https://github.com/Nyholm/psr7-server/pull/49 | ||
if ($serverRequest->hasHeader('Host')) { | ||
$serverRequest = $serverRequest->withHeader('Host', $serverRequest->getHeader('Host')[0]); | ||
} | ||
|
||
return $serverRequest; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psr\Http\Message\RequestInterface; | ||
|
||
final class ServerRequestTest extends TestCase | ||
{ | ||
public function testItCanCreateARequest(): RequestInterface | ||
{ | ||
$_SERVER['HTTP_HOST'] = 'hostname.com'; | ||
$_SERVER['REQUEST_METHOD'] = 'GET'; | ||
|
||
$request = create_psr_server_request(); | ||
|
||
$this->assertInstanceOf(RequestInterface::class, $request); | ||
|
||
return $request; | ||
} | ||
|
||
/** @depends testItCanCreateARequest */ | ||
public function testItHasOnlyOneHostHeader(RequestInterface $request): void | ||
{ | ||
$this->assertCount(1, $request->getHeader('Host')); | ||
$this->assertEquals('hostname.com', $request->getHeaderLine('Host')); | ||
} | ||
} |