DirectFileTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\DAV\Tests\Unit\Direct;
  25. use OCA\DAV\Db\Direct;
  26. use OCA\DAV\Direct\DirectFile;
  27. use OCP\Files\File;
  28. use OCP\Files\Folder;
  29. use OCP\Files\IRootFolder;
  30. use Sabre\DAV\Exception\Forbidden;
  31. use Test\TestCase;
  32. class DirectFileTest extends TestCase {
  33. /** @var Direct */
  34. private $direct;
  35. /** @var IRootFolder|\PHPUnit_Framework_MockObject_MockObject */
  36. private $rootFolder;
  37. /** @var Folder|\PHPUnit_Framework_MockObject_MockObject */
  38. private $userFolder;
  39. /** @var File|\PHPUnit_Framework_MockObject_MockObject */
  40. private $file;
  41. /** @var DirectFile */
  42. private $directFile;
  43. protected function setUp(): void {
  44. parent::setUp();
  45. $this->direct = Direct::fromParams([
  46. 'userId' => 'directUser',
  47. 'token' => 'directToken',
  48. 'fileId' => 42,
  49. ]);
  50. $this->rootFolder = $this->createMock(IRootFolder::class);
  51. $this->userFolder = $this->createMock(Folder::class);
  52. $this->rootFolder->method('getUserFolder')
  53. ->with('directUser')
  54. ->willReturn($this->userFolder);
  55. $this->file = $this->createMock(File::class);
  56. $this->userFolder->method('getById')
  57. ->with(42)
  58. ->willReturn([$this->file]);
  59. $this->directFile = new DirectFile($this->direct, $this->rootFolder);
  60. }
  61. public function testPut() {
  62. $this->expectException(Forbidden::class);
  63. $this->directFile->put('foo');
  64. }
  65. public function testGet() {
  66. $this->file->expects($this->once())
  67. ->method('fopen')
  68. ->with('rb');
  69. $this->directFile->get();
  70. }
  71. public function testGetContentType() {
  72. $this->file->method('getMimeType')
  73. ->willReturn('direct/type');
  74. $this->assertSame('direct/type', $this->directFile->getContentType());
  75. }
  76. public function testGetETag() {
  77. $this->file->method('getEtag')
  78. ->willReturn('directEtag');
  79. $this->assertSame('directEtag', $this->directFile->getETag());
  80. }
  81. public function testGetSize() {
  82. $this->file->method('getSize')
  83. ->willReturn(42);
  84. $this->assertSame(42, $this->directFile->getSize());
  85. }
  86. public function testDelete() {
  87. $this->expectException(Forbidden::class);
  88. $this->directFile->delete();
  89. }
  90. public function testGetName() {
  91. $this->assertSame('directToken', $this->directFile->getName());
  92. }
  93. public function testSetName() {
  94. $this->expectException(Forbidden::class);
  95. $this->directFile->setName('foobar');
  96. }
  97. public function testGetLastModified() {
  98. $this->file->method('getMTime')
  99. ->willReturn(42);
  100. $this->assertSame(42, $this->directFile->getLastModified());
  101. }
  102. }