From f7b43501afa339dd11abca62c5e8eaa9a2c67d4e Mon Sep 17 00:00:00 2001 From: Sean Molenaar Date: Fri, 8 Jan 2021 22:45:43 +0100 Subject: Fix nc 21 phpunit issue --- AUTHORS.md | 1 + phpunit.xml | 48 +++++----- tests/Unit/Command/ExploreGeneratorTest.php | 23 +++-- tests/Unit/Db/FeedMapperTest.php | 11 ++- tests/Unit/Db/MapperTestUtility.php | 8 +- tests/Unit/Db/NewsMapperTest.php | 17 ++-- tests/Unit/Migration/MigrateStatusFlagsTest.php | 111 ------------------------ 7 files changed, 62 insertions(+), 157 deletions(-) delete mode 100644 tests/Unit/Migration/MigrateStatusFlagsTest.php diff --git a/AUTHORS.md b/AUTHORS.md index d81d5e579..522e46238 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -41,6 +41,7 @@ * [Qingping Hou](mailto:dave2008713@gmail.com) * [Roman](mailto:reverse@jamm.me) * [b_b](mailto:bruno@eliaz.fr) +* [heyarne](mailto:arne@schlueter.is) * [Andreas Fischer](mailto:bantu@owncloud.com) * [David Guillot](mailto:david@guillot.me) * [Gioele Falcetti](mailto:thegio.f@gmail.com) diff --git a/phpunit.xml b/phpunit.xml index 9c52c7234..6a2f2d01b 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,25 +1,27 @@ - - - - ./lib/ - - - ./lib/AppInfo/Application.php - ./lib/Controller/JSONHttpErrorTrait.php - ./lib/**Exception.php - - - - - - - - - ./tests/Unit - - - - - + + + + ./tests/Unit + + + + + ./lib/ + + + ./lib/AppInfo/Application.php + ./lib/Controller/JSONHttpErrorTrait.php + ./lib/**Exception.php + + + + + + + + + + diff --git a/tests/Unit/Command/ExploreGeneratorTest.php b/tests/Unit/Command/ExploreGeneratorTest.php index ec04c4241..4482b91f3 100644 --- a/tests/Unit/Command/ExploreGeneratorTest.php +++ b/tests/Unit/Command/ExploreGeneratorTest.php @@ -26,22 +26,24 @@ use Favicon\Favicon; use FeedIo\Reader\Result; use OCA\News\Command\ExploreGenerator; +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; +use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -use Test\TestCase; class ExploreGeneratorTest extends TestCase { - /** @var \PHPUnit_Framework_MockObject_MockObject */ + /** @var MockObject */ protected $favicon; - /** @var \PHPUnit_Framework_MockObject_MockObject */ + /** @var MockObject */ protected $feedio; - /** @var \PHPUnit_Framework_MockObject_MockObject */ + /** @var MockObject */ protected $consoleInput; - /** @var \PHPUnit_Framework_MockObject_MockObject */ + /** @var MockObject */ protected $consoleOutput; - /** @var \Symfony\Component\Console\Command\Command */ + /** @var Command */ protected $command; protected function setUp(): void @@ -110,7 +112,8 @@ class ExploreGeneratorTest extends TestCase ->method('writeln') ->with($this->stringContains('https:\/\/feed.io\/rss.xml')); - self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); + $result = $this->command->run($this->consoleInput, $this->consoleOutput); + $this->assertSame(0, $result); } /** @@ -141,7 +144,8 @@ class ExploreGeneratorTest extends TestCase ->method('writeln') ->withConsecutive(['Failed to fetch feed info:'], ['Failure']); - self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); + $result = $this->command->run($this->consoleInput, $this->consoleOutput); + $this->assertSame(1, $result); } /** @@ -192,6 +196,7 @@ class ExploreGeneratorTest extends TestCase ->method('writeln') ->with($this->stringContains('200')); - self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); + $result = $this->command->run($this->consoleInput, $this->consoleOutput); + $this->assertSame(0, $result); } } diff --git a/tests/Unit/Db/FeedMapperTest.php b/tests/Unit/Db/FeedMapperTest.php index a6a4be866..c14b8995f 100644 --- a/tests/Unit/Db/FeedMapperTest.php +++ b/tests/Unit/Db/FeedMapperTest.php @@ -15,17 +15,17 @@ namespace OCA\News\Tests\Unit\Db; use OCA\News\Db\Feed; use OCA\News\Db\FeedMapperV2; -use OCA\News\Db\Folder; use OCA\News\Utility\Time; use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Db\MultipleObjectsReturnedException; use OCP\DB\QueryBuilder\IFunctionBuilder; +use OCP\DB\QueryBuilder\IQueryFunction; class FeedMapperTest extends MapperTestUtility { /** @var FeedMapperV2 */ private $class; - /** @var Feeds[] */ + /** @var Feed[] */ private $feeds; /** @@ -68,10 +68,13 @@ class FeedMapperTest extends MapperTestUtility $funcbuilder = $this->getMockBuilder(IFunctionBuilder::class) ->getMock(); + $func = $this->getMockBuilder(IQueryFunction::class) + ->getMock(); + $funcbuilder->expects($this->once()) ->method('count') ->with('items.id', 'unreadCount') - ->will($this->returnValue('COUNT_FUNC')); + ->will($this->returnValue($func)); $this->builder->expects($this->once()) ->method('func') @@ -79,7 +82,7 @@ class FeedMapperTest extends MapperTestUtility $this->builder->expects($this->once()) ->method('select') - ->with('feeds.*', 'COUNT_FUNC') + ->with('feeds.*', $func) ->will($this->returnSelf()); $this->builder->expects($this->once()) diff --git a/tests/Unit/Db/MapperTestUtility.php b/tests/Unit/Db/MapperTestUtility.php index 725f0c3c0..3aa1d8aed 100644 --- a/tests/Unit/Db/MapperTestUtility.php +++ b/tests/Unit/Db/MapperTestUtility.php @@ -23,12 +23,11 @@ namespace OCA\News\Tests\Unit\Db; -use Doctrine\DBAL\Driver\PDOStatement; use Doctrine\DBAL\Driver\Statement; -use OCA\News\Tests\Unit\Service\ServiceTest; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; +use PDOStatement; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; @@ -71,13 +70,14 @@ abstract class MapperTestUtility extends TestCase ->disableOriginalConstructor() ->getMock(); - $this->query = $this->getMockBuilder(\PDOStatement::class) + $this->query = $this->getMockBuilder(PDOStatement::class) ->getMock(); $this->builder = $this->getMockBuilder(IQueryBuilder::class) ->getMock(); $this->cursor = $this->getMockBuilder(Statement::class) - ->getMock(); + ->addMethods(['fetch', 'closeCursor']) + ->getMockForAbstractClass(); } } diff --git a/tests/Unit/Db/NewsMapperTest.php b/tests/Unit/Db/NewsMapperTest.php index 5d75f740f..c7ce7342a 100644 --- a/tests/Unit/Db/NewsMapperTest.php +++ b/tests/Unit/Db/NewsMapperTest.php @@ -14,21 +14,26 @@ namespace OCA\News\Tests\Unit\Db; use OCA\News\Db\Feed; -use OCA\News\Db\FeedMapperV2; -use OCA\News\Db\Folder; use OCA\News\Db\NewsMapperV2; use OCA\News\Utility\Time; -use OCP\AppFramework\Db\DoesNotExistException; -use OCP\AppFramework\Db\MultipleObjectsReturnedException; -use OCP\DB\QueryBuilder\IFunctionBuilder; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; -use Test\TestCase; +use PHPUnit\Framework\TestCase; +/** + * Class TmpNewsMapper + * + * @package OCA\News\Tests\Unit\Db + */ abstract class TmpNewsMapper extends NewsMapperV2 { const TABLE_NAME = 'NAME'; } +/** + * Class NewsMapperTest + * + * @package OCA\News\Tests\Unit\Db + */ class NewsMapperTest extends TestCase { /** @var IDBConnection */ diff --git a/tests/Unit/Migration/MigrateStatusFlagsTest.php b/tests/Unit/Migration/MigrateStatusFlagsTest.php deleted file mode 100644 index d71f1a54c..000000000 --- a/tests/Unit/Migration/MigrateStatusFlagsTest.php +++ /dev/null @@ -1,111 +0,0 @@ - - * @copyright Daniel Opitz 2017 - */ - -namespace OCA\News\Tests\Unit\Migration; - -use Doctrine\DBAL\Driver\Statement; -use OCA\News\Migration\MigrateStatusFlags; -use OCP\IConfig; -use OCP\IDBConnection; -use OCP\Migration\IOutput; -use Test\TestCase; - -class MigrateStatusFlagsTest extends TestCase -{ - - /** - * @var IDBConnection|\PHPUnit_Framework_MockObject_MockObject - */ - protected $db; - /** - * @var IConfig|\PHPUnit_Framework_MockObject_MockObject - */ - protected $config; - /** - * @var IOutput|\PHPUnit_Framework_MockObject_MockObject - */ - protected $output; - - protected function setUp(): void - { - $this->db = $this->createMock(IDBConnection::class); - $this->config = $this->createMock(IConfig::class); - $this->output = $this->createMock(IOutput::class); - } - - public function testRun() - { - $statement = $this->createMock(Statement::class); - $statement->expects($this->exactly(1)) - ->method('execute') - ->with() - ->willReturn(true); - - $this->config->expects($this->exactly(1)) - ->method('getAppValue') - ->with('news', 'installed_version', '0.0.0') - ->willReturn('11.0.5'); - - $sql = 'UPDATE `*PREFIX*news_items` ' - . 'SET `unread` = ((`status` & 2) = 2), ' - . '`starred` = ((`status` & 4) = 4)'; - - $this->db->expects($this->exactly(1)) - ->method('prepare') - ->with($sql) - ->willReturn($statement); - - $migration = new MigrateStatusFlags($this->db, $this->config); - $migration->run($this->output); - } - - public function testRunException() - { - $this->expectException('\Exception'); - $this->expectExceptionMessage('Could not migrate status'); - - $statement = $this->createMock(Statement::class); - $statement->expects($this->exactly(1)) - ->method('execute') - ->with() - ->willReturn(false); - - $this->config->expects($this->exactly(1)) - ->method('getAppValue') - ->with('news', 'installed_version', '0.0.0') - ->willReturn('11.0.5'); - - $sql = 'UPDATE `*PREFIX*news_items` ' - . 'SET `unread` = ((`status` & 2) = 2), ' - . '`starred` = ((`status` & 4) = 4)'; - - $this->db->expects($this->exactly(1)) - ->method('prepare') - ->with($sql) - ->willReturn($statement); - - $migration = new MigrateStatusFlags($this->db, $this->config); - $migration->run($this->output); - } - - public function testRunNewerVersion() - { - $this->config->expects($this->exactly(1)) - ->method('getAppValue') - ->with('news', 'installed_version', '0.0.0') - ->willReturn('11.1.0'); - $this->db->expects($this->exactly(0)) - ->method('prepare'); - - $migration = new MigrateStatusFlags($this->db, $this->config); - $migration->run($this->output); - } -} \ No newline at end of file -- cgit v1.2.3