From f3760f138dcbaf7a2190336a0378cf1b2190c9f5 Mon Sep 17 00:00:00 2001 From: Alexandre Alapetite Date: Mon, 17 Apr 2023 08:30:21 +0200 Subject: Complete PHPStan Level 6 (#5305) * Complete PHPStan Level 6 Fix https://github.com/FreshRSS/FreshRSS/issues/4112 And initiate PHPStan Level 7 * PHPStan Level 6 for tests * Use phpstan/phpstan-phpunit * Update to PHPStan version 1.10 * Fix mixed bug * Fix mixed return bug * Fix paginator bug * Fix FreshRSS_UserConfiguration * A couple more Minz_Configuration bug fixes * A few trivial PHPStan Level 7 fixes * A few more simple PHPStan Level 7 * More files passing PHPStan Level 7 Add interface to replace removed class from https://github.com/FreshRSS/FreshRSS/pull/5251 * A few more PHPStan Level 7 preparations * A few last details --- tests/app/Models/CategoryTest.php | 11 ++-- tests/app/Models/LogDAOTest.php | 2 +- tests/app/Models/SearchTest.php | 117 ++++++++++++++++------------------- tests/app/Models/UserQueryTest.php | 64 +++++++++++-------- tests/app/Utils/passwordUtilTest.php | 6 +- 5 files changed, 98 insertions(+), 102 deletions(-) (limited to 'tests/app') diff --git a/tests/app/Models/CategoryTest.php b/tests/app/Models/CategoryTest.php index 93fbdbc63..a438afe9e 100644 --- a/tests/app/Models/CategoryTest.php +++ b/tests/app/Models/CategoryTest.php @@ -2,23 +2,22 @@ class CategoryTest extends PHPUnit\Framework\TestCase { - public function test__construct_whenNoParameters_createsObjectWithDefaultValues() { + public function test__construct_whenNoParameters_createsObjectWithDefaultValues(): void { $category = new FreshRSS_Category(); $this->assertEquals(0, $category->id()); $this->assertEquals('', $category->name()); } /** - * @param string $input - * @param string $expected * @dataProvider provideValidNames */ - public function test_name_whenValidValue_storesModifiedValue($input, $expected) { + public function test_name_whenValidValue_storesModifiedValue(string $input, string $expected): void { $category = new FreshRSS_Category($input); $this->assertEquals($expected, $category->name()); } - public function provideValidNames() { + /** @return array */ + public function provideValidNames(): array { return array( array('', ''), array('this string does not need trimming', 'this string does not need trimming'), @@ -30,7 +29,7 @@ class CategoryTest extends PHPUnit\Framework\TestCase { ); } - public function test_feedOrdering() { + public function test_feedOrdering(): void { $feed_1 = $this->getMockBuilder(FreshRSS_Feed::class) ->disableOriginalConstructor() ->getMock(); diff --git a/tests/app/Models/LogDAOTest.php b/tests/app/Models/LogDAOTest.php index 18cad4968..abe6c6c1f 100644 --- a/tests/app/Models/LogDAOTest.php +++ b/tests/app/Models/LogDAOTest.php @@ -36,7 +36,7 @@ class LogDAOTest extends TestCase { $this->logDAO::truncate(self::LOG_FILE_TEST); - $this->assertStringContainsString('', file_get_contents($this->logPath)); + $this->assertStringContainsString('', file_get_contents($this->logPath) ?: ''); } protected function tearDown(): void { diff --git a/tests/app/Models/SearchTest.php b/tests/app/Models/SearchTest.php index 52c10244d..2b2501589 100644 --- a/tests/app/Models/SearchTest.php +++ b/tests/app/Models/SearchTest.php @@ -6,9 +6,8 @@ class SearchTest extends PHPUnit\Framework\TestCase { /** * @dataProvider provideEmptyInput - * @param string|null $input */ - public function test__construct_whenInputIsEmpty_getsOnlyNullValues($input) { + public function test__construct_whenInputIsEmpty_getsOnlyNullValues(?string $input): void { $search = new FreshRSS_Search($input); $this->assertEquals('', $search->getRawInput()); $this->assertNull($search->getIntitle()); @@ -24,9 +23,9 @@ class SearchTest extends PHPUnit\Framework\TestCase { /** * Return an array of values for the search object. * Here is the description of the values - * @return array + * @return array{array{''},array{null}} */ - public function provideEmptyInput() { + public function provideEmptyInput(): array { return array( array(''), array(null), @@ -35,20 +34,19 @@ class SearchTest extends PHPUnit\Framework\TestCase { /** * @dataProvider provideIntitleSearch - * @param string $input - * @param string $intitle_value - * @param string|null $search_value + * @param array|null $intitle_value + * @param array|null $search_value */ - public function test__construct_whenInputContainsIntitle_setsIntitleProperty($input, $intitle_value, $search_value) { + public function test__construct_whenInputContainsIntitle_setsIntitleProperty(string $input, ?array $intitle_value, ?array $search_value): void { $search = new FreshRSS_Search($input); $this->assertEquals($intitle_value, $search->getIntitle()); $this->assertEquals($search_value, $search->getSearch()); } /** - * @return array + * @return array> */ - public function provideIntitleSearch() { + public function provideIntitleSearch(): array { return array( array('intitle:word1', array('word1'), null), array('intitle:word1-word2', array('word1-word2'), null), @@ -73,20 +71,19 @@ class SearchTest extends PHPUnit\Framework\TestCase { /** * @dataProvider provideAuthorSearch - * @param string $input - * @param string $author_value - * @param string|null $search_value + * @param array|null $author_value + * @param array|null $search_value */ - public function test__construct_whenInputContainsAuthor_setsAuthorValue($input, $author_value, $search_value) { + public function test__construct_whenInputContainsAuthor_setsAuthorValue(string $input, ?array $author_value, ?array $search_value): void { $search = new FreshRSS_Search($input); $this->assertEquals($author_value, $search->getAuthor()); $this->assertEquals($search_value, $search->getSearch()); } /** - * @return array + * @return array> */ - public function provideAuthorSearch() { + public function provideAuthorSearch(): array { return array( array('author:word1', array('word1'), null), array('author:word1-word2', array('word1-word2'), null), @@ -111,20 +108,19 @@ class SearchTest extends PHPUnit\Framework\TestCase { /** * @dataProvider provideInurlSearch - * @param string $input - * @param string $inurl_value - * @param string|null $search_value + * @param array|null $inurl_value + * @param array|null $search_value */ - public function test__construct_whenInputContainsInurl_setsInurlValue($input, $inurl_value, $search_value) { + public function test__construct_whenInputContainsInurl_setsInurlValue(string $input, ?array $inurl_value, ?array $search_value): void { $search = new FreshRSS_Search($input); $this->assertEquals($inurl_value, $search->getInurl()); $this->assertEquals($search_value, $search->getSearch()); } /** - * @return array + * @return array> */ - public function provideInurlSearch() { + public function provideInurlSearch(): array { return array( array('inurl:word1', array('word1'), null), array('inurl: word1', array(), array('word1')), @@ -139,72 +135,65 @@ class SearchTest extends PHPUnit\Framework\TestCase { /** * @dataProvider provideDateSearch - * @param string $input - * @param string $min_date_value - * @param string $max_date_value */ - public function test__construct_whenInputContainsDate_setsDateValues($input, $min_date_value, $max_date_value) { + public function test__construct_whenInputContainsDate_setsDateValues(string $input, ?int $min_date_value, ?int $max_date_value): void { $search = new FreshRSS_Search($input); $this->assertEquals($min_date_value, $search->getMinDate()); $this->assertEquals($max_date_value, $search->getMaxDate()); } /** - * @return array + * @return array> */ - public function provideDateSearch() { + public function provideDateSearch(): array { return array( - array('date:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', '1172754000', '1210519800'), - array('date:2007-03-01T13:00:00Z/P1Y2M10DT2H30M', '1172754000', '1210519799'), - array('date:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', '1172754001', '1210519800'), + array('date:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', 1172754000, 1210519800), + array('date:2007-03-01T13:00:00Z/P1Y2M10DT2H30M', 1172754000, 1210519799), + array('date:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', 1172754001, 1210519800), array('date:2007-03-01/2008-05-11', strtotime('2007-03-01'), strtotime('2008-05-12') - 1), - array('date:2007-03-01/', strtotime('2007-03-01'), ''), - array('date:/2008-05-11', '', strtotime('2008-05-12') - 1), + array('date:2007-03-01/', strtotime('2007-03-01'), null), + array('date:/2008-05-11', null, strtotime('2008-05-12') - 1), ); } /** * @dataProvider providePubdateSearch - * @param string $input - * @param string $min_pubdate_value - * @param string $max_pubdate_value */ - public function test__construct_whenInputContainsPubdate_setsPubdateValues($input, $min_pubdate_value, $max_pubdate_value) { + public function test__construct_whenInputContainsPubdate_setsPubdateValues(string $input, ?int $min_pubdate_value, ?int $max_pubdate_value): void { $search = new FreshRSS_Search($input); $this->assertEquals($min_pubdate_value, $search->getMinPubdate()); $this->assertEquals($max_pubdate_value, $search->getMaxPubdate()); } /** - * @return array + * @return array> */ - public function providePubdateSearch() { + public function providePubdateSearch(): array { return array( - array('pubdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', '1172754000', '1210519800'), - array('pubdate:2007-03-01T13:00:00Z/P1Y2M10DT2H30M', '1172754000', '1210519799'), - array('pubdate:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', '1172754001', '1210519800'), + array('pubdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', 1172754000, 1210519800), + array('pubdate:2007-03-01T13:00:00Z/P1Y2M10DT2H30M', 1172754000, 1210519799), + array('pubdate:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', 1172754001, 1210519800), array('pubdate:2007-03-01/2008-05-11', strtotime('2007-03-01'), strtotime('2008-05-12') - 1), - array('pubdate:2007-03-01/', strtotime('2007-03-01'), ''), - array('pubdate:/2008-05-11', '', strtotime('2008-05-12') - 1), + array('pubdate:2007-03-01/', strtotime('2007-03-01'), null), + array('pubdate:/2008-05-11', null, strtotime('2008-05-12') - 1), ); } /** * @dataProvider provideTagsSearch - * @param string $input - * @param string $tags_value - * @param string|null $search_value + * @param array|null $tags_value + * @param array|null $search_value */ - public function test__construct_whenInputContainsTags_setsTagsValue($input, $tags_value, $search_value) { + public function test__construct_whenInputContainsTags_setsTagsValue(string $input, ?array $tags_value, ?array $search_value): void { $search = new FreshRSS_Search($input); $this->assertEquals($tags_value, $search->getTags()); $this->assertEquals($search_value, $search->getSearch()); } /** - * @return array + * @return array|null>> */ - public function provideTagsSearch() { + public function provideTagsSearch(): array { return array( array('#word1', array('word1'), null), array('# word1', array(), array('#', 'word1')), @@ -219,19 +208,15 @@ class SearchTest extends PHPUnit\Framework\TestCase { /** * @dataProvider provideMultipleSearch - * @param string $input - * @param string $author_value - * @param string $min_date_value - * @param string $max_date_value - * @param string $intitle_value - * @param string $inurl_value - * @param string $min_pubdate_value - * @param string $max_pubdate_value - * @param array $tags_value - * @param string|null $search_value + * @param array|null $author_value + * @param array $intitle_value + * @param array|null $inurl_value + * @param array|null $tags_value + * @param array|null $search_value */ - public function test__construct_whenInputContainsMultipleKeywords_setsValues($input, $author_value, $min_date_value, - $max_date_value, $intitle_value, $inurl_value, $min_pubdate_value, $max_pubdate_value, $tags_value, $search_value) { + public function test__construct_whenInputContainsMultipleKeywords_setsValues(string $input, ?array $author_value, ?int $min_date_value, + ?int $max_date_value, ?array $intitle_value, ?array $inurl_value, ?int $min_pubdate_value, + ?int $max_pubdate_value, ?array $tags_value, ?array $search_value): void { $search = new FreshRSS_Search($input); $this->assertEquals($author_value, $search->getAuthor()); $this->assertEquals($min_date_value, $search->getMinDate()); @@ -245,7 +230,8 @@ class SearchTest extends PHPUnit\Framework\TestCase { $this->assertEquals($input, $search->getRawInput()); } - public function provideMultipleSearch() { + /** @return array> */ + public function provideMultipleSearch(): array { return array( array( 'author:word1 date:2007-03-01/2008-05-11 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 #word5', @@ -302,13 +288,14 @@ class SearchTest extends PHPUnit\Framework\TestCase { * @dataProvider provideParentheses * @param array $values */ - public function test__construct_parentheses(string $input, string $sql, $values) { + public function test__construct_parentheses(string $input, string $sql, array $values): void { list($filterValues, $filterSearch) = FreshRSS_EntryDAOPGSQL::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input)); $this->assertEquals($sql, $filterSearch); $this->assertEquals($values, $filterValues); } - public function provideParentheses() { + /** @return array> */ + public function provideParentheses(): array { return [ [ 'f:1 (f:2 OR f:3 OR f:4) (f:5 OR (f:6 OR f:7))', diff --git a/tests/app/Models/UserQueryTest.php b/tests/app/Models/UserQueryTest.php index 7b1e88907..d8610a81b 100644 --- a/tests/app/Models/UserQueryTest.php +++ b/tests/app/Models/UserQueryTest.php @@ -5,21 +5,21 @@ */ class UserQueryTest extends PHPUnit\Framework\TestCase { - public function test__construct_whenAllQuery_storesAllParameters() { + public function test__construct_whenAllQuery_storesAllParameters(): void { $query = array('get' => 'a'); $user_query = new FreshRSS_UserQuery($query); $this->assertEquals('all', $user_query->getGetName()); $this->assertEquals('all', $user_query->getGetType()); } - public function test__construct_whenFavoriteQuery_storesFavoriteParameters() { + public function test__construct_whenFavoriteQuery_storesFavoriteParameters(): void { $query = array('get' => 's'); $user_query = new FreshRSS_UserQuery($query); $this->assertEquals('favorite', $user_query->getGetName()); $this->assertEquals('favorite', $user_query->getGetType()); } - public function test__construct_whenCategoryQueryAndNoDao_throwsException() { + public function test__construct_whenCategoryQueryAndNoDao_throwsException(): void { $this->expectException(FreshRSS_DAO_Exception::class); $this->expectExceptionMessage('Category DAO is not loaded in UserQuery'); @@ -27,13 +27,15 @@ class UserQueryTest extends PHPUnit\Framework\TestCase { new FreshRSS_UserQuery($query); } - public function test__construct_whenCategoryQuery_storesCategoryParameters() { + public function test__construct_whenCategoryQuery_storesCategoryParameters(): void { $category_name = 'some category name'; + /** @var FreshRSS_Category&PHPUnit\Framework\MockObject\MockObject */ $cat = $this->createMock('FreshRSS_Category'); $cat->expects($this->atLeastOnce()) ->method('name') ->withAnyParameters() ->willReturn($category_name); + /** @var FreshRSS_CategoryDAO&PHPUnit\Framework\MockObject\MockObject */ $cat_dao = $this->createMock('FreshRSS_CategoryDAO'); $cat_dao->expects($this->atLeastOnce()) ->method('searchById') @@ -45,7 +47,7 @@ class UserQueryTest extends PHPUnit\Framework\TestCase { $this->assertEquals('category', $user_query->getGetType()); } - public function test__construct_whenFeedQueryAndNoDao_throwsException() { + public function test__construct_whenFeedQueryAndNoDao_throwsException(): void { $this->expectException(FreshRSS_DAO_Exception::class); $this->expectExceptionMessage('Feed DAO is not loaded in UserQuery'); @@ -53,13 +55,15 @@ class UserQueryTest extends PHPUnit\Framework\TestCase { new FreshRSS_UserQuery($query); } - public function test__construct_whenFeedQuery_storesFeedParameters() { + public function test__construct_whenFeedQuery_storesFeedParameters(): void { $feed_name = 'some feed name'; - $feed = $this->createMock('FreshRSS_Feed', array(), array('', false)); + /** @var FreshRSS_Feed&PHPUnit\Framework\MockObject\MockObject */ + $feed = $this->createMock('FreshRSS_Feed'); $feed->expects($this->atLeastOnce()) ->method('name') ->withAnyParameters() ->willReturn($feed_name); + /** @var FreshRSS_FeedDAO&PHPUnit\Framework\MockObject\MockObject */ $feed_dao = $this->createMock('FreshRSS_FeedDAO'); $feed_dao->expects($this->atLeastOnce()) ->method('searchById') @@ -71,48 +75,48 @@ class UserQueryTest extends PHPUnit\Framework\TestCase { $this->assertEquals('feed', $user_query->getGetType()); } - public function test__construct_whenUnknownQuery_doesStoreParameters() { + public function test__construct_whenUnknownQuery_doesStoreParameters(): void { $query = array('get' => 'q'); $user_query = new FreshRSS_UserQuery($query); $this->assertEmpty($user_query->getGetName()); $this->assertEmpty($user_query->getGetType()); } - public function test__construct_whenName_storesName() { + public function test__construct_whenName_storesName(): void { $name = 'some name'; $query = array('name' => $name); $user_query = new FreshRSS_UserQuery($query); $this->assertEquals($name, $user_query->getName()); } - public function test__construct_whenOrder_storesOrder() { + public function test__construct_whenOrder_storesOrder(): void { $order = 'some order'; $query = array('order' => $order); $user_query = new FreshRSS_UserQuery($query); $this->assertEquals($order, $user_query->getOrder()); } - public function test__construct_whenState_storesState() { + public function test__construct_whenState_storesState(): void { $state = FreshRSS_Entry::STATE_ALL; $query = array('state' => $state); $user_query = new FreshRSS_UserQuery($query); $this->assertEquals($state, $user_query->getState()); } - public function test__construct_whenUrl_storesUrl() { + public function test__construct_whenUrl_storesUrl(): void { $url = 'some url'; $query = array('url' => $url); $user_query = new FreshRSS_UserQuery($query); $this->assertEquals($url, $user_query->getUrl()); } - public function testToArray_whenNoData_returnsEmptyArray() { + public function testToArray_whenNoData_returnsEmptyArray(): void { $user_query = new FreshRSS_UserQuery(array()); $this->assertIsIterable($user_query->toArray()); $this->assertCount(0, $user_query->toArray()); } - public function testToArray_whenData_returnsArray() { + public function testToArray_whenData_returnsArray(): void { $query = array( 'get' => 's', 'name' => 'some name', @@ -127,7 +131,7 @@ class UserQueryTest extends PHPUnit\Framework\TestCase { $this->assertEquals($query, $user_query->toArray()); } - public function testHasSearch_whenSearch_returnsTrue() { + public function testHasSearch_whenSearch_returnsTrue(): void { $query = array( 'search' => 'some search', ); @@ -135,31 +139,33 @@ class UserQueryTest extends PHPUnit\Framework\TestCase { $this->assertTrue($user_query->hasSearch()); } - public function testHasSearch_whenNoSearch_returnsFalse() { + public function testHasSearch_whenNoSearch_returnsFalse(): void { $user_query = new FreshRSS_UserQuery(array()); $this->assertFalse($user_query->hasSearch()); } - public function testHasParameters_whenAllQuery_returnsFalse() { + public function testHasParameters_whenAllQuery_returnsFalse(): void { $query = array('get' => 'a'); $user_query = new FreshRSS_UserQuery($query); $this->assertFalse($user_query->hasParameters()); } - public function testHasParameters_whenNoParameter_returnsFalse() { + public function testHasParameters_whenNoParameter_returnsFalse(): void { $query = array(); $user_query = new FreshRSS_UserQuery($query); $this->assertFalse($user_query->hasParameters()); } - public function testHasParameters_whenParameter_returnTrue() { + public function testHasParameters_whenParameter_returnTrue(): void { $query = array('get' => 's'); $user_query = new FreshRSS_UserQuery($query); $this->assertTrue($user_query->hasParameters()); } - public function testIsDeprecated_whenCategoryExists_returnFalse() { + public function testIsDeprecated_whenCategoryExists_returnFalse(): void { + /** @var FreshRSS_Category&PHPUnit\Framework\MockObject\MockObject */ $cat = $this->createMock('FreshRSS_Category'); + /** @var FreshRSS_CategoryDAO&PHPUnit\Framework\MockObject\MockObject */ $cat_dao = $this->createMock('FreshRSS_CategoryDAO'); $cat_dao->expects($this->atLeastOnce()) ->method('searchById') @@ -170,7 +176,8 @@ class UserQueryTest extends PHPUnit\Framework\TestCase { $this->assertFalse($user_query->isDeprecated()); } - public function testIsDeprecated_whenCategoryDoesNotExist_returnTrue() { + public function testIsDeprecated_whenCategoryDoesNotExist_returnTrue(): void { + /** @var FreshRSS_CategoryDAO&PHPUnit\Framework\MockObject\MockObject */ $cat_dao = $this->createMock('FreshRSS_CategoryDAO'); $cat_dao->expects($this->atLeastOnce()) ->method('searchById') @@ -181,8 +188,10 @@ class UserQueryTest extends PHPUnit\Framework\TestCase { $this->assertTrue($user_query->isDeprecated()); } - public function testIsDeprecated_whenFeedExists_returnFalse() { - $feed = $this->createMock('FreshRSS_Feed', array(), array('', false)); + public function testIsDeprecated_whenFeedExists_returnFalse(): void { + /** @var FreshRSS_Feed&PHPUnit\Framework\MockObject\MockObject */ + $feed = $this->createMock('FreshRSS_Feed'); + /** @var FreshRSS_FeedDAO&PHPUnit\Framework\MockObject\MockObject */ $feed_dao = $this->createMock('FreshRSS_FeedDAO'); $feed_dao->expects($this->atLeastOnce()) ->method('searchById') @@ -193,7 +202,8 @@ class UserQueryTest extends PHPUnit\Framework\TestCase { $this->assertFalse($user_query->isDeprecated()); } - public function testIsDeprecated_whenFeedDoesNotExist_returnTrue() { + public function testIsDeprecated_whenFeedDoesNotExist_returnTrue(): void { + /** @var FreshRSS_FeedDAO&PHPUnit\Framework\MockObject\MockObject */ $feed_dao = $this->createMock('FreshRSS_FeedDAO'); $feed_dao->expects($this->atLeastOnce()) ->method('searchById') @@ -204,19 +214,19 @@ class UserQueryTest extends PHPUnit\Framework\TestCase { $this->assertTrue($user_query->isDeprecated()); } - public function testIsDeprecated_whenAllQuery_returnFalse() { + public function testIsDeprecated_whenAllQuery_returnFalse(): void { $query = array('get' => 'a'); $user_query = new FreshRSS_UserQuery($query); $this->assertFalse($user_query->isDeprecated()); } - public function testIsDeprecated_whenFavoriteQuery_returnFalse() { + public function testIsDeprecated_whenFavoriteQuery_returnFalse(): void { $query = array('get' => 's'); $user_query = new FreshRSS_UserQuery($query); $this->assertFalse($user_query->isDeprecated()); } - public function testIsDeprecated_whenUnknownQuery_returnFalse() { + public function testIsDeprecated_whenUnknownQuery_returnFalse(): void { $query = array('get' => 'q'); $user_query = new FreshRSS_UserQuery($query); $this->assertFalse($user_query->isDeprecated()); diff --git a/tests/app/Utils/passwordUtilTest.php b/tests/app/Utils/passwordUtilTest.php index ddccb784f..113476c63 100644 --- a/tests/app/Utils/passwordUtilTest.php +++ b/tests/app/Utils/passwordUtilTest.php @@ -1,7 +1,7 @@ assertTrue($ok); } - public function testCheckReturnsFalseIfEmpty() { + public function testCheckReturnsFalseIfEmpty(): void { $password = ''; $ok = FreshRSS_password_Util::check($password); @@ -17,7 +17,7 @@ class passwordUtilTest extends PHPUnit\Framework\TestCase { $this->assertFalse($ok); } - public function testCheckReturnsFalseIfLessThan7Characters() { + public function testCheckReturnsFalseIfLessThan7Characters(): void { $password = '123456'; $ok = FreshRSS_password_Util::check($password); -- cgit v1.2.3