DirectFileTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\Tests\Unit\Direct;
  8. use OCA\DAV\Db\Direct;
  9. use OCA\DAV\Direct\DirectFile;
  10. use OCP\EventDispatcher\IEventDispatcher;
  11. use OCP\Files\File;
  12. use OCP\Files\Folder;
  13. use OCP\Files\IRootFolder;
  14. use Sabre\DAV\Exception\Forbidden;
  15. use Test\TestCase;
  16. class DirectFileTest extends TestCase {
  17. /** @var Direct */
  18. private $direct;
  19. /** @var IRootFolder|\PHPUnit\Framework\MockObject\MockObject */
  20. private $rootFolder;
  21. /** @var Folder|\PHPUnit\Framework\MockObject\MockObject */
  22. private $userFolder;
  23. /** @var File|\PHPUnit\Framework\MockObject\MockObject */
  24. private $file;
  25. /** @var DirectFile */
  26. private $directFile;
  27. /** @var IEventDispatcher */
  28. private $eventDispatcher;
  29. protected function setUp(): void {
  30. parent::setUp();
  31. $this->direct = Direct::fromParams([
  32. 'userId' => 'directUser',
  33. 'token' => 'directToken',
  34. 'fileId' => 42,
  35. ]);
  36. $this->rootFolder = $this->createMock(IRootFolder::class);
  37. $this->userFolder = $this->createMock(Folder::class);
  38. $this->rootFolder->method('getUserFolder')
  39. ->with('directUser')
  40. ->willReturn($this->userFolder);
  41. $this->file = $this->createMock(File::class);
  42. $this->userFolder->method('getFirstNodeById')
  43. ->with(42)
  44. ->willReturn($this->file);
  45. $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
  46. $this->directFile = new DirectFile($this->direct, $this->rootFolder, $this->eventDispatcher);
  47. }
  48. public function testPut(): void {
  49. $this->expectException(Forbidden::class);
  50. $this->directFile->put('foo');
  51. }
  52. public function testGet(): void {
  53. $this->file->expects($this->once())
  54. ->method('fopen')
  55. ->with('rb');
  56. $this->directFile->get();
  57. }
  58. public function testGetContentType(): void {
  59. $this->file->method('getMimeType')
  60. ->willReturn('direct/type');
  61. $this->assertSame('direct/type', $this->directFile->getContentType());
  62. }
  63. public function testGetETag(): void {
  64. $this->file->method('getEtag')
  65. ->willReturn('directEtag');
  66. $this->assertSame('directEtag', $this->directFile->getETag());
  67. }
  68. public function testGetSize(): void {
  69. $this->file->method('getSize')
  70. ->willReturn(42);
  71. $this->assertSame(42, $this->directFile->getSize());
  72. }
  73. public function testDelete(): void {
  74. $this->expectException(Forbidden::class);
  75. $this->directFile->delete();
  76. }
  77. public function testGetName(): void {
  78. $this->assertSame('directToken', $this->directFile->getName());
  79. }
  80. public function testSetName(): void {
  81. $this->expectException(Forbidden::class);
  82. $this->directFile->setName('foobar');
  83. }
  84. public function testGetLastModified(): void {
  85. $this->file->method('getMTime')
  86. ->willReturn(42);
  87. $this->assertSame(42, $this->directFile->getLastModified());
  88. }
  89. }