For a PHP application I’m developing, I need to read the current git revision SHA which of course I can get easily by using shell_exec or backticks to execute the git command line client.
I have obviously put this call into a method of its very own, so that I can easily isolate and mock this for the rest of my unit tests. So my class looks a bit like this:
class Task_Bundle
{
public function execute()
{
// Do things
$revision = $this->git_sha();
// Do more things
}
protected function git_sha()
{
return `git rev-parse --short HEAD`;
}
}
Of course, although I can test most of the class by mocking git_sha, I’m struggling to see how to test the actual git_sha() method because I don’t see a way to create a known state for it. I don’t think there’s any real value in a unit test that also calls git rev-parse to compare the results? I was wondering about at least asserting that the command had been run, but I can’t see any way to get a history of shell commands executed by PHP – even if I specify that PHP should use BASH rather than SH the history list comes up empty, I presume because the separate backticks executions are separate terminal sessions.
I’d love to hear any suggestions for how I might test this, or is it OK to just leave that method untested and be careful with it when the app is being maintained in future?
Although you have accepted an answer, let me post something about what the way unittests should work because even if the solution posted does work and is indeed nice, it has some backdraws.
First of all you don’t want to actually execute your command, as this would test an external software. From this moment you are depending on external stuff you cannot control (what happens if I want to test your software on my pc? Do I have to instal git or any git-mock?). What you really want to do is see if the command line command is correct and is executed well.
As I struggled with this before, here is what I did: I created a class which executes shell_exec. Everywhere I want to interact with the shell, I inject the class and use it. So for testing I can mock the class and see if the correct method is executed and if the correct parameters are set.
This has many benefits:
Of course to validate that your software works as a whole, you have to do integration tests which do not mock anything. But this is a totally different situation. Unittests should make sure your class (or method) does what it needs to do. Sometimes this is not clear cut, as you may decide to use a real class somewhere because mocking it would lead to much overhead. But, as far as I’m concerned, it’s no longer a unit test by any means if it relys on external libraries/software which may not be installed or may not work for some reason.