BrokerTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * @copyright 2022 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author 2022 Christoph Wurst <christoph@winzerhof-wurst.at>
  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. namespace Test\Talk;
  24. use OC\AppFramework\Bootstrap\Coordinator;
  25. use OC\AppFramework\Bootstrap\RegistrationContext;
  26. use OC\AppFramework\Bootstrap\ServiceRegistration;
  27. use OC\Talk\Broker;
  28. use OCP\AppFramework\QueryException;
  29. use OCP\IServerContainer;
  30. use OCP\Talk\IConversationOptions;
  31. use OCP\Talk\ITalkBackend;
  32. use Psr\Log\LoggerInterface;
  33. use RuntimeException;
  34. use Test\TestCase;
  35. class BrokerTest extends TestCase {
  36. private Coordinator $coordinator;
  37. private IServerContainer $container;
  38. private LoggerInterface $logger;
  39. private Broker $broker;
  40. protected function setUp(): void {
  41. parent::setUp();
  42. $this->coordinator = $this->createMock(Coordinator::class);
  43. $this->container = $this->createMock(IServerContainer::class);
  44. $this->logger = $this->createMock(LoggerInterface::class);
  45. $this->broker = new Broker(
  46. $this->coordinator,
  47. $this->container,
  48. $this->logger,
  49. );
  50. }
  51. public function testHasNoBackendCalledTooEarly(): void {
  52. $this->expectException(RuntimeException::class);
  53. $this->broker->hasBackend();
  54. }
  55. public function testHasNoBackend(): void {
  56. $this->coordinator->expects(self::once())
  57. ->method('getRegistrationContext')
  58. ->willReturn($this->createMock(RegistrationContext::class));
  59. self::assertFalse(
  60. $this->broker->hasBackend()
  61. );
  62. }
  63. public function testHasFaultyBackend(): void {
  64. $fakeTalkServiceClass = '\\OCA\\Spreed\\TalkBackend';
  65. $registrationContext = $this->createMock(RegistrationContext::class);
  66. $this->coordinator->expects(self::once())
  67. ->method('getRegistrationContext')
  68. ->willReturn($registrationContext);
  69. $registrationContext->expects(self::once())
  70. ->method('getTalkBackendRegistration')
  71. ->willReturn(new ServiceRegistration('spreed', $fakeTalkServiceClass));
  72. $this->container->expects(self::once())
  73. ->method('get')
  74. ->willThrowException(new QueryException());
  75. $this->logger->expects(self::once())
  76. ->method('error');
  77. self::assertFalse(
  78. $this->broker->hasBackend()
  79. );
  80. }
  81. public function testHasBackend(): void {
  82. $fakeTalkServiceClass = '\\OCA\\Spreed\\TalkBackend';
  83. $registrationContext = $this->createMock(RegistrationContext::class);
  84. $this->coordinator->expects(self::once())
  85. ->method('getRegistrationContext')
  86. ->willReturn($registrationContext);
  87. $registrationContext->expects(self::once())
  88. ->method('getTalkBackendRegistration')
  89. ->willReturn(new ServiceRegistration('spreed', $fakeTalkServiceClass));
  90. $talkService = $this->createMock(ITalkBackend::class);
  91. $this->container->expects(self::once())
  92. ->method('get')
  93. ->with($fakeTalkServiceClass)
  94. ->willReturn($talkService);
  95. self::assertTrue(
  96. $this->broker->hasBackend()
  97. );
  98. }
  99. public function testNewConversationOptions(): void {
  100. $this->broker->newConversationOptions();
  101. // Nothing to assert
  102. $this->addToAssertionCount(1);
  103. }
  104. public function testCreateConversation(): void {
  105. $fakeTalkServiceClass = '\\OCA\\Spreed\\TalkBackend';
  106. $registrationContext = $this->createMock(RegistrationContext::class);
  107. $this->coordinator->expects(self::once())
  108. ->method('getRegistrationContext')
  109. ->willReturn($registrationContext);
  110. $registrationContext->expects(self::once())
  111. ->method('getTalkBackendRegistration')
  112. ->willReturn(new ServiceRegistration('spreed', $fakeTalkServiceClass));
  113. $talkService = $this->createMock(ITalkBackend::class);
  114. $this->container->expects(self::once())
  115. ->method('get')
  116. ->with($fakeTalkServiceClass)
  117. ->willReturn($talkService);
  118. $options = $this->createMock(IConversationOptions::class);
  119. $talkService->expects(self::once())
  120. ->method('createConversation')
  121. ->with('Watercooler', [], $options);
  122. $this->broker->createConversation(
  123. 'Watercooler',
  124. [],
  125. $options
  126. );
  127. }
  128. }