xml = '';
$this->xml .= '';
$this->xmlNoViewBox = '';
$this->xmlNoViewBox .= '';
$this->xmlNoWH = '';
$this->xmlNoWH .= '';
$this->xmlUnknown = '';
$this->xmlUnknown .= '';
$this->xmlValue = '';
$this->xmlValue .= '';
$this->xmlEntities = '';
$this->xmlEntities .= '';
}
public function testShouldReturnAnImageOrNull()
{
// should return an instance of SVG
$svgReader = new SVGReader();
$result = $svgReader->parseString($this->xml);
$this->assertInstanceOf('\SVG\SVG', $result);
// should return null when parsing fails
$result = $svgReader->parseString('');
$this->assertNull($result);
}
public function testShouldSetAllAttributesAndNamespaces()
{
// should retain all document attributes and namespaces
$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',
'xmlns:testns' => 'test-namespace',
'width' => '37',
'height' => '42',
'viewBox' => '10 20 74 84',
), $result->getDocument()->getSerializableAttributes());
// should deal with missing viewBox
$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',
'width' => '37',
'height' => '42',
), $result->getDocument()->getSerializableAttributes());
// should deal with missing width/height
$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',
'viewBox' => '10 20 74 84',
), $result->getDocument()->getSerializableAttributes());
// should set all attributes, including namespace prefixed ones
$svgReader = new SVGReader();
$result = $svgReader->parseString($this->xml);
$rect = $result->getDocument()->getChild(0);
$this->assertEquals(array(
'id' => 'testrect',
'testns:attr' => 'test',
'xlink:foo' => 'bar',
), $rect->getSerializableAttributes());
}
public function testShouldSetStyles()
{
$svgReader = new SVGReader();
$result = $svgReader->parseString($this->xml);
$rect = $result->getDocument()->getChild(0);
// should detect style attributes
$this->assertNull($rect->getAttribute('fill'));
$this->assertSame('#ABCDEF', $rect->getStyle('fill'));
// should parse and set the 'style' attribute
$this->assertEquals('.5', $rect->getStyle('opacity'));
$this->assertEquals('#AABBCC', $rect->getStyle('stroke'));
}
public function testShouldRecursivelyAddChildren()
{
// should recursively add all child nodes
$svgReader = new SVGReader();
$result = $svgReader->parseString($this->xml);
$g = $result->getDocument()->getChild(1);
$this->assertSame(2, $g->countChildren());
$circle = $g->getChild(0);
$this->assertEquals(array(
'cx' => '10',
'cy' => '20',
'r' => '42',
), $circle->getSerializableAttributes());
$ellipse = $g->getChild(1);
$this->assertEquals(array(
'cx' => '50',
'cy' => '60',
'rx' => '10',
'ry' => '20',
), $ellipse->getSerializableAttributes());
}
public function testShouldRetrieveUnknownNodes()
{
$svgReader = new SVGReader();
$result = $svgReader->parseString($this->xmlUnknown);
$doc = $result->getDocument();
// should include unknown nodes
$this->assertSame(3, $doc->countChildren());
$this->assertSame('circle', $doc->getChild(0)->getName());
$this->assertSame('unknown', $doc->getChild(1)->getName());
$this->assertSame('ellipse', $doc->getChild(2)->getName());
// should set attributes on unknown nodes
$this->assertSame('bar', $doc->getChild(1)->getAttribute('foo'));
// should include children of unknown nodes
$this->assertSame(1, $doc->getChild(1)->countChildren());
$this->assertSame('baz', $doc->getChild(1)->getChild(0)->getName());
}
public function testShouldSetValue()
{
$svgReader = new SVGReader();
$result = $svgReader->parseString($this->xmlValue);
$doc = $result->getDocument();
// should set value on nodes
$this->assertSame('hello world', $doc->getChild(0)->getValue());
}
public function testShouldDecodeEntities()
{
$svgReader = new SVGReader();
$result = $svgReader->parseString($this->xmlEntities);
$doc = $result->getDocument();
// should decode entities in attributes
$this->assertSame('" foo&bar>', $doc->getChild(0)->getAttribute('id'));
$this->assertSame('&none', $doc->getChild(0)->getStyle('display'));
// should decode entities in style body
$this->assertSame('" foo&bar>', $doc->getChild(0)->getCss());
// should decode entities in value
$this->assertSame('" foo&bar>', $doc->getChild(1)->getValue());
}
public function testParseStylesWithEmptyString()
{
$this->assertCount(0, SVGStyleParser::parseStyles(''));
}
}