DesignConfigRepositoryTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Theme\Test\Unit\Model;
  7. use Magento\Theme\Model\Data\Design\Config;
  8. use Magento\Theme\Model\DesignConfigRepository;
  9. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  10. class DesignConfigRepositoryTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /** @var \Magento\Theme\Model\Design\Config\Storage|\PHPUnit_Framework_MockObject_MockObject */
  13. protected $configStorage;
  14. /** @var \Magento\Framework\App\Config\ReinitableConfigInterface|\PHPUnit_Framework_MockObject_MockObject */
  15. protected $reinitableConfig;
  16. /** @var \Magento\Framework\Indexer\IndexerRegistry|\PHPUnit_Framework_MockObject_MockObject */
  17. protected $indexerRegistry;
  18. /** @var \Magento\Theme\Api\Data\DesignConfigInterface|\PHPUnit_Framework_MockObject_MockObject */
  19. protected $designConfig;
  20. /** @var \Magento\Theme\Api\Data\DesignConfigExtensionInterface|\PHPUnit_Framework_MockObject_MockObject */
  21. protected $designExtension;
  22. /** @var \Magento\Theme\Api\Data\DesignConfigDataInterface|\PHPUnit_Framework_MockObject_MockObject */
  23. protected $designConfigData;
  24. /** @var \Magento\Framework\Indexer\IndexerInterface|\PHPUnit_Framework_MockObject_MockObject */
  25. protected $indexer;
  26. /** @var DesignConfigRepository */
  27. protected $repository;
  28. /**
  29. * @var \PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $validator;
  32. public function setUp()
  33. {
  34. $this->configStorage = $this->createMock(\Magento\Theme\Model\Design\Config\Storage::class);
  35. $this->reinitableConfig = $this->getMockForAbstractClass(
  36. \Magento\Framework\App\Config\ReinitableConfigInterface::class,
  37. [],
  38. '',
  39. false
  40. );
  41. $this->indexerRegistry = $this->createMock(\Magento\Framework\Indexer\IndexerRegistry::class);
  42. $this->designConfig = $this->getMockForAbstractClass(
  43. \Magento\Theme\Api\Data\DesignConfigInterface::class,
  44. [],
  45. '',
  46. false
  47. );
  48. $this->designExtension = $this->getMockForAbstractClass(
  49. \Magento\Theme\Api\Data\DesignConfigExtensionInterface::class,
  50. [],
  51. '',
  52. false,
  53. false,
  54. true,
  55. ['getDesignConfigData']
  56. );
  57. $this->designConfigData = $this->getMockForAbstractClass(
  58. \Magento\Theme\Api\Data\DesignConfigDataInterface::class,
  59. [],
  60. '',
  61. false
  62. );
  63. $this->indexer = $this->getMockForAbstractClass(
  64. \Magento\Framework\Indexer\IndexerInterface::class,
  65. [],
  66. '',
  67. false
  68. );
  69. $this->validator = $this->createMock(\Magento\Theme\Model\Design\Config\Validator::class);
  70. $objectManagerHelper = new ObjectManager($this);
  71. $this->repository = $objectManagerHelper->getObject(
  72. DesignConfigRepository::class,
  73. [
  74. 'configStorage' => $this->configStorage,
  75. 'reinitableConfig' => $this->reinitableConfig,
  76. 'indexerRegistry' => $this->indexerRegistry,
  77. 'validator' => $this->validator
  78. ]
  79. );
  80. }
  81. public function testSave()
  82. {
  83. $this->designConfig->expects($this->exactly(2))
  84. ->method('getExtensionAttributes')
  85. ->willReturn($this->designExtension);
  86. $this->designExtension->expects($this->once())
  87. ->method('getDesignConfigData')
  88. ->willReturn([$this->designConfigData]);
  89. $this->configStorage->expects($this->once())
  90. ->method('save')
  91. ->willReturn($this->designConfig);
  92. $this->reinitableConfig->expects($this->once())
  93. ->method('reinit');
  94. $this->indexerRegistry->expects($this->once())
  95. ->method('get')
  96. ->with(Config::DESIGN_CONFIG_GRID_INDEXER_ID)
  97. ->willReturn($this->indexer);
  98. $this->indexer->expects($this->once())
  99. ->method('reindexAll');
  100. $this->validator->expects($this->once())->method('validate')->with($this->designConfig);
  101. $this->assertSame($this->designConfig, $this->repository->save($this->designConfig));
  102. }
  103. /**
  104. * @expectedExceptionMessage The config can't be saved because it's empty. Complete the config and try again.
  105. * @expectedException \Magento\Framework\Exception\LocalizedException
  106. */
  107. public function testSaveWithoutConfig()
  108. {
  109. $this->designConfig->expects($this->exactly(2))
  110. ->method('getExtensionAttributes')
  111. ->willReturn($this->designExtension);
  112. $this->designExtension->expects($this->once())
  113. ->method('getDesignConfigData')
  114. ->willReturn(false);
  115. $this->repository->save($this->designConfig);
  116. }
  117. public function testDelete()
  118. {
  119. $this->designConfig->expects($this->exactly(2))
  120. ->method('getExtensionAttributes')
  121. ->willReturn($this->designExtension);
  122. $this->designExtension->expects($this->once())
  123. ->method('getDesignConfigData')
  124. ->willReturn([$this->designConfigData]);
  125. $this->configStorage->expects($this->once())
  126. ->method('delete')
  127. ->with($this->designConfig);
  128. $this->reinitableConfig->expects($this->once())
  129. ->method('reinit');
  130. $this->indexerRegistry->expects($this->once())
  131. ->method('get')
  132. ->with(Config::DESIGN_CONFIG_GRID_INDEXER_ID)
  133. ->willReturn($this->indexer);
  134. $this->indexer->expects($this->once())
  135. ->method('reindexAll');
  136. $this->assertSame($this->designConfig, $this->repository->delete($this->designConfig));
  137. }
  138. }