Преглед изворни кода

ZF-8809
- Fixed Zend_Barcode tests and reliance on TTF support in GD

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@20366 44c647ce-9c0f-0410-b52a-842ac1e357ba

ralph пре 16 година
родитељ
комит
fdf67e80f2

+ 8 - 0
library/Zend/Barcode/Renderer/Image.php

@@ -435,6 +435,14 @@ class Zend_Barcode_Renderer_Image extends Zend_Barcode_Renderer_RendererAbstract
             }
             imagestring($this->_resource, $font, $positionX, $positionY, $text, $color);
         } else {
+            
+            if (!function_exists('imagettfbbox')) {
+                require_once 'Zend/Barcode/Renderer/Exception.php';
+                throw new Zend_Barcode_Renderer_Exception(
+                    'A font was provided, but this instance of PHP does not have TTF (FreeType) support'
+                    );
+            }
+            
             $box = imagettfbbox($size, 0, $font, $text);
             switch ($alignment) {
                 case 'left':

+ 39 - 0
tests/Zend/Barcode/FactoryTest.php

@@ -38,6 +38,8 @@ class Zend_Barcode_FactoryTest extends PHPUnit_Framework_TestCase
 
     public function testMinimalFactory()
     {
+        $this->_checkGDRequirement();
+        
         $renderer = Zend_Barcode::factory('code39');
         $this->assertTrue($renderer instanceof Zend_Barcode_Renderer_Image);
         $this->assertTrue($renderer->getBarcode() instanceof Zend_Barcode_Object_Code39);
@@ -52,6 +54,8 @@ class Zend_Barcode_FactoryTest extends PHPUnit_Framework_TestCase
 
     public function testFactoryWithOptions()
     {
+        $this->_checkGDRequirement();
+        
         $options = array('barHeight' => 123);
         $renderer = Zend_Barcode::factory('code39', 'image', $options);
         $this->assertEquals(123, $renderer->getBarcode()->getBarHeight());
@@ -59,6 +63,8 @@ class Zend_Barcode_FactoryTest extends PHPUnit_Framework_TestCase
 
     public function testFactoryWithAutomaticExceptionRendering()
     {
+        $this->_checkGDRequirement();
+        
         $options = array('barHeight' => - 1);
         $renderer = Zend_Barcode::factory('code39', 'image', $options);
         $this->assertTrue($renderer instanceof Zend_Barcode_Renderer_Image);
@@ -79,6 +85,8 @@ class Zend_Barcode_FactoryTest extends PHPUnit_Framework_TestCase
      */
     public function testFactoryWithoutAutomaticRendererExceptionRendering()
     {
+        $this->_checkGDRequirement();
+        
         $options = array('imageType' => 'my');
         $renderer = Zend_Barcode::factory('code39', 'image', array(), $options, false);
         $this->markTestIncomplete('Need to throw a configuration exception in renderer');
@@ -86,6 +94,8 @@ class Zend_Barcode_FactoryTest extends PHPUnit_Framework_TestCase
 
     public function testFactoryWithZendConfig()
     {
+        $this->_checkGDRequirement();
+        
         $config = new Zend_Config(
                 array('barcode' => 'code39' ,
                         'renderer' => 'image'));
@@ -97,6 +107,8 @@ class Zend_Barcode_FactoryTest extends PHPUnit_Framework_TestCase
 
     public function testFactoryWithZendConfigAndObjectOptions()
     {
+        $this->_checkGDRequirement();
+        
         $config = new Zend_Config(
                 array('barcode' => 'code25' ,
                         'barcodeParams' => array(
@@ -109,6 +121,8 @@ class Zend_Barcode_FactoryTest extends PHPUnit_Framework_TestCase
 
     public function testFactoryWithZendConfigAndRendererOptions()
     {
+        $this->_checkGDRequirement();
+        
         $config = new Zend_Config(
                 array('barcode' => 'code25' ,
                         'rendererParams' => array(
@@ -121,6 +135,8 @@ class Zend_Barcode_FactoryTest extends PHPUnit_Framework_TestCase
 
     public function testFactoryWithoutBarcodeWithAutomaticExceptionRender()
     {
+        $this->_checkGDRequirement();
+        
         $renderer = Zend_Barcode::factory(null);
         $this->assertTrue($renderer instanceof Zend_Barcode_Renderer_Image);
         $this->assertTrue($renderer->getBarcode() instanceof Zend_Barcode_Object_Error);
@@ -128,6 +144,8 @@ class Zend_Barcode_FactoryTest extends PHPUnit_Framework_TestCase
 
     public function testFactoryWithoutBarcodeWithAutomaticExceptionRenderWithZendConfig()
     {
+        $this->_checkGDRequirement();
+        
         $config = new Zend_Config(array('barcode' => null));
         $renderer = Zend_Barcode::factory($config);
         $this->assertTrue($renderer instanceof Zend_Barcode_Renderer_Image);
@@ -136,6 +154,8 @@ class Zend_Barcode_FactoryTest extends PHPUnit_Framework_TestCase
 
     public function testFactoryWithExistingBarcodeObject()
     {
+        $this->_checkGDRequirement();
+        
         $barcode = new Zend_Barcode_Object_Code25();
         $renderer = Zend_Barcode::factory($barcode);
         $this->assertSame($barcode, $renderer->getBarcode());
@@ -230,6 +250,8 @@ class Zend_Barcode_FactoryTest extends PHPUnit_Framework_TestCase
 
     public function testBarcodeRendererFactoryWithExistingBarcodeRenderer()
     {
+        $this->_checkGDRequirement();
+        
         $renderer = new Zend_Barcode_Renderer_Image();
         $generatedBarcode = Zend_Barcode::makeRenderer($renderer);
         $this->assertSame($renderer, $generatedBarcode);
@@ -237,12 +259,16 @@ class Zend_Barcode_FactoryTest extends PHPUnit_Framework_TestCase
 
     public function testBarcodeRendererFactoryWithBarcodeAsString()
     {
+        $this->_checkGDRequirement();
+        
         $renderer = Zend_Barcode::makeRenderer('image');
         $this->assertTrue($renderer instanceof Zend_Barcode_Renderer_Image);
     }
 
     public function testBarcodeRendererFactoryWithBarcodeAsStringAndConfigAsArray()
     {
+        $this->_checkGDRequirement();
+        
         $renderer = Zend_Barcode::makeRenderer('image', array('imageType' => 'gif'));
         $this->assertTrue($renderer instanceof Zend_Barcode_Renderer_Image);
         $this->assertSame('gif', $renderer->getImageType());
@@ -250,6 +276,8 @@ class Zend_Barcode_FactoryTest extends PHPUnit_Framework_TestCase
 
     public function testBarcodeRendererFactoryWithBarcodeAsStringAndConfigAsZendConfig()
     {
+        $this->_checkGDRequirement();
+        
         $config = new Zend_Config(array('imageType' => 'gif'));
         $renderer = Zend_Barcode::makeRenderer('image', $config);
         $this->assertTrue($renderer instanceof Zend_Barcode_Renderer_Image);
@@ -258,6 +286,8 @@ class Zend_Barcode_FactoryTest extends PHPUnit_Framework_TestCase
 
     public function testBarcodeRendererFactoryWithBarcodeAsZendConfig()
     {
+        $this->_checkGDRequirement();
+        
         $config = new Zend_Config(
                 array('renderer' => 'image' ,
                         'rendererParams' => array(
@@ -289,6 +319,8 @@ class Zend_Barcode_FactoryTest extends PHPUnit_Framework_TestCase
 
     public function testBarcodeRendererFactoryWithNamespace()
     {
+        $this->_checkGDRequirement();
+        
         require_once dirname(__FILE__) . '/Renderer/_files/RendererNamespace.php';
         $renderer = Zend_Barcode::makeRenderer('image',
                 array(
@@ -341,4 +373,11 @@ class Zend_Barcode_FactoryTest extends PHPUnit_Framework_TestCase
         $this->assertSame('my_font.ttf', $barcode->getFont());
         Zend_Barcode::setBarcodeFont('');
     }
+    
+    protected function _checkGDRequirement()
+    {
+        if (!extension_loaded('gd')) {
+            $this->markTestSkipped('This test requires the GD extension');
+        }
+    }
 }

+ 38 - 61
tests/Zend/Barcode/Renderer/ImageTest.php

@@ -40,7 +40,7 @@ class Zend_Barcode_Renderer_ImageTest extends Zend_Barcode_Renderer_TestCommon
 
     public function setUp()
     {
-        if (!function_exists('gd_info')) {
+        if (!extension_loaded('gd')) {
             $this->markTestSkipped('Zend_Barcode_Renderer_ImageTest requires the GD extension');
         }
         parent::setUp();
@@ -58,10 +58,6 @@ class Zend_Barcode_Renderer_ImageTest extends Zend_Barcode_Renderer_TestCommon
 
     public function testGoodImageResource()
     {
-        if (! extension_loaded('gd')) {
-            $this->markTestSkipped(
-                    'GD extension is required to run this test');
-        }
         $imageResource = imagecreatetruecolor(1, 1);
         $this->_renderer->setResource($imageResource);
     }
@@ -130,10 +126,8 @@ class Zend_Barcode_Renderer_ImageTest extends Zend_Barcode_Renderer_TestCommon
 
     public function testDrawReturnResource()
     {
-        if (! extension_loaded('gd')) {
-            $this->markTestSkipped(
-                    'GD extension is required to run this test');
-        }
+        $this->_checkTTFRequirement();
+        
         $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
         $this->_renderer->setBarcode($barcode);
         $resource = $this->_renderer->draw();
@@ -144,10 +138,8 @@ class Zend_Barcode_Renderer_ImageTest extends Zend_Barcode_Renderer_TestCommon
 
     public function testDrawWithExistantResourceReturnResource()
     {
-        if (! extension_loaded('gd')) {
-            $this->markTestSkipped(
-                    'GD extension is required to run this test');
-        }
+        $this->_checkTTFRequirement();
+        
         $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
         $this->_renderer->setBarcode($barcode);
         $imageResource = imagecreatetruecolor(500, 500);
@@ -203,10 +195,6 @@ class Zend_Barcode_Renderer_ImageTest extends Zend_Barcode_Renderer_TestCommon
 
     public function testGoodHeightOfUserResource()
     {
-        if (! extension_loaded('gd')) {
-            $this->markTestSkipped(
-                    'GD extension is required to run this test');
-        }
         $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
         $this->assertEquals(62, $barcode->getHeight());
         $imageResource = imagecreatetruecolor(500, 62);
@@ -220,10 +208,6 @@ class Zend_Barcode_Renderer_ImageTest extends Zend_Barcode_Renderer_TestCommon
      */
     public function testBadHeightOfUserResource()
     {
-        if (! extension_loaded('gd')) {
-            $this->markTestSkipped(
-                    'GD extension is required to run this test');
-        }
         $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
         $this->assertEquals(62, $barcode->getHeight());
         $this->_renderer->setBarcode($barcode);
@@ -234,10 +218,6 @@ class Zend_Barcode_Renderer_ImageTest extends Zend_Barcode_Renderer_TestCommon
 
     public function testGoodWidthOfUserResource()
     {
-        if (! extension_loaded('gd')) {
-            $this->markTestSkipped(
-                    'GD extension is required to run this test');
-        }
         $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
         $this->assertEquals(211, $barcode->getWidth());
         $imageResource = imagecreatetruecolor(211, 500);
@@ -251,10 +231,6 @@ class Zend_Barcode_Renderer_ImageTest extends Zend_Barcode_Renderer_TestCommon
      */
     public function testBadWidthOfUserResource()
     {
-        if (! extension_loaded('gd')) {
-            $this->markTestSkipped(
-                    'GD extension is required to run this test');
-        }
         $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
         $this->assertEquals(211, $barcode->getWidth());
         $this->_renderer->setBarcode($barcode);
@@ -268,10 +244,6 @@ class Zend_Barcode_Renderer_ImageTest extends Zend_Barcode_Renderer_TestCommon
      */
     public function testNoFontWithOrientation()
     {
-        if (! extension_loaded('gd')) {
-            $this->markTestSkipped(
-                    'GD extension is required to run this test');
-        }
         Zend_Barcode::setBarcodeFont('');
         $barcode = new Zend_Barcode_Object_Code39(array('text' => '0123456789'));
         $barcode->setOrientation(1);
@@ -289,64 +261,69 @@ class Zend_Barcode_Renderer_ImageTest extends Zend_Barcode_Renderer_TestCommon
      */
     public function testRendererWithUnkownInstructionProvideByObject()
     {
-        if (! extension_loaded('gd')) {
-            $this->markTestSkipped(
-                    'GD extension is required to run this test');
-        }
         parent::testRendererWithUnkownInstructionProvideByObject();
     }
 
     public function testHorizontalPositionToLeft()
     {
-        if (! extension_loaded('gd')) {
-            $this->markTestSkipped(
-                    'GD extension is required to run this test');
-        }
+        $this->_checkTTFRequirement();
+        
         parent::testHorizontalPositionToLeft();
     }
 
     public function testHorizontalPositionToCenter()
     {
-        if (! extension_loaded('gd')) {
-            $this->markTestSkipped(
-                    'GD extension is required to run this test');
-        }
+        $this->_checkTTFRequirement();
+        
         parent::testHorizontalPositionToCenter();
     }
 
     public function testHorizontalPositionToRight()
     {
-        if (! extension_loaded('gd')) {
-            $this->markTestSkipped(
-                    'GD extension is required to run this test');
-        }
+        $this->_checkTTFRequirement();
+        
         parent::testHorizontalPositionToRight();
     }
 
     public function testVerticalPositionToTop()
     {
-        if (! extension_loaded('gd')) {
-            $this->markTestSkipped(
-                    'GD extension is required to run this test');
-        }
+        $this->_checkTTFRequirement();
+        
         parent::testVerticalPositionToTop();
     }
 
     public function testVerticalPositionToMiddle()
     {
-        if (! extension_loaded('gd')) {
-            $this->markTestSkipped(
-                    'GD extension is required to run this test');
-        }
+        $this->_checkTTFRequirement();
+        
         parent::testVerticalPositionToMiddle();
     }
 
     public function testVerticalPositionToBottom()
     {
-        if (! extension_loaded('gd')) {
-            $this->markTestSkipped(
-                    'GD extension is required to run this test');
-        }
+        $this->_checkTTFRequirement();
+        
         parent::testVerticalPositionToBottom();
     }
+    
+    public function testLeftOffsetOverrideHorizontalPosition()
+    {
+        $this->_checkTTFRequirement();
+        
+        parent::testLeftOffsetOverrideHorizontalPosition();
+    }
+    
+    public function testTopOffsetOverrideVerticalPosition()
+    {
+        $this->_checkTTFRequirement();
+        
+        parent::testTopOffsetOverrideVerticalPosition();
+    } 
+    
+    protected function _checkTTFRequirement()
+    {
+        if (!function_exists('imagettfbbox')) {
+            $this->markTestSkipped('TTF (FreeType) support is required in order to run this test');
+        }
+    }
 }