DrawingTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * This file is part of PHPOffice Common
  4. *
  5. * PHPOffice Common is free software distributed under the terms of the GNU Lesser
  6. * General Public License version 3 as published by the Free Software Foundation.
  7. *
  8. * For the full copyright and license information, please read the LICENSE
  9. * file that was distributed with this source code. For the full list of
  10. * contributors, visit https://github.com/PHPOffice/Common/contributors.
  11. *
  12. * @link https://github.com/PHPOffice/Common
  13. * @copyright 2009-2016 PHPOffice Common contributors
  14. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  15. */
  16. namespace PhpOffice\Common\Tests;
  17. use PhpOffice\Common\Drawing;
  18. /**
  19. * Test class for IOFactory
  20. *
  21. * @coversDefaultClass PhpOffice\Common\IOFactory
  22. */
  23. class DrawingTest extends \PHPUnit\Framework\TestCase
  24. {
  25. /**
  26. */
  27. public function testDegreesAngle()
  28. {
  29. $value = rand(1, 100);
  30. $this->assertEquals(0, Drawing::degreesToAngle());
  31. $this->assertEquals((int) round($value * 60000), Drawing::degreesToAngle($value));
  32. $this->assertEquals(0, Drawing::angleToDegrees());
  33. $this->assertEquals(round($value / 60000), Drawing::angleToDegrees($value));
  34. }
  35. /**
  36. */
  37. public function testPixelsCentimeters()
  38. {
  39. $value = rand(1, 100);
  40. $this->assertEquals(0, Drawing::pixelsToCentimeters());
  41. $this->assertEquals($value / Drawing::DPI_96 * 2.54, Drawing::pixelsToCentimeters($value));
  42. $this->assertEquals(0, Drawing::centimetersToPixels());
  43. $this->assertEquals($value / 2.54 * Drawing::DPI_96, Drawing::centimetersToPixels($value));
  44. }
  45. /**
  46. */
  47. public function testPixelsEMU()
  48. {
  49. $value = rand(1, 100);
  50. $this->assertEquals(0, Drawing::pixelsToEmu());
  51. $this->assertEquals(round($value*9525), Drawing::pixelsToEmu($value));
  52. $this->assertEquals(0, Drawing::emuToPixels());
  53. $this->assertEquals(round($value/9525), Drawing::emuToPixels($value));
  54. }
  55. /**
  56. */
  57. public function testPixelsPoints()
  58. {
  59. $value = rand(1, 100);
  60. $this->assertEquals(0, Drawing::pixelsToPoints());
  61. $this->assertEquals($value*0.67777777, Drawing::pixelsToPoints($value));
  62. $this->assertEquals(0, Drawing::pointsToPixels());
  63. $this->assertEquals($value* 1.333333333, Drawing::pointsToPixels($value));
  64. }
  65. /**
  66. */
  67. public function testPointsCentimeters()
  68. {
  69. $value = rand(1, 100);
  70. $this->assertEquals(0, Drawing::pointsToCentimeters());
  71. $this->assertEquals($value * 1.333333333 / Drawing::DPI_96 * 2.54, Drawing::pointsToCentimeters($value));
  72. }
  73. /**
  74. */
  75. public function testTwips()
  76. {
  77. $value = rand(1, 100);
  78. // Centimeters
  79. $this->assertEquals(0, Drawing::centimetersToTwips());
  80. $this->assertEquals($value * 566.928, Drawing::centimetersToTwips($value));
  81. $this->assertEquals(0, Drawing::twipsToCentimeters());
  82. $this->assertEquals($value / 566.928, Drawing::twipsToCentimeters($value));
  83. // Inches
  84. $this->assertEquals(0, Drawing::inchesToTwips());
  85. $this->assertEquals($value * 1440, Drawing::inchesToTwips($value));
  86. $this->assertEquals(0, Drawing::twipsToInches());
  87. $this->assertEquals($value / 1440, Drawing::twipsToInches($value));
  88. // Pixels
  89. $this->assertEquals(0, Drawing::twipsToPixels());
  90. $this->assertEquals(round($value / 15.873984), Drawing::twipsToPixels($value));
  91. }
  92. public function testHTML()
  93. {
  94. $this->assertFalse(Drawing::htmlToRGB('0'));
  95. $this->assertFalse(Drawing::htmlToRGB('00'));
  96. $this->assertFalse(Drawing::htmlToRGB('0000'));
  97. $this->assertFalse(Drawing::htmlToRGB('00000'));
  98. $this->assertInternalType('array', Drawing::htmlToRGB('ABCDEF'));
  99. $this->assertCount(3, Drawing::htmlToRGB('ABCDEF'));
  100. $this->assertEquals(array(0xAB, 0xCD, 0xEF), Drawing::htmlToRGB('ABCDEF'));
  101. $this->assertEquals(array(0xAB, 0xCD, 0xEF), Drawing::htmlToRGB('#ABCDEF'));
  102. $this->assertEquals(array(0xAA, 0xBB, 0xCC), Drawing::htmlToRGB('ABC'));
  103. $this->assertEquals(array(0xAA, 0xBB, 0xCC), Drawing::htmlToRGB('#ABC'));
  104. }
  105. }