From 06d3af0ac390e9dfed7733b90e31859ffbf1bbe4 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Fri, 24 Jan 2020 15:54:08 -0500 Subject: [PATCH] Fix remaining Picofeed-related test failures --- lib/Feed/Exception.php | 5 +---- tests/cases/CLI/TestCLI.php | 3 ++- tests/cases/Database/SeriesSubscription.php | 3 ++- tests/cases/Feed/TestFetching.php | 2 +- tests/cases/REST/TinyTinyRSS/TestAPI.php | 9 +++++---- tests/lib/AbstractTest.php | 14 ++++++++++++++ 6 files changed, 25 insertions(+), 11 deletions(-) diff --git a/lib/Feed/Exception.php b/lib/Feed/Exception.php index 3cab2c93..b9d20560 100644 --- a/lib/Feed/Exception.php +++ b/lib/Feed/Exception.php @@ -29,10 +29,7 @@ class Exception extends \JKingWeb\Arsse\AbstractException { $msgID = "tooManyRedirects"; break; default: - $c = $e->getCode(); - if ($c >= 400 && $c < 600) { - $msgID = "transmissionError"; - } + $msgID = "transmissionError"; } } elseif ($e instanceof TooManyRedirectsException) { $msgID = "maxRedirect"; diff --git a/tests/cases/CLI/TestCLI.php b/tests/cases/CLI/TestCLI.php index 6056912b..ef302f22 100644 --- a/tests/cases/CLI/TestCLI.php +++ b/tests/cases/CLI/TestCLI.php @@ -6,6 +6,7 @@ declare(strict_types=1); namespace JKingWeb\Arsse\TestCase\CLI; +use GuzzleHttp\Exception\ClientException; use JKingWeb\Arsse\Arsse; use JKingWeb\Arsse\Conf; use JKingWeb\Arsse\User; @@ -81,7 +82,7 @@ class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest { public function testRefreshAFeed(string $cmd, int $exitStatus, string $output): void { Arsse::$db = \Phake::mock(Database::class); \Phake::when(Arsse::$db)->feedUpdate(1, true)->thenReturn(true); - \Phake::when(Arsse::$db)->feedUpdate(2, true)->thenThrow(new \JKingWeb\Arsse\Feed\Exception("http://example.com/", new \PicoFeed\Client\InvalidUrlException)); + \Phake::when(Arsse::$db)->feedUpdate(2, true)->thenThrow(new \JKingWeb\Arsse\Feed\Exception("http://example.com/", $this->mockGuzzleException(ClientException::class, "", 404))); $this->assertConsole($this->cli, $cmd, $exitStatus, $output); \Phake::verify($this->cli)->loadConf; \Phake::verify(Arsse::$db)->feedUpdate; diff --git a/tests/cases/Database/SeriesSubscription.php b/tests/cases/Database/SeriesSubscription.php index 434427e5..a89319e9 100644 --- a/tests/cases/Database/SeriesSubscription.php +++ b/tests/cases/Database/SeriesSubscription.php @@ -6,6 +6,7 @@ declare(strict_types=1); namespace JKingWeb\Arsse\TestCase\Database; +use GuzzleHttp\Exception\ClientException; use JKingWeb\Arsse\Arsse; use JKingWeb\Arsse\Test\Database; use JKingWeb\Arsse\Feed\Exception as FeedException; @@ -200,7 +201,7 @@ trait SeriesSubscription { public function testAddASubscriptionToAnInvalidFeed(): void { $url = "http://example.org/feed1"; $feedID = $this->nextID("arsse_feeds"); - \Phake::when(Arsse::$db)->feedUpdate->thenThrow(new FeedException($url, new \PicoFeed\Client\InvalidUrlException())); + \Phake::when(Arsse::$db)->feedUpdate->thenThrow(new FeedException($url, $this->mockGuzzleException(ClientException::class, "", 404))); $this->assertException("invalidUrl", "Feed"); try { Arsse::$db->subscriptionAdd($this->user, $url, "", "", false); diff --git a/tests/cases/Feed/TestFetching.php b/tests/cases/Feed/TestFetching.php index 4ac7ece5..3ce3e8d2 100644 --- a/tests/cases/Feed/TestFetching.php +++ b/tests/cases/Feed/TestFetching.php @@ -58,7 +58,7 @@ class TestFetching extends \JKingWeb\Arsse\Test\AbstractTest { } public function testHandleAnOverlyLargeFeed(): void { - $this->markTestIncomplete(); + $this->markTestIncomplete("The nicolus/picofeed library does not implement miniflux/picofeed's max-size setting"); Arsse::$conf->fetchSizeLimit = 512; $this->assertException("maxSize", "Feed"); new Feed(null, $this->base."Fetching/TooLarge"); diff --git a/tests/cases/REST/TinyTinyRSS/TestAPI.php b/tests/cases/REST/TinyTinyRSS/TestAPI.php index 8f94c76c..0db7f1f8 100644 --- a/tests/cases/REST/TinyTinyRSS/TestAPI.php +++ b/tests/cases/REST/TinyTinyRSS/TestAPI.php @@ -6,6 +6,7 @@ declare(strict_types=1); namespace JKingWeb\Arsse\TestCase\REST\TinyTinyRSS; +use GuzzleHttp\Exception\ClientException; use JKingWeb\Arsse\Arsse; use JKingWeb\Arsse\User; use JKingWeb\Arsse\Database; @@ -785,12 +786,12 @@ LONG_STRING; ]; $out = [ ['code' => 1, 'feed_id' => 2], - ['code' => 5, 'message' => (new \JKingWeb\Arsse\Feed\Exception("http://example.com/1", new \PicoFeed\Client\UnauthorizedException()))->getMessage()], + ['code' => 5, 'message' => (new \JKingWeb\Arsse\Feed\Exception("http://example.com/1", $this->mockGuzzleException(ClientException::class, "", 401)))->getMessage()], ['code' => 1, 'feed_id' => 0], ['code' => 0, 'feed_id' => 3], ['code' => 0, 'feed_id' => 1], ['code' => 3, 'message' => (new \JKingWeb\Arsse\Feed\Exception("http://localhost:8000/Feed/Discovery/Invalid", new \PicoFeed\Reader\SubscriptionNotFoundException()))->getMessage()], - ['code' => 2, 'message' => (new \JKingWeb\Arsse\Feed\Exception("http://example.com/6", new \PicoFeed\Client\InvalidUrlException()))->getMessage()], + ['code' => 2, 'message' => (new \JKingWeb\Arsse\Feed\Exception("http://example.com/6", $this->mockGuzzleException(ClientException::class, "", 404)))->getMessage()], ['code' => 6, 'message' => (new \JKingWeb\Arsse\Feed\Exception("http://example.com/7", new \PicoFeed\Parser\MalformedXmlException()))->getMessage()], ['code' => 1, 'feed_id' => 4], ['code' => 0, 'feed_id' => 4], @@ -802,12 +803,12 @@ LONG_STRING; ['id' => 4, 'url' => "http://example.com/9"], ]; \Phake::when(Arsse::$db)->subscriptionAdd(...$db[0])->thenReturn(2); - \Phake::when(Arsse::$db)->subscriptionAdd(...$db[1])->thenThrow(new \JKingWeb\Arsse\Feed\Exception("http://example.com/1", new \PicoFeed\Client\UnauthorizedException())); + \Phake::when(Arsse::$db)->subscriptionAdd(...$db[1])->thenThrow(new \JKingWeb\Arsse\Feed\Exception("http://example.com/1", $this->mockGuzzleException(ClientException::class, "", 401))); \Phake::when(Arsse::$db)->subscriptionAdd(...$db[2])->thenReturn(2); \Phake::when(Arsse::$db)->subscriptionAdd(...$db[3])->thenThrow(new ExceptionInput("constraintViolation")); \Phake::when(Arsse::$db)->subscriptionAdd(...$db[4])->thenThrow(new ExceptionInput("constraintViolation")); \Phake::when(Arsse::$db)->subscriptionAdd(...$db[5])->thenThrow(new ExceptionInput("constraintViolation")); - \Phake::when(Arsse::$db)->subscriptionAdd(...$db[6])->thenThrow(new \JKingWeb\Arsse\Feed\Exception("http://example.com/6", new \PicoFeed\Client\InvalidUrlException())); + \Phake::when(Arsse::$db)->subscriptionAdd(...$db[6])->thenThrow(new \JKingWeb\Arsse\Feed\Exception("http://example.com/6", $this->mockGuzzleException(ClientException::class, "", 404))); \Phake::when(Arsse::$db)->subscriptionAdd(...$db[7])->thenThrow(new \JKingWeb\Arsse\Feed\Exception("http://example.com/7", new \PicoFeed\Parser\MalformedXmlException())); \Phake::when(Arsse::$db)->subscriptionAdd(...$db[8])->thenReturn(4); \Phake::when(Arsse::$db)->subscriptionAdd(...$db[9])->thenThrow(new ExceptionInput("constraintViolation")); diff --git a/tests/lib/AbstractTest.php b/tests/lib/AbstractTest.php index 0a553d13..1a032788 100644 --- a/tests/lib/AbstractTest.php +++ b/tests/lib/AbstractTest.php @@ -6,6 +6,8 @@ declare(strict_types=1); namespace JKingWeb\Arsse\Test; +use GuzzleHttp\Exception\GuzzleException; +use GuzzleHttp\Exception\RequestException; use JKingWeb\Arsse\Exception; use JKingWeb\Arsse\Arsse; use JKingWeb\Arsse\Conf; @@ -325,4 +327,16 @@ abstract class AbstractTest extends \PHPUnit\Framework\TestCase { $this->assertArraySubset($expected, [], false, "Expectations not in result set."); } } + + /** Guzzle's exception classes require some fairly complicated construction; this abstracts it all away so that only message and code need be supplied */ + protected function mockGuzzleException(string $class, ?string $message = null, ?int $code = null, ?\Throwable $e = null): GuzzleException { + if (is_a($class, RequestException::class, true)) { + $req = \Phake::mock(RequestInterface::class); + $res = \Phake::mock(ResponseInterface::class); + \Phake::when($res)->getStatusCode->thenReturn($code ?? 0); + return new $class($message ?? "", $req, $res, $e); + } else { + return new $class($message ?? "", $code ?? 0, $e); + } + } }