1
0

NotifierTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\UpdateNotification\Tests\Notification;
  8. use OCA\UpdateNotification\Notification\Notifier;
  9. use OCP\IConfig;
  10. use OCP\IGroupManager;
  11. use OCP\IURLGenerator;
  12. use OCP\IUserSession;
  13. use OCP\L10N\IFactory;
  14. use OCP\Notification\AlreadyProcessedException;
  15. use OCP\Notification\IManager;
  16. use OCP\Notification\INotification;
  17. use Test\TestCase;
  18. class NotifierTest extends TestCase {
  19. /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
  20. protected $urlGenerator;
  21. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  22. protected $config;
  23. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  24. protected $notificationManager;
  25. /** @var IFactory|\PHPUnit\Framework\MockObject\MockObject */
  26. protected $l10nFactory;
  27. /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  28. protected $userSession;
  29. /** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */
  30. protected $groupManager;
  31. protected function setUp(): void {
  32. parent::setUp();
  33. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  34. $this->config = $this->createMock(IConfig::class);
  35. $this->notificationManager = $this->createMock(IManager::class);
  36. $this->l10nFactory = $this->createMock(IFactory::class);
  37. $this->userSession = $this->createMock(IUserSession::class);
  38. $this->groupManager = $this->createMock(IGroupManager::class);
  39. }
  40. /**
  41. * @param array $methods
  42. * @return Notifier|\PHPUnit\Framework\MockObject\MockObject
  43. */
  44. protected function getNotifier(array $methods = []) {
  45. if (empty($methods)) {
  46. return new Notifier(
  47. $this->urlGenerator,
  48. $this->config,
  49. $this->notificationManager,
  50. $this->l10nFactory,
  51. $this->userSession,
  52. $this->groupManager
  53. );
  54. }
  55. {
  56. return $this->getMockBuilder(Notifier::class)
  57. ->setConstructorArgs([
  58. $this->urlGenerator,
  59. $this->config,
  60. $this->notificationManager,
  61. $this->l10nFactory,
  62. $this->userSession,
  63. $this->groupManager,
  64. ])
  65. ->onlyMethods($methods)
  66. ->getMock();
  67. }
  68. }
  69. public function dataUpdateAlreadyInstalledCheck(): array {
  70. return [
  71. ['1.1.0', '1.0.0', false],
  72. ['1.1.0', '1.1.0', true],
  73. ['1.1.0', '1.2.0', true],
  74. ];
  75. }
  76. /**
  77. * @dataProvider dataUpdateAlreadyInstalledCheck
  78. *
  79. * @param string $versionNotification
  80. * @param string $versionInstalled
  81. * @param bool $exception
  82. */
  83. public function testUpdateAlreadyInstalledCheck(string $versionNotification, string $versionInstalled, bool $exception): void {
  84. $notifier = $this->getNotifier();
  85. $notification = $this->createMock(INotification::class);
  86. $notification->expects($this->once())
  87. ->method('getObjectId')
  88. ->willReturn($versionNotification);
  89. try {
  90. self::invokePrivate($notifier, 'updateAlreadyInstalledCheck', [$notification, $versionInstalled]);
  91. $this->assertFalse($exception);
  92. } catch (\Exception $e) {
  93. $this->assertTrue($exception);
  94. $this->assertInstanceOf(AlreadyProcessedException::class, $e);
  95. }
  96. }
  97. }