123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Theme\Test\Unit\Model\Theme;
- use Magento\Framework\View\Design\ThemeInterface;
- use Magento\Theme\Model\Theme;
- use Magento\Theme\Model\Theme\Collection;
- class CollectionTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var Collection
- */
- private $model;
- /**
- * @var \Magento\Framework\Config\ThemeFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- private $themeConfigFactory;
- /**
- * @var \Magento\Framework\Filesystem\Directory\ReadInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $directory;
- /**
- * @var \Magento\Framework\Data\Collection\EntityFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- private $entityFactory;
- /**
- * @var \Magento\Framework\View\Design\Theme\ThemePackageList|\PHPUnit_Framework_MockObject_MockObject
- */
- private $themePackageList;
- /**
- * @var \Magento\Framework\Filesystem\Directory\ReadFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- private $readDirFactory;
- protected function setUp()
- {
- $this->entityFactory = $this->getMockBuilder(\Magento\Framework\Data\Collection\EntityFactory::class)
- ->disableOriginalConstructor()
- ->setMethods(['create'])
- ->getMock();
- $this->themeConfigFactory = $this->getMockBuilder(\Magento\Framework\Config\ThemeFactory::class)
- ->setMethods(['create'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->directory = $this->getMockBuilder(\Magento\Framework\Filesystem\Directory\ReadInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->themePackageList = $this->createMock(\Magento\Framework\View\Design\Theme\ThemePackageList::class);
- $this->readDirFactory = $this->createMock(\Magento\Framework\Filesystem\Directory\ReadFactory::class);
- $this->readDirFactory->expects($this->any())
- ->method('create')
- ->will($this->returnValue($this->directory));
- $this->model = new Collection(
- $this->entityFactory,
- $this->themeConfigFactory,
- $this->themePackageList,
- $this->readDirFactory
- );
- }
- /**
- * @test
- * @return void
- */
- public function testLoadData()
- {
- $fileContent = 'content file';
- $media = ['preview_image' => 'preview.jpg'];
- $themeTitle = 'Theme title';
- $themeConfigFile = 'theme.xml';
- $themeConfig = $this->getMockBuilder(
- \Magento\Framework\Config\Theme::class
- )->disableOriginalConstructor()->getMock();
- $theme = $this->getMockBuilder(Theme::class)->disableOriginalConstructor()->getMock();
- $parentTheme = ['parentThemeCode'];
- $parentThemePath = 'frontend/parent/theme';
- $themePackage = $this->createMock(\Magento\Framework\View\Design\Theme\ThemePackage::class);
- $themePackage->expects($this->any())
- ->method('getArea')
- ->will($this->returnValue('frontend'));
- $themePackage->expects($this->any())
- ->method('getVendor')
- ->will($this->returnValue('theme'));
- $themePackage->expects($this->any())
- ->method('getName')
- ->will($this->returnValue('code'));
- $this->themePackageList->expects($this->once())
- ->method('getThemes')
- ->will($this->returnValue([$themePackage]));
- $this->directory->expects($this->once())
- ->method('isExist')
- ->with($themeConfigFile)
- ->willReturn(true);
- $this->directory->expects($this->once())
- ->method('readFile')
- ->with($themeConfigFile)
- ->willReturn($fileContent);
- $this->themeConfigFactory->expects($this->once())
- ->method('create')
- ->with(['configContent' => $fileContent])
- ->willReturn($themeConfig);
- $this->entityFactory->expects($this->any())
- ->method('create')
- ->with(ThemeInterface::class)
- ->willReturn($theme);
- $themeConfig->expects($this->once())
- ->method('getMedia')
- ->willReturn($media);
- $themeConfig->expects($this->once())
- ->method('getParentTheme')
- ->willReturn($parentTheme);
- $themeConfig->expects($this->once())
- ->method('getThemeTitle')
- ->willReturn($themeTitle);
- $theme->expects($this->once())
- ->method('addData')
- ->with(
- [
- 'parent_id' => null,
- 'type' => ThemeInterface::TYPE_PHYSICAL,
- 'area' => 'frontend',
- 'theme_path' => 'theme/code',
- 'code' => 'theme/code',
- 'theme_title' => $themeTitle,
- 'preview_image' => $media['preview_image'],
- 'parent_theme_path' => 'theme/parentThemeCode'
- ]
- )
- ->willReturnSelf();
- $theme->expects($this->once())
- ->method('getData')
- ->with('parent_theme_path')
- ->willReturn($parentThemePath);
- $theme->expects($this->once())
- ->method('getArea')
- ->willReturn('frontend');
- $this->assertInstanceOf(get_class($this->model), $this->model->loadData());
- }
- /**
- * @expectedException \UnexpectedValueException
- * @expectedExceptionMessage Constraint 'unsupported_type' is not supported
- */
- public function testAddConstraintUnsupportedType()
- {
- $this->model->addConstraint('unsupported_type', 'value');
- }
- /**
- * @param array $inputValues
- * @param array $expected
- *
- * @dataProvider addConstraintDataProvider
- */
- public function testAddConstraint(array $inputValues, array $expected)
- {
- foreach ($inputValues as $data) {
- $type = $data[0];
- $value = $data[1];
- $this->model->addConstraint($type, $value);
- }
- $default = [
- Collection::CONSTRAINT_AREA => [],
- Collection::CONSTRAINT_VENDOR => [],
- Collection::CONSTRAINT_THEME_NAME => []
- ];
- $expected = array_merge($default, $expected);
- $this->assertAttributeSame($expected, 'constraints', $this->model);
- }
- /**
- * @return array
- */
- public function addConstraintDataProvider()
- {
- return [
- 'area' => [
- [[Collection::CONSTRAINT_AREA, 'area']],
- [Collection::CONSTRAINT_AREA => ['area']]
- ],
- 'vendor' => [
- [[Collection::CONSTRAINT_VENDOR, 'Vendor']],
- [Collection::CONSTRAINT_VENDOR => ['Vendor']]
- ],
- 'theme name' => [
- [[Collection::CONSTRAINT_THEME_NAME, 'theme_name']],
- [Collection::CONSTRAINT_THEME_NAME => ['theme_name']]
- ],
- 'area, vendor and theme name' => [
- [
- [Collection::CONSTRAINT_AREA, 'area_one'],
- [Collection::CONSTRAINT_AREA, 'area_two'],
- [Collection::CONSTRAINT_VENDOR, 'Vendor'],
- [Collection::CONSTRAINT_VENDOR, 'Vendor'],
- [Collection::CONSTRAINT_THEME_NAME, 'theme_name']
- ],
- [
- Collection::CONSTRAINT_AREA => ['area_one', 'area_two'],
- Collection::CONSTRAINT_VENDOR => ['Vendor'],
- Collection::CONSTRAINT_THEME_NAME => ['theme_name']
- ]
- ],
- ];
- }
- }
|