diff options
author | Joas Schilling <coding@schilljs.com> | 2017-02-10 18:34:47 +0300 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2017-02-10 18:57:28 +0300 |
commit | f95985000d0be15061d932c4a9e614bb9405f32b (patch) | |
tree | ba8aac50eaf30063ba4459ad51b1540122c8ec82 | |
parent | a2c08bea51a8a4bd2881282db641054e563256c4 (diff) |
Add notification unit tests
Signed-off-by: Joas Schilling <coding@schilljs.com>
-rw-r--r-- | tests/Notification/NotifierTest.php | 163 |
1 files changed, 163 insertions, 0 deletions
diff --git a/tests/Notification/NotifierTest.php b/tests/Notification/NotifierTest.php new file mode 100644 index 0000000..81c8f71 --- /dev/null +++ b/tests/Notification/NotifierTest.php @@ -0,0 +1,163 @@ +<?php +/** + * @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com> + * + * @author Joas Schilling <coding@schilljs.com> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * 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 + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\AdminNotifications\Tests\Notification; + +use OCA\AdminNotifications\AppInfo\Application; +use OCA\AdminNotifications\Notification\Notifier; +use OCP\IL10N; +use OCP\IURLGenerator; +use OCP\L10N\IFactory; +use OCP\Notification\INotification; + +class NotifierTest extends \Test\TestCase { + /** @var Notifier */ + protected $notifier; + + /** @var IFactory|\PHPUnit_Framework_MockObject_MockObject */ + protected $factory; + /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */ + protected $urlGenerator; + /** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */ + protected $l; + + protected function setUp() { + parent::setUp(); + + $this->urlGenerator = $this->createMock(IURLGenerator::class); + $this->l = $this->createMock(IL10N::class); + $this->l->expects($this->any()) + ->method('t') + ->willReturnCallback(function($string, $args) { + return vsprintf($string, $args); + }); + $this->factory = $this->createMock(IFactory::class); + $this->factory->expects($this->any()) + ->method('get') + ->willReturn($this->l); + + $this->notifier = new Notifier( + $this->factory, + $this->urlGenerator + ); + } + + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage Unknown app + */ + public function testPrepareWrongApp() { + /** @var INotification|\PHPUnit_Framework_MockObject_MockObject $notification */ + $notification = $this->createMock(INotification::class); + + $notification->expects($this->once()) + ->method('getApp') + ->willReturn('notifications'); + $notification->expects($this->never()) + ->method('getSubject'); + + $this->notifier->prepare($notification, 'en'); + } + + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage Unknown subject + */ + public function testPrepareWrongSubject() { + /** @var INotification|\PHPUnit_Framework_MockObject_MockObject $notification */ + $notification = $this->createMock(INotification::class); + + $notification->expects($this->once()) + ->method('getApp') + ->willReturn(Application::APP_ID); + $notification->expects($this->once()) + ->method('getSubject') + ->willReturn('wrong subject'); + + $this->notifier->prepare($notification, 'en'); + } + + public function dataPrepare() { + return [ + ['ocs', ['subject'], ['message'], true], + ]; + } + + /** + * @dataProvider dataPrepare + * + * @param string $subject + * @param array $subjectParams + * @param array $messageParams + * @param bool $setMessage + */ + public function testPrepare($subject, $subjectParams, $messageParams, $setMessage) { + /** @var INotification|\PHPUnit_Framework_MockObject_MockObject $notification */ + $notification = $this->createMock(INotification::class); + + $notification->expects($this->once()) + ->method('getApp') + ->willReturn(Application::APP_ID); + $notification->expects($this->once()) + ->method('getSubject') + ->willReturn($subject); + $notification->expects($this->once()) + ->method('getSubjectParameters') + ->willReturn($subjectParams); + $notification->expects($this->once()) + ->method('getMessageParameters') + ->willReturn($messageParams); + + $notification->expects($this->once()) + ->method('setParsedSubject') + ->with($subjectParams[0]) + ->willReturnSelf(); + + if ($setMessage) { + $notification->expects($this->once()) + ->method('setParsedMessage') + ->with($messageParams[0]) + ->willReturnSelf(); + } else { + $notification->expects($this->never()) + ->method('setParsedMessage'); + } + + $this->urlGenerator->expects($this->once()) + ->method('imagePath') + ->with(Application::APP_ID, 'app-dark.svg') + ->willReturn('icon-url'); + $this->urlGenerator->expects($this->once()) + ->method('getAbsoluteURL') + ->with('icon-url') + ->willReturn('absolute-icon-url'); + $notification->expects($this->once()) + ->method('setIcon') + ->with('absolute-icon-url') + ->willReturnSelf(); + + $return = $this->notifier->prepare($notification, 'en'); + + $this->assertEquals($notification, $return); + } +} |