1
0

EntityCollectionTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\Tests\unit\Comments;
  8. use OCA\DAV\Comments\EntityCollection;
  9. use OCP\Comments\IComment;
  10. use OCP\Comments\ICommentsManager;
  11. use OCP\IUserManager;
  12. use OCP\IUserSession;
  13. use Psr\Log\LoggerInterface;
  14. class EntityCollectionTest extends \Test\TestCase {
  15. /** @var \OCP\Comments\ICommentsManager|\PHPUnit\Framework\MockObject\MockObject */
  16. protected $commentsManager;
  17. /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  18. protected $userManager;
  19. /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
  20. protected $logger;
  21. /** @var EntityCollection */
  22. protected $collection;
  23. /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  24. protected $userSession;
  25. protected function setUp(): void {
  26. parent::setUp();
  27. $this->commentsManager = $this->getMockBuilder(ICommentsManager::class)
  28. ->disableOriginalConstructor()
  29. ->getMock();
  30. $this->userManager = $this->getMockBuilder(IUserManager::class)
  31. ->disableOriginalConstructor()
  32. ->getMock();
  33. $this->userSession = $this->getMockBuilder(IUserSession::class)
  34. ->disableOriginalConstructor()
  35. ->getMock();
  36. $this->logger = $this->getMockBuilder(LoggerInterface::class)
  37. ->disableOriginalConstructor()
  38. ->getMock();
  39. $this->collection = new \OCA\DAV\Comments\EntityCollection(
  40. '19',
  41. 'files',
  42. $this->commentsManager,
  43. $this->userManager,
  44. $this->userSession,
  45. $this->logger
  46. );
  47. }
  48. public function testGetId(): void {
  49. $this->assertSame($this->collection->getId(), '19');
  50. }
  51. public function testGetChild(): void {
  52. $this->commentsManager->expects($this->once())
  53. ->method('get')
  54. ->with('55')
  55. ->willReturn(
  56. $this->getMockBuilder(IComment::class)
  57. ->disableOriginalConstructor()
  58. ->getMock()
  59. );
  60. $node = $this->collection->getChild('55');
  61. $this->assertTrue($node instanceof \OCA\DAV\Comments\CommentNode);
  62. }
  63. public function testGetChildException(): void {
  64. $this->expectException(\Sabre\DAV\Exception\NotFound::class);
  65. $this->commentsManager->expects($this->once())
  66. ->method('get')
  67. ->with('55')
  68. ->will($this->throwException(new \OCP\Comments\NotFoundException()));
  69. $this->collection->getChild('55');
  70. }
  71. public function testGetChildren(): void {
  72. $this->commentsManager->expects($this->once())
  73. ->method('getForObject')
  74. ->with('files', '19')
  75. ->willReturn([
  76. $this->getMockBuilder(IComment::class)
  77. ->disableOriginalConstructor()
  78. ->getMock()
  79. ]);
  80. $result = $this->collection->getChildren();
  81. $this->assertSame(count($result), 1);
  82. $this->assertTrue($result[0] instanceof \OCA\DAV\Comments\CommentNode);
  83. }
  84. public function testFindChildren(): void {
  85. $dt = new \DateTime('2016-01-10 18:48:00');
  86. $this->commentsManager->expects($this->once())
  87. ->method('getForObject')
  88. ->with('files', '19', 5, 15, $dt)
  89. ->willReturn([
  90. $this->getMockBuilder(IComment::class)
  91. ->disableOriginalConstructor()
  92. ->getMock()
  93. ]);
  94. $result = $this->collection->findChildren(5, 15, $dt);
  95. $this->assertSame(count($result), 1);
  96. $this->assertTrue($result[0] instanceof \OCA\DAV\Comments\CommentNode);
  97. }
  98. public function testChildExistsTrue(): void {
  99. $this->assertTrue($this->collection->childExists('44'));
  100. }
  101. public function testChildExistsFalse(): void {
  102. $this->commentsManager->expects($this->once())
  103. ->method('get')
  104. ->with('44')
  105. ->will($this->throwException(new \OCP\Comments\NotFoundException()));
  106. $this->assertFalse($this->collection->childExists('44'));
  107. }
  108. }