EntityCollectionTest.php 4.2 KB

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