VersionManagerTest.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 OCA\files_versions\tests\Versions;
  8. use OC\Files\Storage\Local;
  9. use OCA\Files_Versions\Versions\IVersionBackend;
  10. use OCA\Files_Versions\Versions\VersionManager;
  11. use OCP\Files\Storage\IStorage;
  12. use Test\TestCase;
  13. class VersionManagerTest extends TestCase {
  14. private function getBackend(bool $shouldUse = true): IVersionBackend {
  15. $backend = $this->createMock(IVersionBackend::class);
  16. $backend->method('useBackendForStorage')
  17. ->willReturn($shouldUse);
  18. return $backend;
  19. }
  20. private function getStorage(string $class): IStorage {
  21. return $this->getMockBuilder($class)
  22. ->disableOriginalConstructor()
  23. ->setMethodsExcept(['instanceOfStorage'])
  24. ->getMock();
  25. }
  26. public function testGetBackendSingle() {
  27. $manager = new VersionManager();
  28. $backend = $this->getBackend();
  29. $manager->registerBackend(IStorage::class, $backend);
  30. $this->assertEquals($backend, $manager->getBackendForStorage($this->getStorage(Local::class)));
  31. }
  32. public function testGetBackendMoreSpecific() {
  33. $manager = new VersionManager();
  34. $backend1 = $this->getBackend();
  35. $backend2 = $this->getBackend();
  36. $manager->registerBackend(IStorage::class, $backend1);
  37. $manager->registerBackend(Local::class, $backend2);
  38. $this->assertEquals($backend2, $manager->getBackendForStorage($this->getStorage(Local::class)));
  39. }
  40. public function testGetBackendNoUse() {
  41. $manager = new VersionManager();
  42. $backend1 = $this->getBackend();
  43. $backend2 = $this->getBackend(false);
  44. $manager->registerBackend(IStorage::class, $backend1);
  45. $manager->registerBackend(Local::class, $backend2);
  46. $this->assertEquals($backend1, $manager->getBackendForStorage($this->getStorage(Local::class)));
  47. }
  48. public function testGetBackendMultiple() {
  49. $manager = new VersionManager();
  50. $backend1 = $this->getBackend();
  51. $backend2 = $this->getBackend(false);
  52. $backend3 = $this->getBackend();
  53. $manager->registerBackend(IStorage::class, $backend1);
  54. $manager->registerBackend(Local::class, $backend2);
  55. $manager->registerBackend(Local::class, $backend3);
  56. $this->assertEquals($backend3, $manager->getBackendForStorage($this->getStorage(Local::class)));
  57. }
  58. }