storagefactory.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * Copyright (c) 2015 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test\Files\Storage;
  9. use OC\Files\Mount\MountPoint;
  10. use OCP\Files\Mount\IMountPoint;
  11. use OCP\Files\Storage as IStorage;
  12. use Test\TestCase;
  13. use OC\Files\Storage\Wrapper\Wrapper;
  14. class DummyWrapper extends Wrapper {
  15. public $data;
  16. public function __construct($arguments) {
  17. parent::__construct($arguments);
  18. if (isset($arguments['data'])) {
  19. $this->data = $arguments['data'];
  20. }
  21. }
  22. }
  23. class StorageFactory extends TestCase {
  24. public function testSimpleWrapper() {
  25. $instance = new \OC\Files\Storage\StorageFactory();
  26. $mount = new MountPoint('\OC\Files\Storage\Temporary', '/foo', [[]], $instance);
  27. $instance->addStorageWrapper('dummy', function ($mountPoint, IStorage $storage, IMountPoint $mount) {
  28. $this->assertInstanceOf('\OC\Files\Storage\Temporary', $storage);
  29. $this->assertEquals('/foo/', $mount->getMountPoint());
  30. $this->assertEquals('/foo/', $mountPoint);
  31. return new DummyWrapper(['storage' => $storage]);
  32. });
  33. $wrapped = $mount->getStorage();
  34. $this->assertInstanceOf('\Test\Files\Storage\DummyWrapper', $wrapped);
  35. }
  36. public function testRemoveWrapper() {
  37. $instance = new \OC\Files\Storage\StorageFactory();
  38. $mount = new MountPoint('\OC\Files\Storage\Temporary', '/foo', [[]], $instance);
  39. $instance->addStorageWrapper('dummy', function ($mountPoint, IStorage $storage) {
  40. return new DummyWrapper(['storage' => $storage]);
  41. });
  42. $instance->removeStorageWrapper('dummy');
  43. $wrapped = $mount->getStorage();
  44. $this->assertInstanceOf('\OC\Files\Storage\Temporary', $wrapped);
  45. }
  46. public function testWrapperPriority() {
  47. $instance = new \OC\Files\Storage\StorageFactory();
  48. $mount = new MountPoint('\OC\Files\Storage\Temporary', '/foo', [[]], $instance);
  49. $instance->addStorageWrapper('dummy1', function ($mountPoint, IStorage $storage) {
  50. return new DummyWrapper(['storage' => $storage, 'data' => 1]);
  51. }, 1);
  52. $instance->addStorageWrapper('dummy2', function ($mountPoint, IStorage $storage) {
  53. return new DummyWrapper(['storage' => $storage, 'data' => 100]);
  54. }, 100);
  55. $instance->addStorageWrapper('dummy3', function ($mountPoint, IStorage $storage) {
  56. return new DummyWrapper(['storage' => $storage, 'data' => 50]);
  57. }, 50);
  58. /** @var \Test\Files\Storage\DummyWrapper $wrapped */
  59. $wrapped = $mount->getStorage();
  60. $this->assertInstanceOf('\Test\Files\Storage\DummyWrapper', $wrapped);
  61. $this->assertEquals(1, $wrapped->data);// lowest priority is applied last, called first
  62. $this->assertEquals(50, $wrapped->getWrapperStorage()->data);
  63. $this->assertEquals(100, $wrapped->getWrapperStorage()->getWrapperStorage()->data);
  64. }
  65. }