Skip to content

Commit

Permalink
Merge pull request #140 from ramunasd/temp_files
Browse files Browse the repository at this point in the history
Fixed temporary files cleanup
  • Loading branch information
mbontemps committed Mar 11, 2015
2 parents 4d46c5d + e13b102 commit eda1b39
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/Knp/Snappy/AbstractGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -605,7 +606,7 @@ protected function filesize($filename)
*/
protected function unlink($filename)
{
return unlink($filename);
return $this->fileExists($filename) ? unlink($filename) : false;
}

/**
Expand Down
58 changes: 58 additions & 0 deletions test/Knp/Snappy/AbstractGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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/>', '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);
}
}
2 changes: 1 addition & 1 deletion test/Knp/Snappy/PdfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand Down

0 comments on commit eda1b39

Please sign in to comment.