AppDataTest.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Test\Files\AppData;
  7. use OC\Files\AppData\AppData;
  8. use OC\SystemConfig;
  9. use OCP\Files\File;
  10. use OCP\Files\Folder;
  11. use OCP\Files\IAppData;
  12. use OCP\Files\IRootFolder;
  13. use OCP\Files\Node;
  14. use OCP\Files\SimpleFS\ISimpleFolder;
  15. class AppDataTest extends \Test\TestCase {
  16. /** @var IRootFolder|\PHPUnit\Framework\MockObject\MockObject */
  17. private $rootFolder;
  18. /** @var SystemConfig|\PHPUnit\Framework\MockObject\MockObject */
  19. private $systemConfig;
  20. /** @var IAppData */
  21. private $appData;
  22. protected function setUp(): void {
  23. parent::setUp();
  24. $this->rootFolder = $this->createMock(IRootFolder::class);
  25. $this->systemConfig = $this->createMock(SystemConfig::class);
  26. $this->appData = new AppData($this->rootFolder, $this->systemConfig, 'myApp');
  27. $this->systemConfig->expects($this->any())
  28. ->method('getValue')
  29. ->with('instanceid', null)
  30. ->willReturn('iid');
  31. }
  32. private function setupAppFolder() {
  33. $appFolder = $this->createMock(Folder::class);
  34. $this->rootFolder->expects($this->any())
  35. ->method('get')
  36. ->with($this->equalTo('appdata_iid/myApp'))
  37. ->willReturn($appFolder);
  38. return $appFolder;
  39. }
  40. public function testGetFolder() {
  41. $folder = $this->createMock(Folder::class);
  42. $this->rootFolder->expects($this->once())
  43. ->method('get')
  44. ->with($this->equalTo('appdata_iid/myApp/folder'))
  45. ->willReturn($folder);
  46. $result = $this->appData->getFolder('folder');
  47. $this->assertInstanceOf(ISimpleFolder::class, $result);
  48. }
  49. public function testNewFolder() {
  50. $appFolder = $this->setupAppFolder();
  51. $folder = $this->createMock(Folder::class);
  52. $appFolder->expects($this->once())
  53. ->method('newFolder')
  54. ->with($this->equalTo('folder'))
  55. ->willReturn($folder);
  56. $result = $this->appData->newFolder('folder');
  57. $this->assertInstanceOf(ISimpleFolder::class, $result);
  58. }
  59. public function testGetDirectoryListing() {
  60. $appFolder = $this->setupAppFolder();
  61. $file = $this->createMock(File::class);
  62. $folder = $this->createMock(Folder::class);
  63. $node = $this->createMock(Node::class);
  64. $appFolder->expects($this->once())
  65. ->method('getDirectoryListing')
  66. ->willReturn([$file, $folder, $node]);
  67. $result = $this->appData->getDirectoryListing();
  68. $this->assertCount(1, $result);
  69. $this->assertInstanceOf(ISimpleFolder::class, $result[0]);
  70. }
  71. }