CssLengthTest.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace Svg\Tests;
  3. use Svg\CssLength;
  4. use PHPUnit\Framework\TestCase;
  5. class CssLengthTest extends TestCase
  6. {
  7. public function test_toPixels()
  8. {
  9. $convert = function(string $size, float $reference = 11.0, float $dpi = 96.0) {
  10. return (new CssLength($size))->toPixels($reference, $dpi);
  11. };
  12. // Absolute lengths
  13. $this->assertEquals(1, $convert('1'));
  14. $this->assertEquals(10, $convert("10px"));
  15. $this->assertEquals((10 * 96) / 72 , $convert("10pt"));
  16. $this->assertEquals((10 * 72) / 72 , $convert("10pt", 11, 72));
  17. $this->assertEquals(8, $convert("80%", 10, 72));
  18. $this->assertEquals((10 * 96) / 2.54, $convert("10cm"));
  19. $this->assertEquals((10 * 96) / 25.4, $convert("10mm"));
  20. $this->assertEquals(10 * 96, $convert("10in"));
  21. $this->assertEquals((10 * 96) / 6, $convert("10pc"));
  22. // Relative lengths
  23. $this->assertEquals(200, $convert("10em", 20));
  24. $this->assertEquals(200, $convert("10ex", 20));
  25. $this->assertEquals(200, $convert("10ch", 20));
  26. $this->assertEquals(200, $convert("10rem", 20));
  27. $this->assertEquals(2, $convert("10vw", 20));
  28. $this->assertEquals(2, $convert("10vh", 20));
  29. $this->assertEquals(2, $convert("10vmin", 20));
  30. $this->assertEquals(2, $convert("10vmax", 20));
  31. }
  32. public function test_getUnit()
  33. {
  34. $this->assertEquals('em', (new CssLength('30em'))->getUnit());
  35. $this->assertEquals('%', (new CssLength('100%'))->getUnit());
  36. $this->assertEquals('vmin', (new CssLength('40vmin'))->getUnit());
  37. $this->assertEquals('q', (new CssLength('50Q'))->getUnit());
  38. $this->assertEquals('', (new CssLength('50GB'))->getUnit());
  39. $this->assertEquals('rem', (new CssLength('44.5435rem'))->getUnit());
  40. }
  41. }