PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/module-ui/Test/Unit/Model/ResourceModel/BookmarkRepositoryTest.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 244 lines | 203 code | 17 blank | 24 comment | 0 complexity | bdb2bb79218dbf342092c1134381e866 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Ui\Test\Unit\Model\ResourceModel;
  7. use Magento\Framework\Api\SortOrder;
  8. use Magento\Ui\Model\ResourceModel\BookmarkRepository;
  9. /**
  10. * Class BookmarkRepositoryTest
  11. */
  12. class BookmarkRepositoryTest extends \PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * @var BookmarkRepository|\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $bookmarkRepository;
  18. /**
  19. * @var \Magento\Ui\Api\Data\BookmarkInterface|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $bookmarkMock;
  22. /**
  23. * @var \Magento\Ui\Model\ResourceModel\Bookmark|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $bookmarkResourceMock;
  26. /**
  27. * @var \Magento\Ui\Api\Data\BookmarkSearchResultsInterface|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $searchResultsMock;
  30. /**
  31. * Set up
  32. */
  33. public function setUp()
  34. {
  35. $this->bookmarkMock = $this->getMockBuilder('Magento\Ui\Model\Bookmark')
  36. ->disableOriginalConstructor()
  37. ->getMock();
  38. $bookmarkFactoryMock = $this->getMockBuilder('Magento\Ui\Api\Data\BookmarkInterfaceFactory')
  39. ->disableOriginalConstructor()
  40. ->setMethods(['create'])
  41. ->getMock();
  42. /** @var $bookmarkFactoryMock \Magento\Ui\Api\Data\BookmarkInterfaceFactory */
  43. $bookmarkFactoryMock->expects($this->any())
  44. ->method('create')
  45. ->willReturn($this->bookmarkMock);
  46. $this->bookmarkResourceMock = $this->getMockBuilder('Magento\Ui\Model\ResourceModel\Bookmark')
  47. ->disableOriginalConstructor()
  48. ->setMethods(['load', 'save', 'delete'])
  49. ->getMock();
  50. $this->searchResultsMock = $this->getMockBuilder('Magento\Ui\Api\Data\BookmarkSearchResultsInterface')
  51. ->getMockForAbstractClass();
  52. /** @var $searchResultsFactoryMock \Magento\Ui\Api\Data\BookmarkSearchResultsInterfaceFactory */
  53. $searchResultsFactoryMock = $this->getMockBuilder(
  54. 'Magento\Ui\Api\Data\BookmarkSearchResultsInterfaceFactory'
  55. )->disableOriginalConstructor()->setMethods(['create'])->getMock();
  56. $searchResultsFactoryMock->expects($this->any())->method('create')->willReturn($this->searchResultsMock);
  57. $this->bookmarkRepository = new BookmarkRepository(
  58. $bookmarkFactoryMock,
  59. $this->bookmarkResourceMock,
  60. $searchResultsFactoryMock
  61. );
  62. }
  63. public function testSave()
  64. {
  65. $this->bookmarkResourceMock->expects($this->once())
  66. ->method('save')
  67. ->with($this->bookmarkMock);
  68. $this->assertEquals($this->bookmarkMock, $this->bookmarkRepository->save($this->bookmarkMock));
  69. }
  70. public function testSaveWithException()
  71. {
  72. $exceptionMessage = 'Some Message';
  73. $this->bookmarkResourceMock->expects($this->once())
  74. ->method('save')
  75. ->with($this->bookmarkMock)
  76. ->willThrowException(new \Exception($exceptionMessage));
  77. $this->setExpectedException('Magento\Framework\Exception\CouldNotSaveException', __($exceptionMessage));
  78. $this->bookmarkRepository->save($this->bookmarkMock);
  79. }
  80. public function testGetById()
  81. {
  82. $bookmarkId = 1;
  83. $this->bookmarkMock->expects($this->once())
  84. ->method('getId')
  85. ->willReturn($bookmarkId);
  86. $this->bookmarkResourceMock->expects($this->once())
  87. ->method('load')
  88. ->with($this->bookmarkMock, $bookmarkId)
  89. ->willReturn($this->bookmarkMock);
  90. $this->assertEquals($this->bookmarkMock, $this->bookmarkRepository->getById($bookmarkId));
  91. }
  92. public function testGetByIdWithException()
  93. {
  94. $notExistsBookmarkId = 2;
  95. $this->bookmarkMock->expects($this->once())
  96. ->method('getId')
  97. ->willReturn(null);
  98. $this->bookmarkResourceMock->expects($this->once())
  99. ->method('load')
  100. ->with($this->bookmarkMock, $notExistsBookmarkId)
  101. ->willReturn($this->bookmarkMock);
  102. $this->setExpectedException(
  103. 'Magento\Framework\Exception\NoSuchEntityException',
  104. __('Bookmark with id "%1" does not exist.', $notExistsBookmarkId)
  105. );
  106. $this->bookmarkRepository->getById($notExistsBookmarkId);
  107. }
  108. public function testDelete()
  109. {
  110. $this->bookmarkResourceMock->expects($this->once())
  111. ->method('delete')
  112. ->with($this->bookmarkMock);
  113. $this->assertTrue($this->bookmarkRepository->delete($this->bookmarkMock));
  114. }
  115. public function testDeleteWithException()
  116. {
  117. $exceptionMessage = 'Some Message';
  118. $this->bookmarkResourceMock->expects($this->once())
  119. ->method('delete')
  120. ->with($this->bookmarkMock)
  121. ->willThrowException(new \Exception($exceptionMessage));
  122. $this->setExpectedException('Magento\Framework\Exception\CouldNotDeleteException', __($exceptionMessage));
  123. $this->assertTrue($this->bookmarkRepository->delete($this->bookmarkMock));
  124. }
  125. public function testGetList()
  126. {
  127. $bookmarkId = 1;
  128. $fieldNameAsc = 'first_field';
  129. $fieldValueAsc = 'first_value';
  130. $fieldNameDesc = 'second_field';
  131. $fieldValueDesc = 'second_value';
  132. $this->bookmarkMock->expects($this->any())
  133. ->method('getId')
  134. ->willReturn($bookmarkId);
  135. $this->bookmarkResourceMock->expects($this->once())
  136. ->method('load')
  137. ->with($this->bookmarkMock, $bookmarkId)
  138. ->willReturn($this->bookmarkMock);
  139. $filterGroup = $this->getMockBuilder('Magento\Framework\Api\Search\FilterGroup')
  140. ->disableOriginalConstructor()
  141. ->getMock();
  142. $sortOrderAsc = $this->getMockBuilder('Magento\Framework\Api\SortOrder')
  143. ->disableOriginalConstructor()
  144. ->getMock();
  145. $sortOrderAsc->expects($this->once())
  146. ->method('getField')
  147. ->willReturn($fieldNameAsc);
  148. $sortOrderAsc->expects($this->once())
  149. ->method('getDirection')
  150. ->willReturn(SortOrder::SORT_ASC);
  151. $sortOrderDesc = $this->getMockBuilder('Magento\Framework\Api\SortOrder')
  152. ->disableOriginalConstructor()
  153. ->getMock();
  154. $sortOrderDesc->expects($this->once())
  155. ->method('getField')
  156. ->willReturn($fieldNameDesc);
  157. $sortOrderDesc->expects($this->once())
  158. ->method('getDirection')
  159. ->willReturn(SortOrder::SORT_DESC);
  160. $fieldAsc = $this->getMockBuilder('Magento\Framework\Api\Filter')
  161. ->disableOriginalConstructor()
  162. ->getMock();
  163. $fieldAsc->expects($this->once())
  164. ->method('getField')
  165. ->willReturn($fieldNameAsc);
  166. $fieldAsc->expects($this->once())
  167. ->method('getValue')
  168. ->willReturn($fieldValueAsc);
  169. $fieldAsc->expects($this->any())
  170. ->method('getConditionType')
  171. ->willReturn(false);
  172. $fieldDesc = $this->getMockBuilder('Magento\Framework\Api\Filter')
  173. ->disableOriginalConstructor()
  174. ->getMock();
  175. $fieldDesc->expects($this->once())
  176. ->method('getField')
  177. ->willReturn($fieldNameDesc);
  178. $fieldDesc->expects($this->once())
  179. ->method('getValue')
  180. ->willReturn($fieldValueDesc);
  181. $fieldDesc->expects($this->any())
  182. ->method('getConditionType')
  183. ->willReturn('eq');
  184. $filterGroup->expects($this->once())
  185. ->method('getFilters')
  186. ->willReturn([$fieldAsc, $fieldDesc]);
  187. $collection = $this->getMockBuilder('Magento\Ui\Model\ResourceModel\Bookmark\Collection')
  188. ->disableOriginalConstructor()
  189. ->getMock();
  190. $collection->expects($this->once())
  191. ->method('getItems')
  192. ->willReturn([$this->bookmarkMock]);
  193. $collection->expects($this->any())
  194. ->method('addOrder')
  195. ->willReturnMap([[$fieldNameAsc, SortOrder::SORT_ASC], [$fieldNameDesc, SortOrder::SORT_DESC]]);
  196. $collection->expects($this->any())
  197. ->method('addFieldToFilter')
  198. ->willReturnMap([[$fieldNameAsc, [$fieldValueAsc => 'eq']], [$fieldNameDesc, [$fieldValueDesc => 'eq']]]);
  199. $this->bookmarkMock->expects($this->once())
  200. ->method('getCollection')
  201. ->willReturn($collection);
  202. $searchCriteria = $this->getMockBuilder('Magento\Framework\Api\SearchCriteriaInterface')
  203. ->getMockForAbstractClass();
  204. $searchCriteria->expects($this->once())
  205. ->method('getFilterGroups')
  206. ->willReturn([$filterGroup]);
  207. $searchCriteria->expects($this->once())
  208. ->method('getSortOrders')
  209. ->willReturn([$sortOrderAsc, $sortOrderDesc]);
  210. $this->assertEquals($this->searchResultsMock, $this->bookmarkRepository->getList($searchCriteria));
  211. }
  212. public function testDeleteById()
  213. {
  214. $bookmarkId = 1;
  215. $this->bookmarkMock->expects($this->once())
  216. ->method('getId')
  217. ->willReturn($bookmarkId);
  218. $this->bookmarkResourceMock->expects($this->once())
  219. ->method('load')
  220. ->with($this->bookmarkMock, $bookmarkId)
  221. ->willReturn($this->bookmarkMock);
  222. $this->bookmarkResourceMock->expects($this->once())
  223. ->method('delete')
  224. ->with($this->bookmarkMock);
  225. $this->assertTrue($this->bookmarkRepository->deleteById($bookmarkId));
  226. }
  227. }