views:

78

answers:

2

Hi, I was trying to learn unit test with PHP. I know it's a bit too far for someone who just jump into unit test in PHP. Anyway, here's the case:

function doresize(&$mp3file)
{
    global $tmpdir, $mp3info, $ffmpeg;

    if(dirname($mp3file) != $tmpdir )
    {
     copy($mp3file , $tmpdir . '/' . $mp3file);
     $mp3file = $tmpdir . '/' . $mp3file;
    }
    $mp3filenew = basename($file, '.swf') . "_new.mp3";

    $command2 = "$mp3info -x \"$mp3file\" 2> /dev/null";

    exec($command2, $buffer);

    $mp3length = getLengthFromBuffer($buffer);

    debug("  \$mp3length: $mp3length");

    $lengthTranslated = roundLengthToSeconds($mp3length);

    $maxlength = floor( $lengthTranslated / 2);

    $halfmaxlength = floor( $maxlength / 2);

    $start = rand($halfmaxlength, $maxlength);

    $command3 = "$ffmpeg -y -acodec copy -ss $start -t $maxlength -i \"$mp3file\" $mp3filenew 2> /dev/null";


    exec($command3);

    @unlink($mp3file);
    rename($mp3filenew, $mp3file);
}

From the code above, what aspect of that code should I add in test case?

FYI: The code above is used to cut mp3 file in half.

+2  A: 

As I can see, you are using external program ffmpeg to do the work. So my suggestions are that:

1) Test the command: You extract this function into two functions. One for generating the command (in this case is $command3). Create a unit test to check that the command is valid. Another function execute the command.

2) Test the final result. You manually, create a result file. Do what ever it take (by hand) to ensure that the result file is what you want. Write a unit test to create the command, execute it and then compare the new output file and the file you have generate earlier. The comparison can be done by tools such as bsdiff or using php to read byte-by-byte (fread comes to mind) to compare them.

You may create multiple files to do this test (like for each bandwidth, sample rate or codec).

NOTE I that the second test might not work if the convert process always produce exactly the same binary.

NOTE II If the conversion does not always produce exact same binary or it is not possible to have one test file to represent all other files. You have to do a more transparent test by knowing the format of the output file and analyze it. For example, read the original and ouptut file audio-length, sampling rates and compare it.

Hope this helps.

EDIT I forgot to say something. If you trust ffmpeg. You can simply skip Test 2 (binary comparison) entirely and only check the command.

NawaMan
well, I guess, my option here is to check the command. Thank you.
silent
A: 

This function as a few different things that it is doing that can be unit tested. I would start by breaking into small pieces that can be tested. For example:

function doresize(&$mp3file)
{    
    $mp3file = move_to_tmp_if_not_already($mp3file);

    $lengthTranslated = getFileLengthTranslated($mp3file)

    $maxlength = floor( $lengthTranslated / 2);
    $halfmaxlength = floor( $maxlength / 2);
    $start = rand($halfmaxlength, $maxlength);
    $mp3filenew = extract_mp3($mp3file, start, $maxlength);

    @unlink($mp3file);
    rename($mp3filenew, $mp3file);
}
ndp