Skip to content

Commit

Permalink
Fixes for PHPUnit 8 (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
Firehed authored Feb 12, 2019
1 parent 2f0a045 commit c4a3fa7
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 15 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
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
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
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
24 changes: 24 additions & 0 deletions src/Traits/Testing/PHPUnit8Shim.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);

namespace Firehed\API\Traits\Testing;

/**
* phpcs:disable
*
* Implement some horrible hacks to allow PHP7.0 users to use assertIsString
* which natively has a void return type.
*
* @internal
*/
if (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);
}
}
10 changes: 6 additions & 4 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 Down Expand Up @@ -425,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
6 changes: 3 additions & 3 deletions tests/Traits/Authentication/BearerTokenTest.php
Original file line number Diff line number Diff line change
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 Down
6 changes: 3 additions & 3 deletions tests/Traits/Authentication/NoneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ public function testAuthenticate()
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 Down
6 changes: 3 additions & 3 deletions tests/Traits/EndpointTestCasesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ public function testUris()
$data = $this->uris();
foreach ($data as $testCase) {
list($uri, $shouldMatch, $matches) = $testCase;
$this->assertInternalType('string', $uri);
$this->assertInternalType('bool', $shouldMatch);
$this->assertInternalType('array', $matches);
$this->assertIsString($uri);
$this->assertIsBool($shouldMatch);
$this->assertIsArray($matches);
}
}

Expand Down

0 comments on commit c4a3fa7

Please sign in to comment.