NotifierTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\UpdateNotification\Tests\Notification;
  24. use OCA\UpdateNotification\Notification\Notifier;
  25. use OCP\IConfig;
  26. use OCP\IGroupManager;
  27. use OCP\IURLGenerator;
  28. use OCP\IUserSession;
  29. use OCP\L10N\IFactory;
  30. use OCP\Notification\IManager;
  31. use OCP\Notification\INotification;
  32. use Test\TestCase;
  33. class NotifierTest extends TestCase {
  34. /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
  35. protected $urlGenerator;
  36. /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
  37. protected $config;
  38. /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */
  39. protected $notificationManager;
  40. /** @var IFactory|\PHPUnit_Framework_MockObject_MockObject */
  41. protected $l10nFactory;
  42. /** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */
  43. protected $userSession;
  44. /** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */
  45. protected $groupManager;
  46. public function setUp() {
  47. parent::setUp();
  48. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  49. $this->config = $this->createMock(IConfig::class);
  50. $this->notificationManager = $this->createMock(IManager::class);
  51. $this->l10nFactory = $this->createMock(IFactory::class);
  52. $this->userSession = $this->createMock(IUserSession::class);
  53. $this->groupManager = $this->createMock(IGroupManager::class);
  54. }
  55. /**
  56. * @param array $methods
  57. * @return Notifier|\PHPUnit_Framework_MockObject_MockObject
  58. */
  59. protected function getNotifier(array $methods = []) {
  60. if (empty($methods)) {
  61. return new Notifier(
  62. $this->urlGenerator,
  63. $this->config,
  64. $this->notificationManager,
  65. $this->l10nFactory,
  66. $this->userSession,
  67. $this->groupManager
  68. );
  69. } {
  70. return $this->getMockBuilder(Notifier::class)
  71. ->setConstructorArgs([
  72. $this->urlGenerator,
  73. $this->config,
  74. $this->notificationManager,
  75. $this->l10nFactory,
  76. $this->userSession,
  77. $this->groupManager,
  78. ])
  79. ->setMethods($methods)
  80. ->getMock();
  81. }
  82. }
  83. public function dataUpdateAlreadyInstalledCheck(): array {
  84. return [
  85. ['1.1.0', '1.0.0', false],
  86. ['1.1.0', '1.1.0', true],
  87. ['1.1.0', '1.2.0', true],
  88. ];
  89. }
  90. /**
  91. * @dataProvider dataUpdateAlreadyInstalledCheck
  92. *
  93. * @param string $versionNotification
  94. * @param string $versionInstalled
  95. * @param bool $exception
  96. */
  97. public function testUpdateAlreadyInstalledCheck(string $versionNotification, string $versionInstalled, bool $exception) {
  98. $notifier = $this->getNotifier();
  99. $notification = $this->createMock(INotification::class);
  100. $notification->expects($this->once())
  101. ->method('getObjectId')
  102. ->willReturn($versionNotification);
  103. if ($exception) {
  104. $this->notificationManager->expects($this->once())
  105. ->method('markProcessed')
  106. ->with($notification);
  107. } else {
  108. $this->notificationManager->expects($this->never())
  109. ->method('markProcessed');
  110. }
  111. try {
  112. self::invokePrivate($notifier, 'updateAlreadyInstalledCheck', [$notification, $versionInstalled]);
  113. $this->assertFalse($exception);
  114. } catch (\Exception $e) {
  115. $this->assertTrue($exception);
  116. $this->assertInstanceOf('InvalidArgumentException', $e);
  117. }
  118. }
  119. }