1
0

SimpleFileTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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\File\SimpleFS;
  7. use OC\Files\SimpleFS\SimpleFile;
  8. use OCP\Files\File;
  9. use OCP\Files\Folder;
  10. use OCP\Files\NotFoundException;
  11. class SimpleFileTest extends \Test\TestCase {
  12. /** @var File|\PHPUnit\Framework\MockObject\MockObject */
  13. private $file;
  14. /** @var SimpleFile */
  15. private $simpleFile;
  16. protected function setUp(): void {
  17. parent::setUp();
  18. $this->file = $this->createMock(File::class);
  19. $this->simpleFile = new SimpleFile($this->file);
  20. }
  21. public function testGetName() {
  22. $this->file->expects($this->once())
  23. ->method('getName')
  24. ->willReturn('myname');
  25. $this->assertEquals('myname', $this->simpleFile->getName());
  26. }
  27. public function testGetSize() {
  28. $this->file->expects($this->once())
  29. ->method('getSize')
  30. ->willReturn(42);
  31. $this->assertEquals(42, $this->simpleFile->getSize());
  32. }
  33. public function testGetETag() {
  34. $this->file->expects($this->once())
  35. ->method('getETag')
  36. ->willReturn('etag');
  37. $this->assertEquals('etag', $this->simpleFile->getETag());
  38. }
  39. public function testGetMTime() {
  40. $this->file->expects($this->once())
  41. ->method('getMTime')
  42. ->willReturn(101);
  43. $this->assertEquals(101, $this->simpleFile->getMTime());
  44. }
  45. public function testGetContent() {
  46. $this->file->expects($this->once())
  47. ->method('getContent')
  48. ->willReturn('foo');
  49. $this->assertEquals('foo', $this->simpleFile->getContent());
  50. }
  51. public function testPutContent() {
  52. $this->file->expects($this->once())
  53. ->method('putContent')
  54. ->with($this->equalTo('bar'));
  55. $this->simpleFile->putContent('bar');
  56. }
  57. public function testDelete() {
  58. $this->file->expects($this->once())
  59. ->method('delete');
  60. $this->simpleFile->delete();
  61. }
  62. public function testGetMimeType() {
  63. $this->file->expects($this->once())
  64. ->method('getMimeType')
  65. ->willReturn('app/awesome');
  66. $this->assertEquals('app/awesome', $this->simpleFile->getMimeType());
  67. }
  68. public function testGetContentInvalidAppData() {
  69. $this->file->method('getContent')
  70. ->willReturn(false);
  71. $this->file->method('stat')->willReturn(false);
  72. $parent = $this->createMock(Folder::class);
  73. $parent->method('stat')->willReturn(false);
  74. $root = $this->createMock(Folder::class);
  75. $root->method('stat')->willReturn([]);
  76. $this->file->method('getParent')->willReturn($parent);
  77. $parent->method('getParent')->willReturn($root);
  78. $this->expectException(NotFoundException::class);
  79. $this->simpleFile->getContent();
  80. }
  81. public function testRead() {
  82. $this->file->expects($this->once())
  83. ->method('fopen')
  84. ->with('r');
  85. $this->simpleFile->read();
  86. }
  87. public function testWrite() {
  88. $this->file->expects($this->once())
  89. ->method('fopen')
  90. ->with('w');
  91. $this->simpleFile->write();
  92. }
  93. }