Skip to content

Commit

Permalink
Code improvements and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
PopNatanael committed Jul 20, 2023
1 parent fa1091e commit 7255849
Show file tree
Hide file tree
Showing 12 changed files with 150 additions and 47 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@
* Nothing

### Fixed
* Nothing
* Nothing
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# dot-response-header

![OSS Lifecycle](https://img.shields.io/osslifecycle/dotkernel/dot-response-header)
![PHP from Packagist (specify version)](https://img.shields.io/packagist/php-v/dotkernel/dot-response-header/3.1.0)
![PHP from Packagist (specify version)](https://img.shields.io/packagist/php-v/dotkernel/dot-response-header/3.2.0)

[![GitHub issues](https://img.shields.io/github/issues/dotkernel/dot-response-header)](https://github.com/dotkernel/dot-response-header/issues)
[![GitHub forks](https://img.shields.io/github/forks/dotkernel/dot-response-header)](https://github.com/dotkernel/dot-response-header/network)
[![GitHub stars](https://img.shields.io/github/stars/dotkernel/dot-response-header)](https://github.com/dotkernel/dot-response-header/stargazers)
[![GitHub license](https://img.shields.io/github/license/dotkernel/dot-response-header)](https://github.com/dotkernel/response-header/LICENSE.md)
[![GitHub license](https://img.shields.io/github/license/dotkernel/dot-response-header)](https://github.com/dotkernel/dot-response-header/blob/3.0/LICENSE)

[![Build Static](https://github.com/dotkernel/dot-response-header/actions/workflows/static-analysis.yml/badge.svg?branch=3.0)](https://github.com/dotkernel/dot-response-header/actions/workflows/static-analysis.yml)

[![SymfonyInsight](https://insight.symfony.com/projects/dce88959-bd29-40ef-b1e7-d12815145438/big.svg)](https://insight.symfony.com/projects/dce88959-bd29-40ef-b1e7-d12815145438)

Expand All @@ -15,7 +17,7 @@ Middleware for setting and overwriting custom response headers.


### Requirements
- PHP >= 7.4
- PHP >= 8.1

### Installation

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@
},
"autoload": {
"psr-4": {
"Dot\\ResponseHeader\\": "src"
"Dot\\ResponseHeader\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"DotTest\\ResponseHeader\\": "tests/"
"DotTest\\ResponseHeader\\": "test/"
}
},
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion config/response-header.global.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ return [
]
]
]
];
];
2 changes: 1 addition & 1 deletion phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<!-- Paths to check -->
<file>config</file>
<file>src</file>
<file>tests</file>
<file>test</file>

<!-- Include all rules from the Laminas Coding Standard -->
<rule ref="LaminasCodingStandard"/>
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<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>
<directory>./test</directory>
</testsuite>
</testsuites>
<coverage/>
Expand Down
27 changes: 27 additions & 0 deletions src/Factory/ResponseHeaderMiddlewareFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,39 @@
namespace Dot\ResponseHeader\Factory;

use Dot\ResponseHeader\Middleware\ResponseHeaderMiddleware;
use Exception;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;

use function array_key_exists;
use function is_array;

class ResponseHeaderMiddlewareFactory
{
public const MESSAGE_MISSING_CONFIG = 'Unable to find config.';
public const MESSAGE_MISSING_PACKAGE_CONFIG = 'Unable to find dot-response-header config.';

/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws Exception
*/
public function __invoke(ContainerInterface $container): ResponseHeaderMiddleware
{
if (! $container->has('config')) {
throw new Exception(self::MESSAGE_MISSING_CONFIG);
}
$config = $container->get('config');

if (
! array_key_exists('dot_response_headers', $config)
|| ! is_array($config['dot_response_headers'])
|| empty($config['dot_response_headers'])
) {
throw new Exception(self::MESSAGE_MISSING_PACKAGE_CONFIG);
}

return new ResponseHeaderMiddleware($container->get('config')['dot_response_headers'] ?? []);
}
}
2 changes: 1 addition & 1 deletion src/Middleware/ResponseHeaderMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function addHeaders(ResponseInterface $response, string $route): Response
if (! array_key_exists('value', $data)) {
continue;
}
$overwrite = isset($data['overwrite']) && $data['overwrite'] === true ? true : false;
$overwrite = isset($data['overwrite']) && $data['overwrite'] === true;
if ($overwrite) {
$response = $response->withHeader($header, $data['value']);
}
Expand Down
32 changes: 32 additions & 0 deletions test/ConfigProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace DotTest\ResponseHeader;

use Dot\ResponseHeader\ConfigProvider;
use PHPUnit\Framework\TestCase;

class ConfigProviderTest extends TestCase
{
private ConfigProvider $configProvider;

public function setUp(): void
{
$this->configProvider = new ConfigProvider();
}

public function testInvoke()
{
$data = $this->configProvider->__invoke();

$this->assertIsArray($data);
}

public function testGetDependencies()
{
$data = $this->configProvider->getDependencies();

$this->assertIsArray($data);
}
}
74 changes: 74 additions & 0 deletions test/ResponseHeaderMiddlewareFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

namespace DotTest\ResponseHeader;

use Dot\ResponseHeader\Factory\ResponseHeaderMiddlewareFactory;
use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;

class ResponseHeaderMiddlewareFactoryTest extends TestCase
{
private ResponseHeaderMiddlewareFactory $responseHeaderMiddlewareFactory;

private ContainerInterface|MockObject $containerInterface;

/**
* @throws Exception
*/
public function setUp(): void
{
$this->responseHeaderMiddlewareFactory = new ResponseHeaderMiddlewareFactory();
$this->containerInterface = $this->createMock(ContainerInterface::class);
}

/**
* @throws ContainerExceptionInterface
* @throws Exception
* @throws NotFoundExceptionInterface
*/
public function testWillNotCreateApplicationWithoutConfig(): void
{
$container = $this->createMock(ContainerInterface::class);

$container->expects($this->once())
->method('has')
->with('config')
->willReturn(false);

$this->expectException(\Exception::class);
$this->expectExceptionMessage(ResponseHeaderMiddlewareFactory::MESSAGE_MISSING_CONFIG);
(new ResponseHeaderMiddlewareFactory())($container);
}

/**
* @throws Exception
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function testWillNotCreateApplicationWithoutPackageConfig(): void
{
$container = $this->createMock(ContainerInterface::class);

$container->expects($this->once())
->method('has')
->with('config')
->willReturn(true);

$container->expects($this->once())
->method('get')
->with('config')
->willReturn([
'test',
]);

$this->expectException(\Exception::class);
$this->expectExceptionMessage(ResponseHeaderMiddlewareFactory::MESSAGE_MISSING_PACKAGE_CONFIG);
(new ResponseHeaderMiddlewareFactory())($container);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace DotTest\ResponseHeader;

use Dot\ResponseHeader\Middleware\ResponseHeaderMiddleware;
use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
Expand All @@ -22,10 +23,11 @@ class ResponseHeaderMiddlewareTest extends TestCase

private ResponseInterface $responseInterface;

/**
* @throws Exception
*/
public function setUp(): void
{
parent::setUp();

$this->responseHeader = new ResponseHeaderMiddleware([]);
$this->serverRequest = $this->createMock(ServerRequestInterface::class);
$this->requestHandler = $this->createMock(RequestHandlerInterface::class);
Expand All @@ -47,7 +49,7 @@ public function testProcess()

public function testAddHeaders()
{
$data = $this->responseHeader->addHeaders($this->responseInterface, 'home');
$data = $this->responseHeader->addHeaders($this->responseInterface, '');

$this->assertInstanceOf(ResponseInterface::class, $data);
}
Expand Down
34 changes: 0 additions & 34 deletions tests/ResponseHeaderMiddlewareFactoryTest.php

This file was deleted.

0 comments on commit 7255849

Please sign in to comment.