diff --git a/src/Knp/Snappy/AbstractGenerator.php b/src/Knp/Snappy/AbstractGenerator.php index 303958e2..19d4e0d3 100644 --- a/src/Knp/Snappy/AbstractGenerator.php +++ b/src/Knp/Snappy/AbstractGenerator.php @@ -360,8 +360,9 @@ protected function createTemporaryFile($content = null, $extension = null) if (null !== $content) { file_put_contents($filename, $content); - $this->temporaryFiles[md5($filename)] = $filename; } + + $this->temporaryFiles[] = $filename; return $filename; } @@ -605,7 +606,7 @@ protected function filesize($filename) */ protected function unlink($filename) { - return unlink($filename); + return $this->fileExists($filename) ? unlink($filename) : false; } /** diff --git a/test/Knp/Snappy/AbstractGeneratorTest.php b/test/Knp/Snappy/AbstractGeneratorTest.php index 04932e45..411ab8be 100644 --- a/test/Knp/Snappy/AbstractGeneratorTest.php +++ b/test/Knp/Snappy/AbstractGeneratorTest.php @@ -756,4 +756,62 @@ public function dataForIsAssociativeArray() ), ); } + + public function testCleanupEmptyTemporaryFiles() + { + $generator = $this->getMock( + 'Knp\Snappy\AbstractGenerator', + array( + 'configure', + 'unlink', + ), + array( + 'the_binary' + ) + ); + $generator + ->expects($this->once()) + ->method('unlink'); + + $create = new \ReflectionMethod($generator, 'createTemporaryFile'); + $create->setAccessible(true); + $create->invoke($generator, null, null); + + $files = new \ReflectionProperty($generator, 'temporaryFiles'); + $files->setAccessible(true); + $this->assertCount(1, $files->getValue($generator)); + + $remove = new \ReflectionMethod($generator, 'removeTemporaryFiles'); + $remove->setAccessible(true); + $remove->invoke($generator); + } + + public function testleanupTemporaryFiles() + { + $generator = $this->getMock( + 'Knp\Snappy\AbstractGenerator', + array( + 'configure', + 'unlink', + ), + array( + 'the_binary' + ) + ); + $generator + ->expects($this->once()) + ->method('unlink'); + + $create = new \ReflectionMethod($generator, 'createTemporaryFile'); + $create->setAccessible(true); + $create->invoke($generator, '', 'html'); + + $files = new \ReflectionProperty($generator, 'temporaryFiles'); + $files->setAccessible(true); + $this->assertCount(1, $files->getValue($generator)); + + $remove = new \ReflectionMethod($generator, 'removeTemporaryFiles'); + $remove->setAccessible(true); + $remove->invoke($generator); + } } diff --git a/test/Knp/Snappy/PdfTest.php b/test/Knp/Snappy/PdfTest.php index 53d17dff..98333e45 100644 --- a/test/Knp/Snappy/PdfTest.php +++ b/test/Knp/Snappy/PdfTest.php @@ -71,7 +71,7 @@ protected function checkOutput($output, $command) public function getOutput($input, array $options = array()) { $filename = $this->createTemporaryFile(null, $this->getDefaultExtension()); - $this->generate($input, $filename, $options); + $this->generate($input, $filename, $options, true); return "output"; }