DimensionTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Helper;
  3. use PhpOffice\PhpSpreadsheet\Exception;
  4. use PhpOffice\PhpSpreadsheet\Helper\Dimension;
  5. use PHPUnit\Framework\TestCase;
  6. class DimensionTest extends TestCase
  7. {
  8. /**
  9. * @dataProvider providerCellWidth
  10. */
  11. public function testCreateDimension(float $expectedResult, string $dimension): void
  12. {
  13. $result = (new Dimension($dimension))->width();
  14. self::assertSame($expectedResult, $result);
  15. }
  16. /**
  17. * @dataProvider providerConvertUoM
  18. */
  19. public function testConvertDimension(float $expectedResult, string $dimension, string $unitOfMeasure): void
  20. {
  21. $result = (new Dimension($dimension))->toUnit($unitOfMeasure);
  22. self::assertEqualsWithDelta($expectedResult, $result, 1.0e-12);
  23. }
  24. public function testConvertDimensionInvalidUoM(): void
  25. {
  26. $this->expectException(Exception::class);
  27. $this->expectExceptionMessage('pikachu is not a vaid unit of measure');
  28. (new Dimension('999'))->toUnit('pikachu');
  29. }
  30. public function providerCellWidth(): array
  31. {
  32. return [
  33. [12.0, '12'],
  34. [2.2852, '12pt'],
  35. [4.5703, '24 pt'],
  36. [5.1416, '36px'],
  37. [5.7129, '2.5pc'],
  38. [13.7109, '2.54cm'],
  39. [13.7109, '25.4mm'],
  40. [13.7109, '1in'],
  41. [4.27, '50%'],
  42. [3.7471, '3.2em'],
  43. [2.3419, '2ch'],
  44. [4.6838, '4ex'],
  45. [14.0515, '12rem'],
  46. ];
  47. }
  48. public function providerConvertUoM(): array
  49. {
  50. return [
  51. [60, '8.54', Dimension::UOM_PIXELS],
  52. [100, '100px', Dimension::UOM_PIXELS],
  53. [150, '200px', Dimension::UOM_POINTS],
  54. [45, '8.54', Dimension::UOM_POINTS],
  55. [12.5, '200px', Dimension::UOM_PICA],
  56. [3.75, '8.54', Dimension::UOM_PICA],
  57. [3.125, '300px', Dimension::UOM_INCHES],
  58. [0.625, '8.54', Dimension::UOM_INCHES],
  59. [7.9375, '300px', Dimension::UOM_CENTIMETERS],
  60. [1.5875, '8.54', Dimension::UOM_CENTIMETERS],
  61. [79.375, '300px', Dimension::UOM_MILLIMETERS],
  62. [15.875, '8.54', Dimension::UOM_MILLIMETERS],
  63. ];
  64. }
  65. }