PackageInfoTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Module\Test\Unit;
  7. use Magento\Framework\Module\PackageInfo;
  8. class PackageInfoTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Framework\Component\ComponentRegistrar|\PHPUnit_Framework_MockObject_MockObject
  12. */
  13. private $componentRegistrar;
  14. /**
  15. * @var \Magento\Framework\Module\Dir\Reader|\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. private $reader;
  18. /**
  19. * @var PackageInfo
  20. */
  21. private $packageInfo;
  22. /**
  23. * @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $serializerMock;
  26. protected function setUp()
  27. {
  28. $this->componentRegistrar = $this->createMock(\Magento\Framework\Component\ComponentRegistrar::class);
  29. $this->reader = $this->createMock(\Magento\Framework\Module\Dir\Reader::class);
  30. $this->componentRegistrar->expects($this->once())
  31. ->method('getPaths')
  32. ->will($this->returnValue(['A' => 'A', 'B' => 'B', 'C' => 'C', 'D' => 'D', 'E' => 'E']));
  33. $composerData = [
  34. 'A/composer.json' => '{"name":"a", "require":{"b":"0.1"}, "conflict":{"c":"0.1"}, "version":"0.1"}',
  35. 'B/composer.json' => '{"name":"b", "require":{"d":"0.3"}, "version":"0.2"}',
  36. 'C/composer.json' => '{"name":"c", "require":{"e":"0.1"}, "version":"0.1"}',
  37. 'D/composer.json' => '{"name":"d", "conflict":{"c":"0.1"}, "version":"0.3"}',
  38. 'E/composer.json' => '{"name":"e", "version":"0.4"}',
  39. ];
  40. $fileIteratorMock = $this->createMock(\Magento\Framework\Config\FileIterator::class);
  41. $fileIteratorMock->expects($this->once())
  42. ->method('toArray')
  43. ->will($this->returnValue($composerData));
  44. $this->reader->expects($this->once())
  45. ->method('getComposerJsonFiles')
  46. ->will($this->returnValue($fileIteratorMock));
  47. $this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
  48. ->getMock();
  49. $this->serializerMock->expects($this->any())
  50. ->method('unserialize')
  51. ->willReturnCallback(
  52. function ($serializedData) {
  53. return json_decode($serializedData, true);
  54. }
  55. );
  56. $this->packageInfo = new PackageInfo(
  57. $this->reader,
  58. $this->componentRegistrar,
  59. $this->serializerMock
  60. );
  61. }
  62. public function testGetModuleName()
  63. {
  64. $this->assertEquals('A', $this->packageInfo->getModuleName('a'));
  65. $this->assertEquals('B', $this->packageInfo->getModuleName('b'));
  66. $this->assertEquals('C', $this->packageInfo->getModuleName('c'));
  67. $this->assertEquals('D', $this->packageInfo->getModuleName('d'));
  68. $this->assertEquals('E', $this->packageInfo->getModuleName('e'));
  69. $this->assertEquals(
  70. 'Magento_TestModuleName',
  71. $this->packageInfo->getModuleName('magento/module-test-module-name')
  72. );
  73. $this->assertArrayHasKey('Magento_TestModuleName', $this->packageInfo->getNonExistingDependencies());
  74. }
  75. public function testGetPackageName()
  76. {
  77. $this->assertEquals('a', $this->packageInfo->getPackageName('A'));
  78. $this->assertEquals('b', $this->packageInfo->getPackageName('B'));
  79. $this->assertEquals('c', $this->packageInfo->getPackageName('C'));
  80. $this->assertEquals('d', $this->packageInfo->getPackageName('D'));
  81. $this->assertEquals('e', $this->packageInfo->getPackageName('E'));
  82. }
  83. public function testGetRequireReturnModuleName()
  84. {
  85. $this->assertEquals(['B'], $this->packageInfo->getRequire('A'));
  86. $this->assertEquals(['D'], $this->packageInfo->getRequire('B'));
  87. $this->assertEquals(['E'], $this->packageInfo->getRequire('C'));
  88. $this->assertEquals([], $this->packageInfo->getRequire('D'));
  89. $this->assertEquals([], $this->packageInfo->getRequire('E'));
  90. }
  91. public function testGetConflictReturnModuleName()
  92. {
  93. $this->assertEquals(['C' => '0.1'], $this->packageInfo->getConflict('A'));
  94. $this->assertEquals([], $this->packageInfo->getConflict('B'));
  95. $this->assertEquals([], $this->packageInfo->getConflict('C'));
  96. $this->assertEquals(['C' => '0.1'], $this->packageInfo->getConflict('D'));
  97. $this->assertEquals([], $this->packageInfo->getConflict('E'));
  98. }
  99. public function testGetVersion()
  100. {
  101. $this->assertEquals('0.1', $this->packageInfo->getVersion('A'));
  102. $this->assertEquals('0.2', $this->packageInfo->getVersion('B'));
  103. $this->assertEquals('0.1', $this->packageInfo->getVersion('C'));
  104. $this->assertEquals('0.3', $this->packageInfo->getVersion('D'));
  105. $this->assertEquals('0.4', $this->packageInfo->getVersion('E'));
  106. $this->assertEquals('', $this->packageInfo->getVersion('F'));
  107. }
  108. public function testGetRequiredBy()
  109. {
  110. $this->assertEquals(['A'], $this->packageInfo->getRequiredBy('b'));
  111. }
  112. }