EntityCollectionTest.php 4.3 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. public function setUp() {
  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. ->will($this->returnValue(
  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. /**
  82. * @expectedException \Sabre\DAV\Exception\NotFound
  83. */
  84. public function testGetChildException() {
  85. $this->commentsManager->expects($this->once())
  86. ->method('get')
  87. ->with('55')
  88. ->will($this->throwException(new \OCP\Comments\NotFoundException()));
  89. $this->collection->getChild('55');
  90. }
  91. public function testGetChildren() {
  92. $this->commentsManager->expects($this->once())
  93. ->method('getForObject')
  94. ->with('files', '19')
  95. ->will($this->returnValue([
  96. $this->getMockBuilder(IComment::class)
  97. ->disableOriginalConstructor()
  98. ->getMock()
  99. ]));
  100. $result = $this->collection->getChildren();
  101. $this->assertSame(count($result), 1);
  102. $this->assertTrue($result[0] instanceof \OCA\DAV\Comments\CommentNode);
  103. }
  104. public function testFindChildren() {
  105. $dt = new \DateTime('2016-01-10 18:48:00');
  106. $this->commentsManager->expects($this->once())
  107. ->method('getForObject')
  108. ->with('files', '19', 5, 15, $dt)
  109. ->will($this->returnValue([
  110. $this->getMockBuilder(IComment::class)
  111. ->disableOriginalConstructor()
  112. ->getMock()
  113. ]));
  114. $result = $this->collection->findChildren(5, 15, $dt);
  115. $this->assertSame(count($result), 1);
  116. $this->assertTrue($result[0] instanceof \OCA\DAV\Comments\CommentNode);
  117. }
  118. public function testChildExistsTrue() {
  119. $this->assertTrue($this->collection->childExists('44'));
  120. }
  121. public function testChildExistsFalse() {
  122. $this->commentsManager->expects($this->once())
  123. ->method('get')
  124. ->with('44')
  125. ->will($this->throwException(new \OCP\Comments\NotFoundException()));
  126. $this->assertFalse($this->collection->childExists('44'));
  127. }
  128. }