wiki.techinc.nl/tests/phpunit/includes/utils/FileContentsHasherTest.php
Chad Horohoe c84b69a11b Let a few unit tests actually be unit tests
MediaWikiTestCase is a terrible terrible thing with dumb overhead

Change-Id: I2a73a0f2819ed7e73b57642b7969036c2db2c991
2016-09-02 10:16:06 -07:00

55 lines
1.7 KiB
PHP

<?php
/**
* @covers FileContentsHasherTest
*/
class FileContentsHasherTest extends PHPUnit_Framework_TestCase {
public function provideSingleFile() {
return array_map( function ( $file ) {
return [ $file, file_get_contents( $file ) ];
}, glob( __DIR__ . '/../../data/filecontentshasher/*.*' ) );
}
public function provideMultipleFiles() {
return [
[ $this->provideSingleFile() ]
];
}
/**
* @covers FileContentsHasher::getFileContentsHash
* @covers FileContentsHasher::getFileContentsHashInternal
* @dataProvider provideSingleFile
*/
public function testSingleFileHash( $fileName, $contents ) {
foreach ( [ 'md4', 'md5' ] as $algo ) {
$expectedHash = hash( $algo, $contents );
$actualHash = FileContentsHasher::getFileContentsHash( $fileName, $algo );
$this->assertEquals( $expectedHash, $actualHash );
$actualHashRepeat = FileContentsHasher::getFileContentsHash( $fileName, $algo );
$this->assertEquals( $expectedHash, $actualHashRepeat );
}
}
/**
* @covers FileContentsHasher::getFileContentsHash
* @covers FileContentsHasher::getFileContentsHashInternal
* @dataProvider provideMultipleFiles
*/
public function testMultipleFileHash( $files ) {
$fileNames = [];
$hashes = [];
foreach ( $files as $fileInfo ) {
list( $fileName, $contents ) = $fileInfo;
$fileNames[] = $fileName;
$hashes[] = md5( $contents );
}
$expectedHash = md5( implode( '', $hashes ) );
$actualHash = FileContentsHasher::getFileContentsHash( $fileNames, 'md5' );
$this->assertEquals( $expectedHash, $actualHash );
$actualHashRepeat = FileContentsHasher::getFileContentsHash( $fileNames, 'md5' );
$this->assertEquals( $expectedHash, $actualHashRepeat );
}
}