EntityCollectionTest.php 3.6 KB

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