You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
3.4 KiB

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2024 Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Talk\Tests\php\Service;
  25. use OCA\Talk\Model\ProxyCacheMessage;
  26. use OCA\Talk\Model\ProxyCacheMessageMapper;
  27. use OCA\Talk\Service\ProxyCacheMessageService;
  28. use OCP\AppFramework\Db\DoesNotExistException;
  29. use OCP\AppFramework\Utility\ITimeFactory;
  30. use OCP\IDBConnection;
  31. use PHPUnit\Framework\MockObject\MockObject;
  32. use Psr\Log\LoggerInterface;
  33. use Test\TestCase;
  34. /**
  35. * @group DB
  36. */
  37. class ProxyCacheMessageServiceTest extends TestCase {
  38. protected LoggerInterface&MockObject $logger;
  39. protected ITimeFactory&MockObject $timeFactory;
  40. protected ?ProxyCacheMessageMapper $mapper = null;
  41. protected ?ProxyCacheMessageService $service = null;
  42. public function setUp(): void {
  43. parent::setUp();
  44. $this->mapper = new ProxyCacheMessageMapper(\OCP\Server::get(IDBConnection::class));
  45. $this->logger = $this->createMock(LoggerInterface::class);
  46. $this->timeFactory = $this->createMock(ITimeFactory::class);
  47. $this->service = new ProxyCacheMessageService(
  48. $this->mapper,
  49. $this->logger,
  50. $this->timeFactory,
  51. );
  52. $this->clearMessages();
  53. }
  54. public function tearDown(): void {
  55. $this->clearMessages();
  56. parent::tearDown();
  57. }
  58. protected function clearMessages(): void {
  59. $query = \OCP\Server::get(IDBConnection::class)->getQueryBuilder();
  60. $query->delete('talk_proxy_messages')
  61. ->where($query->expr()->eq('remote_server_url', $query->createNamedParameter('phpunittests')));
  62. $query->executeStatement();
  63. }
  64. public static function dataDeleteExpiredMessages(): array {
  65. return [
  66. [1234, 12345, true],
  67. [1234567, 12345, false],
  68. [null, 12345, false],
  69. ];
  70. }
  71. /**
  72. * @dataProvider dataDeleteExpiredMessages
  73. */
  74. public function testDeleteExpiredMessages(?int $messageTime, int $currentTime, bool $expired): void {
  75. $m1 = new ProxyCacheMessage();
  76. $m1->setLocalToken('local_token');
  77. $m1->setRemoteServerUrl('phpunittests');
  78. $m1->setRemoteToken('remote_token');
  79. $m1->setRemoteMessageId(12345);
  80. $m1->setActorType('actor_type');
  81. $m1->setActorId('actor_id');
  82. $m1->setMessageType('message_type');
  83. if ($messageTime === null) {
  84. $m1->setExpirationDatetime($messageTime);
  85. } else {
  86. $m1->setExpirationDatetime(new \DateTime('@' . $messageTime));
  87. }
  88. $this->mapper->insert($m1);
  89. $this->mapper->findById($m1->getId());
  90. $this->timeFactory->method('getDateTime')
  91. ->willReturn(new \DateTime('@' . $currentTime));
  92. $this->service->deleteExpiredMessages();
  93. if ($expired) {
  94. $this->expectException(DoesNotExistException::class);
  95. }
  96. $actual = $this->mapper->findById($m1->getId());
  97. if (!$expired) {
  98. $this->assertEquals($m1->getId(), $actual->getId());
  99. }
  100. }
  101. }