123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Theme\Test\Unit\Model\Theme;
- use Magento\Framework\App\Area;
- use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
- use Magento\Framework\View\Design\ThemeInterface;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class ThemeProviderTest extends \PHPUnit\Framework\TestCase
- {
- /** Theme path used by tests */
- const THEME_PATH = 'frontend/Magento/luma';
- /** Theme ID used by tests */
- const THEME_ID = 755;
- /** @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager */
- private $objectManager;
- /** @var \Magento\Theme\Model\ResourceModel\Theme\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject */
- private $collectionFactory;
- /** @var \Magento\Theme\Model\ThemeFactory|\PHPUnit_Framework_MockObject_MockObject */
- private $themeFactory;
- /** @var \Magento\Framework\App\CacheInterface|\PHPUnit_Framework_MockObject_MockObject */
- private $cache;
- /** @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject */
- private $serializer;
- /** @var \Magento\Theme\Model\Theme\ThemeProvider|\PHPUnit_Framework_MockObject_MockObject */
- private $themeProvider;
- /** @var \Magento\Theme\Model\Theme|\PHPUnit_Framework_MockObject_MockObject */
- private $theme;
- protected function setUp()
- {
- $this->objectManager = new ObjectManagerHelper($this);
- $this->collectionFactory = $this->createPartialMock(
- \Magento\Theme\Model\ResourceModel\Theme\CollectionFactory::class,
- ['create']
- );
- $this->themeFactory = $this->createPartialMock(\Magento\Theme\Model\ThemeFactory::class, ['create']);
- $this->cache = $this->getMockBuilder(\Magento\Framework\App\CacheInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->serializer = $this->createMock(\Magento\Framework\Serialize\Serializer\Json::class);
- $this->themeProvider = $this->objectManager->getObject(
- \Magento\Theme\Model\Theme\ThemeProvider::class,
- [
- 'collectionFactory' => $this->collectionFactory,
- 'themeFactory' => $this->themeFactory,
- 'cache' => $this->cache,
- 'serializer' => $this->serializer
- ]
- );
- $this->theme = $this->createMock(\Magento\Theme\Model\Theme::class);
- }
- public function testGetByFullPath()
- {
- $themeArray = ['theme_data' => 'theme_data'];
- $this->theme->expects($this->exactly(2))
- ->method('getId')
- ->willReturn(self::THEME_ID);
- $this->theme->expects($this->exactly(2))
- ->method('toArray')
- ->willReturn($themeArray);
- $collectionMock = $this->createMock(\Magento\Theme\Model\ResourceModel\Theme\Collection::class);
- $collectionMock->expects($this->once())
- ->method('getThemeByFullPath')
- ->with(self::THEME_PATH)
- ->willReturn($this->theme);
- $this->collectionFactory->expects($this->once())
- ->method('create')
- ->willReturn($collectionMock);
- $this->serializer->expects($this->exactly(2))
- ->method('serialize')
- ->with($themeArray)
- ->willReturn('serialized theme');
- $deploymentConfig = $this->getMockBuilder(\Magento\Framework\App\DeploymentConfig::class)
- ->disableOriginalConstructor()
- ->getMock();
- $deploymentConfig->expects($this->once())
- ->method('isDbAvailable')
- ->willReturn(true);
- $objectManagerMock = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
- $objectManagerMock->expects($this->any())
- ->method('get')
- ->willReturnMap([
- [\Magento\Framework\App\DeploymentConfig::class, $deploymentConfig],
- ]);
- \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock);
- $this->assertSame(
- $this->theme,
- $this->themeProvider->getThemeByFullPath(self::THEME_PATH),
- 'Unable to load Theme'
- );
- $this->assertSame(
- $this->theme,
- $this->themeProvider->getThemeByFullPath(self::THEME_PATH),
- 'Unable to load Theme from object cache'
- );
- }
- public function testGetByFullPathWithCache()
- {
- $deploymentConfig = $this->getMockBuilder(\Magento\Framework\App\DeploymentConfig::class)
- ->disableOriginalConstructor()
- ->getMock();
- $deploymentConfig->expects($this->once())
- ->method('isDbAvailable')
- ->willReturn(true);
- $objectManagerMock = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
- $objectManagerMock->expects($this->any())
- ->method('get')
- ->willReturnMap([
- [\Magento\Framework\App\DeploymentConfig::class, $deploymentConfig],
- ]);
- \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock);
- $serializedTheme = '{"theme_data":"theme_data"}';
- $themeArray = ['theme_data' => 'theme_data'];
- $this->theme->expects($this->once())
- ->method('populateFromArray')
- ->with($themeArray)
- ->willReturnSelf();
- $this->themeFactory->expects($this->once())
- ->method('create')
- ->willReturn($this->theme);
- $this->serializer->expects($this->once())
- ->method('unserialize')
- ->with($serializedTheme)
- ->willReturn($themeArray);
- $this->cache->expects($this->once())
- ->method('load')
- ->with('theme' . self::THEME_PATH)
- ->willReturn($serializedTheme);
- $this->assertSame(
- $this->theme,
- $this->themeProvider->getThemeByFullPath(self::THEME_PATH),
- 'Unable to load Theme from application cache'
- );
- $this->assertSame(
- $this->theme,
- $this->themeProvider->getThemeByFullPath(self::THEME_PATH),
- 'Unable to load Theme from object cache'
- );
- }
- public function testGetById()
- {
- $themeArray = ['theme_data' => 'theme_data'];
- $this->theme->expects($this->once())
- ->method('load')
- ->with(self::THEME_ID)
- ->willReturnSelf();
- $this->theme->expects($this->once())
- ->method('getId')
- ->willReturn(self::THEME_ID);
- $this->theme->expects($this->once())
- ->method('toArray')
- ->willReturn($themeArray);
- $this->themeFactory->expects($this->once())->method('create')->will($this->returnValue($this->theme));
- $this->cache->expects($this->once())
- ->method('load')
- ->with('theme-by-id-' . self::THEME_ID)
- ->willReturn(false);
- $this->serializer->expects($this->once())
- ->method('serialize')
- ->with($themeArray)
- ->willReturn('{"theme_data":"theme_data"}');
- $this->assertSame(
- $this->theme,
- $this->themeProvider->getThemeById(self::THEME_ID),
- 'Unable to load Theme'
- );
- $this->assertSame(
- $this->theme,
- $this->themeProvider->getThemeById(self::THEME_ID),
- 'Unable to load Theme from object cache'
- );
- }
- public function testGetByIdWithCache()
- {
- $serializedTheme = '{"theme_data":"theme_data"}';
- $themeArray = ['theme_data' => 'theme_data'];
- $this->theme->expects($this->once())
- ->method('populateFromArray')
- ->with($themeArray)
- ->willReturnSelf();
- $this->cache->expects($this->once())
- ->method('load')
- ->with('theme-by-id-' . self::THEME_ID)
- ->willReturn($serializedTheme);
- $this->serializer->expects($this->once())
- ->method('unserialize')
- ->with($serializedTheme)
- ->willReturn($themeArray);
- $this->themeFactory->expects($this->once())
- ->method('create')
- ->willReturn($this->theme);
- $this->assertSame(
- $this->theme,
- $this->themeProvider->getThemeById(self::THEME_ID),
- 'Unable to load Theme from application cache'
- );
- $this->assertSame(
- $this->theme,
- $this->themeProvider->getThemeById(self::THEME_ID),
- 'Unable to load Theme from object cache'
- );
- }
- public function testGetThemeCustomizations()
- {
- $collection = $this->getMockBuilder(\Magento\Theme\Model\ResourceModel\Theme\Collection::class)
- ->disableOriginalConstructor()
- ->getMock();
- $collection->expects($this->once())
- ->method('addAreaFilter')
- ->with(Area::AREA_FRONTEND)
- ->willReturnSelf();
- $collection->expects($this->once())
- ->method('addTypeFilter')
- ->with(ThemeInterface::TYPE_VIRTUAL)
- ->willReturnSelf();
- $this->collectionFactory->expects($this->once())
- ->method('create')
- ->willReturn($collection);
- $this->assertInstanceOf(get_class($collection), $this->themeProvider->getThemeCustomizations());
- }
- }
|