Skip to content
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

Newer php fixes #132

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
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
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).
## [3.2.2] - 2019-02-12
### Summary
- Change tests to resolve deprecation warnings that appear under PHPUnit 8 (#93)

## [3.2.1] - 2018-10-24
### Summary
- Widen range of supported Zend Diactoros version

## [3.2.0] - 2018-09-19
### Summary
- Added support for `PATCH` HTTP method

### Added
- `Traits\Request\Patch`

## [3.1.0] - 2018-07-01
### Summary
Expand Down
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"name": "firehed/api",
"require": {
"php": "^7.0",
"php": "^7.0 || ^8.0",
"firehed/common": "^1.0",
"firehed/input": "^2.0",
"psr/container": "^1.0",
"psr/container": "^1.0 || ^2.0",
"psr/http-message": "^1.0",
"psr/http-server-handler": "^1.0",
"psr/http-server-middleware": "^1.0",
"psr/log": "^1.0",
"zendframework/zend-diactoros": "^1.3"
"zendframework/zend-diactoros": "^1.3 || ^2.0"
},
"suggest": {
"firehed/inputobjects": "Pre-made Input components for validation"
Expand All @@ -24,9 +24,9 @@
},
"require-dev": {
"firehed/arctools": "^1.0",
"phpstan/phpstan": "^0.9.2",
"phpstan/phpstan-phpunit": "^0.9.4",
"phpunit/phpunit": "^6.0 | ^7.0",
"phpstan/phpstan": "^0.9.2 || ^1.0",
"phpstan/phpstan-phpunit": "^0.9.4 || ^1.0",
"phpunit/phpunit": "^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0",
"squizlabs/php_codesniffer": "^3.1"
},
"autoload": {
Expand Down
4 changes: 4 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
includes:
- vendor/phpstan/phpstan-phpunit/extension.neon

parameters:
ignoreErrors:
- '#Call to an undefined static method PHPUnit\\Framework\\TestCase::assertIs(Array|Bool|String)\(\)#'
reportUnmatchedIgnoredErrors: false
2 changes: 2 additions & 0 deletions src/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ private function transformRequestToServerRequest(RequestInterface $request): Ser
foreach ($request->getHeaders() as $name => $values) {
$serverRequest = $serverRequest->withHeader($name, $values);
}
// ZD2 hints the return type of withHeader to MessageInterface not SRI
assert($serverRequest instanceof ServerRequestInterface);
return $serverRequest;
}

Expand Down
2 changes: 2 additions & 0 deletions src/Enums/HTTPMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* @method static HTTPMethod DELETE()
* @method static HTTPMethod GET()
* @method static HTTPMethod OPTIONS()
* @method static HTTPMethod PATCH()
* @method static HTTPMethod POST()
* @method static HTTPMethod PUT()
*/
Expand All @@ -19,6 +20,7 @@ class HTTPMethod extends Enum
// Other methods exist, but these are the only relevant ones for RESTful
// APIs
const GET = 'GET';
const PATCH = 'PATCH';
const POST = 'POST';
const PUT = 'PUT';
const DELETE = 'DELETE';
Expand Down
4 changes: 2 additions & 2 deletions src/Traits/EndpointTestCases.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
trait EndpointTestCases
{
use HandlesOwnErrorsTestCases;
use Testing\PHPUnit8Shim;
use ValidationTestTrait;

/**
Expand Down Expand Up @@ -47,8 +48,7 @@ protected function getValidation(): ValidationInterface
public function testGetUri(string $uri, bool $match, array $expectedMatches)
{
$endpoint = $this->getEndpoint();
$this->assertInternalType(
'string',
$this->assertIsString(
$endpoint->getUri(),
'getUri did not return a string'
);
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/HandlesOwnErrorsTestCases.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function testHandleException(Throwable $e)
*
* @return array<array<Exception>>
*/
public function exceptionsToHandle(): array
public static function exceptionsToHandle(): array
{
return [
[new \Exception()],
Expand Down
14 changes: 14 additions & 0 deletions src/Traits/Request/Patch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
declare(strict_types=1);

namespace Firehed\API\Traits\Request;

use Firehed\API\Enums\HTTPMethod;

trait Patch
{
public function getMethod(): HTTPMethod
{
return HTTPMethod::PATCH();
}
}
31 changes: 31 additions & 0 deletions src/Traits/Testing/PHPUnit8Shim.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);

namespace Firehed\API\Traits\Testing;

use PHPUnit\Runner\Version;

/**
* phpcs:disable
*
* Implement some horrible hacks to allow PHP7.0 users to use assertIsString
* which natively has a void return type.
*
* @internal
*/
if (class_exists(Version::class) && version_compare(Version::id(), '10.0.0', '>=')) {
trait PHPUnit8Shim
{
// Intentionally empty
}
} elseif (version_compare(PHP_VERSION, '7.1.0', '>=')) {
trait PHPUnit8Shim
{
use PHPUnit8ShimPHPGTE71;
}
} else {
trait PHPUnit8Shim
{
use PHPUnit8ShimPHPLT71;
}
}
42 changes: 42 additions & 0 deletions src/Traits/Testing/PHPUnit8ShimPHPGTE71.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);

namespace Firehed\API\Traits\Testing;

use PHPUnit\Framework\TestCase;

/**
* Create assertIsFoo static methods introduced in PHPUnit 7.5 for users with
* earlier versions
*
* @internal
*/
trait PHPUnit8ShimPHPGTE71
{
public static function assertIsArray($actual, string $message = ''): void
{
if (method_exists(TestCase::class, 'assertIsArray')) {
TestCase::assertIsArray($actual, $message);
} else {
TestCase::assertInternalType('array', $actual, $message);
}
}

public static function assertIsBool($actual, string $message = ''): void
{
if (method_exists(TestCase::class, 'assertIsBool')) {
TestCase::assertIsBool($actual, $message);
} else {
TestCase::assertInternalType('bool', $actual, $message);
}
}

public static function assertIsString($actual, string $message = ''): void
{
if (method_exists(TestCase::class, 'assertIsString')) {
TestCase::assertIsString($actual, $message);
} else {
TestCase::assertInternalType('string', $actual, $message);
}
}
}
31 changes: 31 additions & 0 deletions src/Traits/Testing/PHPUnit8ShimPHPLT71.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);

namespace Firehed\API\Traits\Testing;

use PHPUnit\Framework\TestCase;

/**
* PHP 7.0-compatible shim (stripped return types)
* Simply wraps assertInternalType since PHPUnit 7.5 (where the new versions
* became available) requires 7.1 or later.
*
* @internal
*/
trait PHPUnit8ShimPHPLT71
{
public static function assertIsArray($actual, string $message = '')
{
TestCase::assertInternalType('array', $actual, $message);
}

public static function assertIsBool($actual, string $message = '')
{
TestCase::assertInternalType('bool', $actual, $message);
}

public static function assertIsString($actual, string $message = '')
{
TestCase::assertInternalType('string', $actual, $message);
}
}
8 changes: 5 additions & 3 deletions src/renderResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
*
* @deprecated 3.1.0 Use ResponseRenderer instead
*/
function renderResponse(ResponseInterface $response)
{
ResponseRenderer::render($response);
if (!function_exists(__NAMESPACE__.'\renderResponse')) {
function renderResponse(ResponseInterface $response)
{
ResponseRenderer::render($response);
}
}
4 changes: 2 additions & 2 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function testLoad(string $file, string $exceptionClass = null)
$this->assertInstanceOf(ContainerInterface::class, $config);
}

public function constructProvider(): array
public static function constructProvider(): array
{
return [
[
Expand Down Expand Up @@ -120,7 +120,7 @@ public function constructProvider(): array
];
}

public function loadProvider(): array
public static function loadProvider(): array
{
return [
[__DIR__.'/fixtures/valid_apiconfig.json'],
Expand Down
2 changes: 1 addition & 1 deletion tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ContainerTest extends \PHPUnit\Framework\TestCase
/** @var Container */
private $c;

public function setUp()
public function setUp(): void
{
$this->c = new Container(['key' => 'value']);
}
Expand Down
15 changes: 9 additions & 6 deletions tests/DispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

namespace Firehed\API;

use BadMethodCallException;
use Exception;
use Firehed\API\Authentication;
use Firehed\API\Authorization;
use Firehed\API\Interfaces\EndpointInterface;
use Firehed\API\Errors\HandlerInterface;
use OutOfBoundsException;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
Expand All @@ -32,13 +34,13 @@ class DispatcherTest extends \PHPUnit\Framework\TestCase

private $reporting;

public function setUp()
public function setUp(): void
{
$this->reporting = error_reporting();
error_reporting($this->reporting & ~E_USER_DEPRECATED);
}

public function tearDown()
public function tearDown(): void
{
error_reporting($this->reporting);
}
Expand Down Expand Up @@ -92,6 +94,7 @@ public function testSetRequestReturnsSelf()
{
$d = new Dispatcher();
$req = $this->createMock(RequestInterface::class);
$req->method('getMethod')->willReturn('GET');
$req->method('getHeaders')->willReturn([]);
$req->method('getBody')->willReturn($this->createMock(StreamInterface::class));
$req->method('getUri')->willReturn($this->createMock(UriInterface::class));
Expand Down Expand Up @@ -424,24 +427,24 @@ public function testErrorInResponseHandler()

/**
* @covers ::dispatch
* @expectedException BadMethodCallException
* @expectedExceptionCode 500
*/
public function testDispatchThrowsWhenMissingData()
{
$d = new Dispatcher();
$this->expectException(BadMethodCallException::class);
$this->expectExceptionCode(500);
$ret = $d->dispatch();
}

/**
* @covers ::dispatch
* @expectedException OutOfBoundsException
* @expectedExceptionCode 404
*/
public function testNoRouteMatchReturns404()
{
$req = $this->getMockRequestWithUriPath('/');

$this->expectException(OutOfBoundsException::class);
$this->expectExceptionCode(404);
$ret = (new Dispatcher())
->setRequest($req)
->setEndpointList([]) // No routes
Expand Down
12 changes: 6 additions & 6 deletions tests/Traits/Authentication/BearerTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ class BearerTokenTest extends \PHPUnit\Framework\TestCase

private $reporting;

public function setUp()
public function setUp(): void
{
$this->reporting = error_reporting();
error_reporting($this->reporting & ~E_USER_DEPRECATED);
}

public function tearDown()
public function tearDown(): void
{
error_reporting($this->reporting);
}
Expand Down Expand Up @@ -116,13 +116,13 @@ private function getEndpoint($setCallback = true): EndpointInterface
use Traits\Request\Get;
use Traits\Input\NoRequired;
use Traits\Input\NoOptional;
function getUri(): string
public function getUri(): string
{
}
function handleException(\Throwable $e): Message\ResponseInterface
public function handleException(\Throwable $e): Message\ResponseInterface
{
}
function execute(SafeInput $input): Message\ResponseInterface
public function execute(SafeInput $input): Message\ResponseInterface
{
}
};
Expand All @@ -143,7 +143,7 @@ private function getRequest(string $headerValue = null): Message\RequestInterfac
}

// Data provider for testAuthenticate
public function bearerTokens(): array
public static function bearerTokens(): array
{
return [
['sometoken'],
Expand Down
Loading