ObjectStoreTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Test\Files\ObjectStore;
  7. use Test\TestCase;
  8. abstract class ObjectStoreTest extends TestCase {
  9. /** @var string[] */
  10. private $cleanup = [];
  11. /**
  12. * @return \OCP\Files\ObjectStore\IObjectStore
  13. */
  14. abstract protected function getInstance();
  15. protected function cleanupAfter(string $urn) {
  16. $this->cleanup[] = $urn;
  17. }
  18. public function tearDown(): void {
  19. parent::tearDown();
  20. $instance = $this->getInstance();
  21. foreach ($this->cleanup as $urn) {
  22. $instance->deleteObject($urn);
  23. }
  24. }
  25. protected function stringToStream($data) {
  26. $stream = fopen('php://temp', 'w+');
  27. fwrite($stream, $data);
  28. rewind($stream);
  29. return $stream;
  30. }
  31. public function testWriteRead(): void {
  32. $stream = $this->stringToStream('foobar');
  33. $instance = $this->getInstance();
  34. $instance->writeObject('1', $stream);
  35. $result = $instance->readObject('1');
  36. $instance->deleteObject('1');
  37. $this->assertEquals('foobar', stream_get_contents($result));
  38. }
  39. public function testDelete(): void {
  40. $stream = $this->stringToStream('foobar');
  41. $instance = $this->getInstance();
  42. $instance->writeObject('2', $stream);
  43. $instance->deleteObject('2');
  44. try {
  45. // to to read to verify that the object no longer exists
  46. $instance->readObject('2');
  47. $this->fail();
  48. } catch (\Exception $e) {
  49. // dummy assert to keep phpunit happy
  50. $this->assertEquals(1, 1);
  51. }
  52. }
  53. public function testReadNonExisting(): void {
  54. $instance = $this->getInstance();
  55. try {
  56. $instance->readObject('non-existing');
  57. $this->fail();
  58. } catch (\Exception $e) {
  59. // dummy assert to keep phpunit happy
  60. $this->assertEquals(1, 1);
  61. }
  62. }
  63. public function testDeleteNonExisting(): void {
  64. $instance = $this->getInstance();
  65. try {
  66. $instance->deleteObject('non-existing');
  67. $this->fail();
  68. } catch (\Exception $e) {
  69. // dummy assert to keep phpunit happy
  70. $this->assertEquals(1, 1);
  71. }
  72. }
  73. public function testExists(): void {
  74. $stream = $this->stringToStream('foobar');
  75. $instance = $this->getInstance();
  76. $this->assertFalse($instance->objectExists('2'));
  77. $instance->writeObject('2', $stream);
  78. $this->assertTrue($instance->objectExists('2'));
  79. $instance->deleteObject('2');
  80. $this->assertFalse($instance->objectExists('2'));
  81. }
  82. public function testCopy(): void {
  83. $this->cleanupAfter('source');
  84. $this->cleanupAfter('target');
  85. $stream = $this->stringToStream('foobar');
  86. $instance = $this->getInstance();
  87. $instance->writeObject('source', $stream);
  88. $this->assertFalse($instance->objectExists('target'));
  89. $instance->copyObject('source', 'target');
  90. $this->assertTrue($instance->objectExists('target'));
  91. $this->assertEquals('foobar', stream_get_contents($instance->readObject('target')));
  92. }
  93. public function testFseekSize(): void {
  94. $instance = $this->getInstance();
  95. $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
  96. $size = filesize($textFile);
  97. $instance->writeObject('source', fopen($textFile, 'r'));
  98. $fh = $instance->readObject('source');
  99. fseek($fh, 0, SEEK_END);
  100. $pos = ftell($fh);
  101. $this->assertEquals($size, $pos);
  102. }
  103. }