NativeEscposImageTest.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. use Mike42\Escpos\NativeEscposImage;
  3. use Mike42\Escpos\EscposImage;
  4. class NativeEscposImageTest extends PHPUnit\Framework\TestCase
  5. {
  6. /**
  7. * Native tests - Load tiny images and check how they are printed
  8. * These are skipped if you don't have Native
  9. */
  10. public function testBadFilename()
  11. {
  12. $this -> expectException(Exception::class);
  13. $this -> loadAndCheckImg('not a real file.png', 1, 1, null, null);
  14. }
  15. /**
  16. * @medium
  17. */
  18. public function testBlack()
  19. {
  20. foreach (array('bmp', 'gif', 'png') as $format) {
  21. $this -> loadAndCheckImg('canvas_black.' . $format, 1, 1, "\x80", array("\x80"));
  22. }
  23. }
  24. /**
  25. * @medium
  26. */
  27. public function testBlackTransparent()
  28. {
  29. foreach (array('gif', 'png') as $format) {
  30. $this -> loadAndCheckImg('black_transparent.' . $format, 2, 2, "\xc0\x00", array("\x80\x80"));
  31. }
  32. }
  33. /**
  34. * @medium
  35. */
  36. public function testBlackWhite()
  37. {
  38. foreach (array('bmp', 'png', 'gif') as $format) {
  39. $this -> loadAndCheckImg('black_white.' . $format, 2, 2, "\xc0\x00", array("\x80\x80"));
  40. }
  41. }
  42. /**
  43. * @medium
  44. */
  45. public function testBlackWhiteTall()
  46. {
  47. // We're very interested in correct column format chopping here at 8 pixels
  48. $this -> loadAndCheckImg('black_white_tall.png', 2, 16,
  49. "\xc0\xc0\xc0\xc0\xc0\xc0\xc0\xc0\x00\x00\x00\x00\x00\x00\x00\x00", array("\xff\xff", "\x00\x00"));
  50. }
  51. /**
  52. * @medium
  53. */
  54. public function testWhite()
  55. {
  56. foreach (array('bmp', 'png', 'gif') as $format) {
  57. $this -> loadAndCheckImg('canvas_white.' . $format, 1, 1, "\x00", array("\x00"));
  58. }
  59. }
  60. /**
  61. * Load an EscposImage and run a check.
  62. */
  63. private function loadAndCheckImg($fn, $width, $height, $rasterFormat = null, $columnFormat = null)
  64. {
  65. $onDisk = ($fn === null ? null : (dirname(__FILE__) . "/resources/$fn"));
  66. // With optimisations
  67. $imgOptimised = new NativeEscposImage($onDisk, true);
  68. $this -> checkImg($imgOptimised, $width, $height, $rasterFormat, $columnFormat);
  69. // ... and without
  70. $imgUnoptimised = new NativeEscposImage($onDisk, false);
  71. $this -> checkImg($imgUnoptimised, $width, $height, $rasterFormat, $columnFormat);
  72. }
  73. /**
  74. * Check image against known width, height, output.
  75. */
  76. private function checkImg(EscposImage $img, $width, $height, $rasterFormatExpected = null, $columnFormatExpected = null)
  77. {
  78. $rasterFormatActual = $img -> toRasterFormat();
  79. $columnFormatActual = $img -> toColumnFormat();
  80. if ($rasterFormatExpected === null) {
  81. echo "\nImage was: " . $img -> getWidth() . "x" . $img -> getHeight() . ", raster data \"" . friendlyBinary($rasterFormatActual) . "\"";
  82. }
  83. if ($columnFormatExpected === null) {
  84. echo "\nImage was: " . $img -> getWidth() . "x" . $img -> getHeight() . ", column data \"" . friendlyBinary($columnFormatActual) . "\"";
  85. }
  86. $this -> assertEquals($height , $img -> getHeight());
  87. $this -> assertEquals($width, $img -> getWidth());
  88. $this -> assertEquals($rasterFormatExpected, $rasterFormatActual, "Raster format did not match expected");
  89. $this -> assertEquals($columnFormatExpected, $columnFormatActual, "Column format did not match expected");
  90. }
  91. }