Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PackageInfoTest.php
Go to the documentation of this file.
1 <?php
7 
9 
10 class PackageInfoTest extends \PHPUnit\Framework\TestCase
11 {
15  private $componentRegistrar;
16 
20  private $reader;
21 
25  private $packageInfo;
26 
30  private $serializerMock;
31 
32  protected function setUp()
33  {
34  $this->componentRegistrar = $this->createMock(\Magento\Framework\Component\ComponentRegistrar::class);
35  $this->reader = $this->createMock(\Magento\Framework\Module\Dir\Reader::class);
36  $this->componentRegistrar->expects($this->once())
37  ->method('getPaths')
38  ->will($this->returnValue(['A' => 'A', 'B' => 'B', 'C' => 'C', 'D' => 'D', 'E' => 'E']));
39 
40  $composerData = [
41  'A/composer.json' => '{"name":"a", "require":{"b":"0.1"}, "conflict":{"c":"0.1"}, "version":"0.1"}',
42  'B/composer.json' => '{"name":"b", "require":{"d":"0.3"}, "version":"0.2"}',
43  'C/composer.json' => '{"name":"c", "require":{"e":"0.1"}, "version":"0.1"}',
44  'D/composer.json' => '{"name":"d", "conflict":{"c":"0.1"}, "version":"0.3"}',
45  'E/composer.json' => '{"name":"e", "version":"0.4"}',
46  ];
47  $fileIteratorMock = $this->createMock(\Magento\Framework\Config\FileIterator::class);
48  $fileIteratorMock->expects($this->once())
49  ->method('toArray')
50  ->will($this->returnValue($composerData));
51  $this->reader->expects($this->once())
52  ->method('getComposerJsonFiles')
53  ->will($this->returnValue($fileIteratorMock));
54 
55  $this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
56  ->getMock();
57 
58  $this->serializerMock->expects($this->any())
59  ->method('unserialize')
60  ->willReturnCallback(
61  function ($serializedData) {
62  return json_decode($serializedData, true);
63  }
64  );
65  $this->packageInfo = new PackageInfo(
66  $this->reader,
67  $this->componentRegistrar,
68  $this->serializerMock
69  );
70  }
71 
72  public function testGetModuleName()
73  {
74  $this->assertEquals('A', $this->packageInfo->getModuleName('a'));
75  $this->assertEquals('B', $this->packageInfo->getModuleName('b'));
76  $this->assertEquals('C', $this->packageInfo->getModuleName('c'));
77  $this->assertEquals('D', $this->packageInfo->getModuleName('d'));
78  $this->assertEquals('E', $this->packageInfo->getModuleName('e'));
79  $this->assertEquals(
80  'Magento_TestModuleName',
81  $this->packageInfo->getModuleName('magento/module-test-module-name')
82  );
83  $this->assertArrayHasKey('Magento_TestModuleName', $this->packageInfo->getNonExistingDependencies());
84  }
85 
86  public function testGetPackageName()
87  {
88  $this->assertEquals('a', $this->packageInfo->getPackageName('A'));
89  $this->assertEquals('b', $this->packageInfo->getPackageName('B'));
90  $this->assertEquals('c', $this->packageInfo->getPackageName('C'));
91  $this->assertEquals('d', $this->packageInfo->getPackageName('D'));
92  $this->assertEquals('e', $this->packageInfo->getPackageName('E'));
93  }
94 
96  {
97  $this->assertEquals(['B'], $this->packageInfo->getRequire('A'));
98  $this->assertEquals(['D'], $this->packageInfo->getRequire('B'));
99  $this->assertEquals(['E'], $this->packageInfo->getRequire('C'));
100  $this->assertEquals([], $this->packageInfo->getRequire('D'));
101  $this->assertEquals([], $this->packageInfo->getRequire('E'));
102  }
103 
105  {
106  $this->assertEquals(['C' => '0.1'], $this->packageInfo->getConflict('A'));
107  $this->assertEquals([], $this->packageInfo->getConflict('B'));
108  $this->assertEquals([], $this->packageInfo->getConflict('C'));
109  $this->assertEquals(['C' => '0.1'], $this->packageInfo->getConflict('D'));
110  $this->assertEquals([], $this->packageInfo->getConflict('E'));
111  }
112 
113  public function testGetVersion()
114  {
115  $this->assertEquals('0.1', $this->packageInfo->getVersion('A'));
116  $this->assertEquals('0.2', $this->packageInfo->getVersion('B'));
117  $this->assertEquals('0.1', $this->packageInfo->getVersion('C'));
118  $this->assertEquals('0.3', $this->packageInfo->getVersion('D'));
119  $this->assertEquals('0.4', $this->packageInfo->getVersion('E'));
120  $this->assertEquals('', $this->packageInfo->getVersion('F'));
121  }
122 
123  public function testGetRequiredBy()
124  {
125  $this->assertEquals(['A'], $this->packageInfo->getRequiredBy('b'));
126  }
127 }
if(!file_exists($composerFile)) $composerData