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. use OCP\Files\NotPermittedException;
  29. class SimpleFileTest extends \Test\TestCase {
  30. /** @var File|\PHPUnit_Framework_MockObject_MockObject */
  31. private $file;
  32. /** @var SimpleFile */
  33. private $simpleFile;
  34. public function setUp() {
  35. parent::setUp();
  36. $this->file = $this->createMock(File::class);
  37. $this->simpleFile = new SimpleFile($this->file);
  38. }
  39. public function testGetName() {
  40. $this->file->expects($this->once())
  41. ->method('getName')
  42. ->willReturn('myname');
  43. $this->assertEquals('myname', $this->simpleFile->getName());
  44. }
  45. public function testGetSize() {
  46. $this->file->expects($this->once())
  47. ->method('getSize')
  48. ->willReturn(42);
  49. $this->assertEquals(42, $this->simpleFile->getSize());
  50. }
  51. public function testGetETag() {
  52. $this->file->expects($this->once())
  53. ->method('getETag')
  54. ->willReturn('etag');
  55. $this->assertEquals('etag', $this->simpleFile->getETag());
  56. }
  57. public function testGetMTime() {
  58. $this->file->expects($this->once())
  59. ->method('getMTime')
  60. ->willReturn(101);
  61. $this->assertEquals(101, $this->simpleFile->getMTime());
  62. }
  63. public function testGetContent() {
  64. $this->file->expects($this->once())
  65. ->method('getContent')
  66. ->willReturn('foo');
  67. $this->assertEquals('foo', $this->simpleFile->getContent());
  68. }
  69. public function testPutContent() {
  70. $this->file->expects($this->once())
  71. ->method('putContent')
  72. ->with($this->equalTo('bar'));
  73. $this->simpleFile->putContent('bar');
  74. }
  75. public function testDelete() {
  76. $this->file->expects($this->once())
  77. ->method('delete');
  78. $this->simpleFile->delete();
  79. }
  80. public function testGetMimeType() {
  81. $this->file->expects($this->once())
  82. ->method('getMimeType')
  83. ->willReturn('app/awesome');
  84. $this->assertEquals('app/awesome', $this->simpleFile->getMimeType());
  85. }
  86. public function testGetContentInvalidAppData() {
  87. $this->file->method('getContent')
  88. ->willReturn(false);
  89. $this->file->method('stat')->willReturn(false);
  90. $parent = $this->createMock(Folder::class);
  91. $parent->method('stat')->willReturn(false);
  92. $root = $this->createMock(Folder::class);
  93. $root->method('stat')->willReturn([]);
  94. $this->file->method('getParent')->willReturn($parent);
  95. $parent->method('getParent')->willReturn($root);
  96. $this->expectException(NotFoundException::class);
  97. $this->simpleFile->getContent();
  98. }
  99. public function testRead() {
  100. $this->file->expects($this->once())
  101. ->method('fopen')
  102. ->with('r');
  103. $this->simpleFile->read();
  104. }
  105. public function testWrite() {
  106. $this->file->expects($this->once())
  107. ->method('fopen')
  108. ->with('w');
  109. $this->simpleFile->write();
  110. }
  111. }