From c4b1409f13d6a06a96e243237875b8ba003809ec Mon Sep 17 00:00:00 2001 From: Barnaby Walters Date: Wed, 9 Jun 2021 00:21:33 +0200 Subject: [PATCH] Cleaned up some unused use statements, added code coverage script. 79% covered already --- .gitignore | 3 ++- run_coverage.sh | 1 + src/Server.php | 5 +++++ src/functions.php | 6 +++--- tests/ServerTest.php | 4 ---- 5 files changed, 11 insertions(+), 8 deletions(-) create mode 100755 run_coverage.sh diff --git a/.gitignore b/.gitignore index 31df0e3..9de216f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .DS_Store vendor -tests/tmp/* \ No newline at end of file +tests/tmp/* +tests/coverage \ No newline at end of file diff --git a/run_coverage.sh b/run_coverage.sh new file mode 100755 index 0000000..def2b3e --- /dev/null +++ b/run_coverage.sh @@ -0,0 +1 @@ +XDEBUG_MODE=coverage ./vendor/bin/phpunit tests --coverage-filter src --coverage-html tests/coverage \ No newline at end of file diff --git a/src/Server.php b/src/Server.php index a61d355..11a1c85 100644 --- a/src/Server.php +++ b/src/Server.php @@ -139,6 +139,8 @@ class Server { if (!is_callable($httpGetWithEffectiveUrl)) { if (class_exists('\GuzzleHttp\Client')) { $httpGetWithEffectiveUrl = function (string $uri) { + // This code can’t be tested, ignore it for coverage purposes. + // @codeCoverageIgnoreStart $resp = (new \GuzzleHttp\Client([ \GuzzleHttp\RequestOptions::ALLOW_REDIRECTS => [ 'max' => 10, @@ -152,6 +154,7 @@ class Server { $effectiveUrl = empty($rdh) ? $uri : array_values($rdh)[count($rdh) - 1]; return [$resp, $effectiveUrl]; + // @codeCoverageIgnoreEnd }; } else { throw new Exception('No valid $httpGetWithEffectiveUrl was provided, and guzzlehttp/guzzle was not installed. Either require guzzlehttp/guzzle, or provide a valid callable.'); @@ -255,8 +258,10 @@ class Server { // In theory this code should never be reached, as we already checked the request for valid parameters. // However, it’s possible for hashAuthorizationRequestParameters() to return null, and if for whatever // reason it does, the library should handle that case as elegantly as possible. + // @codeCoverageIgnoreStart $this->logger->warning("Calculating the expected hash for an authorization approval request failed. This SHOULD NOT happen; if you encounter this error please contact the maintainers of taproot/indieauth."); throw IndieAuthException::create(IndieAuthException::REQUEST_MISSING_PARAMETER, $request); + // @codeCoverageIgnoreEnd } if (!array_key_exists(self::HASH_QUERY_STRING_KEY, $queryParams)) { diff --git a/src/functions.php b/src/functions.php index d8f284e..be31875 100644 --- a/src/functions.php +++ b/src/functions.php @@ -3,17 +3,16 @@ namespace Taproot\IndieAuth; use Exception; -use IndieAuth\Client; use Psr\Http\Message\ServerRequestInterface; use Psr\Log\LoggerAwareInterface; use Psr\Log\LoggerInterface; -use function BarnabyWalters\Mf2\parseUrl; - // From https://github.com/indieweb/indieauth-client-php/blob/main/src/IndieAuth/Client.php, thanks aaronpk. function generateRandomString($numBytes) { if (function_exists('random_bytes')) { $bytes = random_bytes($numBytes); + // We can’t easily test the following code. + // @codeCoverageIgnoreStart } elseif (function_exists('openssl_random_pseudo_bytes')){ $bytes = openssl_random_pseudo_bytes($numBytes); } else { @@ -21,6 +20,7 @@ function generateRandomString($numBytes) { for($i=0, $bytes=''; $i < $numBytes; $i++) { $bytes .= chr(mt_rand(0, 255)); } + // @codeCoverageIgnoreEnd } return bin2hex($bytes); } diff --git a/tests/ServerTest.php b/tests/ServerTest.php index 7cea1f1..3844bd6 100644 --- a/tests/ServerTest.php +++ b/tests/ServerTest.php @@ -6,15 +6,11 @@ use Exception; use Nyholm\Psr7\Response; use Nyholm\Psr7\ServerRequest; use Nyholm\Psr7\Request; -use PDO; use PHPUnit\Framework\TestCase; -use Psr\Http\Message\RequestInterface; -use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Taproot\IndieAuth\Callback\DefaultAuthorizationForm; use Taproot\IndieAuth\Callback\SingleUserPasswordAuthenticationCallback; use Taproot\IndieAuth\IndieAuthException; -use Taproot\IndieAuth\Middleware\NoOpMiddleware; use Taproot\IndieAuth\Server; use Taproot\IndieAuth\Storage\FilesystemJsonStorage;