Skip to content

Commit

Permalink
Merge pull request #848 from Art4/phpunit-10
Browse files Browse the repository at this point in the history
Enable test execution with PHPUnit 10
  • Loading branch information
mblaney committed Sep 20, 2023
2 parents 8f45077 + facbe97 commit be9a239
Show file tree
Hide file tree
Showing 34 changed files with 236 additions and 176 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ vendor/
composer.lock
phpunit.xml
.php-cs-fixer.cache
.phpunit.result.cache
.phpunit.cache/
1 change: 0 additions & 1 deletion build/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@
declare(strict_types=1);

require_once dirname(__DIR__) . '/SimplePie.compiled.php';
require_once dirname(__DIR__) . '/vendor/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php';
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
"friendsofphp/php-cs-fixer": "^2.19 || ^3.8",
"mf2/mf2": "^0.5.0",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^8 || ^9 || ^10",
"psr/http-client": "^1.0",
"psr/http-factory": "^1.0",
"psr/simple-cache": "^1 || ^2 || ^3",
"yoast/phpunit-polyfills": "^1.0.1"
"psr/simple-cache": "^1 || ^2 || ^3"
},
"suggest": {
"ext-curl": "",
Expand Down Expand Up @@ -64,6 +64,7 @@
"sort-packages": true
},
"scripts": {
"coverage": "phpunit --coverage-html=.phpunit.cache/code-coverage",
"cs": "php-cs-fixer fix --verbose --dry-run --diff",
"fix": "php-cs-fixer fix --verbose --diff",
"phpstan": "phpstan analyze --memory-limit 512M",
Expand Down
7 changes: 4 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" bootstrap="tests/bootstrap.php" colors="true" convertDeprecationsToExceptions="true">
<coverage includeUncoveredFiles="true">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd" bootstrap="tests/bootstrap.php" colors="true" cacheDirectory=".phpunit.cache">
<coverage includeUncoveredFiles="true"/>
<source>
<include>
<directory suffix=".php">library</directory>
<directory suffix=".php">src</directory>
</include>
</coverage>
</source>
<testsuites>
<testsuite name="SimplePie Test Suite">
<directory>./tests</directory>
Expand Down
6 changes: 4 additions & 2 deletions src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,12 @@ public function __construct(string $url, int $timeout = 10, int $redirects = 5,
}
} else {
$this->method = \SimplePie\SimplePie::FILE_SOURCE_LOCAL | \SimplePie\SimplePie::FILE_SOURCE_FILE_GET_CONTENTS;
if (empty($url) || !($this->body = trim(file_get_contents($url)))) {
$this->error = 'file_get_contents() could not read the file';
if (empty($url) || ! is_readable($url) || false === $filebody = file_get_contents($url)) {
$this->body = '';
$this->error = sprintf('file "%s" is not readable', $url);
$this->success = false;
} else {
$this->body = trim($filebody);
$this->status_code = 200;
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/HTTP/Psr18Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ private function requestUrl(string $method, string $url, array $headers): Respon

private function requestLocalFile(string $path): Response
{
if (! is_readable($path)) {
throw new HttpException(sprintf('file "%s" is not readable', $path));
}

try {
$raw = file_get_contents($path);
} catch (Throwable $th) {
Expand Down
4 changes: 3 additions & 1 deletion tests/BCTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@

declare(strict_types=1);

use PHPUnit\Framework\TestCase;

/**
* Encoding tests for SimplePie_Misc::change_encoding() and SimplePie_Misc::encoding()
*/
class BCTest extends PHPUnit\Framework\TestCase
class BCTest extends TestCase
{
/**
* Test class SimplePie_Core exists
Expand Down
31 changes: 25 additions & 6 deletions tests/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@

declare(strict_types=1);

namespace SimplePie\Tests;

use Exception;
use PHPUnit\Framework\TestCase;
use SimplePie;
use SimplePie\Cache;
use SimplePie\File;
use SimplePie\Tests\Fixtures\Exception\SuccessException;
use SimplePie\Tests\Fixtures\FileMock;
use Yoast\PHPUnitPolyfills\Polyfills\ExpectPHPException;
use SimplePie_Cache;
use TypeError;

class Mock_CacheLegacy extends SimplePie_Cache
{
Expand All @@ -35,10 +41,8 @@ public function create($location, $filename, $extension)
}
}

class CacheTest extends PHPUnit\Framework\TestCase
class CacheTest extends TestCase
{
use ExpectPHPException;

public function testDirectOverrideLegacy(): void
{
if (version_compare(PHP_VERSION, '8.0', '<')) {
Expand All @@ -47,11 +51,26 @@ public function testDirectOverrideLegacy(): void
// PHP 8.0 will throw a `TypeError` for trying to call a non-static method statically.
// This is no longer supported in PHP, so there is just no way to continue to provide BC
// for the old non-static cache methods.
$this->expectError();
$this->expectException(TypeError::class);
$this->expectExceptionMessage('call_user_func_array(): Argument #1 ($callback) must be a valid callback, non-static method SimplePie\Tests\Mock_CacheLegacy::create() cannot be called statically');
}

$feed = new SimplePie();
$this->expectDeprecation();

// PHPUnit 10 compatible way to test trigger_error().
set_error_handler(
function ($errno, $errstr): bool {
$this->assertSame(
'"SimplePie\SimplePie::set_cache_class()" is deprecated since SimplePie 1.3, please use "SimplePie\SimplePie::set_cache()" instead.',
$errstr
);

restore_error_handler();
return true;
},
E_USER_DEPRECATED
);

$feed->set_cache_class(Mock_CacheLegacy::class);
$feed->get_registry()->register(File::class, FileMock::class);
$feed->set_feed_url('http://example.com/feed/');
Expand Down
4 changes: 3 additions & 1 deletion tests/EncodingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@

declare(strict_types=1);

use PHPUnit\Framework\TestCase;

/**
* Encoding tests for SimplePie_Misc::change_encoding() and SimplePie_Misc::encoding()
*/
class EncodingTest extends PHPUnit\Framework\TestCase
class EncodingTest extends TestCase
{
/* ## UTF-8 methods */

Expand Down
4 changes: 3 additions & 1 deletion tests/HTTPParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@

declare(strict_types=1);

use PHPUnit\Framework\TestCase;

/**
* HTTP parsing tests
*/
class HTTPParserTest extends PHPUnit\Framework\TestCase
class HTTPParserTest extends TestCase
{
/**
* @return array<array{string, string}>
Expand Down
23 changes: 17 additions & 6 deletions tests/IRITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@

declare(strict_types=1);

use Yoast\PHPUnitPolyfills\Polyfills\ExpectPHPException;
use PHPUnit\Framework\TestCase;

/**
* IRI test cases
*/
class IRITest extends PHPUnit\Framework\TestCase
class IRITest extends TestCase
{
use ExpectPHPException;

/**
* @return array<array{string, string}>
*/
Expand Down Expand Up @@ -450,10 +448,23 @@ public function testWriteAliased(): void

public function testNonexistantProperty(): void
{
$this->expectNotice();

$iri = new SimplePie_IRI();
$this->assertFalse(isset($iri->nonexistant_prop));

// PHPUnit 10 compatible way to test trigger_error().
set_error_handler(
function ($errno, $errstr): bool {
$this->assertSame(
'Undefined property: SimplePie\IRI::nonexistant_prop',
$errstr
);

restore_error_handler();
return true;
},
E_USER_NOTICE
);

$should_fail = $iri->nonexistant_prop;
}

Expand Down
5 changes: 1 addition & 4 deletions tests/Integration/CachingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@
use SimplePie\SimplePie;
use SimplePie\Tests\Fixtures\Cache\BaseCacheWithCallbacksMock;
use SimplePie\Tests\Fixtures\FileMock;
use Yoast\PHPUnitPolyfills\Polyfills\ExpectPHPException;

class CachingTest extends TestCase
{
use ExpectPHPException;

/**
* @dataProvider provideSavedCacheData
* @param array<string, mixed> $currentDataCached
Expand Down Expand Up @@ -121,7 +118,7 @@ public function testInitWithDifferentCacheStateCallsCacheCorrectly(
/**
* @return array<array{string, array<string, mixed>, array<string, mixed>, int}>
*/
public function provideSavedCacheData(): array
public static function provideSavedCacheData(): array
{
$defaultMtime = time();
$defaultExpirationTime = $defaultMtime + 3600;
Expand Down
60 changes: 36 additions & 24 deletions tests/Integration/HTTP/ClientsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,25 @@

class ClientsTest extends TestCase
{
/**
* @return array<Client[]>
*/
public function provideHttpClientsForLocalFiles(): iterable
public function testFileClientGetContentOfLocalFile(): void
{
yield [new FileClient(new Registry())];
$this->runTestsWithClientGetContentOfLocalFile(
new FileClient(new Registry())
);
}

yield [new Psr18Client(
$this->createMock(ClientInterface::class),
$this->createMock(RequestFactoryInterface::class),
$this->createMock(UriFactoryInterface::class)
)];
public function testPrs18ClientGetContentOfLocalFile(): void
{
$this->runTestsWithClientGetContentOfLocalFile(
new Psr18Client(
$this->createMock(ClientInterface::class),
$this->createMock(RequestFactoryInterface::class),
$this->createMock(UriFactoryInterface::class)
)
);
}

/**
* @dataProvider provideHttpClientsForLocalFiles
*/
public function testClientGetContentOfLocalFile(Client $client): void
private function runTestsWithClientGetContentOfLocalFile(Client $client): void
{
$filepath = dirname(__FILE__, 3) . '/data/feed_rss-2.0.xml';

Expand All @@ -51,21 +52,32 @@ public function testClientGetContentOfLocalFile(Client $client): void
$this->assertStringStartsWith('<rss version="2.0">', $response->get_body_content());
}

/**
* @dataProvider provideHttpClientsForLocalFiles
*/
public function testClientThrowsHttpException(Client $client): void
public function testFileClientThrowsHttpException(): void
{
$this->runTestWithClientThrowsHttpException(
new FileClient(new Registry())
);
}

public function testPsr18ClientThrowsHttpException(): void
{
$this->runTestWithClientThrowsHttpException(
new Psr18Client(
$this->createMock(ClientInterface::class),
$this->createMock(RequestFactoryInterface::class),
$this->createMock(UriFactoryInterface::class)
)
);
}

private function runTestWithClientThrowsHttpException(Client $client): void
{
$filepath = dirname(__FILE__, 3) . '/data/this-file-does-not-exist';

$this->expectException(HttpException::class);
$this->expectExceptionCode(2);
$this->expectExceptionCode(0);

if (version_compare(PHP_VERSION, '8.0', '<')) {
$this->expectExceptionMessage('file_get_contents('.$filepath.'): failed to open stream: No such file or directory');
} else {
$this->expectExceptionMessage('file_get_contents('.$filepath.'): Failed to open stream: No such file or directory');
}
$this->expectExceptionMessage(sprintf('file "%s" is not readable', $filepath));

$client->request(Client::METHOD_GET, $filepath);
}
Expand Down
4 changes: 3 additions & 1 deletion tests/ItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@

declare(strict_types=1);

use PHPUnit\Framework\TestCase;

/**
* Tests for SimplePie\Item
*/
class ItemTest extends PHPUnit\Framework\TestCase
class ItemTest extends TestCase
{
/**
* Run a test using a sprintf template and data
Expand Down
6 changes: 2 additions & 4 deletions tests/LocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@

declare(strict_types=1);

use PHPUnit\Framework\TestCase;
use SimplePie\File;
use SimplePie\Tests\Fixtures\FileMock;
use Yoast\PHPUnitPolyfills\Polyfills\ExpectPHPException;

/**
* Tests for autodiscovery
*/
class LocatorTest extends PHPUnit\Framework\TestCase
class LocatorTest extends TestCase
{
use ExpectPHPException;

/**
* @return array<array{string}>
*/
Expand Down
6 changes: 4 additions & 2 deletions tests/SanitizeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

declare(strict_types=1);

class SanitizeTest extends PHPUnit\Framework\TestCase
use PHPUnit\Framework\TestCase;

class SanitizeTest extends TestCase
{
public function testSanitize(): void
{
Expand All @@ -31,7 +33,7 @@ public function testSanitize(): void
/**
* @return array<array{string, string}>
*/
public function sanitizeURLProvider(): array
public static function sanitizeURLProvider(): array
{
return [
'simple absolute valid a href, resolved' => [
Expand Down
3 changes: 2 additions & 1 deletion tests/SubscribeUrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@

declare(strict_types=1);

use PHPUnit\Framework\TestCase;
use SimplePie\File;
use SimplePie\Tests\Fixtures\FileWithRedirectMock;

class SubscribeUrlTest extends PHPUnit\Framework\TestCase
class SubscribeUrlTest extends TestCase
{
public function testDirectOverrideLegacy(): void
{
Expand Down

0 comments on commit be9a239

Please sign in to comment.