From e309858afbaca44dbc0b555b67ee516a87e0bfea Mon Sep 17 00:00:00 2001 From: peter279k Date: Fri, 2 Mar 2018 02:10:50 +0800 Subject: [PATCH] Travis 5.3 test (#46) * add the PHP 5.3 in Travis Build. * Fix some issues for the source code when running the PHP 5.3 Travis build. --- .travis.yml | 4 ++ src/Utilities/SVGStyleParser.php | 4 +- src/Writing/SVGWriter.php | 3 +- tests/Nodes/SVGNodeContainerTest.php | 44 ++++++++++++------- tests/Nodes/Shapes/SVGPathTest.php | 10 +++++ tests/Nodes/Shapes/SVGPolygonalShapeTest.php | 3 +- .../Renderers/SVGImageRendererTest.php | 4 +- tests/Reading/SVGReaderTest.php | 30 ++++++++----- tests/Writing/SVGWriterTest.php | 20 ++++----- 9 files changed, 80 insertions(+), 42 deletions(-) diff --git a/.travis.yml b/.travis.yml index fa28736..867ec63 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,6 +6,10 @@ php: - 7.0 - 7.1 - 7.2 +matrix: + include: + - php: 5.3 + dist: precise install: - composer install --no-interaction script: vendor/bin/phpunit --configuration .phpunit.xml diff --git a/src/Utilities/SVGStyleParser.php b/src/Utilities/SVGStyleParser.php index 27b3d40..e4fc167 100644 --- a/src/Utilities/SVGStyleParser.php +++ b/src/Utilities/SVGStyleParser.php @@ -47,12 +47,12 @@ public static function parseStyles($string) */ public static function parseCss($css) { - $result = []; + $result = array(); preg_match_all('/(?ims)([a-z0-9\s\,\.\:#_\-@^*()\[\]\"\'=]+)\{([^\}]*)\}/', $css, $arr); foreach ($arr[0] as $i => $x) { $selectors = explode(',', trim($arr[1][$i])); - if (in_array($selectors[0], ['@font-face', '@keyframes', '@media'])) { + if (in_array($selectors[0], array('@font-face', '@keyframes', '@media'))) { continue; } $rules = self::parseStyles(trim($arr[2][$i])); diff --git a/src/Writing/SVGWriter.php b/src/Writing/SVGWriter.php index e3b91e2..980913d 100644 --- a/src/Writing/SVGWriter.php +++ b/src/Writing/SVGWriter.php @@ -18,6 +18,7 @@ class SVGWriter public function __construct() { $this->outString = ''; + defined('ENT_XML1') ? ENT_XML1 : define('ENT_XML1', 16); } /** @@ -120,7 +121,7 @@ private function appendAttributes(array $attrs) * @return void */ private function appendAttribute($attrName, $attrValue) - { + { $attrName = htmlspecialchars($attrName, ENT_XML1 | ENT_COMPAT); $attrValue = htmlspecialchars($attrValue, ENT_XML1 | ENT_COMPAT); diff --git a/tests/Nodes/SVGNodeContainerTest.php b/tests/Nodes/SVGNodeContainerTest.php index a1f0f50..4077e9e 100644 --- a/tests/Nodes/SVGNodeContainerTest.php +++ b/tests/Nodes/SVGNodeContainerTest.php @@ -87,20 +87,27 @@ public function testRasterize() public function testGetElementsByTagName() { $obj = new SVGNodeContainerSubclass(); + $root_0 = new \SVG\Nodes\Structures\SVGGroup(); + $root_0_0 = new \SVG\Nodes\Shapes\SVGLine(); + $root_0_1 = new \SVG\Nodes\Shapes\SVGRect(); $obj->addChild( - $root_0 = (new \SVG\Nodes\Structures\SVGGroup())->addChild( - $root_0_0 = new \SVG\Nodes\Shapes\SVGLine() + $root_0->addChild( + $root_0_0 )->addChild( - $root_0_1 = new \SVG\Nodes\Shapes\SVGRect() + $root_0_1 ) ); + $root_1 = new \SVG\Nodes\Structures\SVGGroup(); + $root_1_0 = new \SVG\Nodes\Structures\SVGGroup(); + $root_1_0_0 = new \SVG\Nodes\Shapes\SVGRect(); + $root_1_1 = new \SVG\Nodes\Shapes\SVGRect(); $obj->addChild( - $root_1 = (new \SVG\Nodes\Structures\SVGGroup())->addChild( - $root_1_0 = (new \SVG\Nodes\Structures\SVGGroup())->addChild( - $root_1_0_0 = new \SVG\Nodes\Shapes\SVGRect() + $root_1->addChild( + $root_1_0->addChild( + $root_1_0_0 ) )->addChild( - $root_1_1 = new \SVG\Nodes\Shapes\SVGRect() + $root_1_1 ) ); @@ -122,21 +129,28 @@ public function testGetElementsByTagName() public function testGetElementsByClassName() { - $obj = (new SVGNodeContainerSubclass()); + $obj = new SVGNodeContainerSubclass(); + $root_0 = new \SVG\Nodes\Structures\SVGGroup(); + $root_0_0 = new \SVG\Nodes\Shapes\SVGRect(); + $root_0_1 = new \SVG\Nodes\Shapes\SVGRect(); $obj->addChild( - $root_0 = (new \SVG\Nodes\Structures\SVGGroup())->addChild( - $root_0_0 = new \SVG\Nodes\Shapes\SVGRect() + $root_0->addChild( + $root_0_0 )->addChild( - $root_0_1 = new \SVG\Nodes\Shapes\SVGRect() + $root_0_1 ) ); + $root_1 = new \SVG\Nodes\Structures\SVGGroup(); + $root_1_0 = new \SVG\Nodes\Structures\SVGGroup(); + $root_1_0_0 = new \SVG\Nodes\Shapes\SVGRect(); + $root_1_1 = new \SVG\Nodes\Shapes\SVGRect(); $obj->addChild( - $root_1 = (new \SVG\Nodes\Structures\SVGGroup())->addChild( - $root_1_0 = (new \SVG\Nodes\Structures\SVGGroup())->addChild( - $root_1_0_0 = new \SVG\Nodes\Shapes\SVGRect() + $root_1->addChild( + $root_1_0->addChild( + $root_1_0_0 ) )->addChild( - $root_1_1 = new \SVG\Nodes\Shapes\SVGRect() + $root_1_1 ) ); diff --git a/tests/Nodes/Shapes/SVGPathTest.php b/tests/Nodes/Shapes/SVGPathTest.php index 416d943..22af963 100644 --- a/tests/Nodes/Shapes/SVGPathTest.php +++ b/tests/Nodes/Shapes/SVGPathTest.php @@ -53,6 +53,16 @@ public function testSetDescription() $this->assertSame(self::$sampleDescription, $obj->getAttribute('d')); } + public function testRasterizeWithNull() + { + $rast = $this->getMockBuilder('\SVG\Rasterization\SVGRasterizer') + ->disableOriginalConstructor() + ->getMock(); + $obj = new SVGPath(); + + $this->assertNull($obj->rasterize($rast)); + } + public function testRasterize() { $obj = new SVGPath(self::$sampleDescription); diff --git a/tests/Nodes/Shapes/SVGPolygonalShapeTest.php b/tests/Nodes/Shapes/SVGPolygonalShapeTest.php index 0e9a2de..b9bc901 100644 --- a/tests/Nodes/Shapes/SVGPolygonalShapeTest.php +++ b/tests/Nodes/Shapes/SVGPolygonalShapeTest.php @@ -107,8 +107,9 @@ public function testGetPoint() array(37, 37), )); $obj->setPoint(1, array(100, 100)); + $point = $obj->getPoint(0); - $this->assertSame(42.5, $obj->getPoint(0)[0]); + $this->assertSame(42.5, $point[0]); } public function testGetSerializableAttributes() diff --git a/tests/Rasterization/Renderers/SVGImageRendererTest.php b/tests/Rasterization/Renderers/SVGImageRendererTest.php index f21faee..c74b83d 100644 --- a/tests/Rasterization/Renderers/SVGImageRendererTest.php +++ b/tests/Rasterization/Renderers/SVGImageRendererTest.php @@ -25,13 +25,13 @@ class SVGImageRendererTest extends \PHPUnit\Framework\TestCase public function testRender() { $rast = new \SVG\Rasterization\SVGRasterizer(10, 20, null, 100, 200); - $options = [ + $options = array( 'href' => __DIR__.'/../../sample.svg', 'x' => 10.5, 'y' => 10.5, 'width' => 100.5, 'height' => 100.5 - ]; + ); $svgImageRender = new SVGImageRenderer(); $context = new SVGNodeClass(); diff --git a/tests/Reading/SVGReaderTest.php b/tests/Reading/SVGReaderTest.php index 13fba9e..f090f8d 100644 --- a/tests/Reading/SVGReaderTest.php +++ b/tests/Reading/SVGReaderTest.php @@ -59,18 +59,20 @@ public function setUp() public function testShouldReturnAnImageOrNull() { // should return an instance of SVGImage - $result = (new SVGReader())->parseString($this->xml); + $svgReader = new SVGReader(); + $result = $svgReader->parseString($this->xml); $this->assertInstanceOf('\SVG\SVGImage', $result); // should return null when parsing fails - $result = (new SVGReader())->parseString(''); + $result = $svgReader->parseString(''); $this->assertNull($result); } public function testShouldSetAllAttributesAndNamespaces() { // should retain all document attributes and namespaces - $result = (new SVGReader())->parseString($this->xml); + $svgReader = new SVGReader(); + $result = $svgReader->parseString($this->xml); $this->assertEquals(array( 'xmlns' => 'http://www.w3.org/2000/svg', 'xmlns:xlink' => 'http://www.w3.org/1999/xlink', @@ -81,7 +83,8 @@ public function testShouldSetAllAttributesAndNamespaces() ), $result->getDocument()->getSerializableAttributes()); // should deal with missing viewBox - $result = (new SVGReader())->parseString($this->xmlNoViewBox); + $svgReader = new SVGReader(); + $result = $svgReader->parseString($this->xmlNoViewBox); $this->assertEquals(array( 'xmlns' => 'http://www.w3.org/2000/svg', 'xmlns:xlink' => 'http://www.w3.org/1999/xlink', @@ -90,7 +93,8 @@ public function testShouldSetAllAttributesAndNamespaces() ), $result->getDocument()->getSerializableAttributes()); // should deal with missing width/height - $result = (new SVGReader())->parseString($this->xmlNoWH); + $svgReader = new SVGReader(); + $result = $svgReader->parseString($this->xmlNoWH); $this->assertEquals(array( 'xmlns' => 'http://www.w3.org/2000/svg', 'xmlns:xlink' => 'http://www.w3.org/1999/xlink', @@ -98,7 +102,8 @@ public function testShouldSetAllAttributesAndNamespaces() ), $result->getDocument()->getSerializableAttributes()); // should set all attributes, including namespace prefixed ones - $result = (new SVGReader())->parseString($this->xml); + $svgReader = new SVGReader(); + $result = $svgReader->parseString($this->xml); $rect = $result->getDocument()->getChild(0); $this->assertEquals(array( 'id' => 'testrect', @@ -109,7 +114,8 @@ public function testShouldSetAllAttributesAndNamespaces() public function testShouldSetStyles() { - $result = (new SVGReader())->parseString($this->xml); + $svgReader = new SVGReader(); + $result = $svgReader->parseString($this->xml); $rect = $result->getDocument()->getChild(0); // should detect style attributes @@ -124,8 +130,8 @@ public function testShouldSetStyles() public function testShouldRecursivelyAddChildren() { // should recursively add all child nodes - - $result = (new SVGReader())->parseString($this->xml); + $svgReader = new SVGReader(); + $result = $svgReader->parseString($this->xml); $g = $result->getDocument()->getChild(1); $this->assertSame(2, $g->countChildren()); @@ -149,7 +155,8 @@ public function testShouldRecursivelyAddChildren() public function testShouldIgnoreUnknownNodes() { // should skip unknown node types without failing - $result = (new SVGReader())->parseString($this->xmlUnknown); + $svgReader = new SVGReader(); + $result = $svgReader->parseString($this->xmlUnknown); $doc = $result->getDocument(); $this->assertSame(2, $doc->countChildren()); $this->assertSame('circle', $doc->getChild(0)->getName()); @@ -158,7 +165,8 @@ public function testShouldIgnoreUnknownNodes() public function testShouldDecodeEntities() { - $result = (new SVGReader())->parseString($this->xmlEntities); + $svgReader = new SVGReader(); + $result = $svgReader->parseString($this->xmlEntities); $doc = $result->getDocument(); // should decode entities in attributes diff --git a/tests/Writing/SVGWriterTest.php b/tests/Writing/SVGWriterTest.php index e091e67..24885f1 100644 --- a/tests/Writing/SVGWriterTest.php +++ b/tests/Writing/SVGWriterTest.php @@ -66,10 +66,11 @@ public function testShouldWriteChildren() // should write children $obj = new SVGWriter(); $node = new \SVG\Nodes\Structures\SVGGroup(); - $node->addChild( - (new \SVG\Nodes\Structures\SVGGroup()) - ->addChild(new \SVG\Nodes\Shapes\SVGRect()) - ); + $childNode = new \SVG\Nodes\Structures\SVGGroup(); + $svgRect = new \SVG\Nodes\Shapes\SVGRect(); + $childNode->addChild($svgRect); + + $node->addChild($childNode); $obj->writeNode($node); $expect = $this->xmlDeclaration.''; $this->assertEquals($expect, $obj->getString()); @@ -90,18 +91,17 @@ public function testShouldEncodeEntities() { // should encode entities in attributes $obj = new SVGWriter(); - $obj->writeNode( - (new \SVG\Nodes\Structures\SVGGroup()) - ->setAttribute('id', '" foo&bar>') - ->setStyle('content', '" foo&bar>') - ); + $svgGroup = new \SVG\Nodes\Structures\SVGGroup(); + $svgGroup->setAttribute('id', '" foo&bar>')->setStyle('content', '" foo&bar>'); + $obj->writeNode($svgGroup); $expect = $this->xmlDeclaration.''; $this->assertEquals($expect, $obj->getString()); // should encode entities in style body $obj = new SVGWriter(); - $obj->writeNode(new \SVG\Nodes\Structures\SVGStyle('" foo&bar>')); + $svgStyle = new \SVG\Nodes\Structures\SVGStyle('" foo&bar>'); + $obj->writeNode($svgStyle); $expect = $this->xmlDeclaration.''; $this->assertEquals($expect, $obj->getString());