Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ThemeModularTest.php
Go to the documentation of this file.
1 <?php
7 
12 
13 class ThemeModularTest extends \PHPUnit\Framework\TestCase
14 {
18  private $model;
19 
23  private $themeDirectory;
24 
28  private $fileFactory;
29 
34 
38  private $readDirFactory;
39 
43  private $componentRegistrar;
44 
45  protected function setUp()
46  {
47  $this->themeDirectory = $this->createPartialMock(
48  \Magento\Framework\Filesystem\Directory\Read::class,
49  ['getAbsolutePath', 'search']
50  );
51  $this->pathPatternHelperMock = $this->getMockBuilder(\Magento\Framework\View\Helper\PathPattern::class)
52  ->disableOriginalConstructor()
53  ->getMock();
54  $this->fileFactory = $this->createMock(\Magento\Framework\View\File\Factory::class);
55  $this->readDirFactory = $this->createMock(\Magento\Framework\Filesystem\Directory\ReadFactory::class);
56  $this->readDirFactory->expects($this->any())
57  ->method('create')
58  ->will($this->returnValue($this->themeDirectory));
59  $this->componentRegistrar = $this->getMockForAbstractClass(
60  \Magento\Framework\Component\ComponentRegistrarInterface::class
61  );
62  $this->model = new \Magento\Framework\View\File\Collector\ThemeModular(
63  $this->fileFactory,
64  $this->readDirFactory,
65  $this->componentRegistrar,
66  $this->pathPatternHelperMock,
67  'subdir'
68  );
69  }
70 
71  public function testGetFilesWrongTheme()
72  {
73  $this->componentRegistrar->expects($this->once())
74  ->method('getPath')
75  ->will($this->returnValue(''));
76  $theme = $this->getMockForAbstractClass(\Magento\Framework\View\Design\ThemeInterface::class);
77  $theme->expects($this->once())
78  ->method('getFullPath')
79  ->will($this->returnValue('area/Vendor/theme'));
80  $this->assertSame([], $this->model->getFiles($theme, ''));
81  }
82 
90  public function testGetFiles($files, $filePath, $pathPattern)
91  {
92  $theme = $this->getMockForAbstractClass(\Magento\Framework\View\Design\ThemeInterface::class);
93  $themePath = 'area/theme/path';
94  $theme->expects($this->once())->method('getFullPath')->willReturn($themePath);
95 
96  $handlePath = 'design/area/theme/path/%s/subdir/%s';
97  $returnKeys = [];
98  foreach ($files as $file) {
99  $returnKeys[] = sprintf($handlePath, $file['module'], $file['handle']);
100  }
101 
102  $this->componentRegistrar->expects($this->once())
103  ->method('getPath')
104  ->with(ComponentRegistrar::THEME, $themePath)
105  ->will($this->returnValue('/full/theme/path'));
106  $this->pathPatternHelperMock->expects($this->any())
107  ->method('translatePatternFromGlob')
108  ->with($filePath)
109  ->willReturn($pathPattern);
110  $this->themeDirectory->expects($this->once())
111  ->method('search')
112  ->willReturn($returnKeys);
113  $this->themeDirectory->expects($this->any())
114  ->method('getAbsolutePath')
115  ->willReturnArgument(0);
116 
117  $checkResult = [];
118  foreach ($files as $key => $file) {
119  $checkResult[$key] = new \Magento\Framework\View\File($file['handle'], $file['module'], $theme);
120  $checkResult[$key] = $this->createMock(\Magento\Framework\View\File::class);
121  $this->fileFactory
122  ->expects($this->at($key))
123  ->method('create')
124  ->with(sprintf($handlePath, $file['module'], $file['handle']), $file['module'], $theme)
125  ->willReturn($checkResult[$key]);
126  }
127  $this->assertSame($checkResult, $this->model->getFiles($theme, $filePath));
128  }
129 
133  public function getFilesDataProvider()
134  {
135  return [
136  [
137  [
138  ['handle' => '1.xml', 'module' => 'Module_One'],
139  ['handle' => '2.xml', 'module' => 'Module_One'],
140  ['handle' => '3.xml', 'module' => 'Module_Two'],
141  ],
142  '*.xml',
143  '[^/]*\\.xml'
144  ],
145  [
146  [
147  ['handle' => 'preset/4', 'module' => 'Module_Fourth'],
148  ],
149  'preset/4',
150  'preset/4'
151  ],
152  ];
153  }
154 }
$pathPattern
$theme
foreach($appDirs as $dir) $files