ManagerTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 OCP\Calendar\Resource\IBackend;
  13. use OCP\IServerContainer;
  14. use PHPUnit\Framework\MockObject\MockObject;
  15. use Test\TestCase;
  16. class ManagerTest extends TestCase {
  17. /** @var Coordinator|MockObject */
  18. private $coordinator;
  19. /** @var IServerContainer|MockObject */
  20. private $server;
  21. /** @var Manager */
  22. private $manager;
  23. protected function setUp(): void {
  24. parent::setUp();
  25. $this->coordinator = $this->createMock(Coordinator::class);
  26. $this->server = $this->createMock(IServerContainer::class);
  27. $this->manager = new Manager(
  28. $this->coordinator,
  29. $this->server,
  30. );
  31. }
  32. public function testRegisterUnregisterBackend(): void {
  33. $backend1 = $this->createMock(IBackend::class);
  34. $backend1->method('getBackendIdentifier')->willReturn('backend_1');
  35. $backend2 = $this->createMock(IBackend::class);
  36. $backend2->method('getBackendIdentifier')->willReturn('backend_2');
  37. $this->server->expects(self::exactly(2))
  38. ->method('query')
  39. ->willReturnMap([
  40. ['calendar_resource_backend1', true, $backend1,],
  41. ['calendar_resource_backend2', true, $backend2,],
  42. ]);
  43. $this->manager->registerBackend('calendar_resource_backend1');
  44. $this->manager->registerBackend('calendar_resource_backend2');
  45. self::assertEquals([
  46. $backend1, $backend2
  47. ], $this->manager->getBackends());
  48. $this->manager->unregisterBackend('calendar_resource_backend1');
  49. self::assertEquals([
  50. $backend2
  51. ], $this->manager->getBackends());
  52. }
  53. public function testGetBackendFromBootstrapRegistration(): void {
  54. $backendClass = '\\OCA\\CalendarResourceFoo\\Backend';
  55. $backend = $this->createMock(IBackend::class);
  56. $backend->method('getBackendIdentifier')->willReturn('from_bootstrap');
  57. $context = $this->createMock(RegistrationContext::class);
  58. $this->coordinator->expects(self::once())
  59. ->method('getRegistrationContext')
  60. ->willReturn($context);
  61. $context->expects(self::once())
  62. ->method('getCalendarResourceBackendRegistrations')
  63. ->willReturn([
  64. new ServiceRegistration('calendar_resource_foo', $backendClass)
  65. ]);
  66. $this->server->expects(self::once())
  67. ->method('query')
  68. ->with($backendClass)
  69. ->willReturn($backend);
  70. self::assertEquals($backend, $this->manager->getBackend('from_bootstrap'));
  71. }
  72. public function testGetBackend(): void {
  73. $backend1 = $this->createMock(IBackend::class);
  74. $backend1->method('getBackendIdentifier')->willReturn('backend_1');
  75. $backend2 = $this->createMock(IBackend::class);
  76. $backend2->method('getBackendIdentifier')->willReturn('backend_2');
  77. $this->server->expects(self::exactly(2))
  78. ->method('query')
  79. ->willReturnMap([
  80. ['calendar_resource_backend1', true, $backend1,],
  81. ['calendar_resource_backend2', true, $backend2,],
  82. ]);
  83. $this->manager->registerBackend('calendar_resource_backend1');
  84. $this->manager->registerBackend('calendar_resource_backend2');
  85. self::assertEquals($backend1, $this->manager->getBackend('backend_1'));
  86. self::assertEquals($backend2, $this->manager->getBackend('backend_2'));
  87. }
  88. public function testClear(): void {
  89. $backend1 = $this->createMock(IBackend::class);
  90. $backend1->method('getBackendIdentifier')->willReturn('backend_1');
  91. $backend2 = $this->createMock(IBackend::class);
  92. $backend2->method('getBackendIdentifier')->willReturn('backend_2');
  93. $this->server->expects(self::exactly(2))
  94. ->method('query')
  95. ->willReturnMap([
  96. ['calendar_resource_backend1', true, $backend1,],
  97. ['calendar_resource_backend2', true, $backend2,],
  98. ]);
  99. $this->manager->registerBackend('calendar_resource_backend1');
  100. $this->manager->registerBackend('calendar_resource_backend2');
  101. self::assertEquals([
  102. $backend1, $backend2
  103. ], $this->manager->getBackends());
  104. $this->manager->clear();
  105. self::assertEquals([], $this->manager->getBackends());
  106. }
  107. }