BlockRepositoryTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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\BlockRepository;
  8. use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
  9. /**
  10. * Test for Magento\Cms\Model\BlockRepository
  11. *
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. */
  14. class BlockRepositoryTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /**
  17. * @var BlockRepository
  18. */
  19. protected $repository;
  20. /**
  21. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Cms\Model\ResourceModel\Block
  22. */
  23. protected $blockResource;
  24. /**
  25. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Cms\Model\Block
  26. */
  27. protected $block;
  28. /**
  29. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Cms\Api\Data\BlockInterface
  30. */
  31. protected $blockData;
  32. /**
  33. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Cms\Api\Data\BlockSearchResultsInterface
  34. */
  35. protected $blockSearchResult;
  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\Block\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->blockResource = $this->getMockBuilder(\Magento\Cms\Model\ResourceModel\Block::class)
  62. ->disableOriginalConstructor()
  63. ->getMock();
  64. $this->dataObjectProcessor = $this->getMockBuilder(\Magento\Framework\Reflection\DataObjectProcessor::class)
  65. ->disableOriginalConstructor()
  66. ->getMock();
  67. $blockFactory = $this->getMockBuilder(\Magento\Cms\Model\BlockFactory::class)
  68. ->disableOriginalConstructor()
  69. ->setMethods(['create'])
  70. ->getMock();
  71. $blockDataFactory = $this->getMockBuilder(\Magento\Cms\Api\Data\BlockInterfaceFactory::class)
  72. ->disableOriginalConstructor()
  73. ->setMethods(['create'])
  74. ->getMock();
  75. $blockSearchResultFactory = $this->getMockBuilder(
  76. \Magento\Cms\Api\Data\BlockSearchResultsInterfaceFactory::class
  77. )
  78. ->disableOriginalConstructor()
  79. ->setMethods(['create'])
  80. ->getMock();
  81. $collectionFactory = $this->getMockBuilder(\Magento\Cms\Model\ResourceModel\Block\CollectionFactory::class)
  82. ->disableOriginalConstructor()
  83. ->setMethods(['create'])
  84. ->getMock();
  85. $this->storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
  86. ->disableOriginalConstructor()
  87. ->getMock();
  88. $store = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
  89. ->disableOriginalConstructor()
  90. ->getMock();
  91. $store->expects($this->any())->method('getId')->willReturn(0);
  92. $this->storeManager->expects($this->any())->method('getStore')->willReturn($store);
  93. $this->block = $this->getMockBuilder(\Magento\Cms\Model\Block::class)->disableOriginalConstructor()->getMock();
  94. $this->blockData = $this->getMockBuilder(\Magento\Cms\Api\Data\BlockInterface::class)
  95. ->getMock();
  96. $this->blockSearchResult = $this->getMockBuilder(\Magento\Cms\Api\Data\BlockSearchResultsInterface::class)
  97. ->getMock();
  98. $this->collection = $this->getMockBuilder(\Magento\Cms\Model\ResourceModel\Block\Collection::class)
  99. ->disableOriginalConstructor()
  100. ->setMethods(['addFieldToFilter', 'getSize', 'setCurPage', 'setPageSize', 'load', 'addOrder'])
  101. ->getMock();
  102. $blockFactory->expects($this->any())
  103. ->method('create')
  104. ->willReturn($this->block);
  105. $blockDataFactory->expects($this->any())
  106. ->method('create')
  107. ->willReturn($this->blockData);
  108. $blockSearchResultFactory->expects($this->any())
  109. ->method('create')
  110. ->willReturn($this->blockSearchResult);
  111. $collectionFactory->expects($this->any())
  112. ->method('create')
  113. ->willReturn($this->collection);
  114. /**
  115. * @var \Magento\Cms\Model\BlockFactory $blockFactory
  116. * @var \Magento\Cms\Api\Data\BlockInterfaceFactory $blockDataFactory
  117. * @var \Magento\Cms\Api\Data\BlockSearchResultsInterfaceFactory $blockSearchResultFactory
  118. * @var \Magento\Cms\Model\ResourceModel\Block\CollectionFactory $collectionFactory
  119. */
  120. $this->dataHelper = $this->getMockBuilder(\Magento\Framework\Api\DataObjectHelper::class)
  121. ->disableOriginalConstructor()
  122. ->getMock();
  123. $this->collectionProcessor = $this->getMockBuilder(CollectionProcessorInterface::class)
  124. ->getMockForAbstractClass();
  125. $this->repository = new BlockRepository(
  126. $this->blockResource,
  127. $blockFactory,
  128. $blockDataFactory,
  129. $collectionFactory,
  130. $blockSearchResultFactory,
  131. $this->dataHelper,
  132. $this->dataObjectProcessor,
  133. $this->storeManager,
  134. $this->collectionProcessor
  135. );
  136. }
  137. /**
  138. * @test
  139. */
  140. public function testSave()
  141. {
  142. $this->blockResource->expects($this->once())
  143. ->method('save')
  144. ->with($this->block)
  145. ->willReturnSelf();
  146. $this->assertEquals($this->block, $this->repository->save($this->block));
  147. }
  148. /**
  149. * @test
  150. */
  151. public function testDeleteById()
  152. {
  153. $blockId = '123';
  154. $this->block->expects($this->once())
  155. ->method('getId')
  156. ->willReturn(true);
  157. $this->blockResource->expects($this->once())
  158. ->method('load')
  159. ->with($this->block, $blockId)
  160. ->willReturn($this->block);
  161. $this->blockResource->expects($this->once())
  162. ->method('delete')
  163. ->with($this->block)
  164. ->willReturnSelf();
  165. $this->assertTrue($this->repository->deleteById($blockId));
  166. }
  167. /**
  168. * @test
  169. *
  170. * @expectedException \Magento\Framework\Exception\CouldNotSaveException
  171. */
  172. public function testSaveException()
  173. {
  174. $this->blockResource->expects($this->once())
  175. ->method('save')
  176. ->with($this->block)
  177. ->willThrowException(new \Exception());
  178. $this->repository->save($this->block);
  179. }
  180. /**
  181. * @test
  182. *
  183. * @expectedException \Magento\Framework\Exception\CouldNotDeleteException
  184. */
  185. public function testDeleteException()
  186. {
  187. $this->blockResource->expects($this->once())
  188. ->method('delete')
  189. ->with($this->block)
  190. ->willThrowException(new \Exception());
  191. $this->repository->delete($this->block);
  192. }
  193. /**
  194. * @test
  195. *
  196. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  197. */
  198. public function testGetByIdException()
  199. {
  200. $blockId = '123';
  201. $this->block->expects($this->once())
  202. ->method('getId')
  203. ->willReturn(false);
  204. $this->blockResource->expects($this->once())
  205. ->method('load')
  206. ->with($this->block, $blockId)
  207. ->willReturn($this->block);
  208. $this->repository->getById($blockId);
  209. }
  210. /**
  211. * @test
  212. */
  213. public function testGetList()
  214. {
  215. $total = 10;
  216. /** @var \Magento\Framework\Api\SearchCriteriaInterface $criteria */
  217. $criteria = $this->getMockBuilder(\Magento\Framework\Api\SearchCriteriaInterface::class)->getMock();
  218. $this->collection->addItem($this->block);
  219. $this->collection->expects($this->once())
  220. ->method('getSize')
  221. ->willReturn($total);
  222. $this->collectionProcessor->expects($this->once())
  223. ->method('process')
  224. ->with($criteria, $this->collection)
  225. ->willReturnSelf();
  226. $this->blockSearchResult->expects($this->once())
  227. ->method('setSearchCriteria')
  228. ->with($criteria)
  229. ->willReturnSelf();
  230. $this->blockSearchResult->expects($this->once())
  231. ->method('setTotalCount')
  232. ->with($total)
  233. ->willReturnSelf();
  234. $this->blockSearchResult->expects($this->once())
  235. ->method('setItems')
  236. ->with([$this->block])
  237. ->willReturnSelf();
  238. $this->assertEquals($this->blockSearchResult, $this->repository->getList($criteria));
  239. }
  240. }