Skip to content

Commit

Permalink
Issue #7: Implemented unit tests, cs-check, static analysis.
Browse files Browse the repository at this point in the history
  • Loading branch information
PopNatanael committed Jul 12, 2023
1 parent 919bc50 commit 5de158b
Show file tree
Hide file tree
Showing 13 changed files with 325 additions and 44 deletions.
46 changes: 46 additions & 0 deletions .github/workflows/cs-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
on:
- push

name: Run phpcs checks

jobs:
mutation:
name: PHP ${{ matrix.php }}-${{ matrix.os }}

runs-on: ${{ matrix.os }}

strategy:
matrix:
os:
- ubuntu-latest

php:
- "8.1"
- "8.2"

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: "${{ matrix.php }}"
tools: composer:v2, cs2pr
coverage: none

- name: Determine composer cache directory
run: echo "COMPOSER_CACHE_DIR=$(composer config cache-dir)" >> $GITHUB_ENV

- name: Cache dependencies installed with composer
uses: actions/cache@v3
with:
path: ${{ env.COMPOSER_CACHE_DIR }}
key: php${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: |
php${{ matrix.php }}-composer-
- name: Install dependencies with composer
run: composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi

- name: Run phpcs checks
run: vendor/bin/phpcs
46 changes: 46 additions & 0 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
on:
- push

name: Run static analysis

jobs:
mutation:
name: PHP ${{ matrix.php }}-${{ matrix.os }}

runs-on: ${{ matrix.os }}

strategy:
matrix:
os:
- ubuntu-latest

php:
- "8.1"
- "8.2"

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: "${{ matrix.php }}"
tools: composer:v2, cs2pr
coverage: none

- name: Determine composer cache directory
run: echo "COMPOSER_CACHE_DIR=$(composer config cache-dir)" >> $GITHUB_ENV

- name: Cache dependencies installed with composer
uses: actions/cache@v3
with:
path: ${{ env.COMPOSER_CACHE_DIR }}
key: php${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: |
php${{ matrix.php }}-composer-
- name: Install dependencies with composer
run: composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi

- name: Run static analysis
run: vendor/bin/psalm --no-cache --output-format=github --show-info=false --threads=4
47 changes: 47 additions & 0 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
on:
- push

name: Run PHPUnit tests

jobs:
mutation:
name: PHP ${{ matrix.php }}-${{ matrix.os }}

runs-on: ${{ matrix.os }}

strategy:
matrix:
os:
- ubuntu-latest

php:
- "8.1"
- "8.2"

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: "${{ matrix.php }}"
tools: composer:v2, cs2pr
coverage: none

- name: Determine composer cache directory
run: echo "COMPOSER_CACHE_DIR=$(composer config cache-dir)" >> $GITHUB_ENV

- name: Cache dependencies installed with composer
uses: actions/cache@v3
with:
path: ${{ env.COMPOSER_CACHE_DIR }}
key: php${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: |
php${{ matrix.php }}-composer-
- name: Install dependencies with composer
run: composer install --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi

- name: Run PHPUnit tests
run: vendor/bin/phpunit --colors=always
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
composer.phar
/vendor/
.phpcs-cache
.idea
composer.lock
.phpunit.result.cache
34 changes: 30 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,41 @@
"mezzio"
],
"require": {
"php": "~7.4.0 || ~8.0.0 || ~8.1.0"
"php": "~8.1.0 || ~8.2.0",
"mezzio/mezzio-router": "^3.16",
"psr/http-client": "^1.0",
"psr/http-message": "^2.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5.20",
"squizlabs/php_codesniffer": "^3.6.2"
"phpunit/phpunit": "^10.2",
"laminas/laminas-coding-standard": "^2.5",
"vimeo/psalm": "^5.13"
},
"autoload": {
"psr-4": {
"Dot\\ResponseHeader\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"DotTest\\ResponseHeader\\": "tests/"
}
},
"scripts": {
"check": [
"@cs-check",
"@test"
],
"cs-check": "phpcs",
"cs-fix": "phpcbf",
"test": "phpunit --colors=always",
"test-coverage": "phpunit --colors=always --coverage-clover clover.xml",
"static-analysis": "psalm --shepherd --stats"
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
},
"sort-packages": true
}
}
}
21 changes: 21 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?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="cache" value=".phpcs-cache"/>
<arg name="colors"/>
<arg name="extensions" value="php"/>
<arg name="parallel" value="80"/>

<!-- Show progress -->
<arg value="p"/>

<!-- Paths to check -->
<file>config</file>
<file>src</file>
<file>tests</file>

<!-- Include all rules from the Laminas Coding Standard -->
<rule ref="LaminasCodingStandard"/>
</ruleset>
14 changes: 14 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd" bootstrap="./vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="dot-response-header Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
<coverage/>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
17 changes: 17 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0"?>
<psalm
errorLevel="4"
resolveFromConfigFile="true"
findUnusedCode="false"
findUnusedBaselineEntry="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
</psalm>
13 changes: 2 additions & 11 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
@@ -1,30 +1,21 @@
<?php

declare(strict_types=1);

namespace Dot\ResponseHeader;

use Dot\ResponseHeader\Factory\ResponseHeaderMiddlewareFactory;
use Dot\ResponseHeader\Middleware\ResponseHeaderMiddleware;

/**
* Class ConfigProvider
* @package Dot\ResponseHeader
*/
class ConfigProvider
{
/**
* @return array
*/
public function __invoke(): array
{
return [
'dependencies' => $this->getDependencies(),
];
}

/**
* @return array
*/
public function getDependencies(): array
{
return [
Expand All @@ -33,4 +24,4 @@ public function getDependencies(): array
],
];
}
}
}
11 changes: 2 additions & 9 deletions src/Factory/ResponseHeaderMiddlewareFactory.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
<?php

declare(strict_types=1);

namespace Dot\ResponseHeader\Factory;

use Dot\ResponseHeader\Middleware\ResponseHeaderMiddleware;
use Psr\Container\ContainerInterface;

/**
* Class ResponseHeaderMiddlewareFactory
* @package Dot\ResponseHeader\Factory
*/
class ResponseHeaderMiddlewareFactory
{
/**
* @param ContainerInterface $container
* @return ResponseHeaderMiddleware
*/
public function __invoke(ContainerInterface $container): ResponseHeaderMiddleware
{
return new ResponseHeaderMiddleware($container->get('config')['dot_response_headers'] ?? []);
}
}
}
27 changes: 7 additions & 20 deletions src/Middleware/ResponseHeaderMiddleware.php
Original file line number Diff line number Diff line change
@@ -1,69 +1,56 @@
<?php

declare(strict_types=1);

namespace Dot\ResponseHeader\Middleware;


use Mezzio\Router\RouteResult;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

use function array_key_exists;

class ResponseHeaderMiddleware implements MiddlewareInterface
{
private const ALL_ROUTES = '*';

/** @var array $config */
private array $config;

/**
* ResponseHeaderMiddleware constructor.
* @param array $config
*/
public function __construct(array $config)
{
$this->config = $config;
}

/**
* @param ServerRequestInterface $request
* @param RequestHandlerInterface $handler
* @return ResponseInterface
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$response = $handler->handle($request);
if (empty($this->config)) {
return $response;
}

$response = $this->addHeaders($response, self::ALL_ROUTES);
$response = $this->addHeaders($response, self::ALL_ROUTES);
$routeResult = $request->getAttribute(RouteResult::class);
if ($routeResult instanceof RouteResult && $routeResult->isSuccess()) {
$response = $this->addHeaders($response, $routeResult->getMatchedRouteName());
}
return $response;
}

/**
* @param ResponseInterface $response
* @param string $route
* @return ResponseInterface
*/
private function addHeaders(ResponseInterface $response, string $route): ResponseInterface
public function addHeaders(ResponseInterface $response, string $route): ResponseInterface
{
if (array_key_exists($route, $this->config)) {
foreach ($this->config[$route] as $header => $data) {
if (! array_key_exists('value', $data)) {
continue;
}
$overwrite = (isset($data['overwrite']) && $data['overwrite'] === true) ? true : false;
$overwrite = isset($data['overwrite']) && $data['overwrite'] === true ? true : false;
if ($overwrite) {
$response = $response->withHeader($header, $data['value']);
}
}
}
return $response;
}
}
}
Loading

0 comments on commit 5de158b

Please sign in to comment.