ComposerLockTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Test\Integrity;
  7. /**
  8. * A test that enforces composer.lock is up to date with composer.json
  9. */
  10. class ComposerLockTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @return string
  14. */
  15. public function testLockFileExists()
  16. {
  17. $lockFilePath = BP . '/composer.lock';
  18. $this->assertLockFileExists($lockFilePath);
  19. return $lockFilePath;
  20. }
  21. /**
  22. * @depends testLockFileExists
  23. * @param string $lockFilePath
  24. * @return string
  25. */
  26. public function testLockFileReadable($lockFilePath)
  27. {
  28. $this->assertLockFileReadable($lockFilePath);
  29. return $lockFilePath;
  30. }
  31. /**
  32. * @depends testLockFileReadable
  33. * @param string $lockFilePath
  34. * @return string
  35. */
  36. public function testLockFileContainsJson($lockFilePath)
  37. {
  38. $lockFileContent = file_get_contents($lockFilePath);
  39. $this->assertLockFileContainsValidJson($lockFileContent);
  40. return $lockFileContent;
  41. }
  42. /**
  43. * @depends testLockFileContainsJson
  44. * @param string $lockFileContent
  45. */
  46. public function testUpToDate($lockFileContent)
  47. {
  48. $lockData = json_decode($lockFileContent, true);
  49. $composerFilePath = BP . '/composer.json';
  50. $this->assertLockDataRelevantToComposerFile($lockData, $composerFilePath);
  51. }
  52. /**
  53. * @param string $lockFilePath
  54. */
  55. private function assertLockFileExists($lockFilePath)
  56. {
  57. $this->assertFileExists($lockFilePath, 'composer.lock file does not exist');
  58. }
  59. /**
  60. * @param string $lockFilePath
  61. */
  62. private function assertLockFileReadable($lockFilePath)
  63. {
  64. if (!is_readable($lockFilePath)) {
  65. $this->fail('composer.lock file is not readable');
  66. }
  67. }
  68. /**
  69. * @param string $lockFileContent
  70. */
  71. private function assertLockFileContainsValidJson($lockFileContent)
  72. {
  73. $this->assertJson($lockFileContent, 'composer.lock file does not contains valid json');
  74. }
  75. /**
  76. * @param array $lockData
  77. * @param string $composerFilePath
  78. */
  79. private function assertLockDataRelevantToComposerFile(array $lockData, $composerFilePath)
  80. {
  81. if (isset($lockData['content-hash'])) {
  82. $this->assertLockDataRelevantToMeaningfulComposerConfig($lockData, $composerFilePath);
  83. } else if (isset($lockData['hash'])) {
  84. $this->assertLockDataRelevantToFullComposerConfig($lockData, $composerFilePath);
  85. } else {
  86. $this->fail('composer.lock does not linked to composer.json data');
  87. }
  88. }
  89. /**
  90. * @param array $lockData
  91. * @param string $composerFilePath
  92. */
  93. private function assertLockDataRelevantToMeaningfulComposerConfig(array $lockData, $composerFilePath)
  94. {
  95. $contentHashCalculator = 'Composer\Package\Locker::getContentHash';
  96. if (!is_callable($contentHashCalculator)) {
  97. $this->markTestSkipped('Unable to check composer.lock file by content hash');
  98. }
  99. $composerContentHash = call_user_func($contentHashCalculator, file_get_contents($composerFilePath));
  100. $this->assertSame(
  101. $composerContentHash,
  102. $lockData['content-hash'],
  103. 'composer.lock file is not up to date (composer.json file was modified)'
  104. );
  105. }
  106. /**
  107. * @param array $lockData
  108. * @param string $composerFilePath
  109. */
  110. private function assertLockDataRelevantToFullComposerConfig(array $lockData, $composerFilePath)
  111. {
  112. $composerFileHash = hash_file('md5', $composerFilePath);
  113. $this->assertSame(
  114. $composerFileHash,
  115. $lockData['hash'],
  116. 'composer.lock file is not up to date (composer.json file was modified)'
  117. );
  118. }
  119. }