1
0

InMemoryFileTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018, Michael Weimann <mail@michael-weimann.eu>
  5. *
  6. * @author Michael Weimann <mail@michael-weimann.eu>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. */
  22. namespace Test\File\SimpleFS;
  23. use OCP\Files\NotPermittedException;
  24. use OCP\Files\SimpleFS\InMemoryFile;
  25. use Test\TestCase;
  26. /**
  27. * This class provide test casesf or the InMemoryFile.
  28. *
  29. * @package Test\File\SimpleFS
  30. */
  31. class InMemoryFileTest extends TestCase {
  32. /**
  33. * Holds a pdf file with know attributes for tests.
  34. *
  35. * @var InMemoryFile
  36. */
  37. private $testPdf;
  38. /**
  39. * Sets the test file from "./resources/test.pdf".
  40. *
  41. * @before
  42. * @return void
  43. */
  44. public function setupTestPdf() {
  45. $fileContents = file_get_contents(
  46. __DIR__ . '/../../../data/test.pdf'
  47. );
  48. $this->testPdf = new InMemoryFile('test.pdf', $fileContents);
  49. }
  50. /**
  51. * Asserts that putContent replaces the file contents.
  52. *
  53. * @return void
  54. */
  55. public function testPutContent() {
  56. $this->testPdf->putContent('test');
  57. self::assertEquals('test', $this->testPdf->getContent());
  58. }
  59. /**
  60. * Asserts that delete() doesn't rise an exception.
  61. *
  62. * @return void
  63. */
  64. public function testDelete() {
  65. $this->testPdf->delete();
  66. // assert true, otherwise phpunit complains about not doing any assert
  67. self::assertTrue(true);
  68. }
  69. /**
  70. * Asserts that getName returns the name passed on file creation.
  71. *
  72. * @return void
  73. */
  74. public function testGetName() {
  75. self::assertEquals('test.pdf', $this->testPdf->getName());
  76. }
  77. /**
  78. * Asserts that the file size is the size of the test file.
  79. *
  80. * @return void
  81. */
  82. public function testGetSize() {
  83. self::assertEquals(7083, $this->testPdf->getSize());
  84. }
  85. /**
  86. * Asserts the file contents are the same than the original file contents.
  87. *
  88. * @return void
  89. */
  90. public function testGetContent() {
  91. self::assertEquals(
  92. file_get_contents(__DIR__ . '/../../../data/test.pdf'),
  93. $this->testPdf->getContent()
  94. );
  95. }
  96. /**
  97. * Asserts the test file modification time is an integer.
  98. *
  99. * @return void
  100. */
  101. public function testGetMTime() {
  102. self::assertTrue(is_int($this->testPdf->getMTime()));
  103. }
  104. /**
  105. * Asserts the test file mime type is "application/json".
  106. *
  107. * @return void
  108. */
  109. public function testGetMimeType() {
  110. self::assertEquals('application/pdf', $this->testPdf->getMimeType());
  111. }
  112. /**
  113. * Asserts that read() raises an NotPermittedException.
  114. *
  115. * @return void
  116. */
  117. public function testRead() {
  118. self::expectException(NotPermittedException::class);
  119. $this->testPdf->read();
  120. }
  121. /**
  122. * Asserts that write() raises an NotPermittedException.
  123. *
  124. * @return void
  125. */
  126. public function testWrite() {
  127. self::expectException(NotPermittedException::class);
  128. $this->testPdf->write();
  129. }
  130. }