ProviderManagerTest.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Collaboration\Resources;
  8. use OC\Collaboration\Resources\ProviderManager;
  9. use OCA\Files\Collaboration\Resources\ResourceProvider;
  10. use OCP\AppFramework\QueryException;
  11. use OCP\Collaboration\Resources\IProviderManager;
  12. use OCP\IServerContainer;
  13. use Psr\Log\LoggerInterface;
  14. use Test\TestCase;
  15. class ProviderManagerTest extends TestCase {
  16. /** @var IServerContainer */
  17. protected $serverContainer;
  18. /** @var LoggerInterface */
  19. protected $logger;
  20. /** @var IProviderManager */
  21. protected $providerManager;
  22. protected function setUp(): void {
  23. parent::setUp();
  24. $this->serverContainer = $this->createMock(IServerContainer::class);
  25. $this->logger = $this->createMock(LoggerInterface::class);
  26. $this->providerManager = new class($this->serverContainer, $this->logger) extends ProviderManager {
  27. public function countProviders(): int {
  28. return count($this->providers);
  29. }
  30. };
  31. }
  32. public function testRegisterResourceProvider(): void {
  33. $this->providerManager->registerResourceProvider('AwesomeResourceProvider');
  34. $this->assertSame(1, $this->providerManager->countProviders());
  35. }
  36. public function testGetResourceProvidersNoProvider(): void {
  37. $this->assertCount(0, $this->providerManager->getResourceProviders());
  38. }
  39. public function testGetResourceProvidersValidProvider(): void {
  40. $this->serverContainer->expects($this->once())
  41. ->method('query')
  42. ->with($this->equalTo(ResourceProvider::class))
  43. ->willReturn($this->createMock(ResourceProvider::class));
  44. $this->providerManager->registerResourceProvider(ResourceProvider::class);
  45. $resourceProviders = $this->providerManager->getResourceProviders();
  46. $this->assertCount(1, $resourceProviders);
  47. $this->assertInstanceOf(ResourceProvider::class, $resourceProviders[0]);
  48. }
  49. public function testGetResourceProvidersInvalidProvider(): void {
  50. $this->serverContainer->expects($this->once())
  51. ->method('query')
  52. ->with($this->equalTo('InvalidResourceProvider'))
  53. ->willThrowException(new QueryException('A meaningful error message'));
  54. $this->logger->expects($this->once())
  55. ->method('error');
  56. $this->providerManager->registerResourceProvider('InvalidResourceProvider');
  57. $resourceProviders = $this->providerManager->getResourceProviders();
  58. $this->assertCount(0, $resourceProviders);
  59. }
  60. public function testGetResourceProvidersValidAndInvalidProvider(): void {
  61. $this->serverContainer->expects($this->exactly(2))
  62. ->method('query')
  63. ->withConsecutive(
  64. [$this->equalTo('InvalidResourceProvider')],
  65. [$this->equalTo(ResourceProvider::class)],
  66. )->willReturnOnConsecutiveCalls(
  67. $this->throwException(new QueryException('A meaningful error message')),
  68. $this->createMock(ResourceProvider::class),
  69. );
  70. $this->logger->expects($this->once())
  71. ->method('error');
  72. $this->providerManager->registerResourceProvider('InvalidResourceProvider');
  73. $this->providerManager->registerResourceProvider(ResourceProvider::class);
  74. $resourceProviders = $this->providerManager->getResourceProviders();
  75. $this->assertCount(1, $resourceProviders);
  76. }
  77. }