PHPUnit Test for chain of fopen/fwrite
<p>In one project I have found such lines of code:</p>

<pre><code>protected function save($content, $path)
{
    // ...
    if (($handler = @fopen($path, 'w')) === false) {
        throw new Exception('...');
    }

    // ...
    if (@fwrite($handler, $content) === false) {
        throw new Exception('...');
    }

    // ...
    @fclose($handler);
}
</code></pre>

<p>I would like to test this method with PHPUnit, but I am a little bit stuck with correct test-case. If I will pass incorrect <code>$path</code> or a correct <code>$path</code> with incorrect permissions (e.g. <code>0444</code>) then everything will stop at the first exception. If I pass correct <code>$path</code> with correct permissions than PHP will also be able to write to the file and the second exception won't be reached.</p>

<p>So is there any way to test the second exception without rewriting this method? </p>

<p>Or it is better to check both <code>fopen</code> and <code>fwrite</code> in one condition and use only one exception for both? </p>

<p>Or the best option is to separate this method into two - one for opening and one for writing - and test them separately?</p>