VersionManagerTest.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019 Robin Appelman <robin@icewind.nl>
  5. *
  6. * @author Robin Appelman <robin@icewind.nl>
  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. */
  24. namespace OCA\files_versions\tests\Versions;
  25. use OC\Files\Storage\Local;
  26. use OCA\Files_Versions\Versions\IVersionBackend;
  27. use OCA\Files_Versions\Versions\VersionManager;
  28. use OCP\Files\Storage\IStorage;
  29. use Test\TestCase;
  30. class VersionManagerTest extends TestCase {
  31. private function getBackend(bool $shouldUse = true): IVersionBackend {
  32. $backend = $this->createMock(IVersionBackend::class);
  33. $backend->method('useBackendForStorage')
  34. ->willReturn($shouldUse);
  35. return $backend;
  36. }
  37. private function getStorage(string $class): IStorage {
  38. return $this->getMockBuilder($class)
  39. ->disableOriginalConstructor()
  40. ->setMethodsExcept(['instanceOfStorage'])
  41. ->getMock();
  42. }
  43. public function testGetBackendSingle() {
  44. $manager = new VersionManager();
  45. $backend = $this->getBackend();
  46. $manager->registerBackend(IStorage::class, $backend);
  47. $this->assertEquals($backend, $manager->getBackendForStorage($this->getStorage(Local::class)));
  48. }
  49. public function testGetBackendMoreSpecific() {
  50. $manager = new VersionManager();
  51. $backend1 = $this->getBackend();
  52. $backend2 = $this->getBackend();
  53. $manager->registerBackend(IStorage::class, $backend1);
  54. $manager->registerBackend(Local::class, $backend2);
  55. $this->assertEquals($backend2, $manager->getBackendForStorage($this->getStorage(Local::class)));
  56. }
  57. public function testGetBackendNoUse() {
  58. $manager = new VersionManager();
  59. $backend1 = $this->getBackend();
  60. $backend2 = $this->getBackend(false);
  61. $manager->registerBackend(IStorage::class, $backend1);
  62. $manager->registerBackend(Local::class, $backend2);
  63. $this->assertEquals($backend1, $manager->getBackendForStorage($this->getStorage(Local::class)));
  64. }
  65. public function testGetBackendMultiple() {
  66. $manager = new VersionManager();
  67. $backend1 = $this->getBackend();
  68. $backend2 = $this->getBackend(false);
  69. $backend3 = $this->getBackend();
  70. $manager->registerBackend(IStorage::class, $backend1);
  71. $manager->registerBackend(Local::class, $backend2);
  72. $manager->registerBackend(Local::class, $backend3);
  73. $this->assertEquals($backend3, $manager->getBackendForStorage($this->getStorage(Local::class)));
  74. }
  75. }