title: PHPUnit temp file helpers
body: <p>After writing PHPUnit tests for a while I've come across a pattern that I'd like to find an elegant solution for. Unit tests for my project use a lot of temp files. I'll have a function that outputs a file, and I need to make several assertions on the output (temp) file. Afterwards I want to make sure that the temp files get cleaned up, even if some of the assertions fail. </p>
  
  <p>Here's an example test file using with some cruft to handle the temp files:</p>
  
  <pre><code>class FooTest extends AhshayTestUnit
  {
      function setUp()
      {
          $this-&gt;tmps[] = array();
      }
  
      function tearDown()
      {
          foreach ($this-&gt;tmps as $tmp)
          {
              @unlink($tmp);
          }
      }
  
      function tmp()
      {
          $this-&gt;tmps[] = $tmp = tempnam('/tmp', 'test_foo');
          unlink($tmp);
          return $tmp;
      }
  
      function testFoo()
      {
          $in = 'html2pdf.html';
          $out = $this-&gt;tmp();
  
          $success = html2Pdf($in, $out);
  
          $this-&gt;assertFileExists($out, "should create PDF file");
          $this-&gt;assertGreaterThan(1024, filesize($out), "output file should be atleast 1k");
  
          # and so on
      }
  }
  </code></pre>
  
  <p>This way, each test function doesn't need to worry about deleting <code>$out</code>, even if the first assertion fails.</p>
  
  <p>Now that I've said all that, I'd like to bake these helper functions into every test object I have. I already have my own class that extends <code>PHPUnit_Framework_TestCase</code>. I could put my custom <code>setUp()</code> and <code>tearDown()</code> in my subclass, but it seems a bit lame to require all my tests that have their own setup/teardown to remember to call <code>parent::__construct()</code>. Is there a more elegant way to do this? Is there something already built into PHPUnit to handle this? It seems like the best way would be to have some way for PHPUnit to chain setup/teardown callbacks.</p>