InMemoryFileTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace Test\File\SimpleFS;
  8. use OCP\Files\NotPermittedException;
  9. use OCP\Files\SimpleFS\InMemoryFile;
  10. use Test\TestCase;
  11. /**
  12. * This class provide test casesf or the InMemoryFile.
  13. *
  14. * @package Test\File\SimpleFS
  15. */
  16. class InMemoryFileTest extends TestCase {
  17. /**
  18. * Holds a pdf file with know attributes for tests.
  19. *
  20. * @var InMemoryFile
  21. */
  22. private $testPdf;
  23. /**
  24. * Sets the test file from "./resources/test.pdf".
  25. *
  26. * @before
  27. * @return void
  28. */
  29. public function setupTestPdf() {
  30. $fileContents = file_get_contents(
  31. __DIR__ . '/../../../data/test.pdf'
  32. );
  33. $this->testPdf = new InMemoryFile('test.pdf', $fileContents);
  34. }
  35. /**
  36. * Asserts that putContent replaces the file contents.
  37. *
  38. * @return void
  39. */
  40. public function testPutContent(): void {
  41. $this->testPdf->putContent('test');
  42. self::assertEquals('test', $this->testPdf->getContent());
  43. }
  44. /**
  45. * Asserts that delete() doesn't rise an exception.
  46. *
  47. * @return void
  48. */
  49. public function testDelete(): void {
  50. $this->testPdf->delete();
  51. // assert true, otherwise phpunit complains about not doing any assert
  52. self::assertTrue(true);
  53. }
  54. /**
  55. * Asserts that getName returns the name passed on file creation.
  56. *
  57. * @return void
  58. */
  59. public function testGetName(): void {
  60. self::assertEquals('test.pdf', $this->testPdf->getName());
  61. }
  62. /**
  63. * Asserts that the file size is the size of the test file.
  64. *
  65. * @return void
  66. */
  67. public function testGetSize(): void {
  68. self::assertEquals(7083, $this->testPdf->getSize());
  69. }
  70. /**
  71. * Asserts the file contents are the same than the original file contents.
  72. *
  73. * @return void
  74. */
  75. public function testGetContent(): void {
  76. self::assertEquals(
  77. file_get_contents(__DIR__ . '/../../../data/test.pdf'),
  78. $this->testPdf->getContent()
  79. );
  80. }
  81. /**
  82. * Asserts the test file modification time is an integer.
  83. *
  84. * @return void
  85. */
  86. public function testGetMTime(): void {
  87. self::assertTrue(is_int($this->testPdf->getMTime()));
  88. }
  89. /**
  90. * Asserts the test file mime type is "application/json".
  91. *
  92. * @return void
  93. */
  94. public function testGetMimeType(): void {
  95. self::assertEquals('application/pdf', $this->testPdf->getMimeType());
  96. }
  97. /**
  98. * Asserts that read() raises an NotPermittedException.
  99. *
  100. * @return void
  101. */
  102. public function testRead(): void {
  103. self::expectException(NotPermittedException::class);
  104. $this->testPdf->read();
  105. }
  106. /**
  107. * Asserts that write() raises an NotPermittedException.
  108. *
  109. * @return void
  110. */
  111. public function testWrite(): void {
  112. self::expectException(NotPermittedException::class);
  113. $this->testPdf->write();
  114. }
  115. }