From b754feebf2f1e842e4fcca6371745669ed3af37f Mon Sep 17 00:00:00 2001 From: Mickael Perraud Date: Sat, 11 Sep 2010 17:10:13 +0200 Subject: [PATCH 1/2] Barcode exceptions: review exception, all tests passed --- src/Barcode.php | 34 ++++++++----------- src/Exception.php | 34 ++----------------- src/InvalidArgumentException.php | 40 +++++++++++++++++++++++ src/Object/AbstractObject.php | 26 +++++++-------- src/Object/BarcodeValidationException.php | 40 +++++++++++++++++++++++ src/Object/Ean8.php | 2 +- src/Object/Exception.php | 2 +- src/Object/InvalidArgumentException.php | 40 +++++++++++++++++++++++ src/Object/OutOfRangeException.php | 40 +++++++++++++++++++++++ src/Object/RuntimeException.php | 40 +++++++++++++++++++++++ src/Object/Upce.php | 2 +- src/Renderer/AbstractRenderer.php | 27 ++++++--------- src/Renderer/Exception.php | 3 +- src/Renderer/Image.php | 38 ++++++++++----------- src/Renderer/InvalidArgumentException.php | 40 +++++++++++++++++++++++ src/Renderer/OutOfRangeException.php | 40 +++++++++++++++++++++++ src/Renderer/Pdf.php | 2 +- src/Renderer/RuntimeException.php | 40 +++++++++++++++++++++++ src/Renderer/Svg.php | 14 ++++---- src/Renderer/UnexpectedValueException.php | 40 +++++++++++++++++++++++ src/RendererCreationException.php | 40 +++++++++++++++++++++++ test/FactoryTest.php | 36 +++++++++++++++++++- 22 files changed, 503 insertions(+), 117 deletions(-) create mode 100644 src/InvalidArgumentException.php create mode 100644 src/Object/BarcodeValidationException.php create mode 100644 src/Object/InvalidArgumentException.php create mode 100644 src/Object/OutOfRangeException.php create mode 100644 src/Object/RuntimeException.php create mode 100644 src/Renderer/InvalidArgumentException.php create mode 100644 src/Renderer/OutOfRangeException.php create mode 100644 src/Renderer/RuntimeException.php create mode 100644 src/Renderer/UnexpectedValueException.php create mode 100644 src/RendererCreationException.php diff --git a/src/Barcode.php b/src/Barcode.php index 59a4909..3099e04 100644 --- a/src/Barcode.php +++ b/src/Barcode.php @@ -23,8 +23,7 @@ * @namespace */ namespace Zend\Barcode; -use Zend\Barcode\Renderer, - Zend\Loader\PluginLoader, +use Zend\Loader\PluginLoader, Zend\Loader\ShortNameLocater, Zend\Config\Config, Zend; @@ -78,7 +77,7 @@ public static function setPluginLoader(ShortNameLocater $loader, $type) self::$_loaders[$type] = $loader; return; default: - throw new Exception(sprintf('Invalid type "%s" provided to setPluginLoader()', $type)); + throw new InvalidArgumentException(sprintf('Invalid type "%s" provided to setPluginLoader()', $type)); } } @@ -107,7 +106,7 @@ public static function getPluginLoader($type) } return self::$_loaders[$type]; default: - throw new Exception(sprintf('Invalid type "%s" provided to getPluginLoader()', $type)); + throw new InvalidArgumentException(sprintf('Invalid type "%s" provided to getPluginLoader()', $type)); } } @@ -166,9 +165,8 @@ public static function factory($barcode, try { $barcode = self::makeBarcode($barcode, $barcodeConfig); $renderer = self::makeRenderer($renderer, $rendererConfig); - } catch (Zend\Exception $e) { - $renderable = ($e instanceof Exception) ? $e->isRenderable() : false; - if ($automaticRenderError && $renderable) { + } catch (Exception $e) { + if ($automaticRenderError && !($e instanceof RendererCreationException)) { $barcode = self::makeBarcode('error', array( 'text' => $e->getMessage() )); $renderer = self::makeRenderer($renderer, array()); } else { @@ -215,7 +213,7 @@ public static function makeBarcode($barcode, $barcodeConfig = array()) * Verify that barcode parameters are in an array. */ if (!is_array($barcodeConfig)) { - throw new Exception( + throw new InvalidArgumentException( 'Barcode parameters must be in an array or a \Zend\Config\Config object' ); } @@ -224,7 +222,7 @@ public static function makeBarcode($barcode, $barcodeConfig = array()) * Verify that an barcode name has been specified. */ if (!is_string($barcode) || empty($barcode)) { - throw new Exception( + throw new InvalidArgumentException( 'Barcode name must be specified in a string' ); } @@ -241,7 +239,7 @@ public static function makeBarcode($barcode, $barcodeConfig = array()) * Verify that the object created is a descendent of the abstract barcode type. */ if (!$bcAdapter instanceof BarcodeObject) { - throw new Exception( + throw new InvalidArgumentException( "Barcode class '$barcodeName' does not implement \Zend\Barcode\BarcodeObject" ); } @@ -257,7 +255,7 @@ public static function makeBarcode($barcode, $barcodeConfig = array()) */ public static function makeRenderer($renderer = 'image', $rendererConfig = array()) { - if ($renderer instanceof Renderer) { + if ($renderer instanceof BarcodeRenderer) { return $renderer; } @@ -281,22 +279,18 @@ public static function makeRenderer($renderer = 'image', $rendererConfig = array * Verify that barcode parameters are in an array. */ if (!is_array($rendererConfig)) { - $e = new Exception( + throw new RendererCreationException( 'Barcode parameters must be in an array or a \Zend\Config\Config object' ); - $e->setIsRenderable(false); - throw $e; } /* * Verify that an barcode name has been specified. */ if (!is_string($renderer) || empty($renderer)) { - $e = new Exception( + throw new RendererCreationException( 'Renderer name must be specified in a string' ); - $e->setIsRenderable(false); - throw $e; } $rendererName = self::getPluginLoader(self::RENDERER)->load($renderer); @@ -310,12 +304,10 @@ public static function makeRenderer($renderer = 'image', $rendererConfig = array /* * Verify that the object created is a descendent of the abstract barcode type. */ - if (!$rdrAdapter instanceof Renderer) { - $e = new Exception( + if (!$rdrAdapter instanceof BarcodeRenderer) { + throw new RendererCreationException( "Renderer class '$rendererName' does not implements \Zend\Barcode\Renderer" ); - $e->setIsRenderable(false); - throw $e; } return $rdrAdapter; } diff --git a/src/Exception.php b/src/Exception.php index 346f415..4f18f6c 100644 --- a/src/Exception.php +++ b/src/Exception.php @@ -23,44 +23,16 @@ * @namespace */ namespace Zend\Barcode; -use Zend; /** - * Zend_Barcode_Exception + * Exception for Zend_Barcode component. * - * @uses \Zend\Exception + * @uses Zend\Exception * @category Zend * @package Zend_Barcode * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ -class Exception extends Zend\Exception +interface Exception { - /** - * Is this exception renderable? - * @var bool - */ - protected $_isRenderable = true; - - /** - * Set renderable flag - * - * @param bool $flag - * @return \Zend\Barcode\Exception - */ - public function setIsRenderable($flag) - { - $this->_isRenderable = (bool) $flag; - return $this; - } - - /** - * Retrieve renderable flag - * - * @return bool - */ - public function isRenderable() - { - return $this->_isRenderable; - } } diff --git a/src/InvalidArgumentException.php b/src/InvalidArgumentException.php new file mode 100644 index 0000000..8f29bbd --- /dev/null +++ b/src/InvalidArgumentException.php @@ -0,0 +1,40 @@ += 0 && $value <= 16777125) { $this->_foreColor = intval($value); } else { - throw new Exception( + throw new InvalidArgumentException( 'Text color must be set as #[0-9A-F]{6}' ); } @@ -450,7 +450,7 @@ public function setBackgroundColor($value) } elseif (is_numeric($value) && $value >= 0 && $value <= 16777125) { $this->_backgroundColor = intval($value); } else { - throw new Exception( + throw new InvalidArgumentException( 'Background color must be set as #[0-9A-F]{6}' ); } @@ -714,7 +714,7 @@ public function setFont($value) { if (is_int($value) && $value >= 1 && $value <= 5) { if (!extension_loaded('gd')) { - throw new Exception( + throw new ExtensionNotLoaded( 'GD extension is required to use numeric font' ); } @@ -727,7 +727,7 @@ public function setFont($value) } elseif (is_string($value)) { $this->_font = $value; } else { - throw new Exception(sprintf( + throw new InvalidArgumentException(sprintf( 'Invalid font "%s" provided to setFont()', $value )); @@ -758,7 +758,7 @@ public function setFontSize($value) } if (!is_numeric($value)) { - throw new Exception( + throw new InvalidArgumentException( 'Font size must be a numeric value' ); } @@ -884,7 +884,7 @@ protected function _checkText($value = null) $value = $this->_text; } if (!strlen($value)) { - throw new Exception( + throw new RuntimeException( 'A text must be provide to Barcode before drawing' ); } @@ -902,7 +902,7 @@ protected function _checkRatio($min = 2, $max = 3) { $ratio = $this->_barThickWidth / $this->_barThinWidth; if (!($ratio >= $min && $ratio <= $max)) { - throw new Exception(sprintf( + throw new OutOfRangeException(sprintf( 'Ratio thick/thin bar must be between %0.1f and %0.1f (actual %0.3f)', $min, $max, @@ -919,7 +919,7 @@ protected function _checkRatio($min = 2, $max = 3) protected function _checkFontAndOrientation() { if (is_numeric($this->_font) && $this->_orientation != 0) { - throw new Exception( + throw new RuntimeException( 'Only drawing with TTF font allow orientation of the barcode.' ); } @@ -1242,7 +1242,7 @@ protected function _validateText($value, $options = array()) if (!$validator->isValid($value)) { $message = implode("\n", $validator->getMessages()); - throw new Exception($message); + throw new BarcodeValidationException($message); } } diff --git a/src/Object/BarcodeValidationException.php b/src/Object/BarcodeValidationException.php new file mode 100644 index 0000000..f74144a --- /dev/null +++ b/src/Object/BarcodeValidationException.php @@ -0,0 +1,40 @@ +isValid($value)) { $message = implode("\n", $validator->getMessages()); - throw new Exception($message); + throw new BarcodeValidationException($message); } } } diff --git a/src/Object/Exception.php b/src/Object/Exception.php index 23f11cb..9fb7da9 100644 --- a/src/Object/Exception.php +++ b/src/Object/Exception.php @@ -33,6 +33,6 @@ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ -class Exception extends \Zend\Barcode\Exception +interface Exception extends \Zend\Barcode\Exception { } diff --git a/src/Object/InvalidArgumentException.php b/src/Object/InvalidArgumentException.php new file mode 100644 index 0000000..2e230a1 --- /dev/null +++ b/src/Object/InvalidArgumentException.php @@ -0,0 +1,40 @@ +isValid($value)) { $message = implode("\n", $validator->getMessages()); - throw new Exception($message); + throw new BarcodeValidationException($message); } } diff --git a/src/Renderer/AbstractRenderer.php b/src/Renderer/AbstractRenderer.php index 7e1029c..5fa1321 100644 --- a/src/Renderer/AbstractRenderer.php +++ b/src/Renderer/AbstractRenderer.php @@ -185,7 +185,7 @@ public function getType() public function setTopOffset($value) { if (!is_numeric($value) || intval($value) < 0) { - throw new Exception( + throw new OutOfRangeException( 'Vertical position must be greater than or equals 0' ); } @@ -211,7 +211,7 @@ public function getTopOffset() public function setLeftOffset($value) { if (!is_numeric($value) || intval($value) < 0) { - throw new Exception( + throw new OutOfRangeException( 'Horizontal position must be greater than or equals 0' ); } @@ -247,7 +247,7 @@ public function setAutomaticRenderError($value) public function setHorizontalPosition($value) { if (!in_array($value, array('left' , 'center' , 'right'))) { - throw new Exception( + throw new UnexpectedValueException( "Invalid barcode position provided must be 'left', 'center' or 'right'" ); } @@ -273,7 +273,7 @@ public function getHorizontalPosition() public function setVerticalPosition($value) { if (!in_array($value, array('top' , 'middle' , 'bottom'))) { - throw new Exception( + throw new UnexpectedValueException( "Invalid barcode position provided must be 'top', 'middle' or 'bottom'" ); } @@ -299,7 +299,7 @@ public function getVerticalPosition() public function setModuleSize($value) { if (!is_numeric($value) || floatval($value) <= 0) { - throw new Exception( + throw new OutOfRangeException( 'Float size must be greater than 0' ); } @@ -334,7 +334,7 @@ public function getAutomaticRenderError() public function setBarcode($barcode) { if (!$barcode instanceof BarcodeObject) { - throw new Exception( + throw new InvalidArgumentException( 'Invalid barcode object provided to setBarcode()' ); } @@ -370,7 +370,7 @@ public function checkParams() protected function _checkBarcodeObject() { if ($this->_barcode === null) { - throw new Exception( + throw new RuntimeException( 'No barcode object provided' ); } @@ -430,12 +430,8 @@ public function draw() $this->checkParams(); $this->_initRenderer(); $this->_drawInstructionList(); - } catch (\Zend\Exception $e) { - $renderable = false; - if ($e instanceof Barcode\Exception) { - $renderable = $e->isRenderable(); - } - if ($this->_automaticRenderError && $renderable) { + } catch (\Zend\Barcode\Exception $e) { + if ($this->_automaticRenderError && !($e instanceof RendererCreationException)) { $barcode = Barcode\Barcode::makeBarcode( 'error', array('text' => $e->getMessage()) @@ -445,9 +441,6 @@ public function draw() $this->_initRenderer(); $this->_drawInstructionList(); } else { - if ($e instanceof Barcode\Exception) { - $e->setIsRenderable(false); - } throw $e; } } @@ -482,7 +475,7 @@ private function _drawInstructionList() ); break; default: - throw new Exception( + throw new UnexpectedValueException( 'Unkown drawing command' ); } diff --git a/src/Renderer/Exception.php b/src/Renderer/Exception.php index dbf7378..1a62e7d 100644 --- a/src/Renderer/Exception.php +++ b/src/Renderer/Exception.php @@ -23,7 +23,6 @@ * @namespace */ namespace Zend\Barcode\Renderer; -use Zend\Barcode; /** * \Zend\Barcode\Renderer\Exception @@ -34,6 +33,6 @@ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ -class Exception extends Barcode\Exception +interface Exception extends \Zend\Barcode\Exception { } diff --git a/src/Renderer/Image.php b/src/Renderer/Image.php index 227541b..45fb23a 100644 --- a/src/Renderer/Image.php +++ b/src/Renderer/Image.php @@ -82,10 +82,15 @@ class Image extends AbstractRenderer */ protected $_userWidth = 0; + /** + * Constructor + * @param array|\Zend\Config\Config $options + * @return void + */ public function __construct($options = null) { if (!function_exists('gd_info')) { - throw new Exception('\Zend\Barcode\Renderer\Image requires the GD extension'); + throw new RendererCreationException('\Zend\Barcode\Renderer\Image requires the GD extension'); } parent::__construct($options); @@ -100,7 +105,7 @@ public function __construct($options = null) public function setHeight($value) { if (!is_numeric($value) || intval($value) < 0) { - throw new Exception( + throw new OutOfRangeException( 'Image height must be greater than or equals 0' ); } @@ -127,7 +132,7 @@ public function getHeight() public function setWidth($value) { if (!is_numeric($value) || intval($value) < 0) { - throw new Exception( + throw new OutOfRangeException( 'Image width must be greater than or equals 0' ); } @@ -155,7 +160,7 @@ public function getWidth() public function setResource($image) { if (gettype($image) != 'resource' || get_resource_type($image) != 'gd') { - throw new Exception( + throw new InvalidArgumentException( 'Invalid image resource provided to setResource()' ); } @@ -177,7 +182,7 @@ public function setImageType($value) } if (!in_array($value, $this->_allowedImageType)) { - throw new Exception(sprintf( + throw new InvalidArgumentException(sprintf( 'Invalid type "%s" provided to setImageType()', $value )); @@ -204,14 +209,6 @@ public function getImageType() */ protected function _initRenderer() { - if (!extension_loaded('gd')) { - $e = new Barcode\Exception( - 'Gd extension must be loaded to render barcode as image' - ); - $e->setIsRenderable(false); - throw $e; - } - $barcodeWidth = $this->_barcode->getWidth(true); $barcodeHeight = $this->_barcode->getHeight(true); @@ -287,7 +284,7 @@ protected function _checkDimensions() { if ($this->_resource !== null) { if (imagesy($this->_resource) < $this->_barcode->getHeight(true)) { - throw new Exception( + throw new RuntimeException( 'Barcode is define outside the image (height)' ); } @@ -295,7 +292,7 @@ protected function _checkDimensions() if ($this->_userHeight) { $height = $this->_barcode->getHeight(true); if ($this->_userHeight < $height) { - throw new Exception(sprintf( + throw new RuntimeException(sprintf( "Barcode is define outside the image (calculated: '%d', provided: '%d')", $height, $this->_userHeight @@ -305,7 +302,7 @@ protected function _checkDimensions() } if ($this->_resource !== null) { if (imagesx($this->_resource) < $this->_barcode->getWidth(true)) { - throw new Exception( + throw new RuntimeException( 'Barcode is define outside the image (width)' ); } @@ -313,7 +310,7 @@ protected function _checkDimensions() if ($this->_userWidth) { $width = $this->_barcode->getWidth(true); if ($this->_userWidth < $width) { - throw new Exception(sprintf( + throw new RuntimeException(sprintf( "Barcode is define outside the image (calculated: '%d', provided: '%d')", $width, $this->_userWidth @@ -400,7 +397,7 @@ protected function _drawText($text, $size, $position, $font, $color, $alignment * to informe user of the problem instead of simply not drawing * the text */ - throw new Exception( + throw new RuntimeException( 'No orientation possible with GD internal font' ); } @@ -421,9 +418,8 @@ protected function _drawText($text, $size, $position, $font, $color, $alignment } else { if (!function_exists('imagettfbbox')) { - throw new Exception( - 'A font was provided, but this instance of PHP does not have TTF (FreeType) support' - ); + throw new RuntimeException( + 'A font was provided, but this instance of PHP does not have TTF (FreeType) support'); } $box = imagettfbbox($size, 0, $font, $text); diff --git a/src/Renderer/InvalidArgumentException.php b/src/Renderer/InvalidArgumentException.php new file mode 100644 index 0000000..662dcca --- /dev/null +++ b/src/Renderer/InvalidArgumentException.php @@ -0,0 +1,40 @@ +_readRootElement(); $height = (float) $this->_rootElement->getAttribute('height'); if ($height < $this->_barcode->getHeight(true)) { - throw new Exception( + throw new RuntimeException( 'Barcode is define outside the image (height)' ); } @@ -253,7 +253,7 @@ protected function _checkDimensions() if ($this->_userHeight) { $height = $this->_barcode->getHeight(true); if ($this->_userHeight < $height) { - throw new Exception(sprintf( + throw new RuntimeException(sprintf( "Barcode is define outside the image (calculated: '%d', provided: '%d')", $height, $this->_userHeight @@ -265,7 +265,7 @@ protected function _checkDimensions() $this->_readRootElement(); $width = $this->_rootElement->getAttribute('width'); if ($width < $this->_barcode->getWidth(true)) { - throw new Exception( + throw new RuntimeException( 'Barcode is define outside the image (width)' ); } @@ -273,7 +273,7 @@ protected function _checkDimensions() if ($this->_userWidth) { $width = (float) $this->_barcode->getWidth(true); if ($this->_userWidth < $width) { - throw new Exception(sprintf( + throw new RuntimeException(sprintf( "Barcode is define outside the image (calculated: '%d', provided: '%d')", $width, $this->_userWidth diff --git a/src/Renderer/UnexpectedValueException.php b/src/Renderer/UnexpectedValueException.php new file mode 100644 index 0000000..4ddc3c2 --- /dev/null +++ b/src/Renderer/UnexpectedValueException.php @@ -0,0 +1,40 @@ + __DIR__ . '/Renderer/TestAsset')); Barcode\Barcode::setPluginLoader($loader, Barcode\Barcode::RENDERER); $renderer = Barcode\Barcode::makeRenderer('rendererNamespace'); - $this->assertTrue($renderer instanceof \Zend\Barcode\Renderer); + $this->assertTrue($renderer instanceof \Zend\Barcode\BarcodeRenderer); } public function testBarcodeFactoryWithNamespaceButWithoutExtendingRendererAbstract() @@ -326,6 +326,16 @@ public function testBarcodeRendererFactoryWithUnexistantRenderer() } public function testProxyBarcodeRendererDrawAsImage() + { + if (! extension_loaded('gd')) { + $this->markTestSkipped('GD extension is required to run this test'); + } + $resource = Barcode\Barcode::draw('code25', 'image', array('text' => '012345')); + $this->assertTrue(gettype($resource) == 'resource', 'Image must be a resource'); + $this->assertTrue(get_resource_type($resource) == 'gd', 'Image must be a GD resource'); + } + + public function testProxyBarcodeRendererDrawAsImageAutomaticallyRenderImageIfException() { if (! extension_loaded('gd')) { $this->markTestSkipped('GD extension is required to run this test'); @@ -336,6 +346,14 @@ public function testProxyBarcodeRendererDrawAsImage() } public function testProxyBarcodeRendererDrawAsPdf() + { + Barcode\Barcode::setBarcodeFont(__DIR__ . '/Object/_fonts/Vera.ttf'); + $resource = Barcode\Barcode::draw('code25', 'pdf', array('text' => '012345')); + $this->assertTrue($resource instanceof Pdf\PdfDocument); + Barcode\Barcode::setBarcodeFont(''); + } + + public function testProxyBarcodeRendererDrawAsPdfAutomaticallyRenderPdfIfException() { Barcode\Barcode::setBarcodeFont(__DIR__ . '/Object/_fonts/Vera.ttf'); $resource = Barcode\Barcode::draw('code25', 'pdf'); @@ -343,6 +361,22 @@ public function testProxyBarcodeRendererDrawAsPdf() Barcode\Barcode::setBarcodeFont(''); } + public function testProxyBarcodeRendererDrawAsSvg() + { + Barcode\Barcode::setBarcodeFont(__DIR__ . '/Object/_fonts/Vera.ttf'); + $resource = Barcode\Barcode::draw('code25', 'svg', array('text' => '012345')); + $this->assertTrue($resource instanceof \DOMDocument); + Barcode\Barcode::setBarcodeFont(''); + } + + public function testProxyBarcodeRendererDrawAsSvgAutomaticallyRenderSvgIfException() + { + Barcode\Barcode::setBarcodeFont(__DIR__ . '/Object/_fonts/Vera.ttf'); + $resource = Barcode\Barcode::draw('code25', 'svg'); + $this->assertTrue($resource instanceof \DOMDocument); + Barcode\Barcode::setBarcodeFont(''); + } + public function testProxyBarcodeObjectFont() { Barcode\Barcode::setBarcodeFont('my_font.ttf'); From d1a20182345b2dda256e832f2d2cc122c7366646 Mon Sep 17 00:00:00 2001 From: Mickael Perraud Date: Tue, 21 Sep 2010 19:26:18 +0200 Subject: [PATCH 2/2] Barcode: add missing use statement for exception --- src/Renderer/Image.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Renderer/Image.php b/src/Renderer/Image.php index 4bfd3ef..842b866 100644 --- a/src/Renderer/Image.php +++ b/src/Renderer/Image.php @@ -25,6 +25,7 @@ */ namespace Zend\Barcode\Renderer; use Zend\Barcode, + Zend\Barcode\Exception\RendererCreationException, Zend\Barcode\Renderer\Exception\RuntimeException, Zend\Barcode\Renderer\Exception\OutOfRangeException, Zend\Barcode\Renderer\Exception\InvalidArgumentException;