BrokerTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Talk;
  8. use OC\AppFramework\Bootstrap\Coordinator;
  9. use OC\AppFramework\Bootstrap\RegistrationContext;
  10. use OC\AppFramework\Bootstrap\ServiceRegistration;
  11. use OC\Talk\Broker;
  12. use OCP\AppFramework\QueryException;
  13. use OCP\IServerContainer;
  14. use OCP\Talk\IConversationOptions;
  15. use OCP\Talk\ITalkBackend;
  16. use Psr\Log\LoggerInterface;
  17. use RuntimeException;
  18. use Test\TestCase;
  19. class BrokerTest extends TestCase {
  20. private Coordinator $coordinator;
  21. private IServerContainer $container;
  22. private LoggerInterface $logger;
  23. private Broker $broker;
  24. protected function setUp(): void {
  25. parent::setUp();
  26. $this->coordinator = $this->createMock(Coordinator::class);
  27. $this->container = $this->createMock(IServerContainer::class);
  28. $this->logger = $this->createMock(LoggerInterface::class);
  29. $this->broker = new Broker(
  30. $this->coordinator,
  31. $this->container,
  32. $this->logger,
  33. );
  34. }
  35. public function testHasNoBackendCalledTooEarly(): void {
  36. $this->expectException(RuntimeException::class);
  37. $this->broker->hasBackend();
  38. }
  39. public function testHasNoBackend(): void {
  40. $this->coordinator->expects(self::once())
  41. ->method('getRegistrationContext')
  42. ->willReturn($this->createMock(RegistrationContext::class));
  43. self::assertFalse(
  44. $this->broker->hasBackend()
  45. );
  46. }
  47. public function testHasFaultyBackend(): void {
  48. $fakeTalkServiceClass = '\\OCA\\Spreed\\TalkBackend';
  49. $registrationContext = $this->createMock(RegistrationContext::class);
  50. $this->coordinator->expects(self::once())
  51. ->method('getRegistrationContext')
  52. ->willReturn($registrationContext);
  53. $registrationContext->expects(self::once())
  54. ->method('getTalkBackendRegistration')
  55. ->willReturn(new ServiceRegistration('spreed', $fakeTalkServiceClass));
  56. $this->container->expects(self::once())
  57. ->method('get')
  58. ->willThrowException(new QueryException());
  59. $this->logger->expects(self::once())
  60. ->method('error');
  61. self::assertFalse(
  62. $this->broker->hasBackend()
  63. );
  64. }
  65. public function testHasBackend(): void {
  66. $fakeTalkServiceClass = '\\OCA\\Spreed\\TalkBackend';
  67. $registrationContext = $this->createMock(RegistrationContext::class);
  68. $this->coordinator->expects(self::once())
  69. ->method('getRegistrationContext')
  70. ->willReturn($registrationContext);
  71. $registrationContext->expects(self::once())
  72. ->method('getTalkBackendRegistration')
  73. ->willReturn(new ServiceRegistration('spreed', $fakeTalkServiceClass));
  74. $talkService = $this->createMock(ITalkBackend::class);
  75. $this->container->expects(self::once())
  76. ->method('get')
  77. ->with($fakeTalkServiceClass)
  78. ->willReturn($talkService);
  79. self::assertTrue(
  80. $this->broker->hasBackend()
  81. );
  82. }
  83. public function testNewConversationOptions(): void {
  84. $this->broker->newConversationOptions();
  85. // Nothing to assert
  86. $this->addToAssertionCount(1);
  87. }
  88. public function testCreateConversation(): void {
  89. $fakeTalkServiceClass = '\\OCA\\Spreed\\TalkBackend';
  90. $registrationContext = $this->createMock(RegistrationContext::class);
  91. $this->coordinator->expects(self::once())
  92. ->method('getRegistrationContext')
  93. ->willReturn($registrationContext);
  94. $registrationContext->expects(self::once())
  95. ->method('getTalkBackendRegistration')
  96. ->willReturn(new ServiceRegistration('spreed', $fakeTalkServiceClass));
  97. $talkService = $this->createMock(ITalkBackend::class);
  98. $this->container->expects(self::once())
  99. ->method('get')
  100. ->with($fakeTalkServiceClass)
  101. ->willReturn($talkService);
  102. $options = $this->createMock(IConversationOptions::class);
  103. $talkService->expects(self::once())
  104. ->method('createConversation')
  105. ->with('Watercooler', [], $options);
  106. $this->broker->createConversation(
  107. 'Watercooler',
  108. [],
  109. $options
  110. );
  111. }
  112. }