From d1561118846d92f1c8e107a52c09512d764cbeaf Mon Sep 17 00:00:00 2001 From: endroid Date: Sat, 16 Apr 2016 22:09:48 +0000 Subject: [PATCH] Add test for image size --- src/QrCode.php | 18 ++++++++------- tests/QrCodeTest.php | 52 ++++++++++++++++++++++++++------------------ 2 files changed, 41 insertions(+), 29 deletions(-) diff --git a/src/QrCode.php b/src/QrCode.php index ac2fadf..ab20a74 100755 --- a/src/QrCode.php +++ b/src/QrCode.php @@ -80,7 +80,7 @@ class QrCode const LABEL_VALIGN_BOTTOM = 4; /** @var string */ - protected $logo = NULL; + protected $logo = null; protected $logo_size = 48; @@ -379,16 +379,19 @@ public function getPath() * Set logo in QR Code. * * @param string $logo Logo Path + * * @throws Exceptions\DataDoesntExistsException + * * @return QrCode */ public function setLogo($logo) { - if(!file_exists($logo)){ + if (!file_exists($logo)) { throw new DataDoesntExistsException("$logo file does not exist"); } - + $this->logo = $logo; + return $this; } @@ -1532,11 +1535,10 @@ public function create() } } - if(!empty($this->logo)) - { + if (!empty($this->logo)) { $logo_image = call_user_func('imagecreatefrom'.$this->image_type, $this->logo); - if(!$logo_image){ - throw new ImageFunctionFailedException('imagecreatefrom'.$this->image_type . ' ' . $this->logo . 'failed' ); + if (!$logo_image) { + throw new ImageFunctionFailedException('imagecreatefrom'.$this->image_type.' '.$this->logo.'failed'); } $src_w = imagesx($logo_image); $src_h = imagesy($logo_image); @@ -1545,7 +1547,7 @@ public function create() $dst_y = ($this->size + $this->padding * 2 - $this->logo_size) / 2; $successful = imagecopyresampled($output_image, $logo_image, $dst_x, $dst_y, 0, 0, $this->logo_size, $this->logo_size, $src_w, $src_h); - if(!$successful){ + if (!$successful) { throw new ImageFunctionFailedException('add logo [image'.$this->format.'] failed.'); } imagedestroy($logo_image); diff --git a/tests/QrCodeTest.php b/tests/QrCodeTest.php index 58f58a1..daace09 100755 --- a/tests/QrCodeTest.php +++ b/tests/QrCodeTest.php @@ -26,7 +26,7 @@ class QrCodeTest extends PHPUnit_Framework_TestCase */ public function testGetDataUri() { - $qrCode = $this->getQrCode(); + $qrCode = $this->createQrCode(); $dataUri = $qrCode->getDataUri(); $this->assertTrue(is_string($dataUri)); @@ -40,12 +40,27 @@ public function testGetDataUri() */ public function testGetImageString() { - $qrCode = $this->getQrCode(); + $qrCode = $this->createQrCode(); $imageString = $qrCode->get('png'); $this->assertTrue(is_string($imageString)); } + /** + * Tests if the resulting image size is correct. + */ + public function testImageSize() + { + $qrCode = $this->createQrCode(); + + $size = $qrCode->getSize(); + $padding = $qrCode->getPadding(); + $image = $qrCode->getImage(); + + $this->assertTrue($padding > 0); + $this->assertTrue(imagesx($image) == $size + 2 * $padding); + } + /** * Tests if a valid image string is returned. * @@ -60,18 +75,6 @@ public function testGetQrCodeWithLogoString() $this->assertTrue(is_string($imageString)); } - /** - * Returns a QR code. - */ - protected function getQrCode() - { - if (!$this->qrCode) { - $this->qrCode = $this->createQrCode(); - } - - return $this->qrCode; - } - /** * Creates a QR code. * @@ -80,19 +83,26 @@ protected function getQrCode() protected function createQrCode() { $qrCode = new QrCode(); - $qrCode->setText('Life is too short to be generating QR codes'); - $qrCode->setSize(300); + $qrCode + ->setText('Life is too short to be generating QR codes') + ->setSize(300) + ->setPadding(20); return $qrCode; } + /** + * Creates a QR code with a logo. + * + * @return QrCode + */ protected function createQrCodeWithLogo() { - $qrCode = new QrCode(); - $qrCode->setText('Life is too short to be generating QR codes') - ->setSize(300) - ->setLogo(dirname(__DIR__) . '/assets/image/logo.png') - ->setLogoSize(60); + $qrCode = $this->createQrCode(); + $qrCode + ->setLogo(dirname(__DIR__).'/assets/image/logo.png') + ->setLogoSize(60) + ; return $qrCode; }