ProviderManagerTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019 Daniel Kesselberg <mail@danielkesselberg.de>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace Test\Collaboration\Resources;
  23. use OC\Collaboration\Resources\ProviderManager;
  24. use OCA\Files\Collaboration\Resources\ResourceProvider;
  25. use OCP\AppFramework\QueryException;
  26. use OCP\Collaboration\Resources\IProviderManager;
  27. use OCP\IServerContainer;
  28. use Psr\Log\LoggerInterface;
  29. use Test\TestCase;
  30. class ProviderManagerTest extends TestCase {
  31. /** @var IServerContainer */
  32. protected $serverContainer;
  33. /** @var LoggerInterface */
  34. protected $logger;
  35. /** @var IProviderManager */
  36. protected $providerManager;
  37. protected function setUp(): void {
  38. parent::setUp();
  39. $this->serverContainer = $this->createMock(IServerContainer::class);
  40. $this->logger = $this->createMock(LoggerInterface::class);
  41. $this->providerManager = new class($this->serverContainer, $this->logger) extends ProviderManager {
  42. public function countProviders(): int {
  43. return count($this->providers);
  44. }
  45. };
  46. }
  47. public function testRegisterResourceProvider(): void {
  48. $this->providerManager->registerResourceProvider('AwesomeResourceProvider');
  49. $this->assertSame(1, $this->providerManager->countProviders());
  50. }
  51. public function testGetResourceProvidersNoProvider(): void {
  52. $this->assertCount(0, $this->providerManager->getResourceProviders());
  53. }
  54. public function testGetResourceProvidersValidProvider(): void {
  55. $this->serverContainer->expects($this->once())
  56. ->method('query')
  57. ->with($this->equalTo(ResourceProvider::class))
  58. ->willReturn($this->createMock(ResourceProvider::class));
  59. $this->providerManager->registerResourceProvider(ResourceProvider::class);
  60. $resourceProviders = $this->providerManager->getResourceProviders();
  61. $this->assertCount(1, $resourceProviders);
  62. $this->assertInstanceOf(ResourceProvider::class, $resourceProviders[0]);
  63. }
  64. public function testGetResourceProvidersInvalidProvider(): void {
  65. $this->serverContainer->expects($this->once())
  66. ->method('query')
  67. ->with($this->equalTo('InvalidResourceProvider'))
  68. ->willThrowException(new QueryException('A meaningful error message'));
  69. $this->logger->expects($this->once())
  70. ->method('error');
  71. $this->providerManager->registerResourceProvider('InvalidResourceProvider');
  72. $resourceProviders = $this->providerManager->getResourceProviders();
  73. $this->assertCount(0, $resourceProviders);
  74. }
  75. public function testGetResourceProvidersValidAndInvalidProvider(): void {
  76. $this->serverContainer->expects($this->exactly(2))
  77. ->method('query')
  78. ->withConsecutive(
  79. [$this->equalTo('InvalidResourceProvider')],
  80. [$this->equalTo(ResourceProvider::class)],
  81. )->willReturnOnConsecutiveCalls(
  82. $this->throwException(new QueryException('A meaningful error message')),
  83. $this->createMock(ResourceProvider::class),
  84. );
  85. $this->logger->expects($this->once())
  86. ->method('error');
  87. $this->providerManager->registerResourceProvider('InvalidResourceProvider');
  88. $this->providerManager->registerResourceProvider(ResourceProvider::class);
  89. $resourceProviders = $this->providerManager->getResourceProviders();
  90. $this->assertCount(1, $resourceProviders);
  91. }
  92. }