From 6f6eb5b301e590cfc49cab0a43db9c032c3d4f85 Mon Sep 17 00:00:00 2001 From: Christoph Wurst Date: Mon, 16 Jan 2017 11:37:41 +0100 Subject: increase GenericProvider's test coverage Signed-off-by: Christoph Wurst --- .../tests/Unit/Activity/GenericProviderTest.php | 132 +++++++++++++++++++++ .../tests/Unit/Activity/ProviderTest.php | 106 ----------------- 2 files changed, 132 insertions(+), 106 deletions(-) create mode 100644 apps/twofactor_backupcodes/tests/Unit/Activity/GenericProviderTest.php delete mode 100644 apps/twofactor_backupcodes/tests/Unit/Activity/ProviderTest.php diff --git a/apps/twofactor_backupcodes/tests/Unit/Activity/GenericProviderTest.php b/apps/twofactor_backupcodes/tests/Unit/Activity/GenericProviderTest.php new file mode 100644 index 00000000000..242c4ab4e8d --- /dev/null +++ b/apps/twofactor_backupcodes/tests/Unit/Activity/GenericProviderTest.php @@ -0,0 +1,132 @@ + + * @copyright Copyright (c) 2016 Christoph Wurst + * + * Two-factor backup codes + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + * + */ + +namespace OCA\TwoFactorBackupCodes\Test\Unit\Activity; + +use InvalidArgumentException; +use OCA\TwoFactorBackupCodes\Activity\GenericProvider; +use OCP\Activity\IEvent; +use OCP\IL10N; +use OCP\ILogger; +use OCP\IURLGenerator; +use OCP\L10N\IFactory; +use PHPUnit_Framework_MockObject_MockObject; +use Test\TestCase; + +class GenericProviderTest extends TestCase { + + /** @var IL10N|PHPUnit_Framework_MockObject_MockObject */ + private $l10n; + + /** @var IURLGenerator|PHPUnit_Framework_MockObject_MockObject */ + private $urlGenerator; + + /** @var ILogger|PHPUnit_Framework_MockObject_MockObject */ + private $logger; + + /** @var GenericProvider */ + private $provider; + + protected function setUp() { + parent::setUp(); + + $this->l10n = $this->createMock(IFactory::class); + $this->urlGenerator = $this->createMock(IURLGenerator::class); + $this->logger = $this->createMock(ILogger::class); + + $this->provider = new GenericProvider($this->l10n, $this->urlGenerator, $this->logger); + } + + public function testParseUnrelated() { + $lang = 'ru'; + $event = $this->createMock(IEvent::class); + $event->expects($this->once()) + ->method('getType') + ->willReturn('comments'); + $this->setExpectedException(InvalidArgumentException::class); + + $this->provider->parse($lang, $event); + } + + public function subjectData() { + return [ + ['twofactor_success'], + ['twofactor_failed'], + ]; + } + + /** + * @dataProvider subjectData + */ + public function testParse($subject) { + $lang = 'ru'; + $event = $this->createMock(IEvent::class); + $l = $this->createMock(IL10N::class); + + $event->expects($this->once()) + ->method('getType') + ->willReturn('twofactor'); + $this->l10n->expects($this->once()) + ->method('get') + ->with('core', $lang) + ->willReturn($l); + $this->urlGenerator->expects($this->once()) + ->method('imagePath') + ->with('core', 'actions/password.svg') + ->willReturn('path/to/image'); + $this->urlGenerator->expects($this->once()) + ->method('getAbsoluteURL') + ->with('path/to/image') + ->willReturn('absolute/path/to/image'); + $event->expects($this->once()) + ->method('setIcon') + ->with('absolute/path/to/image'); + $event->expects($this->once()) + ->method('getSubject') + ->willReturn($subject); + $event->expects($this->once()) + ->method('setParsedSubject'); + + $this->provider->parse($lang, $event); + } + + public function testParseInvalidSubject() { + $lang = 'ru'; + $l = $this->createMock(IL10N::class); + $event = $this->createMock(IEvent::class); + + $event->expects($this->once()) + ->method('getType') + ->willReturn('twofactor'); + $this->l10n->expects($this->once()) + ->method('get') + ->with('core', $lang) + ->willReturn($l); + $event->expects($this->once()) + ->method('getSubject') + ->willReturn('unrelated'); + + $this->expectException(InvalidArgumentException::class); + $this->provider->parse($lang, $event); + } + +} diff --git a/apps/twofactor_backupcodes/tests/Unit/Activity/ProviderTest.php b/apps/twofactor_backupcodes/tests/Unit/Activity/ProviderTest.php deleted file mode 100644 index 36e85ec1872..00000000000 --- a/apps/twofactor_backupcodes/tests/Unit/Activity/ProviderTest.php +++ /dev/null @@ -1,106 +0,0 @@ - - * @copyright Copyright (c) 2016 Christoph Wurst - * - * Two-factor backup codes - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\TwoFactorBackupCodes\Test\Unit\Activity; - -use InvalidArgumentException; -use OCA\TwoFactorBackupCodes\Activity\GenericProvider; -use OCP\Activity\IEvent; -use OCP\IL10N; -use OCP\ILogger; -use OCP\IURLGenerator; -use OCP\L10N\IFactory; -use Test\TestCase; - -class ProviderTest extends TestCase { - - private $l10n; - private $urlGenerator; - private $logger; - - /** @var GenericProvider */ - private $provider; - - protected function setUp() { - parent::setUp(); - - $this->l10n = $this->createMock(IFactory::class); - $this->urlGenerator = $this->createMock(IURLGenerator::class); - $this->logger = $this->createMock(ILogger::class); - - $this->provider = new GenericProvider($this->l10n, $this->urlGenerator, $this->logger); - } - - public function testParseUnrelated() { - $lang = 'ru'; - $event = $this->createMock(IEvent::class); - $event->expects($this->once()) - ->method('getType') - ->will($this->returnValue('comments')); - $this->setExpectedException(InvalidArgumentException::class); - - $this->provider->parse($lang, $event); - } - - public function subjectData() { - return [ - ['twofactor_success'], - ['twofactor_failed'], - ]; - } - - /** - * @dataProvider subjectData - */ - public function testParse($subject) { - $lang = 'ru'; - $event = $this->createMock(IEvent::class); - $l = $this->createMock(IL10N::class); - - $event->expects($this->once()) - ->method('getType') - ->will($this->returnValue('twofactor')); - $this->l10n->expects($this->once()) - ->method('get') - ->with('core', $lang) - ->will($this->returnValue($l)); - $this->urlGenerator->expects($this->once()) - ->method('imagePath') - ->with('core', 'actions/password.svg') - ->will($this->returnValue('path/to/image')); - $this->urlGenerator->expects($this->once()) - ->method('getAbsoluteURL') - ->with('path/to/image') - ->will($this->returnValue('absolute/path/to/image')); - $event->expects($this->once()) - ->method('setIcon') - ->with('absolute/path/to/image'); - $event->expects($this->once()) - ->method('getSubject') - ->will($this->returnValue($subject)); - $event->expects($this->once()) - ->method('setParsedSubject'); - - $this->provider->parse($lang, $event); - } - -} -- cgit v1.2.3 From 3b4b49fa957d6aa7eab5d3c24035dd71ea165d4e Mon Sep 17 00:00:00 2001 From: Christoph Wurst Date: Mon, 16 Jan 2017 11:48:28 +0100 Subject: test activity provider Signed-off-by: Christoph Wurst --- .../lib/Activity/Provider.php | 5 + .../tests/Unit/Activity/ProviderTest.php | 131 +++++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 apps/twofactor_backupcodes/tests/Unit/Activity/ProviderTest.php diff --git a/apps/twofactor_backupcodes/lib/Activity/Provider.php b/apps/twofactor_backupcodes/lib/Activity/Provider.php index cfb16c9f8d3..9c7aaeae630 100644 --- a/apps/twofactor_backupcodes/lib/Activity/Provider.php +++ b/apps/twofactor_backupcodes/lib/Activity/Provider.php @@ -40,6 +40,11 @@ class Provider implements IProvider { /** @var ILogger */ private $logger; + /** + * @param L10nFactory $l10n + * @param IURLGenerator $urlGenerator + * @param ILogger $logger + */ public function __construct(L10nFactory $l10n, IURLGenerator $urlGenerator, ILogger $logger) { $this->logger = $logger; $this->urlGenerator = $urlGenerator; diff --git a/apps/twofactor_backupcodes/tests/Unit/Activity/ProviderTest.php b/apps/twofactor_backupcodes/tests/Unit/Activity/ProviderTest.php new file mode 100644 index 00000000000..6f4c25fc22b --- /dev/null +++ b/apps/twofactor_backupcodes/tests/Unit/Activity/ProviderTest.php @@ -0,0 +1,131 @@ + + * @copyright Copyright (c) 2017 Christoph Wurst + * + * Two-factor backup codes + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + * + */ + +namespace OCA\TwoFactorBackupCodes\Test\Unit\Activity; + +use InvalidArgumentException; +use OCA\TwoFactorBackupCodes\Activity\Provider; +use OCP\Activity\IEvent; +use OCP\IL10N; +use OCP\ILogger; +use OCP\IURLGenerator; +use OCP\L10N\IFactory; +use PHPUnit_Framework_MockObject_MockObject; +use Test\TestCase; + +class ProviderTest extends TestCase { + + /** @var IL10N|PHPUnit_Framework_MockObject_MockObject */ + private $l10n; + + /** @var IURLGenerator|PHPUnit_Framework_MockObject_MockObject */ + private $urlGenerator; + + /** @var ILogger|PHPUnit_Framework_MockObject_MockObject */ + private $logger; + + /** @var Provider */ + private $provider; + + protected function setUp() { + parent::setUp(); + + $this->l10n = $this->createMock(IFactory::class); + $this->urlGenerator = $this->createMock(IURLGenerator::class); + $this->logger = $this->createMock(ILogger::class); + + $this->provider = new Provider($this->l10n, $this->urlGenerator, $this->logger); + } + + public function testParseUnrelated() { + $lang = 'ru'; + $event = $this->createMock(IEvent::class); + $event->expects($this->once()) + ->method('getType') + ->willReturn('comments'); + $this->setExpectedException(InvalidArgumentException::class); + + $this->provider->parse($lang, $event); + } + + public function subjectData() { + return [ + ['codes_generated'], + ]; + } + + /** + * @dataProvider subjectData + */ + public function testParse($subject) { + $lang = 'ru'; + $event = $this->createMock(IEvent::class); + $l = $this->createMock(IL10N::class); + + $event->expects($this->once()) + ->method('getApp') + ->willReturn('twofactor_backupcodes'); + $this->l10n->expects($this->once()) + ->method('get') + ->with('twofactor_backupcodes', $lang) + ->willReturn($l); + $this->urlGenerator->expects($this->once()) + ->method('imagePath') + ->with('core', 'actions/password.svg') + ->willReturn('path/to/image'); + $this->urlGenerator->expects($this->once()) + ->method('getAbsoluteURL') + ->with('path/to/image') + ->willReturn('absolute/path/to/image'); + $event->expects($this->once()) + ->method('setIcon') + ->with('absolute/path/to/image'); + $event->expects($this->once()) + ->method('getSubject') + ->willReturn($subject); + $event->expects($this->once()) + ->method('setParsedSubject'); + + $this->provider->parse($lang, $event); + } + + public function testParseInvalidSubject() { + $lang = 'ru'; + $l = $this->createMock(IL10N::class); + $event = $this->createMock(IEvent::class); + + $event->expects($this->once()) + ->method('getApp') + ->willReturn('twofactor_backupcodes'); + $this->l10n->expects($this->once()) + ->method('get') + ->with('twofactor_backupcodes', $lang) + ->willReturn($l); + $event->expects($this->once()) + ->method('getSubject') + ->willReturn('unrelated'); + + $this->expectException(InvalidArgumentException::class); + $this->provider->parse($lang, $event); + } + +} -- cgit v1.2.3 From 85e4febc24591ad8f5c531164ee4a73c0ae1cae9 Mon Sep 17 00:00:00 2001 From: Christoph Wurst Date: Mon, 16 Jan 2017 12:48:09 +0100 Subject: ignore code we're unable to test Signed-off-by: Christoph Wurst --- apps/twofactor_backupcodes/appinfo/app.php | 3 +++ apps/twofactor_backupcodes/appinfo/routes.php | 2 ++ apps/twofactor_backupcodes/lib/Db/BackupCodeMapper.php | 1 - apps/twofactor_backupcodes/settings/personal.php | 3 ++- apps/twofactor_backupcodes/tests/Unit/Activity/ProviderTest.php | 2 +- 5 files changed, 8 insertions(+), 3 deletions(-) diff --git a/apps/twofactor_backupcodes/appinfo/app.php b/apps/twofactor_backupcodes/appinfo/app.php index 31f9b6b8eae..3d830f57640 100644 --- a/apps/twofactor_backupcodes/appinfo/app.php +++ b/apps/twofactor_backupcodes/appinfo/app.php @@ -19,4 +19,7 @@ * along with this program. If not, see . * */ + +// @codeCoverageIgnoreStart OC_App::registerPersonal('twofactor_backupcodes', 'settings/personal'); +// @codeCoverageIgnoreEnd \ No newline at end of file diff --git a/apps/twofactor_backupcodes/appinfo/routes.php b/apps/twofactor_backupcodes/appinfo/routes.php index f2af12e9b45..dbfced08c92 100644 --- a/apps/twofactor_backupcodes/appinfo/routes.php +++ b/apps/twofactor_backupcodes/appinfo/routes.php @@ -19,6 +19,7 @@ * along with this program. If not, see . * */ +// @codeCoverageIgnoreStart return [ 'routes' => [ [ @@ -33,3 +34,4 @@ return [ ], ] ]; +// @codeCoverageIgnoreEnd \ No newline at end of file diff --git a/apps/twofactor_backupcodes/lib/Db/BackupCodeMapper.php b/apps/twofactor_backupcodes/lib/Db/BackupCodeMapper.php index f64e2e9e60b..85cc174fb6a 100644 --- a/apps/twofactor_backupcodes/lib/Db/BackupCodeMapper.php +++ b/apps/twofactor_backupcodes/lib/Db/BackupCodeMapper.php @@ -22,7 +22,6 @@ namespace OCA\TwoFactorBackupCodes\Db; use OCP\AppFramework\Db\Mapper; -use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; use OCP\IUser; diff --git a/apps/twofactor_backupcodes/settings/personal.php b/apps/twofactor_backupcodes/settings/personal.php index 037516e39a3..dc320c03f5b 100644 --- a/apps/twofactor_backupcodes/settings/personal.php +++ b/apps/twofactor_backupcodes/settings/personal.php @@ -1,5 +1,6 @@ fetchPage(); +// @codeCoverageIgnoreEnd \ No newline at end of file diff --git a/apps/twofactor_backupcodes/tests/Unit/Activity/ProviderTest.php b/apps/twofactor_backupcodes/tests/Unit/Activity/ProviderTest.php index 6f4c25fc22b..e1a13c89c10 100644 --- a/apps/twofactor_backupcodes/tests/Unit/Activity/ProviderTest.php +++ b/apps/twofactor_backupcodes/tests/Unit/Activity/ProviderTest.php @@ -60,7 +60,7 @@ class ProviderTest extends TestCase { $lang = 'ru'; $event = $this->createMock(IEvent::class); $event->expects($this->once()) - ->method('getType') + ->method('getApp') ->willReturn('comments'); $this->setExpectedException(InvalidArgumentException::class); -- cgit v1.2.3 From 2f30a71b1933e28efd6eaff0b9a453e0e4158b2c Mon Sep 17 00:00:00 2001 From: Christoph Wurst Date: Mon, 16 Jan 2017 14:32:43 +0100 Subject: make sure files end with emtpy line Signed-off-by: Christoph Wurst --- apps/twofactor_backupcodes/appinfo/app.php | 2 +- apps/twofactor_backupcodes/appinfo/routes.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/twofactor_backupcodes/appinfo/app.php b/apps/twofactor_backupcodes/appinfo/app.php index 3d830f57640..0cb10531360 100644 --- a/apps/twofactor_backupcodes/appinfo/app.php +++ b/apps/twofactor_backupcodes/appinfo/app.php @@ -22,4 +22,4 @@ // @codeCoverageIgnoreStart OC_App::registerPersonal('twofactor_backupcodes', 'settings/personal'); -// @codeCoverageIgnoreEnd \ No newline at end of file +// @codeCoverageIgnoreEnd diff --git a/apps/twofactor_backupcodes/appinfo/routes.php b/apps/twofactor_backupcodes/appinfo/routes.php index dbfced08c92..0119bfd0b08 100644 --- a/apps/twofactor_backupcodes/appinfo/routes.php +++ b/apps/twofactor_backupcodes/appinfo/routes.php @@ -34,4 +34,4 @@ return [ ], ] ]; -// @codeCoverageIgnoreEnd \ No newline at end of file +// @codeCoverageIgnoreEnd -- cgit v1.2.3 From 65f303164fd51fa4d810df4ff6d64693e6d04c98 Mon Sep 17 00:00:00 2001 From: Christoph Wurst Date: Mon, 16 Jan 2017 20:53:48 +0100 Subject: fix file ending Signed-off-by: Christoph Wurst --- apps/twofactor_backupcodes/settings/personal.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/twofactor_backupcodes/settings/personal.php b/apps/twofactor_backupcodes/settings/personal.php index dc320c03f5b..0a018c0ff28 100644 --- a/apps/twofactor_backupcodes/settings/personal.php +++ b/apps/twofactor_backupcodes/settings/personal.php @@ -3,4 +3,4 @@ $tmpl = new \OCP\Template('twofactor_backupcodes', 'personal'); return $tmpl->fetchPage(); -// @codeCoverageIgnoreEnd \ No newline at end of file +// @codeCoverageIgnoreEnd -- cgit v1.2.3