1
0

ManagerTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Mail\Provider;
  8. use OC\AppFramework\Bootstrap\Coordinator;
  9. use OC\AppFramework\Bootstrap\RegistrationContext;
  10. use OC\AppFramework\Bootstrap\ServiceRegistration;
  11. use OC\Mail\Provider\Manager;
  12. use OCP\Mail\Provider\Address;
  13. use OCP\Mail\Provider\IProvider;
  14. use OCP\Mail\Provider\IService;
  15. use Psr\Container\ContainerInterface;
  16. use Psr\Log\LoggerInterface;
  17. use Test\TestCase;
  18. class ManagerTest extends TestCase {
  19. /** @var Coordinator&MockObject */
  20. private Coordinator $coordinator;
  21. /** @var ContainerInterface&MockObject */
  22. private ContainerInterface $container;
  23. /** @var LoggerInterface&MockObject */
  24. private LoggerInterface $logger;
  25. /** @var IProvider&MockObject */
  26. private IProvider $provider;
  27. /** @var IService&MockObject */
  28. private IService $service;
  29. protected function setUp(): void {
  30. parent::setUp();
  31. $this->logger = $this->createMock(LoggerInterface::class);
  32. // construct service registration
  33. $registration = $this->createMock(ServiceRegistration::class);
  34. $registration
  35. ->method('getService')
  36. ->willReturn('Mock\Provider\MailProvider');
  37. // construct registration context
  38. $context = $this->createMock(RegistrationContext::class);
  39. $context
  40. ->method('getMailProviders')
  41. ->willReturn([$registration]);
  42. // construct coordinator
  43. $this->coordinator = $this->createMock(Coordinator::class);
  44. $this->coordinator
  45. ->method('getRegistrationContext')
  46. ->willReturn($context);
  47. // construct mail service
  48. $this->service = $this->createMock(IService::class);
  49. $this->service
  50. ->method('id')
  51. ->willReturn('100');
  52. $this->service
  53. ->method('getLabel')
  54. ->willReturn('Mock Mail Service');
  55. $this->service
  56. ->method('getPrimaryAddress')
  57. ->willReturn((new Address('user1@testing.com', 'User One')));
  58. // construct mail provider
  59. $this->provider = $this->createMock(IProvider::class);
  60. $this->provider
  61. ->method('id')
  62. ->willReturn('mock-provider');
  63. $this->provider
  64. ->method('label')
  65. ->willReturn('Mock Provider');
  66. $this->provider
  67. ->method('listServices')
  68. ->willReturnMap([
  69. ['user0', []],
  70. ['user1', [$this->service->id() => $this->service]]
  71. ]);
  72. $this->provider
  73. ->method('findServiceById')
  74. ->willReturnMap([
  75. ['user0', '100', null],
  76. ['user1', '100', $this->service]
  77. ]);
  78. $this->provider
  79. ->method('findServiceByAddress')
  80. ->willReturnMap([
  81. ['user0', 'user0@testing.com', null],
  82. ['user1', 'user1@testing.com', $this->service]
  83. ]);
  84. // construct container interface
  85. $this->container = $this->createMock(ContainerInterface::class);
  86. $this->container
  87. ->method('get')
  88. ->willReturnMap([
  89. ['Mock\Provider\MailProvider', $this->provider]
  90. ]);
  91. }
  92. public function testHas(): void {
  93. // construct mail manager
  94. $manager = new Manager($this->coordinator, $this->container, $this->logger);
  95. // test result with providers found
  96. $this->assertTrue($manager->has());
  97. }
  98. public function testCount(): void {
  99. // construct mail manager
  100. $manager = new Manager($this->coordinator, $this->container, $this->logger);
  101. // test result with providers found
  102. $this->assertGreaterThan(0, $manager->count());
  103. }
  104. public function testTypes(): void {
  105. // construct mail manager
  106. $manager = new Manager($this->coordinator, $this->container, $this->logger);
  107. // test result with providers found
  108. $this->assertEquals(['mock-provider' => 'Mock Provider'], $manager->types());
  109. }
  110. public function testProviders(): void {
  111. // construct mail manager
  112. $manager = new Manager($this->coordinator, $this->container, $this->logger);
  113. // test result with providers found
  114. $this->assertEquals([$this->provider->id() => $this->provider], $manager->providers());
  115. }
  116. public function testFindProviderById(): void {
  117. // construct mail manager
  118. $manager = new Manager($this->coordinator, $this->container, $this->logger);
  119. // test result with providers found
  120. $this->assertEquals($this->provider, $manager->findProviderById($this->provider->id()));
  121. }
  122. public function testServices(): void {
  123. // construct mail manager
  124. $manager = new Manager($this->coordinator, $this->container, $this->logger);
  125. // test result with no services found
  126. $this->assertEquals([], $manager->services('user0'));
  127. // test result with services found
  128. $this->assertEquals([$this->provider->id() => [$this->service->id() => $this->service]], $manager->services('user1'));
  129. }
  130. public function testFindServiceById(): void {
  131. // construct mail manager
  132. $manager = new Manager($this->coordinator, $this->container, $this->logger);
  133. // test result with no services found and not provider specified
  134. $this->assertEquals(null, $manager->findServiceById('user0', '100'));
  135. // test result with no services found and provider specified
  136. $this->assertEquals(null, $manager->findServiceById('user0', '100', $this->provider->id()));
  137. // test result with services found and not provider specified
  138. $this->assertEquals($this->service, $manager->findServiceById('user1', '100'));
  139. // test result with services found and provider specified
  140. $this->assertEquals($this->service, $manager->findServiceById('user1', '100', $this->provider->id()));
  141. }
  142. public function testFindServiceByAddress(): void {
  143. // construct mail manager
  144. $manager = new Manager($this->coordinator, $this->container, $this->logger);
  145. // test result with no services found and not provider specified
  146. $this->assertEquals(null, $manager->findServiceByAddress('user0', 'user0@testing.com'));
  147. // test result with no services found and provider specified
  148. $this->assertEquals(null, $manager->findServiceByAddress('user0', 'user0@testing.com', $this->provider->id()));
  149. // test result with services found and not provider specified
  150. $this->assertEquals($this->service, $manager->findServiceByAddress('user1', 'user1@testing.com'));
  151. // test result with services found and provider specified
  152. $this->assertEquals($this->service, $manager->findServiceByAddress('user1', 'user1@testing.com', $this->provider->id()));
  153. }
  154. }