ObjectStoreTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Robin Appelman <robin@icewind.nl>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace Test\Files\ObjectStore;
  22. use Test\TestCase;
  23. abstract class ObjectStoreTest extends TestCase {
  24. /** @var string[] */
  25. private $cleanup = [];
  26. /**
  27. * @return \OCP\Files\ObjectStore\IObjectStore
  28. */
  29. abstract protected function getInstance();
  30. protected function cleanupAfter(string $urn) {
  31. $this->cleanup[] = $urn;
  32. }
  33. public function tearDown(): void {
  34. parent::tearDown();
  35. $instance = $this->getInstance();
  36. foreach ($this->cleanup as $urn) {
  37. $instance->deleteObject($urn);
  38. }
  39. }
  40. protected function stringToStream($data) {
  41. $stream = fopen('php://temp', 'w+');
  42. fwrite($stream, $data);
  43. rewind($stream);
  44. return $stream;
  45. }
  46. public function testWriteRead() {
  47. $stream = $this->stringToStream('foobar');
  48. $instance = $this->getInstance();
  49. $instance->writeObject('1', $stream);
  50. $result = $instance->readObject('1');
  51. $instance->deleteObject('1');
  52. $this->assertEquals('foobar', stream_get_contents($result));
  53. }
  54. public function testDelete() {
  55. $stream = $this->stringToStream('foobar');
  56. $instance = $this->getInstance();
  57. $instance->writeObject('2', $stream);
  58. $instance->deleteObject('2');
  59. try {
  60. // to to read to verify that the object no longer exists
  61. $instance->readObject('2');
  62. $this->fail();
  63. } catch (\Exception $e) {
  64. // dummy assert to keep phpunit happy
  65. $this->assertEquals(1, 1);
  66. }
  67. }
  68. public function testReadNonExisting() {
  69. $instance = $this->getInstance();
  70. try {
  71. $instance->readObject('non-existing');
  72. $this->fail();
  73. } catch (\Exception $e) {
  74. // dummy assert to keep phpunit happy
  75. $this->assertEquals(1, 1);
  76. }
  77. }
  78. public function testDeleteNonExisting() {
  79. $instance = $this->getInstance();
  80. try {
  81. $instance->deleteObject('non-existing');
  82. $this->fail();
  83. } catch (\Exception $e) {
  84. // dummy assert to keep phpunit happy
  85. $this->assertEquals(1, 1);
  86. }
  87. }
  88. public function testExists() {
  89. $stream = $this->stringToStream('foobar');
  90. $instance = $this->getInstance();
  91. $this->assertFalse($instance->objectExists('2'));
  92. $instance->writeObject('2', $stream);
  93. $this->assertTrue($instance->objectExists('2'));
  94. $instance->deleteObject('2');
  95. $this->assertFalse($instance->objectExists('2'));
  96. }
  97. public function testCopy() {
  98. $this->cleanupAfter('source');
  99. $this->cleanupAfter('target');
  100. $stream = $this->stringToStream('foobar');
  101. $instance = $this->getInstance();
  102. $instance->writeObject('source', $stream);
  103. $this->assertFalse($instance->objectExists('target'));
  104. $instance->copyObject('source', 'target');
  105. $this->assertTrue($instance->objectExists('target'));
  106. $this->assertEquals('foobar', stream_get_contents($instance->readObject('target')));
  107. }
  108. public function testFseekSize() {
  109. $instance = $this->getInstance();
  110. $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
  111. $size = filesize($textFile);
  112. $instance->writeObject('source', fopen($textFile, 'r'));
  113. $fh = $instance->readObject('source');
  114. fseek($fh, 0, SEEK_END);
  115. $pos = ftell($fh);
  116. $this->assertEquals($size, $pos);
  117. }
  118. }