Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
All Data Structures Namespaces Files Functions Variables Pages
ComposerLockTest.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Test\Integrity;
7 
11 class ComposerLockTest extends \PHPUnit\Framework\TestCase
12 {
16  public function testLockFileExists()
17  {
18  $lockFilePath = BP . '/composer.lock';
19  $this->assertLockFileExists($lockFilePath);
20  return $lockFilePath;
21  }
22 
28  public function testLockFileReadable($lockFilePath)
29  {
30  $this->assertLockFileReadable($lockFilePath);
31  return $lockFilePath;
32  }
33 
39  public function testLockFileContainsJson($lockFilePath)
40  {
41  $lockFileContent = file_get_contents($lockFilePath);
42  $this->assertLockFileContainsValidJson($lockFileContent);
43  return $lockFileContent;
44  }
45 
50  public function testUpToDate($lockFileContent)
51  {
52  $lockData = json_decode($lockFileContent, true);
53  $composerFilePath = BP . '/composer.json';
54  $this->assertLockDataRelevantToComposerFile($lockData, $composerFilePath);
55  }
56 
60  private function assertLockFileExists($lockFilePath)
61  {
62  $this->assertFileExists($lockFilePath, 'composer.lock file does not exist');
63  }
64 
68  private function assertLockFileReadable($lockFilePath)
69  {
70  if (!is_readable($lockFilePath)) {
71  $this->fail('composer.lock file is not readable');
72  }
73  }
74 
78  private function assertLockFileContainsValidJson($lockFileContent)
79  {
80  $this->assertJson($lockFileContent, 'composer.lock file does not contains valid json');
81  }
82 
87  private function assertLockDataRelevantToComposerFile(array $lockData, $composerFilePath)
88  {
89  if (isset($lockData['content-hash'])) {
90  $this->assertLockDataRelevantToMeaningfulComposerConfig($lockData, $composerFilePath);
91  } else if (isset($lockData['hash'])) {
92  $this->assertLockDataRelevantToFullComposerConfig($lockData, $composerFilePath);
93  } else {
94  $this->fail('composer.lock does not linked to composer.json data');
95  }
96  }
97 
102  private function assertLockDataRelevantToMeaningfulComposerConfig(array $lockData, $composerFilePath)
103  {
104  $contentHashCalculator = 'Composer\Package\Locker::getContentHash';
105  if (!is_callable($contentHashCalculator)) {
106  $this->markTestSkipped('Unable to check composer.lock file by content hash');
107  }
108 
109  $composerContentHash = call_user_func($contentHashCalculator, file_get_contents($composerFilePath));
110  $this->assertSame(
111  $composerContentHash,
112  $lockData['content-hash'],
113  'composer.lock file is not up to date (composer.json file was modified)'
114  );
115  }
116 
121  private function assertLockDataRelevantToFullComposerConfig(array $lockData, $composerFilePath)
122  {
123  $composerFileHash = hash_file('md5', $composerFilePath);
124  $this->assertSame(
125  $composerFileHash,
126  $lockData['hash'],
127  'composer.lock file is not up to date (composer.json file was modified)'
128  );
129  }
130 }
const BP
Definition: autoload.php:14