PruneOutdatedSyncTokensJobTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2018, Georg Ehrke <oc.list@georgehrke.com>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Georg Ehrke <oc.list@georgehrke.com>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. *
  12. * @license GNU AGPL version 3 or any later version
  13. *
  14. * This program is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License as
  16. * published by the Free Software Foundation, either version 3 of the
  17. * License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. *
  27. */
  28. namespace OCA\DAV\Tests\unit\BackgroundJob;
  29. use OCA\DAV\AppInfo\Application;
  30. use OCA\DAV\BackgroundJob\PruneOutdatedSyncTokensJob;
  31. use OCA\DAV\CalDAV\CalDavBackend;
  32. use OCA\DAV\CardDAV\CardDavBackend;
  33. use OCP\AppFramework\Utility\ITimeFactory;
  34. use OCP\IConfig;
  35. use PHPUnit\Framework\MockObject\MockObject;
  36. use Psr\Log\LoggerInterface;
  37. use Test\TestCase;
  38. class PruneOutdatedSyncTokensJobTest extends TestCase {
  39. /** @var ITimeFactory | MockObject */
  40. private $timeFactory;
  41. /** @var CalDavBackend | MockObject */
  42. private $calDavBackend;
  43. /** @var CardDavBackend | MockObject */
  44. private $cardDavBackend;
  45. /** @var IConfig|MockObject */
  46. private $config;
  47. /** @var LoggerInterface|MockObject*/
  48. private $logger;
  49. private PruneOutdatedSyncTokensJob $backgroundJob;
  50. protected function setUp(): void {
  51. parent::setUp();
  52. $this->timeFactory = $this->createMock(ITimeFactory::class);
  53. $this->calDavBackend = $this->createMock(CalDavBackend::class);
  54. $this->cardDavBackend = $this->createMock(CardDavBackend::class);
  55. $this->config = $this->createMock(IConfig::class);
  56. $this->logger = $this->createMock(LoggerInterface::class);
  57. $this->backgroundJob = new PruneOutdatedSyncTokensJob($this->timeFactory, $this->calDavBackend, $this->cardDavBackend, $this->config, $this->logger);
  58. }
  59. /**
  60. * @dataProvider dataForTestRun
  61. */
  62. public function testRun(string $configValue, int $actualLimit, int $deletedCalendarSyncTokens, int $deletedAddressBookSyncTokens): void {
  63. $this->config->expects($this->once())
  64. ->method('getAppValue')
  65. ->with(Application::APP_ID, 'totalNumberOfSyncTokensToKeep', '10000')
  66. ->willReturn($configValue);
  67. $this->calDavBackend->expects($this->once())
  68. ->method('pruneOutdatedSyncTokens')
  69. ->with($actualLimit)
  70. ->willReturn($deletedCalendarSyncTokens);
  71. $this->cardDavBackend->expects($this->once())
  72. ->method('pruneOutdatedSyncTokens')
  73. ->with($actualLimit)
  74. ->willReturn($deletedAddressBookSyncTokens);
  75. $this->logger->expects($this->once())
  76. ->method('info')
  77. ->with('Pruned {calendarSyncTokensNumber} calendar sync tokens and {addressBooksSyncTokensNumber} address book sync tokens', [
  78. 'calendarSyncTokensNumber' => $deletedCalendarSyncTokens,
  79. 'addressBooksSyncTokensNumber' => $deletedAddressBookSyncTokens
  80. ]);
  81. $this->backgroundJob->run(null);
  82. }
  83. public function dataForTestRun(): array {
  84. return [
  85. ['100', 100, 2, 3],
  86. ['0', 1, 0, 0]
  87. ];
  88. }
  89. }