SimpleFileTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * @copyright 2016 Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  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
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace Test\File\SimpleFS;
  24. use OC\Files\SimpleFS\SimpleFile;
  25. use OCP\Files\File;
  26. use OCP\Files\Folder;
  27. use OCP\Files\NotFoundException;
  28. class SimpleFileTest extends \Test\TestCase {
  29. /** @var File|\PHPUnit\Framework\MockObject\MockObject */
  30. private $file;
  31. /** @var SimpleFile */
  32. private $simpleFile;
  33. protected function setUp(): void {
  34. parent::setUp();
  35. $this->file = $this->createMock(File::class);
  36. $this->simpleFile = new SimpleFile($this->file);
  37. }
  38. public function testGetName() {
  39. $this->file->expects($this->once())
  40. ->method('getName')
  41. ->willReturn('myname');
  42. $this->assertEquals('myname', $this->simpleFile->getName());
  43. }
  44. public function testGetSize() {
  45. $this->file->expects($this->once())
  46. ->method('getSize')
  47. ->willReturn(42);
  48. $this->assertEquals(42, $this->simpleFile->getSize());
  49. }
  50. public function testGetETag() {
  51. $this->file->expects($this->once())
  52. ->method('getETag')
  53. ->willReturn('etag');
  54. $this->assertEquals('etag', $this->simpleFile->getETag());
  55. }
  56. public function testGetMTime() {
  57. $this->file->expects($this->once())
  58. ->method('getMTime')
  59. ->willReturn(101);
  60. $this->assertEquals(101, $this->simpleFile->getMTime());
  61. }
  62. public function testGetContent() {
  63. $this->file->expects($this->once())
  64. ->method('getContent')
  65. ->willReturn('foo');
  66. $this->assertEquals('foo', $this->simpleFile->getContent());
  67. }
  68. public function testPutContent() {
  69. $this->file->expects($this->once())
  70. ->method('putContent')
  71. ->with($this->equalTo('bar'));
  72. $this->simpleFile->putContent('bar');
  73. }
  74. public function testDelete() {
  75. $this->file->expects($this->once())
  76. ->method('delete');
  77. $this->simpleFile->delete();
  78. }
  79. public function testGetMimeType() {
  80. $this->file->expects($this->once())
  81. ->method('getMimeType')
  82. ->willReturn('app/awesome');
  83. $this->assertEquals('app/awesome', $this->simpleFile->getMimeType());
  84. }
  85. public function testGetContentInvalidAppData() {
  86. $this->file->method('getContent')
  87. ->willReturn(false);
  88. $this->file->method('stat')->willReturn(false);
  89. $parent = $this->createMock(Folder::class);
  90. $parent->method('stat')->willReturn(false);
  91. $root = $this->createMock(Folder::class);
  92. $root->method('stat')->willReturn([]);
  93. $this->file->method('getParent')->willReturn($parent);
  94. $parent->method('getParent')->willReturn($root);
  95. $this->expectException(NotFoundException::class);
  96. $this->simpleFile->getContent();
  97. }
  98. public function testRead() {
  99. $this->file->expects($this->once())
  100. ->method('fopen')
  101. ->with('r');
  102. $this->simpleFile->read();
  103. }
  104. public function testWrite() {
  105. $this->file->expects($this->once())
  106. ->method('fopen')
  107. ->with('w');
  108. $this->simpleFile->write();
  109. }
  110. }