DirectFileTest.php 3.6 KB

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