BookmarkRepositoryTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Ui\Test\Unit\Model\ResourceModel;
  7. use Magento\Ui\Model\ResourceModel\BookmarkRepository;
  8. /**
  9. * Class BookmarkRepositoryTest
  10. *
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. */
  13. class BookmarkRepositoryTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var BookmarkRepository|\PHPUnit_Framework_MockObject_MockObject
  17. */
  18. protected $bookmarkRepository;
  19. /**
  20. * @var \Magento\Ui\Api\Data\BookmarkInterface|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. protected $bookmarkMock;
  23. /**
  24. * @var \Magento\Ui\Model\ResourceModel\Bookmark|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. protected $bookmarkResourceMock;
  27. /**
  28. * @var \Magento\Ui\Api\Data\BookmarkSearchResultsInterface|\PHPUnit_Framework_MockObject_MockObject
  29. */
  30. protected $searchResultsMock;
  31. /**
  32. * @var \PHPUnit_Framework_MockObject_MockObject
  33. */
  34. private $collectionProcessor;
  35. /**
  36. * Set up
  37. */
  38. protected function setUp()
  39. {
  40. $this->bookmarkMock = $this->getMockBuilder(\Magento\Ui\Model\Bookmark::class)
  41. ->disableOriginalConstructor()
  42. ->getMock();
  43. $bookmarkFactoryMock = $this->getMockBuilder(\Magento\Ui\Api\Data\BookmarkInterfaceFactory::class)
  44. ->disableOriginalConstructor()
  45. ->setMethods(['create'])
  46. ->getMock();
  47. /** @var $bookmarkFactoryMock \Magento\Ui\Api\Data\BookmarkInterfaceFactory */
  48. $bookmarkFactoryMock->expects($this->any())
  49. ->method('create')
  50. ->willReturn($this->bookmarkMock);
  51. $this->bookmarkResourceMock = $this->getMockBuilder(\Magento\Ui\Model\ResourceModel\Bookmark::class)
  52. ->disableOriginalConstructor()
  53. ->setMethods(['load', 'save', 'delete'])
  54. ->getMock();
  55. $this->searchResultsMock = $this->getMockBuilder(\Magento\Ui\Api\Data\BookmarkSearchResultsInterface::class)
  56. ->getMockForAbstractClass();
  57. /** @var $searchResultsFactoryMock \Magento\Ui\Api\Data\BookmarkSearchResultsInterfaceFactory */
  58. $searchResultsFactoryMock = $this->getMockBuilder(
  59. \Magento\Ui\Api\Data\BookmarkSearchResultsInterfaceFactory::class
  60. )->disableOriginalConstructor()->setMethods(['create'])->getMock();
  61. $searchResultsFactoryMock->expects($this->any())->method('create')->willReturn($this->searchResultsMock);
  62. $this->collectionProcessor = $this->createMock(
  63. \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface::class
  64. );
  65. $this->bookmarkRepository = new BookmarkRepository(
  66. $bookmarkFactoryMock,
  67. $this->bookmarkResourceMock,
  68. $searchResultsFactoryMock,
  69. $this->collectionProcessor
  70. );
  71. }
  72. public function testSave()
  73. {
  74. $this->bookmarkResourceMock->expects($this->once())
  75. ->method('save')
  76. ->with($this->bookmarkMock);
  77. $this->assertEquals($this->bookmarkMock, $this->bookmarkRepository->save($this->bookmarkMock));
  78. }
  79. public function testSaveWithException()
  80. {
  81. $exceptionMessage = 'Some Message';
  82. $this->bookmarkResourceMock->expects($this->once())
  83. ->method('save')
  84. ->with($this->bookmarkMock)
  85. ->willThrowException(new \Exception($exceptionMessage));
  86. $this->expectException(\Magento\Framework\Exception\CouldNotSaveException::class);
  87. $this->expectExceptionMessage($exceptionMessage);
  88. $this->bookmarkRepository->save($this->bookmarkMock);
  89. }
  90. public function testGetById()
  91. {
  92. $bookmarkId = 1;
  93. $this->bookmarkMock->expects($this->once())
  94. ->method('getId')
  95. ->willReturn($bookmarkId);
  96. $this->bookmarkResourceMock->expects($this->once())
  97. ->method('load')
  98. ->with($this->bookmarkMock, $bookmarkId)
  99. ->willReturn($this->bookmarkMock);
  100. $this->assertEquals($this->bookmarkMock, $this->bookmarkRepository->getById($bookmarkId));
  101. }
  102. public function testGetByIdWithException()
  103. {
  104. $notExistsBookmarkId = 2;
  105. $this->bookmarkMock->expects($this->once())
  106. ->method('getId')
  107. ->willReturn(null);
  108. $this->bookmarkResourceMock->expects($this->once())
  109. ->method('load')
  110. ->with($this->bookmarkMock, $notExistsBookmarkId)
  111. ->willReturn($this->bookmarkMock);
  112. $this->expectException(\Magento\Framework\Exception\NoSuchEntityException::class);
  113. $exceptionMessage = (string)__(
  114. 'The bookmark with "%1" ID doesn\'t exist. Verify your information and try again.',
  115. $notExistsBookmarkId
  116. );
  117. $this->expectExceptionMessage($exceptionMessage);
  118. $this->bookmarkRepository->getById($notExistsBookmarkId);
  119. }
  120. public function testDelete()
  121. {
  122. $this->bookmarkResourceMock->expects($this->once())
  123. ->method('delete')
  124. ->with($this->bookmarkMock);
  125. $this->assertTrue($this->bookmarkRepository->delete($this->bookmarkMock));
  126. }
  127. public function testDeleteWithException()
  128. {
  129. $exceptionMessage = 'Some Message';
  130. $this->bookmarkResourceMock->expects($this->once())
  131. ->method('delete')
  132. ->with($this->bookmarkMock)
  133. ->willThrowException(new \Exception($exceptionMessage));
  134. $this->expectException(\Magento\Framework\Exception\CouldNotDeleteException::class);
  135. $this->expectExceptionMessage($exceptionMessage);
  136. $this->assertTrue($this->bookmarkRepository->delete($this->bookmarkMock));
  137. }
  138. public function testGetList()
  139. {
  140. $bookmarkId = 1;
  141. $this->bookmarkMock->expects($this->any())
  142. ->method('getId')
  143. ->willReturn($bookmarkId);
  144. $this->bookmarkResourceMock->expects($this->once())
  145. ->method('load')
  146. ->with($this->bookmarkMock, $bookmarkId)
  147. ->willReturn($this->bookmarkMock);
  148. $collection = $this->getMockBuilder(\Magento\Ui\Model\ResourceModel\Bookmark\Collection::class)
  149. ->disableOriginalConstructor()
  150. ->getMock();
  151. $collection->expects($this->once())
  152. ->method('getItems')
  153. ->willReturn([$this->bookmarkMock]);
  154. $this->bookmarkMock->expects($this->once())
  155. ->method('getCollection')
  156. ->willReturn($collection);
  157. $searchCriteria = $this->getMockBuilder(\Magento\Framework\Api\SearchCriteriaInterface::class)
  158. ->getMockForAbstractClass();
  159. $this->assertEquals($this->searchResultsMock, $this->bookmarkRepository->getList($searchCriteria));
  160. }
  161. public function testDeleteById()
  162. {
  163. $bookmarkId = 1;
  164. $this->bookmarkMock->expects($this->once())
  165. ->method('getId')
  166. ->willReturn($bookmarkId);
  167. $this->bookmarkResourceMock->expects($this->once())
  168. ->method('load')
  169. ->with($this->bookmarkMock, $bookmarkId)
  170. ->willReturn($this->bookmarkMock);
  171. $this->bookmarkResourceMock->expects($this->once())
  172. ->method('delete')
  173. ->with($this->bookmarkMock);
  174. $this->assertTrue($this->bookmarkRepository->deleteById($bookmarkId));
  175. }
  176. }