123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423 |
- <?php
- /**
- * @author Joas Schilling <nickvergessen@owncloud.com>
- *
- * @copyright Copyright (c) 2015, ownCloud, Inc.
- * @license AGPL-3.0
- *
- * 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 <http://www.gnu.org/licenses/>
- *
- */
- namespace Test\Notification;
- use OC\Notification\Manager;
- use OCP\Notification\IApp;
- use OCP\Notification\IManager;
- use OCP\Notification\INotification;
- use OCP\Notification\INotifier;
- use OCP\RichObjectStrings\IValidator;
- use Test\TestCase;
- class ManagerTest extends TestCase {
- /** @var IManager */
- protected $manager;
- public function setUp() {
- parent::setUp();
- $validator = $this->createMock(IValidator::class);
- $this->manager = new Manager($validator);
- }
- public function testRegisterApp() {
- $app = $this->getMockBuilder(IApp::class)
- ->disableOriginalConstructor()
- ->getMock();
- $closure = function() use ($app) {
- return $app;
- };
- $this->assertEquals([], $this->invokePrivate($this->manager, 'getApps'));
- $this->manager->registerApp($closure);
- $this->assertEquals([$app], $this->invokePrivate($this->manager, 'getApps'));
- $this->assertEquals([$app], $this->invokePrivate($this->manager, 'getApps'));
- $this->manager->registerApp($closure);
- $this->assertEquals([$app, $app], $this->invokePrivate($this->manager, 'getApps'));
- }
- /**
- * @expectedException \InvalidArgumentException
- */
- public function testRegisterAppInvalid() {
- $notifier = $this->getMockBuilder(INotifier::class)
- ->disableOriginalConstructor()
- ->getMock();
- $closure = function() use ($notifier) {
- return $notifier;
- };
- $this->manager->registerApp($closure);
- $this->invokePrivate($this->manager, 'getApps');
- }
- public function testRegisterNotifier() {
- $notifier = $this->getMockBuilder(INotifier::class)
- ->disableOriginalConstructor()
- ->getMock();
- $closure = function() use ($notifier) {
- return $notifier;
- };
- $this->assertEquals([], $this->invokePrivate($this->manager, 'getNotifiers'));
- $this->assertEquals([], $this->invokePrivate($this->manager, 'listNotifiers'));
- $this->manager->registerNotifier($closure, function() {
- return ['id' => 'test1', 'name' => 'Test One'];
- });
- $this->assertEquals([$notifier], $this->invokePrivate($this->manager, 'getNotifiers'));
- $this->assertEquals(['test1' => 'Test One'], $this->invokePrivate($this->manager, 'listNotifiers'));
- $this->assertEquals([$notifier], $this->invokePrivate($this->manager, 'getNotifiers'));
- $this->assertEquals(['test1' => 'Test One'], $this->invokePrivate($this->manager, 'listNotifiers'));
- $this->manager->registerNotifier($closure, function() {
- return ['id' => 'test2', 'name' => 'Test Two'];
- });
- $this->assertEquals([$notifier, $notifier], $this->invokePrivate($this->manager, 'getNotifiers'));
- $this->assertEquals(['test1' => 'Test One', 'test2' => 'Test Two'], $this->invokePrivate($this->manager, 'listNotifiers'));
- }
- /**
- * @expectedException \InvalidArgumentException
- */
- public function testRegisterNotifierInvalid() {
- $app = $this->getMockBuilder(IApp::class)
- ->disableOriginalConstructor()
- ->getMock();
- $closure = function() use ($app) {
- return $app;
- };
- $this->manager->registerNotifier($closure, function() {
- return ['id' => 'test1', 'name' => 'Test One'];
- });
- $this->invokePrivate($this->manager, 'getNotifiers');
- }
- public function dataRegisterNotifierInfoInvalid() {
- return [
- [null],
- ['No array'],
- [['id' => 'test1', 'name' => 'Test One', 'size' => 'Invalid']],
- [['no-id' => 'test1', 'name' => 'Test One']],
- [['id' => 'test1', 'no-name' => 'Test One']],
- ];
- }
- /**
- * @dataProvider dataRegisterNotifierInfoInvalid
- * @expectedException \InvalidArgumentException
- * @param mixed $data
- */
- public function testRegisterNotifierInfoInvalid($data) {
- $app = $this->getMockBuilder(IApp::class)
- ->disableOriginalConstructor()
- ->getMock();
- $closure = function() use ($app) {
- return $app;
- };
- $this->manager->registerNotifier($closure, function() use ($data) {
- return $data;
- });
- $this->manager->listNotifiers();
- }
- /**
- * @expectedException \InvalidArgumentException
- * @expectedExceptionMessage The given notifier ID test1 is already in use
- */
- public function testRegisterNotifierInfoDuplicate() {
- $app = $this->getMockBuilder(IApp::class)
- ->disableOriginalConstructor()
- ->getMock();
- $closure = function() use ($app) {
- return $app;
- };
- $this->manager->registerNotifier($closure, function() {
- return ['id' => 'test1', 'name' => 'Test One'];
- });
- $this->manager->registerNotifier($closure, function() {
- return ['id' => 'test1', 'name' => 'Test One'];
- });
- $this->manager->listNotifiers();
- }
- public function testCreateNotification() {
- $action = $this->manager->createNotification();
- $this->assertInstanceOf('OCP\Notification\INotification', $action);
- }
- public function testNotify() {
- /** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
- $notification = $this->getMockBuilder(INotification::class)
- ->disableOriginalConstructor()
- ->getMock();
- $notification->expects($this->once())
- ->method('isValid')
- ->willReturn(true);
- /** @var \OCP\Notification\IApp|\PHPUnit_Framework_MockObject_MockObject $app */
- $app = $this->getMockBuilder(IApp::class)
- ->disableOriginalConstructor()
- ->getMock();
- $app->expects($this->once())
- ->method('notify')
- ->with($notification);
- /** @var \OCP\Notification\IApp|\PHPUnit_Framework_MockObject_MockObject $app2 */
- $app2 = $this->getMockBuilder(IApp::class)
- ->disableOriginalConstructor()
- ->getMock();
- $app2->expects($this->once())
- ->method('notify')
- ->with($notification);
- $this->manager->registerApp(function() use ($app) {
- return $app;
- });
- $this->manager->registerApp(function() use ($app2) {
- return $app2;
- });
- $this->manager->notify($notification);
- }
- /**
- * @expectedException \InvalidArgumentException
- */
- public function testNotifyInvalid() {
- /** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
- $notification = $this->getMockBuilder(INotification::class)
- ->disableOriginalConstructor()
- ->getMock();
- $notification->expects($this->once())
- ->method('isValid')
- ->willReturn(false);
- $this->manager->notify($notification);
- }
- public function testPrepare() {
- /** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
- $notification = $this->getMockBuilder(INotification::class)
- ->disableOriginalConstructor()
- ->getMock();
- $notification->expects($this->once())
- ->method('isValidParsed')
- ->willReturn(true);
- /** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification2 */
- $notification2 = $this->getMockBuilder(INotification::class)
- ->disableOriginalConstructor()
- ->getMock();
- $notification2->expects($this->exactly(2))
- ->method('isValidParsed')
- ->willReturn(true);
- /** @var \OCP\Notification\IApp|\PHPUnit_Framework_MockObject_MockObject $notifier */
- $notifier = $this->getMockBuilder(INotifier::class)
- ->disableOriginalConstructor()
- ->getMock();
- $notifier->expects($this->once())
- ->method('prepare')
- ->with($notification, 'en')
- ->willReturnArgument(0);
- /** @var \OCP\Notification\IApp|\PHPUnit_Framework_MockObject_MockObject $notifier2 */
- $notifier2 = $this->getMockBuilder(INotifier::class)
- ->disableOriginalConstructor()
- ->getMock();
- $notifier2->expects($this->once())
- ->method('prepare')
- ->with($notification, 'en')
- ->willReturn($notification2);
- $this->manager->registerNotifier(function() use ($notifier) {
- return $notifier;
- }, function() {
- return ['id' => 'test1', 'name' => 'Test One'];
- });
- $this->manager->registerNotifier(function() use ($notifier2) {
- return $notifier2;
- }, function() {
- return ['id' => 'test2', 'name' => 'Test Two'];
- });
- $this->assertEquals($notification2, $this->manager->prepare($notification, 'en'));
- }
- /**
- * @expectedException \InvalidArgumentException
- */
- public function testPrepareInvalid() {
- /** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
- $notification = $this->getMockBuilder(INotification::class)
- ->disableOriginalConstructor()
- ->getMock();
- $notification->expects($this->once())
- ->method('isValidParsed')
- ->willReturn(false);
- /** @var \OCP\Notification\IApp|\PHPUnit_Framework_MockObject_MockObject $notifier */
- $notifier = $this->getMockBuilder(INotifier::class)
- ->disableOriginalConstructor()
- ->getMock();
- $notifier->expects($this->once())
- ->method('prepare')
- ->with($notification, 'de')
- ->willReturnArgument(0);
- $this->manager->registerNotifier(function() use ($notifier) {
- return $notifier;
- }, function() {
- return ['id' => 'test1', 'name' => 'Test One'];
- });
- $this->manager->prepare($notification, 'de');
- }
- public function testPrepareNotifierThrows() {
- /** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
- $notification = $this->getMockBuilder(INotification::class)
- ->disableOriginalConstructor()
- ->getMock();
- $notification->expects($this->once())
- ->method('isValidParsed')
- ->willReturn(true);
- /** @var \OCP\Notification\IApp|\PHPUnit_Framework_MockObject_MockObject $notifier */
- $notifier = $this->getMockBuilder(INotifier::class)
- ->disableOriginalConstructor()
- ->getMock();
- $notifier->expects($this->once())
- ->method('prepare')
- ->with($notification, 'de')
- ->willThrowException(new \InvalidArgumentException);
- $this->manager->registerNotifier(function() use ($notifier) {
- return $notifier;
- }, function() {
- return ['id' => 'test1', 'name' => 'Test One'];
- });
- $this->assertEquals($notification, $this->manager->prepare($notification, 'de'));
- }
- /**
- * @expectedException \InvalidArgumentException
- */
- public function testPrepareNoNotifier() {
- /** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
- $notification = $this->getMockBuilder(INotification::class)
- ->disableOriginalConstructor()
- ->getMock();
- $notification->expects($this->once())
- ->method('isValidParsed')
- ->willReturn(false);
- $this->manager->prepare($notification, 'en');
- }
- public function testMarkProcessed() {
- /** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
- $notification = $this->getMockBuilder(INotification::class)
- ->disableOriginalConstructor()
- ->getMock();
- /** @var \OCP\Notification\IApp|\PHPUnit_Framework_MockObject_MockObject $app */
- $app = $this->getMockBuilder(IApp::class)
- ->disableOriginalConstructor()
- ->getMock();
- $app->expects($this->once())
- ->method('markProcessed')
- ->with($notification);
- /** @var \OCP\Notification\IApp|\PHPUnit_Framework_MockObject_MockObject $app2 */
- $app2 = $this->getMockBuilder(IApp::class)
- ->disableOriginalConstructor()
- ->getMock();
- $app2->expects($this->once())
- ->method('markProcessed')
- ->with($notification);
- $this->manager->registerApp(function() use ($app) {
- return $app;
- });
- $this->manager->registerApp(function() use ($app2) {
- return $app2;
- });
- $this->manager->markProcessed($notification);
- }
- public function testGetCount() {
- /** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
- $notification = $this->getMockBuilder(INotification::class)
- ->disableOriginalConstructor()
- ->getMock();
- /** @var \OCP\Notification\IApp|\PHPUnit_Framework_MockObject_MockObject $app */
- $app = $this->getMockBuilder(IApp::class)
- ->disableOriginalConstructor()
- ->getMock();
- $app->expects($this->once())
- ->method('getCount')
- ->with($notification)
- ->willReturn(21);
- /** @var \OCP\Notification\IApp|\PHPUnit_Framework_MockObject_MockObject $app2 */
- $app2 = $this->getMockBuilder(IApp::class)
- ->disableOriginalConstructor()
- ->getMock();
- $app2->expects($this->once())
- ->method('getCount')
- ->with($notification)
- ->willReturn(42);
- $this->manager->registerApp(function() use ($app) {
- return $app;
- });
- $this->manager->registerApp(function() use ($app2) {
- return $app2;
- });
- $this->assertSame(63, $this->manager->getCount($notification));
- }
- }
|