SvgDrawerTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. namespace Spipu\Html2Pdf\Tests;
  3. use Spipu\Html2Pdf\Exception\HtmlParsingException;
  4. use Spipu\Html2Pdf\Tests\CrossVersionCompatibility\SvgDrawerTestCase;
  5. /**
  6. * Class SvgDrawerTest
  7. */
  8. class SvgDrawerTest extends SvgDrawerTestCase
  9. {
  10. /**
  11. * Test IsDrawing Exception
  12. */
  13. public function testIsDrawingException()
  14. {
  15. $this->expectException(HtmlParsingException::class);
  16. $properties = [
  17. 'x' => 0,
  18. 'y' => 0,
  19. 'w' => '100mm',
  20. 'h' => '100mm',
  21. ];
  22. $this->svgDrawer->startDrawing($properties);
  23. $this->svgDrawer->startDrawing($properties);
  24. }
  25. /**
  26. * Test IsDrawing
  27. */
  28. public function testIsDrawingOk()
  29. {
  30. $properties = [
  31. 'x' => 0,
  32. 'y' => 0,
  33. 'w' => 100,
  34. 'h' => 100,
  35. ];
  36. $this->assertFalse($this->svgDrawer->isDrawing());
  37. $this->svgDrawer->startDrawing($properties);
  38. $this->assertTrue($this->svgDrawer->isDrawing());
  39. $this->svgDrawer->stopDrawing();
  40. $this->assertFalse($this->svgDrawer->isDrawing());
  41. }
  42. /**
  43. * Test properties
  44. */
  45. public function testProperties()
  46. {
  47. $properties = [
  48. 'x' => 1,
  49. 'y' => 2,
  50. 'w' => 3,
  51. 'h' => 4,
  52. ];
  53. $this->svgDrawer->startDrawing($properties);
  54. $this->assertSame(1, $this->svgDrawer->getProperty('x'));
  55. $this->assertSame(2, $this->svgDrawer->getProperty('y'));
  56. $this->assertSame(3, $this->svgDrawer->getProperty('w'));
  57. $this->assertSame(4, $this->svgDrawer->getProperty('h'));
  58. }
  59. /**
  60. * Test: tokenize
  61. *
  62. * @param mixed $transform
  63. * @param mixed $expected
  64. *
  65. * @dataProvider transformProvider
  66. */
  67. public function testTransform($transform, $expected)
  68. {
  69. $properties = [
  70. 'x' => 0,
  71. 'y' => 0,
  72. 'w' => 100,
  73. 'h' => 100,
  74. ];
  75. $this->svgDrawer->startDrawing($properties);
  76. $result = $this->svgDrawer->prepareTransform($transform);
  77. $this->assertArraySame($expected, $result);
  78. }
  79. /**
  80. * @param array $expected
  81. * @param array $result
  82. */
  83. protected function assertArraySame($expected, $result)
  84. {
  85. if (is_array($expected)) {
  86. foreach ($expected as $key => $value) {
  87. $expected[$key] = round($value, 5);
  88. }
  89. }
  90. if (is_array($result)) {
  91. foreach ($result as $key => $value) {
  92. $result[$key] = round($value, 5);
  93. }
  94. }
  95. $this->assertSame($expected, $result);
  96. }
  97. /**
  98. * provider: tokenize
  99. *
  100. * @return array
  101. */
  102. public function transformProvider()
  103. {
  104. return array(
  105. array(
  106. false,
  107. null
  108. ),
  109. array(
  110. 'no instruction',
  111. null
  112. ),
  113. array(
  114. 'foo(1,2)',
  115. null
  116. ),
  117. array(
  118. 'before scale( 0.1 , 0.2 ) after',
  119. [
  120. 0.1, 0.,
  121. 0., 0.2,
  122. 0., 0.
  123. ]
  124. ),
  125. array(
  126. 'scale(0.1,0.2)',
  127. [
  128. 0.1, 0.,
  129. 0., 0.2,
  130. 0., 0.
  131. ]
  132. ),
  133. array(
  134. 'scale(0.1)',
  135. [
  136. 0.1, 0.,
  137. 0., 0.1,
  138. 0., 0.
  139. ]
  140. ),
  141. array(
  142. 'scale(,)',
  143. [
  144. 1., 0.,
  145. 0., 1.,
  146. 0., 0.
  147. ]
  148. ),
  149. array(
  150. 'scale()',
  151. [
  152. 1., 0.,
  153. 0., 1.,
  154. 0., 0.
  155. ]
  156. ),
  157. array(
  158. 'translate()',
  159. [
  160. 1., 0.,
  161. 0., 1.,
  162. 0., 0.
  163. ]
  164. ),
  165. array(
  166. 'translate(10mm)',
  167. [
  168. 1., 0.,
  169. 0., 1.,
  170. 10., 0.
  171. ]
  172. ),
  173. array(
  174. 'translate(10mm, 20mm)',
  175. [
  176. 1., 0.,
  177. 0., 1.,
  178. 10., 20.
  179. ]
  180. ),
  181. array(
  182. 'rotate()',
  183. [
  184. 1., 0.,
  185. 0., 1.,
  186. 0., 0.
  187. ]
  188. ),
  189. array(
  190. 'rotate(90)',
  191. [
  192. 0., 1.,
  193. -1., 0.,
  194. 0., 0.
  195. ]
  196. ),
  197. array(
  198. 'rotate(180)',
  199. [
  200. -1., 0.,
  201. 0., -1.,
  202. 0., 0.
  203. ]
  204. ),
  205. array(
  206. 'rotate(180, 10mm, 10mm)',
  207. [
  208. -1., 0.,
  209. 0., -1.,
  210. -20., -20.
  211. ]
  212. ),
  213. array(
  214. 'skewx()',
  215. [
  216. 1., 0.,
  217. 0., 1.,
  218. 0., 0.
  219. ]
  220. ),
  221. array(
  222. 'skewx(45)',
  223. [
  224. 1., 0.,
  225. 1., 1.,
  226. 0., 0.
  227. ]
  228. ),
  229. array(
  230. 'skewy()',
  231. [
  232. 1., 0.,
  233. 0., 1.,
  234. 0., 0.
  235. ]
  236. ),
  237. array(
  238. 'skewy(45)',
  239. [
  240. 1., 1.,
  241. 0., 1.,
  242. 0., 0.
  243. ]
  244. ),
  245. array(
  246. 'matrix()',
  247. [
  248. 0., 0.,
  249. 0., 0.,
  250. 0., 0.
  251. ]
  252. ),
  253. array(
  254. 'matrix(1,2,3,4,5%,6%)',
  255. [
  256. 1., 2.,
  257. 3., 4.,
  258. 5., 6.
  259. ]
  260. ),
  261. );
  262. }
  263. }