|
10 | 10 | */
|
11 | 11 | class ComposerLockTest extends \PHPUnit_Framework_TestCase
|
12 | 12 | {
|
13 |
| - public function testUpToDate() |
| 13 | + /** |
| 14 | + * @return string |
| 15 | + */ |
| 16 | + public function testLockFileExists() |
14 | 17 | {
|
15 |
| - $hash = hash_file('md5', BP . '/composer.json'); |
16 | 18 | $lockFilePath = BP . '/composer.lock';
|
17 |
| - if (!file_exists($lockFilePath)) { |
18 |
| - $this->markTestSkipped('composer.lock file doesn\'t exist'); |
| 19 | + $this->assertLockFileExists($lockFilePath); |
| 20 | + return $lockFilePath; |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * @depends testLockFileExists |
| 25 | + * @param string $lockFilePath |
| 26 | + * @return string |
| 27 | + */ |
| 28 | + public function testLockFileReadable($lockFilePath) |
| 29 | + { |
| 30 | + $this->assertLockFileReadable($lockFilePath); |
| 31 | + return $lockFilePath; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @depends testLockFileReadable |
| 36 | + * @param string $lockFilePath |
| 37 | + * @return string |
| 38 | + */ |
| 39 | + public function testLockFileContainsJson($lockFilePath) |
| 40 | + { |
| 41 | + $lockFileContent = file_get_contents($lockFilePath); |
| 42 | + $this->assertLockFileContainsValidJson($lockFileContent); |
| 43 | + return $lockFileContent; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @depends testLockFileContainsJson |
| 48 | + * @param string $lockFileContent |
| 49 | + */ |
| 50 | + public function testUpToDate($lockFileContent) |
| 51 | + { |
| 52 | + $lockData = json_decode($lockFileContent, true); |
| 53 | + $composerFilePath = BP . '/composer.json'; |
| 54 | + $this->assertLockDataRelevantToComposerFile($lockData, $composerFilePath); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @param string $lockFilePath |
| 59 | + */ |
| 60 | + private function assertLockFileExists($lockFilePath) |
| 61 | + { |
| 62 | + $this->assertFileExists($lockFilePath, 'composer.lock file does not exist'); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @param string $lockFilePath |
| 67 | + */ |
| 68 | + private function assertLockFileReadable($lockFilePath) |
| 69 | + { |
| 70 | + if (!is_readable($lockFilePath)) { |
| 71 | + $this->fail('composer.lock file is not readable'); |
19 | 72 | }
|
20 |
| - $jsonData = file_get_contents($lockFilePath); |
21 |
| - $json = json_decode($jsonData); |
22 |
| - $this->assertSame($hash, $json->hash, 'composer.lock file is not up to date'); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @param string $lockFileContent |
| 77 | + */ |
| 78 | + private function assertLockFileContainsValidJson($lockFileContent) |
| 79 | + { |
| 80 | + $this->assertJson($lockFileContent, 'composer.lock file does not contains valid json'); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @param array $lockData |
| 85 | + * @param string $composerFilePath |
| 86 | + */ |
| 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 | + |
| 98 | + /** |
| 99 | + * @param array $lockData |
| 100 | + * @param string $composerFilePath |
| 101 | + */ |
| 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 | + |
| 117 | + /** |
| 118 | + * @param array $lockData |
| 119 | + * @param string $composerFilePath |
| 120 | + */ |
| 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 | + ); |
23 | 129 | }
|
24 | 130 | }
|
0 commit comments