EntityCollectionTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  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, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\DAV\Tests\unit\Comments;
  27. use OCA\DAV\Comments\EntityCollection;
  28. use OCP\Comments\IComment;
  29. use OCP\Comments\ICommentsManager;
  30. use OCP\IUserManager;
  31. use OCP\IUserSession;
  32. use Psr\Log\LoggerInterface;
  33. class EntityCollectionTest extends \Test\TestCase {
  34. /** @var \OCP\Comments\ICommentsManager|\PHPUnit\Framework\MockObject\MockObject */
  35. protected $commentsManager;
  36. /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  37. protected $userManager;
  38. /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
  39. protected $logger;
  40. /** @var EntityCollection */
  41. protected $collection;
  42. /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  43. protected $userSession;
  44. protected function setUp(): void {
  45. parent::setUp();
  46. $this->commentsManager = $this->getMockBuilder(ICommentsManager::class)
  47. ->disableOriginalConstructor()
  48. ->getMock();
  49. $this->userManager = $this->getMockBuilder(IUserManager::class)
  50. ->disableOriginalConstructor()
  51. ->getMock();
  52. $this->userSession = $this->getMockBuilder(IUserSession::class)
  53. ->disableOriginalConstructor()
  54. ->getMock();
  55. $this->logger = $this->getMockBuilder(LoggerInterface::class)
  56. ->disableOriginalConstructor()
  57. ->getMock();
  58. $this->collection = new \OCA\DAV\Comments\EntityCollection(
  59. '19',
  60. 'files',
  61. $this->commentsManager,
  62. $this->userManager,
  63. $this->userSession,
  64. $this->logger
  65. );
  66. }
  67. public function testGetId(): void {
  68. $this->assertSame($this->collection->getId(), '19');
  69. }
  70. public function testGetChild(): void {
  71. $this->commentsManager->expects($this->once())
  72. ->method('get')
  73. ->with('55')
  74. ->willReturn(
  75. $this->getMockBuilder(IComment::class)
  76. ->disableOriginalConstructor()
  77. ->getMock()
  78. );
  79. $node = $this->collection->getChild('55');
  80. $this->assertTrue($node instanceof \OCA\DAV\Comments\CommentNode);
  81. }
  82. public function testGetChildException(): void {
  83. $this->expectException(\Sabre\DAV\Exception\NotFound::class);
  84. $this->commentsManager->expects($this->once())
  85. ->method('get')
  86. ->with('55')
  87. ->will($this->throwException(new \OCP\Comments\NotFoundException()));
  88. $this->collection->getChild('55');
  89. }
  90. public function testGetChildren(): void {
  91. $this->commentsManager->expects($this->once())
  92. ->method('getForObject')
  93. ->with('files', '19')
  94. ->willReturn([
  95. $this->getMockBuilder(IComment::class)
  96. ->disableOriginalConstructor()
  97. ->getMock()
  98. ]);
  99. $result = $this->collection->getChildren();
  100. $this->assertSame(count($result), 1);
  101. $this->assertTrue($result[0] instanceof \OCA\DAV\Comments\CommentNode);
  102. }
  103. public function testFindChildren(): void {
  104. $dt = new \DateTime('2016-01-10 18:48:00');
  105. $this->commentsManager->expects($this->once())
  106. ->method('getForObject')
  107. ->with('files', '19', 5, 15, $dt)
  108. ->willReturn([
  109. $this->getMockBuilder(IComment::class)
  110. ->disableOriginalConstructor()
  111. ->getMock()
  112. ]);
  113. $result = $this->collection->findChildren(5, 15, $dt);
  114. $this->assertSame(count($result), 1);
  115. $this->assertTrue($result[0] instanceof \OCA\DAV\Comments\CommentNode);
  116. }
  117. public function testChildExistsTrue(): void {
  118. $this->assertTrue($this->collection->childExists('44'));
  119. }
  120. public function testChildExistsFalse(): void {
  121. $this->commentsManager->expects($this->once())
  122. ->method('get')
  123. ->with('44')
  124. ->will($this->throwException(new \OCP\Comments\NotFoundException()));
  125. $this->assertFalse($this->collection->childExists('44'));
  126. }
  127. }