NotifierTest.php 3.8 KB

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