1
0

SimpleFolderTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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\SimpleFolder;
  25. use OCP\Files\File;
  26. use OCP\Files\Folder;
  27. use OCP\Files\Node;
  28. use OCP\Files\NotFoundException;
  29. use OCP\Files\SimpleFS\ISimpleFile;
  30. class SimpleFolderTest extends \Test\TestCase {
  31. /** @var Folder|\PHPUnit_Framework_MockObject_MockObject */
  32. private $folder;
  33. /** @var SimpleFolder */
  34. private $simpleFolder;
  35. public function setUp() {
  36. parent::setUp();
  37. $this->folder = $this->createMock(Folder::class);
  38. $this->simpleFolder = new SimpleFolder($this->folder);
  39. }
  40. public function testGetName() {
  41. $this->folder->expects($this->once())
  42. ->method('getName')
  43. ->willReturn('myname');
  44. $this->assertEquals('myname', $this->simpleFolder->getName());
  45. }
  46. public function testDelete() {
  47. $this->folder->expects($this->once())
  48. ->method('delete');
  49. $this->simpleFolder->delete();
  50. }
  51. public function dataFileExists() {
  52. return [
  53. [true],
  54. [false],
  55. ];
  56. }
  57. /**
  58. * @dataProvider dataFileExists
  59. * @param bool $exists
  60. */
  61. public function testFileExists($exists) {
  62. $this->folder->expects($this->once())
  63. ->method('nodeExists')
  64. ->with($this->equalTo('file'))
  65. ->willReturn($exists);
  66. $this->assertEquals($exists, $this->simpleFolder->fileExists('file'));
  67. }
  68. public function dataGetFile() {
  69. return [
  70. [File::class, false],
  71. [Folder::class, true],
  72. [Node::class, true],
  73. ];
  74. }
  75. /**
  76. * @dataProvider dataGetFile
  77. * @param string $class
  78. * @param bool $exception
  79. */
  80. public function testGetFile($class, $exception) {
  81. $node = $this->createMock($class);
  82. $this->folder->expects($this->once())
  83. ->method('get')
  84. ->with($this->equalTo('file'))
  85. ->willReturn($node);
  86. try {
  87. $result = $this->simpleFolder->getFile('file');
  88. $this->assertFalse($exception);
  89. $this->assertInstanceOf(ISimpleFile::class, $result);
  90. } catch (NotFoundException $e) {
  91. $this->assertTrue($exception);
  92. }
  93. }
  94. public function testNewFile() {
  95. $file = $this->createMock(File::class);
  96. $this->folder->expects($this->once())
  97. ->method('newFile')
  98. ->with($this->equalTo('file'))
  99. ->willReturn($file);
  100. $result = $this->simpleFolder->newFile('file');
  101. $this->assertInstanceOf(ISimpleFile::class, $result);
  102. }
  103. public function testGetDirectoryListing() {
  104. $file = $this->createMock(File::class);
  105. $folder = $this->createMock(Folder::class);
  106. $node = $this->createMock(Node::class);
  107. $this->folder->expects($this->once())
  108. ->method('getDirectoryListing')
  109. ->willReturn([$file, $folder, $node]);
  110. $result = $this->simpleFolder->getDirectoryListing();
  111. $this->assertCount(1, $result);
  112. $this->assertInstanceOf(ISimpleFile::class, $result[0]);
  113. }
  114. }