NotifierTest.php 3.0 KB

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