You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ManagerTest.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * @author Joas Schilling <nickvergessen@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2015, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace Test\Notification;
  22. use OC\Notification\Manager;
  23. use OCP\ILogger;
  24. use OCP\Notification\IApp;
  25. use OCP\Notification\IManager;
  26. use OCP\Notification\INotification;
  27. use OCP\Notification\INotifier;
  28. use OCP\RichObjectStrings\IValidator;
  29. use PHPUnit\Framework\MockObject\MockObject;
  30. use Test\TestCase;
  31. class ManagerTest extends TestCase {
  32. /** @var IManager */
  33. protected $manager;
  34. /** @var IValidator|MockObject */
  35. protected $validator;
  36. /** @var ILogger|MockObject */
  37. protected $logger;
  38. public function setUp() {
  39. parent::setUp();
  40. $this->validator = $this->createMock(IValidator::class);
  41. $this->logger = $this->createMock(ILogger::class);
  42. $this->manager = new Manager($this->validator, $this->logger);
  43. }
  44. public function testRegisterApp() {
  45. $this->assertEquals([], self::invokePrivate($this->manager, 'getApps'));
  46. $this->manager->registerApp(DummyApp::class);
  47. $this->assertCount(1, self::invokePrivate($this->manager, 'getApps'));
  48. $this->assertCount(1, self::invokePrivate($this->manager, 'getApps'));
  49. $this->manager->registerApp(DummyApp::class);
  50. $this->assertCount(2, self::invokePrivate($this->manager, 'getApps'));
  51. }
  52. public function testRegisterAppInvalid() {
  53. $this->manager->registerApp(DummyNotifier::class);
  54. $this->logger->expects($this->once())
  55. ->method('error');
  56. self::invokePrivate($this->manager, 'getApps');
  57. }
  58. public function testRegisterNotifier() {
  59. $this->assertEquals([], self::invokePrivate($this->manager, 'getNotifiers'));
  60. $this->manager->registerNotifierService(DummyNotifier::class);
  61. $this->assertCount(1, self::invokePrivate($this->manager, 'getNotifiers'));
  62. $this->assertCount(1, self::invokePrivate($this->manager, 'getNotifiers'));
  63. $this->manager->registerNotifierService(DummyNotifier::class);
  64. $this->assertCount(2, self::invokePrivate($this->manager, 'getNotifiers'));
  65. }
  66. public function testRegisterNotifierInvalid() {
  67. $this->manager->registerNotifierService(DummyApp::class);
  68. $this->logger->expects($this->once())
  69. ->method('error');
  70. self::invokePrivate($this->manager, 'getNotifiers');
  71. }
  72. public function testCreateNotification() {
  73. $action = $this->manager->createNotification();
  74. $this->assertInstanceOf('OCP\Notification\INotification', $action);
  75. }
  76. public function testNotify() {
  77. /** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
  78. $notification = $this->getMockBuilder(INotification::class)
  79. ->disableOriginalConstructor()
  80. ->getMock();
  81. $notification->expects($this->once())
  82. ->method('isValid')
  83. ->willReturn(true);
  84. $manager = $this->getMockBuilder(Manager::class)
  85. ->setConstructorArgs([
  86. $this->validator,
  87. $this->logger,
  88. ])
  89. ->setMethods(['getApps'])
  90. ->getMock();
  91. $manager->expects($this->once())
  92. ->method('getApps')
  93. ->willReturn([]);
  94. $manager->notify($notification);
  95. }
  96. /**
  97. * @expectedException \InvalidArgumentException
  98. */
  99. public function testNotifyInvalid() {
  100. /** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
  101. $notification = $this->getMockBuilder(INotification::class)
  102. ->disableOriginalConstructor()
  103. ->getMock();
  104. $notification->expects($this->once())
  105. ->method('isValid')
  106. ->willReturn(false);
  107. $manager = $this->getMockBuilder(Manager::class)
  108. ->setConstructorArgs([
  109. $this->validator,
  110. $this->logger,
  111. ])
  112. ->setMethods(['getApps'])
  113. ->getMock();
  114. $manager->expects($this->never())
  115. ->method('getApps');
  116. $manager->notify($notification);
  117. }
  118. public function testMarkProcessed() {
  119. /** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
  120. $notification = $this->getMockBuilder(INotification::class)
  121. ->disableOriginalConstructor()
  122. ->getMock();
  123. $manager = $this->getMockBuilder(Manager::class)
  124. ->setConstructorArgs([
  125. $this->validator,
  126. $this->logger,
  127. ])
  128. ->setMethods(['getApps'])
  129. ->getMock();
  130. $manager->expects($this->once())
  131. ->method('getApps')
  132. ->willReturn([]);
  133. $manager->markProcessed($notification);
  134. }
  135. public function testGetCount() {
  136. /** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
  137. $notification = $this->getMockBuilder(INotification::class)
  138. ->disableOriginalConstructor()
  139. ->getMock();
  140. $manager = $this->getMockBuilder(Manager::class)
  141. ->setConstructorArgs([
  142. $this->validator,
  143. $this->logger,
  144. ])
  145. ->setMethods(['getApps'])
  146. ->getMock();
  147. $manager->expects($this->once())
  148. ->method('getApps')
  149. ->willReturn([]);
  150. $manager->getCount($notification);
  151. }
  152. }