Separate MediaWiki unit and integration tests
[lhc/web/wiklou.git] / tests / phpunit / unit / includes / utils / FileContentsHasherTest.php
1 <?php
2
3 /**
4 * @covers FileContentsHasherTest
5 */
6 class FileContentsHasherTest extends PHPUnit\Framework\TestCase {
7
8 use MediaWikiCoversValidator;
9
10 public function provideSingleFile() {
11 return array_map( function ( $file ) {
12 return [ $file, file_get_contents( $file ) ];
13 }, glob( __DIR__ . '/../../../data/filecontentshasher/*.*' ) );
14 }
15
16 public function provideMultipleFiles() {
17 return [
18 [ $this->provideSingleFile() ]
19 ];
20 }
21
22 /**
23 * @covers FileContentsHasher::getFileContentsHash
24 * @covers FileContentsHasher::getFileContentsHashInternal
25 * @dataProvider provideSingleFile
26 */
27 public function testSingleFileHash( $fileName, $contents ) {
28 foreach ( [ 'md4', 'md5' ] as $algo ) {
29 $expectedHash = hash( $algo, $contents );
30 $actualHash = FileContentsHasher::getFileContentsHash( $fileName, $algo );
31 $this->assertEquals( $expectedHash, $actualHash );
32 $actualHashRepeat = FileContentsHasher::getFileContentsHash( $fileName, $algo );
33 $this->assertEquals( $expectedHash, $actualHashRepeat );
34 }
35 }
36
37 /**
38 * @covers FileContentsHasher::getFileContentsHash
39 * @covers FileContentsHasher::getFileContentsHashInternal
40 * @dataProvider provideMultipleFiles
41 */
42 public function testMultipleFileHash( $files ) {
43 $fileNames = [];
44 $hashes = [];
45 foreach ( $files as $fileInfo ) {
46 list( $fileName, $contents ) = $fileInfo;
47 $fileNames[] = $fileName;
48 $hashes[] = md5( $contents );
49 }
50
51 $expectedHash = md5( implode( '', $hashes ) );
52 $actualHash = FileContentsHasher::getFileContentsHash( $fileNames, 'md5' );
53 $this->assertEquals( $expectedHash, $actualHash );
54 $actualHashRepeat = FileContentsHasher::getFileContentsHash( $fileNames, 'md5' );
55 $this->assertEquals( $expectedHash, $actualHashRepeat );
56 }
57 }