Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ThemePackageInfoTest.php
Go to the documentation of this file.
1 <?php
7 
9 
10 class ThemePackageInfoTest extends \PHPUnit\Framework\TestCase
11 {
15  private $dirRead;
16 
20  private $themePackageInfo;
21 
25  private $componentRegistrar;
26 
30  private $dirReadFactory;
31 
33  private $serializerMock;
34 
35  protected function setUp()
36  {
37  $this->componentRegistrar = $this->createMock(\Magento\Framework\Component\ComponentRegistrar::class);
38  $this->dirRead = $this->createMock(\Magento\Framework\Filesystem\Directory\Read::class);
39  $this->dirReadFactory = $this->createMock(\Magento\Framework\Filesystem\Directory\ReadFactory::class);
40  $this->dirReadFactory->expects($this->any())->method('create')->willReturn($this->dirRead);
41  $this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
42  ->getMock();
43  $this->themePackageInfo = new ThemePackageInfo(
44  $this->componentRegistrar,
45  $this->dirReadFactory,
46  $this->serializerMock
47  );
48  }
49 
50  public function testGetPackageName()
51  {
52  $themeFileContents = '{"name": "package"}';
53  $this->componentRegistrar->expects($this->once())->method('getPath')->willReturn('path/to/A');
54  $this->dirRead->expects($this->once())->method('isExist')->with('composer.json')->willReturn(true);
55  $this->dirRead->expects($this->once())
56  ->method('readFile')
57  ->with('composer.json')
58  ->willReturn($themeFileContents);
59  $this->serializerMock->expects($this->once())
60  ->method('unserialize')
61  ->willReturn(json_decode($themeFileContents, true));
62  $this->assertEquals('package', $this->themePackageInfo->getPackageName('themeA'));
63  }
64 
65  public function testGetPackageNameNonExist()
66  {
67  $this->componentRegistrar->expects($this->once())->method('getPath')->willReturn('path/to/A');
68  $this->dirRead->expects($this->once())->method('isExist')->with('composer.json')->willReturn(false);
69  $this->dirRead->expects($this->never())->method('readFile')->with('composer.json');
70  $this->assertEquals('', $this->themePackageInfo->getPackageName('themeA'));
71  }
72 
73  public function testGetFullThemePath()
74  {
75  $themeFileContents = '{"name": "package"}';
76  $this->componentRegistrar->expects($this->once())->method('getPaths')->willReturn(['themeA' => 'path/to/A']);
77  $this->dirRead->expects($this->once())->method('isExist')->willReturn(true);
78  $this->dirRead->expects($this->once())->method('readFile')->willReturn($themeFileContents);
79  $this->serializerMock->expects($this->once())
80  ->method('unserialize')
81  ->willReturn(json_decode($themeFileContents, true));
82  $this->assertEquals('themeA', $this->themePackageInfo->getFullThemePath('package'));
83  // call one more time to make sure only initialize once
84  $this->assertEquals('themeA', $this->themePackageInfo->getFullThemePath('package'));
85  }
86 
87  public function testGetFullThemePathNonExist()
88  {
89  $this->componentRegistrar->expects($this->once())->method('getPaths')->willReturn(['themeA' => 'path/to/A']);
90  $this->dirRead->expects($this->once())->method('isExist')->willReturn(true);
91  $this->dirRead->expects($this->once())->method('readFile')->willReturn('{"name": "package"}');
92  $this->assertEquals('', $this->themePackageInfo->getFullThemePath('package-other'));
93  }
94 
96  {
97  $this->componentRegistrar->expects($this->once())->method('getPath')->willReturn('path/to/A');
98  $this->dirRead->expects($this->once())->method('isExist')->willReturn(true);
99  $this->dirRead->expects($this->once())->method('readFile')->willReturn('{"name": }');
100  $this->serializerMock->expects($this->once())
101  ->method('unserialize')
102  ->willReturn(null);
103  $this->assertEquals('', $this->themePackageInfo->getPackageName('themeA'));
104  }
105 }