MountPointTest.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Files\Mount;
  8. use OC\Files\Storage\StorageFactory;
  9. use OCP\Files\Storage;
  10. class DummyStorage {
  11. }
  12. class MountPointTest extends \Test\TestCase {
  13. public function testGetStorage(): void {
  14. $storage = $this->createMock(Storage::class);
  15. $storage->expects($this->once())
  16. ->method('getId')
  17. ->willReturn(123);
  18. $loader = $this->createMock(StorageFactory::class);
  19. $loader->expects($this->once())
  20. ->method('wrap')
  21. ->willReturn($storage);
  22. $mountPoint = new \OC\Files\Mount\MountPoint(
  23. // just use this because a real class is needed
  24. '\Test\Files\Mount\DummyStorage',
  25. '/mountpoint',
  26. null,
  27. $loader
  28. );
  29. $this->assertEquals($storage, $mountPoint->getStorage());
  30. $this->assertEquals(123, $mountPoint->getStorageId());
  31. $this->assertEquals('/mountpoint/', $mountPoint->getMountPoint());
  32. $mountPoint->setMountPoint('another');
  33. $this->assertEquals('/another/', $mountPoint->getMountPoint());
  34. }
  35. public function testInvalidStorage(): void {
  36. $loader = $this->createMock(StorageFactory::class);
  37. $loader->expects($this->once())
  38. ->method('wrap')
  39. ->will($this->throwException(new \Exception('Test storage init exception')));
  40. $called = false;
  41. $wrapper = function ($mountPoint, $storage) use ($called) {
  42. $called = true;
  43. };
  44. $mountPoint = new \OC\Files\Mount\MountPoint(
  45. // just use this because a real class is needed
  46. '\Test\Files\Mount\DummyStorage',
  47. '/mountpoint',
  48. null,
  49. $loader
  50. );
  51. $this->assertNull($mountPoint->getStorage());
  52. // call it again to make sure the init code only ran once
  53. $this->assertNull($mountPoint->getStorage());
  54. $this->assertNull($mountPoint->getStorageId());
  55. // wrapping doesn't fail
  56. $mountPoint->wrapStorage($wrapper);
  57. $this->assertNull($mountPoint->getStorage());
  58. // storage wrapper never called
  59. $this->assertFalse($called);
  60. }
  61. }