From 3cb3b5b9db1092ea7b59af6556b2b54fde7f9bf9 Mon Sep 17 00:00:00 2001 From: smiley Date: Wed, 25 Oct 2017 00:01:06 +0200 Subject: [PATCH] :octocat: proper cache file saving --- src/Output/QRMarkup.php | 21 ++++++++++--------- src/Output/QROutputAbstract.php | 17 ++++++++++++++++ tests/Output/MarkupTest.php | 36 +++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 9 deletions(-) diff --git a/src/Output/QRMarkup.php b/src/Output/QRMarkup.php index 4d8d9855f..062d49394 100644 --- a/src/Output/QRMarkup.php +++ b/src/Output/QRMarkup.php @@ -35,7 +35,7 @@ class QRMarkup extends QROutputAbstract{ /** * @return string */ - public function dump():string { + public function dump() { switch($this->options->type){ case QRCode::OUTPUT_MARKUP_SVG : return $this->toSVG(); case QRCode::OUTPUT_MARKUP_HTML: @@ -45,9 +45,9 @@ class QRMarkup extends QROutputAbstract{ } /** - * @return string + * @return string|bool */ - protected function toHTML():string { + protected function toHTML(){ $html = ''; foreach($this->matrix as $row){ @@ -69,16 +69,21 @@ class QRMarkup extends QROutputAbstract{ $html .= $this->options->eol; } + if($this->options->cachefile){ + $html = ''.$this->options->eol.$html.''; + + return $this->saveToFile($html); + } + return $html; } /** * @link https://github.com/codemasher/php-qrcode/pull/5 * - * @return string - * @throws \chillerlan\QRCode\Output\QRCodeOutputException + * @return string|bool */ - protected function toSVG():string { + protected function toSVG(){ $length = $this->pixelCount * $this->options->pixelSize + $this->options->marginSize * 2; $class = !empty($this->options->cssClass) ? $this->options->cssClass : hash('crc32', microtime(true)); @@ -124,9 +129,7 @@ class QRMarkup extends QROutputAbstract{ if($this->options->cachefile){ $svg = ''.$this->options->eol.$svg; - if(@file_put_contents($this->options->cachefile, $svg) === false){ - throw new QRCodeOutputException('Could not write to cache file.'); // @codeCoverageIgnore - } + return $this->saveToFile($svg); } return $svg; diff --git a/src/Output/QROutputAbstract.php b/src/Output/QROutputAbstract.php index 2642b7d57..d2af4d422 100644 --- a/src/Output/QROutputAbstract.php +++ b/src/Output/QROutputAbstract.php @@ -81,4 +81,21 @@ abstract class QROutputAbstract implements QROutputInterface{ return $this; } + /** + * @param string $data + * + * @return bool + * @throws \chillerlan\QRCode\Output\QRCodeOutputException + */ + protected function saveToFile(string $data):bool { + + try{ + return (bool)file_put_contents($this->options->cachefile, $data); + } + catch(\Exception $e){ + throw new QRCodeOutputException('Could not write to cache file: '.$e->getMessage()); + } + + } + } diff --git a/tests/Output/MarkupTest.php b/tests/Output/MarkupTest.php index 571ffca95..8d5173c67 100644 --- a/tests/Output/MarkupTest.php +++ b/tests/Output/MarkupTest.php @@ -48,4 +48,40 @@ class MarkupTest extends OutputTestAbstract{ $this->assertEquals(file_get_contents(__DIR__.'/markup/'.$expected), (new QRCode($data, new $this->outputInterfaceClass($this->options)))->output()); } + public function markupTestDataProvider(){ + return [ + [QRCode::OUTPUT_MARKUP_SVG], + [QRCode::OUTPUT_MARKUP_HTML], + ]; + } + + /** + * @dataProvider markupTestDataProvider + */ + public function testSaveToFile(string $type){ + $this->options->type = $type; + $this->options->cssClass = 'foo'; + + $data = (new QRCode('foo', new $this->outputInterfaceClass($this->options)))->output(); + + $this->options->cachefile = __DIR__.'/markup/save_test.'.$type; + + $this->assertTrue((new QRCode('foo', new $this->outputInterfaceClass($this->options)))->output()); + + $this->assertContains($data, file_get_contents($this->options->cachefile)); + } + + /** + * @dataProvider markupTestDataProvider + * + * @expectedException \chillerlan\QRCode\Output\QRCodeOutputException + * @expectedExceptionMessage Could not write to cache file + */ + public function testSaveToFileException(string $type){ + $this->options->type = $type; + $this->options->cachefile = '\\foo'; + + (new QRCode('foo', new $this->outputInterfaceClass($this->options)))->output(); + } + }