NotifierTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\AlreadyProcessedException;
  31. use OCP\Notification\IManager;
  32. use OCP\Notification\INotification;
  33. use Test\TestCase;
  34. class NotifierTest extends TestCase {
  35. /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
  36. protected $urlGenerator;
  37. /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
  38. protected $config;
  39. /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */
  40. protected $notificationManager;
  41. /** @var IFactory|\PHPUnit_Framework_MockObject_MockObject */
  42. protected $l10nFactory;
  43. /** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */
  44. protected $userSession;
  45. /** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */
  46. protected $groupManager;
  47. public function setUp() {
  48. parent::setUp();
  49. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  50. $this->config = $this->createMock(IConfig::class);
  51. $this->notificationManager = $this->createMock(IManager::class);
  52. $this->l10nFactory = $this->createMock(IFactory::class);
  53. $this->userSession = $this->createMock(IUserSession::class);
  54. $this->groupManager = $this->createMock(IGroupManager::class);
  55. }
  56. /**
  57. * @param array $methods
  58. * @return Notifier|\PHPUnit_Framework_MockObject_MockObject
  59. */
  60. protected function getNotifier(array $methods = []) {
  61. if (empty($methods)) {
  62. return new Notifier(
  63. $this->urlGenerator,
  64. $this->config,
  65. $this->notificationManager,
  66. $this->l10nFactory,
  67. $this->userSession,
  68. $this->groupManager
  69. );
  70. } {
  71. return $this->getMockBuilder(Notifier::class)
  72. ->setConstructorArgs([
  73. $this->urlGenerator,
  74. $this->config,
  75. $this->notificationManager,
  76. $this->l10nFactory,
  77. $this->userSession,
  78. $this->groupManager,
  79. ])
  80. ->setMethods($methods)
  81. ->getMock();
  82. }
  83. }
  84. public function dataUpdateAlreadyInstalledCheck(): array {
  85. return [
  86. ['1.1.0', '1.0.0', false],
  87. ['1.1.0', '1.1.0', true],
  88. ['1.1.0', '1.2.0', true],
  89. ];
  90. }
  91. /**
  92. * @dataProvider dataUpdateAlreadyInstalledCheck
  93. *
  94. * @param string $versionNotification
  95. * @param string $versionInstalled
  96. * @param bool $exception
  97. */
  98. public function testUpdateAlreadyInstalledCheck(string $versionNotification, string $versionInstalled, bool $exception) {
  99. $notifier = $this->getNotifier();
  100. $notification = $this->createMock(INotification::class);
  101. $notification->expects($this->once())
  102. ->method('getObjectId')
  103. ->willReturn($versionNotification);
  104. try {
  105. self::invokePrivate($notifier, 'updateAlreadyInstalledCheck', [$notification, $versionInstalled]);
  106. $this->assertFalse($exception);
  107. } catch (\Exception $e) {
  108. $this->assertTrue($exception);
  109. $this->assertInstanceOf(AlreadyProcessedException::class, $e);
  110. }
  111. }
  112. }