StorageFactoryTest.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2019-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\Storage;
  8. use OC\Files\Mount\MountPoint;
  9. use OC\Files\Storage\Wrapper\Wrapper;
  10. use OCP\Files\Mount\IMountPoint;
  11. use OCP\Files\Storage as IStorage;
  12. use Test\TestCase;
  13. class DummyWrapper extends Wrapper {
  14. public $data;
  15. public function __construct($arguments) {
  16. parent::__construct($arguments);
  17. if (isset($arguments['data'])) {
  18. $this->data = $arguments['data'];
  19. }
  20. }
  21. }
  22. class StorageFactoryTest extends TestCase {
  23. public function testSimpleWrapper() {
  24. $instance = new \OC\Files\Storage\StorageFactory();
  25. $mount = new MountPoint('\OC\Files\Storage\Temporary', '/foo', [[]], $instance);
  26. $instance->addStorageWrapper('dummy', function ($mountPoint, IStorage $storage, IMountPoint $mount) {
  27. $this->assertInstanceOf('\OC\Files\Storage\Temporary', $storage);
  28. $this->assertEquals('/foo/', $mount->getMountPoint());
  29. $this->assertEquals('/foo/', $mountPoint);
  30. return new DummyWrapper(['storage' => $storage]);
  31. });
  32. $wrapped = $mount->getStorage();
  33. $this->assertInstanceOf('\Test\Files\Storage\DummyWrapper', $wrapped);
  34. }
  35. public function testRemoveWrapper() {
  36. $instance = new \OC\Files\Storage\StorageFactory();
  37. $mount = new MountPoint('\OC\Files\Storage\Temporary', '/foo', [[]], $instance);
  38. $instance->addStorageWrapper('dummy', function ($mountPoint, IStorage $storage) {
  39. return new DummyWrapper(['storage' => $storage]);
  40. });
  41. $instance->removeStorageWrapper('dummy');
  42. $wrapped = $mount->getStorage();
  43. $this->assertInstanceOf('\OC\Files\Storage\Temporary', $wrapped);
  44. }
  45. public function testWrapperPriority() {
  46. $instance = new \OC\Files\Storage\StorageFactory();
  47. $mount = new MountPoint('\OC\Files\Storage\Temporary', '/foo', [[]], $instance);
  48. $instance->addStorageWrapper('dummy1', function ($mountPoint, IStorage $storage) {
  49. return new DummyWrapper(['storage' => $storage, 'data' => 1]);
  50. }, 1);
  51. $instance->addStorageWrapper('dummy2', function ($mountPoint, IStorage $storage) {
  52. return new DummyWrapper(['storage' => $storage, 'data' => 100]);
  53. }, 100);
  54. $instance->addStorageWrapper('dummy3', function ($mountPoint, IStorage $storage) {
  55. return new DummyWrapper(['storage' => $storage, 'data' => 50]);
  56. }, 50);
  57. /** @var \Test\Files\Storage\DummyWrapper $wrapped */
  58. $wrapped = $mount->getStorage();
  59. $this->assertInstanceOf('\Test\Files\Storage\DummyWrapper', $wrapped);
  60. $this->assertEquals(1, $wrapped->data);// lowest priority is applied last, called first
  61. $this->assertEquals(50, $wrapped->getWrapperStorage()->data);
  62. $this->assertEquals(100, $wrapped->getWrapperStorage()->getWrapperStorage()->data);
  63. }
  64. }