1
0

ManagerTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2018, Georg Ehrke <oc.list@georgehrke.com>
  5. *
  6. * @author Georg Ehrke <oc.list@georgehrke.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 Test\Calendar\Resource;
  25. use OC\AppFramework\Bootstrap\Coordinator;
  26. use OC\AppFramework\Bootstrap\RegistrationContext;
  27. use OC\AppFramework\Bootstrap\ServiceRegistration;
  28. use OC\Calendar\Resource\Manager;
  29. use OCP\Calendar\Resource\IBackend;
  30. use OCP\IServerContainer;
  31. use PHPUnit\Framework\MockObject\MockObject;
  32. use Test\TestCase;
  33. class ManagerTest extends TestCase {
  34. /** @var Coordinator|MockObject */
  35. private $coordinator;
  36. /** @var IServerContainer|MockObject */
  37. private $server;
  38. /** @var Manager */
  39. private $manager;
  40. protected function setUp(): void {
  41. parent::setUp();
  42. $this->coordinator = $this->createMock(Coordinator::class);
  43. $this->server = $this->createMock(IServerContainer::class);
  44. $this->manager = new Manager(
  45. $this->coordinator,
  46. $this->server,
  47. );
  48. }
  49. public function testRegisterUnregisterBackend(): void {
  50. $backend1 = $this->createMock(IBackend::class);
  51. $backend1->method('getBackendIdentifier')->willReturn('backend_1');
  52. $backend2 = $this->createMock(IBackend::class);
  53. $backend2->method('getBackendIdentifier')->willReturn('backend_2');
  54. $this->server->expects(self::exactly(2))
  55. ->method('query')
  56. ->willReturnMap([
  57. ['calendar_resource_backend1', true, $backend1,],
  58. ['calendar_resource_backend2', true, $backend2,],
  59. ]);
  60. $this->manager->registerBackend('calendar_resource_backend1');
  61. $this->manager->registerBackend('calendar_resource_backend2');
  62. self::assertEquals([
  63. $backend1, $backend2
  64. ], $this->manager->getBackends());
  65. $this->manager->unregisterBackend('calendar_resource_backend1');
  66. self::assertEquals([
  67. $backend2
  68. ], $this->manager->getBackends());
  69. }
  70. public function testGetBackendFromBootstrapRegistration(): void {
  71. $backendClass = '\\OCA\\CalendarResourceFoo\\Backend';
  72. $backend = $this->createMock(IBackend::class);
  73. $backend->method('getBackendIdentifier')->willReturn('from_bootstrap');
  74. $context = $this->createMock(RegistrationContext::class);
  75. $this->coordinator->expects(self::once())
  76. ->method('getRegistrationContext')
  77. ->willReturn($context);
  78. $context->expects(self::once())
  79. ->method('getCalendarResourceBackendRegistrations')
  80. ->willReturn([
  81. new ServiceRegistration('calendar_resource_foo', $backendClass)
  82. ]);
  83. $this->server->expects(self::once())
  84. ->method('query')
  85. ->with($backendClass)
  86. ->willReturn($backend);
  87. self::assertEquals($backend, $this->manager->getBackend('from_bootstrap'));
  88. }
  89. public function testGetBackend(): void {
  90. $backend1 = $this->createMock(IBackend::class);
  91. $backend1->method('getBackendIdentifier')->willReturn('backend_1');
  92. $backend2 = $this->createMock(IBackend::class);
  93. $backend2->method('getBackendIdentifier')->willReturn('backend_2');
  94. $this->server->expects(self::exactly(2))
  95. ->method('query')
  96. ->willReturnMap([
  97. ['calendar_resource_backend1', true, $backend1,],
  98. ['calendar_resource_backend2', true, $backend2,],
  99. ]);
  100. $this->manager->registerBackend('calendar_resource_backend1');
  101. $this->manager->registerBackend('calendar_resource_backend2');
  102. self::assertEquals($backend1, $this->manager->getBackend('backend_1'));
  103. self::assertEquals($backend2, $this->manager->getBackend('backend_2'));
  104. }
  105. public function testClear(): void {
  106. $backend1 = $this->createMock(IBackend::class);
  107. $backend1->method('getBackendIdentifier')->willReturn('backend_1');
  108. $backend2 = $this->createMock(IBackend::class);
  109. $backend2->method('getBackendIdentifier')->willReturn('backend_2');
  110. $this->server->expects(self::exactly(2))
  111. ->method('query')
  112. ->willReturnMap([
  113. ['calendar_resource_backend1', true, $backend1,],
  114. ['calendar_resource_backend2', true, $backend2,],
  115. ]);
  116. $this->manager->registerBackend('calendar_resource_backend1');
  117. $this->manager->registerBackend('calendar_resource_backend2');
  118. self::assertEquals([
  119. $backend1, $backend2
  120. ], $this->manager->getBackends());
  121. $this->manager->clear();
  122. self::assertEquals([], $this->manager->getBackends());
  123. }
  124. }