root/cleversvg/trunk/tests/csDocumentTest.php

Revision 336, 6.2 kB (checked in by nperriault, 10 months ago)

Clever Svg:

  • refs #40: more elements tests
  • Removed depth management, as svg handle it natively

Warning: breaks BC from 0.5.

Line 
1 <?php
2 require_once 'PHPUnit/Framework.php';
3 require_once dirname(__FILE__).'/../cleversvg.php';
4 require_once dirname(__FILE__).'/csBaseTestCase.class.php';
5
6 /**
7  * csDocument PHPUnit tests
8  *
9  */
10 class csDocumentTest extends csBaseTestCase
11 {
12
13   public function testAddAsDefinition()
14   {
15     $doc = new csDocument(320, 240, 'Blah');
16     $circle = new csCircle();
17     $circle->setStroke('yellow');
18     $circle->setStrokeWidth(2);
19     $circle->setFill('orange');
20     $circle->setId('circle1');
21     $doc->addAsDefinition($circle, 'c1');
22     $this->assertNodeExists($doc, '/svg:svg/svg:defs/svg:circle[@id = "c1"]');
23     $this->assertType('csCircle', $doc->getElementById('c1'));
24   }
25
26   /**
27    * @expectedException csException
28    *
29    */
30   public function testAddElement()
31   {
32     $doc = new csDocument(320, 240, 'Blah');
33     $c = $doc->createElement('circle');
34     $c->setId('myId');
35     $doc->addElement($c);
36     $this->assertType('csCircle', $doc->getElementById('myId'));
37     $rect = new csRect(100, 50, 40, 20);
38     $rect->setId('myId');
39     $doc->addElement($rect);
40   }
41
42   public function testConstructor()
43   {
44     $doc = new csDocument(320, 240, 'Blah', array('baseProfile' => 'foo/bar'));
45     $this->assertAttrValueEquals($doc, '/svg:svg/@width', 320);
46     $this->assertAttrValueEquals($doc, '/svg:svg/@height', 240);
47     $this->assertTextNodeEquals($doc, '/svg:svg/svg:title', 'Blah');
48     $this->assertAttrValueEquals($doc, '/svg:svg/@baseProfile', 'foo/bar');
49   }
50
51   public function testCreateElement()
52   {
53     $doc = new csDocument(320, 240, 'Blah');
54     $this->assertType('csCircle', $doc->createElement('circle'));
55     $this->assertType('csRect', $doc->createElement('rect'));
56     $this->assertType('csLinearGradient', $doc->createElement('linearGradient'));
57   }
58
59   public function testDropElementById()
60   {
61     $doc = new csDocument(320, 240, 'Blah');
62     $c = new csCircle();
63     $c->setRadius(10);
64     $c->setStroke('red');
65     $c->setId('circle1');
66     $doc->addElement($c);
67     $this->assertNodeExists($doc, '/svg:svg/svg:circle[@id = "circle1"]');
68     $doc->dropElementById('circle1');
69     $this->assertNodeDoesNotExist($doc, '/svg:svg/svg:circle[@id = "circle1"]');
70   }
71
72   public function testGetDomDocument()
73   {
74     $doc = new csDocument(320, 240, 'Blah');
75     $this->assertType('DOMDocument', $doc->getDomDocument());
76   }
77
78   public function testGetElementById()
79   {
80     $doc = new csDocument(320, 240, 'Blah');
81
82     // Testing flat level search
83     $c = new csCircle();
84     $c->setRadius(10);
85     $c->setStroke('red');
86     $c->setId('c1');
87     $doc->addElement($c);
88     $this->assertType('csBaseElement', $doc->getElementById('c1'));
89
90     // Testing deep level search
91     $c2 = clone($c);
92     $c2->setId('c2');
93     $g1 = new csGroup('g1');
94     $g1->addElement($c2);
95     $doc->addElement($g1);
96     $this->assertType('csBaseElement', $doc->getElementById('c2'));
97
98     $c3 = clone($c);
99     $c3->setId('c3');
100     $g2 = new csGroup('g2');
101     $g2->addElement($c3);
102     $g3 = new csGroup('g3');
103     $g3->addElement($g2);
104     $doc->addElement($g3);
105     $this->assertType('csBaseElement', $doc->getElementById('c3'));
106   }
107
108   public function testHasId()
109   {
110     $doc = new csDocument(320, 240, 'Blah');
111     $c = new csCircle();
112     $c->setRadius(10);
113     $c->setStroke('red');
114     $c->setId('circle1');
115     $c2 = clone($c);
116     $c2->setId('circle2');
117     $doc->addElement($c);
118     $doc->addElement($c2);
119     $this->assertTrue($doc->hasId('circle1'));
120     $this->assertTrue($doc->hasId('circle2'));
121     $this->assertFalse($doc->hasId('jfhkjsdfh'));
122     $doc->dropElementById('circle1');
123     $this->assertFalse($doc->hasId('circle1'));
124     $this->assertTrue($doc->hasId('circle2'));
125     $doc->dropElementById('circle2');
126     $this->assertFalse($doc->hasId('circle2'));
127   }
128
129   public function testSetDescription()
130   {
131     $doc = $this->generateTestDoc();
132     $doc->setDescription('Added description');
133     $this->assertTextNodeEquals($doc, '/svg:svg/svg:desc', 'Added description');
134     $doc->setDescription('Changed description');
135     $this->assertTextNodeEquals($doc, '/svg:svg/svg:desc', 'Changed description');
136   }
137
138   /**
139    * @expectedException csException
140    *
141    */
142   public function testNoDuplicateId()
143   {
144     $doc = new csDocument(320, 240, 'Blah');
145     $c = new csCircle();
146     $c->setId('circle1');
147     $c2 = new csCircle();
148     $c2->setId('circle1');
149     $doc->addElement($c);
150     $doc->addElement($c2);
151   }
152
153   public function testSetTitle()
154   {
155     $doc = $this->generateTestDoc();
156     $doc->setTitle('Added title');
157     $this->assertTextNodeEquals($doc, '/svg:svg/svg:title', 'Added title');
158     $doc->setTitle('Changed title');
159     $this->assertTextNodeEquals($doc, '/svg:svg/svg:title', 'Changed title');
160   }
161
162   public function testSwapDepths()
163   {
164     $doc = new csDocument(320, 240, 'Test depths');
165     $c1 = new csCircle(160, 120, 40);
166     $c1->setDepth(20);
167     $c1->setId('c1');
168     $c2 = new csCircle(140, 100, 40);
169     $c2->setDepth(40);
170     $c2->setId('c2');
171     $doc->addElement($c1);
172     $doc->addElement($c2);
173     $doc->swapDepths('c1', 'c2');
174     $this->assertAttrValueEquals($doc, '/svg:svg/svg:circle[@id = "c1"]/@style', 'z-index: 40');
175     $this->assertAttrValueEquals($doc, '/svg:svg/svg:circle[@id = "c2"]/@style', 'z-index: 20');
176   }
177
178   public function testUseDefinition()
179   {
180     $doc = new csDocument(320, 240, 'Blah');
181     $circle = new csCircle();
182     $doc->addAsDefinition($circle, 'c1');
183     $doc->useDefinition('c1', array('x' => 200, 'y' => 100));
184     $this->assertAttrValueEquals($doc, '/svg:svg/svg:use/@xlink:href', '#c1');
185   }
186
187   public function testValidation()
188   {
189     $doc = new csDocument(320, 240, 'Test depths');
190     $c1 = new csCircle(160, 120, 40);
191     $c1->setDepth(20);
192     $c1->setId('c1');
193     $c2 = new csCircle(140, 100, 40);
194     $c2->setDepth(40);
195     $c2->setId('c2');
196     $doc->addElement($c1);
197     $doc->addElement($c2);
198     $doc->setStrictMode(true);
199     $doc->toXML();
200   }
201
202   public function testViewBox()
203   {
204     $doc = new csDocument();
205     $doc->setViewBox(10, 10, 320, 240);
206     $this->assertAttrValueEquals($doc, '/svg:svg/@viewBox', '10 10 320 240');
207   }
208
209   public function testToXml()
210   {
211     $doc = $this->generateTestDoc();
212     $xml = $doc->toXML();
213     $this->assertEquals(preg_match('#<\?xml version="1.0" encoding="UTF-8".*?\?>#s', $xml), 1, $xml);
214   }
215
216 }
217
Note: See TracBrowser for help on using the browser.