dimanche 26 avril 2015

Can I test this method that composes a string command?

This is the first time I'm getting into unit tests, and I would like to know if there is a way I can test a simple class' methods that work by composing a CLI command from the given arguments?

Basically I need to check if the command contains a certain parameter for each argument passed through the method.

I've read that the body of a method is implementation detail and that it would not be testable. If that's true I assume that I cannot test a method like this?

Method Example

public function doSomething(array $params)
{
    $command = ($this->x64 ? 'test_x64' : 'test_enc') . ' '
             . $params['a'] . ' '
             . $params['b'] . ' '
             . $params['c'];

    if (isset($params['d'])) {
        $command .= ' -d=' . $params['d'];
    }

    if (isset($params['e'])) {
        $command .= ' -e=' . $params['e'];
    }

    if (isset($params['f'])) {
        $command .= ' -' . $params['f'] . 'bit';
    }

    return shell_exec($command);
}

Aucun commentaire:

Enregistrer un commentaire