From 4dc617920701dbb70ba77be016507af4e450b7d9 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Fri, 5 Dec 2014 23:24:52 +0100 Subject: [PATCH] Infrastructure for obtaining temporary files. --- tests/PhpseclibTestCase.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/PhpseclibTestCase.php b/tests/PhpseclibTestCase.php index 3369ad16..f0f75d9a 100644 --- a/tests/PhpseclibTestCase.php +++ b/tests/PhpseclibTestCase.php @@ -7,6 +7,32 @@ abstract class PhpseclibTestCase extends PHPUnit_Framework_TestCase { + protected $tempFilesToUnlinkOnTearDown = array(); + + public function tearDown() + { + foreach ($this->tempFilesToUnlinkOnTearDown as $filename) { + if (!file_exists($filename) || unlink($filename)) { + unset($this->tempFilesToUnlinkOnTearDown[$filename]); + } + } + parent::tearDown(); + } + + /** + * Creates a temporary file on the local filesystem and returns its path. + * All files created using this method will be deleted from the filesystem + * on tearDown(), i.e. after each test method was run. + * + * @return string + */ + protected function createTempFile() + { + $filename = tempnam(sys_get_temp_dir(), 'phpseclib-test-'); + $this->tempFilesToUnlinkOnTearDown[] = $filename; + return $filename; + } + /** * @param string $constant * @param mixed $expected