PageRepositoryTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Cms\Test\Unit\Model;
  7. use Magento\Cms\Model\PageRepository;
  8. use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
  9. /**
  10. * Test for Magento\Cms\Model\PageRepository
  11. *
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. */
  14. class PageRepositoryTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /**
  17. * @var PageRepository
  18. */
  19. protected $repository;
  20. /**
  21. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Cms\Model\ResourceModel\Page
  22. */
  23. protected $pageResource;
  24. /**
  25. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Cms\Model\Page
  26. */
  27. protected $page;
  28. /**
  29. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Cms\Api\Data\PageInterface
  30. */
  31. protected $pageData;
  32. /**
  33. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Cms\Api\Data\PageSearchResultsInterface
  34. */
  35. protected $pageSearchResult;
  36. /**
  37. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Api\DataObjectHelper
  38. */
  39. protected $dataHelper;
  40. /**
  41. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Reflection\DataObjectProcessor
  42. */
  43. protected $dataObjectProcessor;
  44. /**
  45. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Cms\Model\ResourceModel\Page\Collection
  46. */
  47. protected $collection;
  48. /**
  49. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Store\Model\StoreManagerInterface
  50. */
  51. private $storeManager;
  52. /**
  53. * @var CollectionProcessorInterface|\PHPUnit_Framework_MockObject_MockObject
  54. */
  55. private $collectionProcessor;
  56. /**
  57. * Initialize repository
  58. */
  59. protected function setUp()
  60. {
  61. $this->pageResource = $this->getMockBuilder(\Magento\Cms\Model\ResourceModel\Page::class)
  62. ->disableOriginalConstructor()
  63. ->getMock();
  64. $this->dataObjectProcessor = $this->getMockBuilder(\Magento\Framework\Reflection\DataObjectProcessor::class)
  65. ->disableOriginalConstructor()
  66. ->getMock();
  67. $pageFactory = $this->getMockBuilder(\Magento\Cms\Model\PageFactory::class)
  68. ->disableOriginalConstructor()
  69. ->setMethods(['create'])
  70. ->getMock();
  71. $pageDataFactory = $this->getMockBuilder(\Magento\Cms\Api\Data\PageInterfaceFactory::class)
  72. ->disableOriginalConstructor()
  73. ->setMethods(['create'])
  74. ->getMock();
  75. $pageSearchResultFactory = $this->getMockBuilder(\Magento\Cms\Api\Data\PageSearchResultsInterfaceFactory::class)
  76. ->disableOriginalConstructor()
  77. ->setMethods(['create'])
  78. ->getMock();
  79. $collectionFactory = $this->getMockBuilder(\Magento\Cms\Model\ResourceModel\Page\CollectionFactory::class)
  80. ->disableOriginalConstructor()
  81. ->setMethods(['create'])
  82. ->getMock();
  83. $this->storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
  84. ->disableOriginalConstructor()
  85. ->getMock();
  86. $store = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
  87. ->disableOriginalConstructor()
  88. ->getMock();
  89. $store->expects($this->any())->method('getId')->willReturn(0);
  90. $this->storeManager->expects($this->any())->method('getStore')->willReturn($store);
  91. $this->page = $this->getMockBuilder(\Magento\Cms\Model\Page::class)->disableOriginalConstructor()->getMock();
  92. $this->pageData = $this->getMockBuilder(\Magento\Cms\Api\Data\PageInterface::class)
  93. ->getMock();
  94. $this->pageSearchResult = $this->getMockBuilder(\Magento\Cms\Api\Data\PageSearchResultsInterface::class)
  95. ->getMock();
  96. $this->collection = $this->getMockBuilder(\Magento\Cms\Model\ResourceModel\Page\Collection::class)
  97. ->disableOriginalConstructor()
  98. ->setMethods(['getSize', 'setCurPage', 'setPageSize', 'load', 'addOrder'])
  99. ->getMock();
  100. $pageFactory->expects($this->any())
  101. ->method('create')
  102. ->willReturn($this->page);
  103. $pageDataFactory->expects($this->any())
  104. ->method('create')
  105. ->willReturn($this->pageData);
  106. $pageSearchResultFactory->expects($this->any())
  107. ->method('create')
  108. ->willReturn($this->pageSearchResult);
  109. $collectionFactory->expects($this->any())
  110. ->method('create')
  111. ->willReturn($this->collection);
  112. /**
  113. * @var \Magento\Cms\Model\PageFactory $pageFactory
  114. * @var \Magento\Cms\Api\Data\PageInterfaceFactory $pageDataFactory
  115. * @var \Magento\Cms\Api\Data\PageSearchResultsInterfaceFactory $pageSearchResultFactory
  116. * @var \Magento\Cms\Model\ResourceModel\Page\CollectionFactory $collectionFactory
  117. */
  118. $this->dataHelper = $this->getMockBuilder(\Magento\Framework\Api\DataObjectHelper::class)
  119. ->disableOriginalConstructor()
  120. ->getMock();
  121. $this->collectionProcessor = $this->getMockBuilder(CollectionProcessorInterface::class)
  122. ->getMockForAbstractClass();
  123. $this->repository = new PageRepository(
  124. $this->pageResource,
  125. $pageFactory,
  126. $pageDataFactory,
  127. $collectionFactory,
  128. $pageSearchResultFactory,
  129. $this->dataHelper,
  130. $this->dataObjectProcessor,
  131. $this->storeManager,
  132. $this->collectionProcessor
  133. );
  134. }
  135. /**
  136. * @test
  137. */
  138. public function testSave()
  139. {
  140. $this->pageResource->expects($this->once())
  141. ->method('save')
  142. ->with($this->page)
  143. ->willReturnSelf();
  144. $this->assertEquals($this->page, $this->repository->save($this->page));
  145. }
  146. /**
  147. * @test
  148. */
  149. public function testDeleteById()
  150. {
  151. $pageId = '123';
  152. $this->page->expects($this->once())
  153. ->method('getId')
  154. ->willReturn(true);
  155. $this->page->expects($this->once())
  156. ->method('load')
  157. ->with($pageId)
  158. ->willReturnSelf();
  159. $this->pageResource->expects($this->once())
  160. ->method('delete')
  161. ->with($this->page)
  162. ->willReturnSelf();
  163. $this->assertTrue($this->repository->deleteById($pageId));
  164. }
  165. /**
  166. * @test
  167. *
  168. * @expectedException \Magento\Framework\Exception\CouldNotSaveException
  169. */
  170. public function testSaveException()
  171. {
  172. $this->pageResource->expects($this->once())
  173. ->method('save')
  174. ->with($this->page)
  175. ->willThrowException(new \Exception());
  176. $this->repository->save($this->page);
  177. }
  178. /**
  179. * @test
  180. *
  181. * @expectedException \Magento\Framework\Exception\CouldNotDeleteException
  182. */
  183. public function testDeleteException()
  184. {
  185. $this->pageResource->expects($this->once())
  186. ->method('delete')
  187. ->with($this->page)
  188. ->willThrowException(new \Exception());
  189. $this->repository->delete($this->page);
  190. }
  191. /**
  192. * @test
  193. *
  194. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  195. */
  196. public function testGetByIdException()
  197. {
  198. $pageId = '123';
  199. $this->page->expects($this->once())
  200. ->method('getId')
  201. ->willReturn(false);
  202. $this->page->expects($this->once())
  203. ->method('load')
  204. ->with($pageId)
  205. ->willReturnSelf();
  206. $this->repository->getById($pageId);
  207. }
  208. /**
  209. * @test
  210. */
  211. public function testGetList()
  212. {
  213. $total = 10;
  214. /** @var \Magento\Framework\Api\SearchCriteriaInterface $criteria */
  215. $criteria = $this->getMockBuilder(\Magento\Framework\Api\SearchCriteriaInterface::class)->getMock();
  216. $this->collection->addItem($this->page);
  217. $this->collection->expects($this->once())
  218. ->method('getSize')
  219. ->willReturn($total);
  220. $this->collectionProcessor->expects($this->once())
  221. ->method('process')
  222. ->with($criteria, $this->collection)
  223. ->willReturnSelf();
  224. $this->pageSearchResult->expects($this->once())
  225. ->method('setSearchCriteria')
  226. ->with($criteria)
  227. ->willReturnSelf();
  228. $this->pageSearchResult->expects($this->once())
  229. ->method('setTotalCount')
  230. ->with($total)
  231. ->willReturnSelf();
  232. $this->pageSearchResult->expects($this->once())
  233. ->method('setItems')
  234. ->with([$this->page])
  235. ->willReturnSelf();
  236. $this->assertEquals($this->pageSearchResult, $this->repository->getList($criteria));
  237. }
  238. }