ManagerTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Calendar\Resource;
  8. use OC\AppFramework\Bootstrap\Coordinator;
  9. use OC\AppFramework\Bootstrap\RegistrationContext;
  10. use OC\AppFramework\Bootstrap\ServiceRegistration;
  11. use OC\Calendar\Resource\Manager;
  12. use OC\Calendar\ResourcesRoomsUpdater;
  13. use OCP\Calendar\Resource\IBackend;
  14. use OCP\IServerContainer;
  15. use PHPUnit\Framework\MockObject\MockObject;
  16. use Test\TestCase;
  17. class ManagerTest extends TestCase {
  18. /** @var Coordinator|MockObject */
  19. private $coordinator;
  20. /** @var IServerContainer|MockObject */
  21. private $server;
  22. /** @var ResourcesRoomsUpdater|MockObject */
  23. private $resourcesRoomsUpdater;
  24. /** @var Manager */
  25. private $manager;
  26. protected function setUp(): void {
  27. parent::setUp();
  28. $this->coordinator = $this->createMock(Coordinator::class);
  29. $this->server = $this->createMock(IServerContainer::class);
  30. $this->resourcesRoomsUpdater = $this->createMock(ResourcesRoomsUpdater::class);
  31. $this->manager = new Manager(
  32. $this->coordinator,
  33. $this->server,
  34. $this->resourcesRoomsUpdater,
  35. );
  36. }
  37. public function testRegisterUnregisterBackend(): void {
  38. $backend1 = $this->createMock(IBackend::class);
  39. $backend1->method('getBackendIdentifier')->willReturn('backend_1');
  40. $backend2 = $this->createMock(IBackend::class);
  41. $backend2->method('getBackendIdentifier')->willReturn('backend_2');
  42. $this->server->expects(self::exactly(2))
  43. ->method('query')
  44. ->willReturnMap([
  45. ['calendar_resource_backend1', true, $backend1,],
  46. ['calendar_resource_backend2', true, $backend2,],
  47. ]);
  48. $this->manager->registerBackend('calendar_resource_backend1');
  49. $this->manager->registerBackend('calendar_resource_backend2');
  50. self::assertEquals([
  51. $backend1, $backend2
  52. ], $this->manager->getBackends());
  53. $this->manager->unregisterBackend('calendar_resource_backend1');
  54. self::assertEquals([
  55. $backend2
  56. ], $this->manager->getBackends());
  57. }
  58. public function testGetBackendFromBootstrapRegistration(): void {
  59. $backendClass = '\\OCA\\CalendarResourceFoo\\Backend';
  60. $backend = $this->createMock(IBackend::class);
  61. $backend->method('getBackendIdentifier')->willReturn('from_bootstrap');
  62. $context = $this->createMock(RegistrationContext::class);
  63. $this->coordinator->expects(self::once())
  64. ->method('getRegistrationContext')
  65. ->willReturn($context);
  66. $context->expects(self::once())
  67. ->method('getCalendarResourceBackendRegistrations')
  68. ->willReturn([
  69. new ServiceRegistration('calendar_resource_foo', $backendClass)
  70. ]);
  71. $this->server->expects(self::once())
  72. ->method('query')
  73. ->with($backendClass)
  74. ->willReturn($backend);
  75. self::assertEquals($backend, $this->manager->getBackend('from_bootstrap'));
  76. }
  77. public function testGetBackend(): void {
  78. $backend1 = $this->createMock(IBackend::class);
  79. $backend1->method('getBackendIdentifier')->willReturn('backend_1');
  80. $backend2 = $this->createMock(IBackend::class);
  81. $backend2->method('getBackendIdentifier')->willReturn('backend_2');
  82. $this->server->expects(self::exactly(2))
  83. ->method('query')
  84. ->willReturnMap([
  85. ['calendar_resource_backend1', true, $backend1,],
  86. ['calendar_resource_backend2', true, $backend2,],
  87. ]);
  88. $this->manager->registerBackend('calendar_resource_backend1');
  89. $this->manager->registerBackend('calendar_resource_backend2');
  90. self::assertEquals($backend1, $this->manager->getBackend('backend_1'));
  91. self::assertEquals($backend2, $this->manager->getBackend('backend_2'));
  92. }
  93. public function testClear(): void {
  94. $backend1 = $this->createMock(IBackend::class);
  95. $backend1->method('getBackendIdentifier')->willReturn('backend_1');
  96. $backend2 = $this->createMock(IBackend::class);
  97. $backend2->method('getBackendIdentifier')->willReturn('backend_2');
  98. $this->server->expects(self::exactly(2))
  99. ->method('query')
  100. ->willReturnMap([
  101. ['calendar_resource_backend1', true, $backend1,],
  102. ['calendar_resource_backend2', true, $backend2,],
  103. ]);
  104. $this->manager->registerBackend('calendar_resource_backend1');
  105. $this->manager->registerBackend('calendar_resource_backend2');
  106. self::assertEquals([
  107. $backend1, $backend2
  108. ], $this->manager->getBackends());
  109. $this->manager->clear();
  110. self::assertEquals([], $this->manager->getBackends());
  111. }
  112. public function testUpdate(): void {
  113. $this->resourcesRoomsUpdater->expects(self::once())
  114. ->method('updateResources');
  115. $this->manager->update();
  116. }
  117. }