From 5bdc84d664e8aa4c351a0868f442674a087f5908 Mon Sep 17 00:00:00 2001 From: Ori Hoch Date: Thu, 13 Jul 2017 15:50:36 +0300 Subject: [PATCH] fixes #5: temporary files should be deleted during unit tests --- tests/SchemaTest.php | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index 72dda26..6b9a41e 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -127,8 +127,8 @@ public function testConstructFromInvalidResource() public function testDifferentValidDescriptorSources() { - $simpleFile = tempnam(sys_get_temp_dir(), 'tableschema-php-tests'); - $fullFile = tempnam(sys_get_temp_dir(), 'tableschema-php-tests'); + $simpleFile = $this->getTempFile(); + $fullFile = $this->getTempFile(); file_put_contents($simpleFile, json_encode($this->simpleDescriptor)); file_put_contents($fullFile, json_encode($this->fullDescriptor)); $descriptors = [ @@ -504,7 +504,7 @@ public function testSchemaToEditableSchema() public function testSave() { $schema = new Schema($this->minDescriptorJson); - $filename = tempnam(sys_get_temp_dir(), 'tableschema-php-tests'); + $filename = $this->getTempFile(); $schema->save($filename); $this->assertEquals($schema->fullDescriptor(), json_decode(file_get_contents($filename))); } @@ -532,6 +532,17 @@ public function testSpecsUriFormat() ]], $validator->getErrors()); } + public function tearDown() + { + foreach ($this->tempFiles as $tempFile) { + if (file_exists($tempFile)) { + unlink($tempFile); + } + } + } + + protected $tempFiles = []; + protected function assertValidationErrors($expectedValidationErrors, $descriptor) { $this->assertEquals( @@ -570,4 +581,12 @@ protected function assertCastRowException($expectedError, $descriptor, $inputRow $this->assertEquals($expectedError, $e->getMessage()); } } + + protected function getTempFile() + { + $file = tempnam(sys_get_temp_dir(), 'tableschema-php-tests'); + $this->tempFiles[] = $file; + + return $file; + } }