EscposPrintBufferTest.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * Example strings are pangrams using different character sets, and are
  4. * testing correct code-table switching.
  5. *
  6. * When printed, they should appear the same as in this source file.
  7. *
  8. * Many of these test strings are from:
  9. * - http://www.cl.cam.ac.uk/~mgk25/ucs/examples/quickbrown.txt
  10. * - http://clagnut.com/blog/2380/ (mirrored from the English Wikipedia)
  11. */
  12. use Mike42\Escpos\Printer;
  13. use Mike42\Escpos\PrintConnectors\DummyPrintConnector;
  14. class EscposPrintBufferTest extends PHPUnit\Framework\TestCase
  15. {
  16. protected $buffer;
  17. protected $outputConnector;
  18. protected function setup()
  19. {
  20. $this -> outputConnector = new DummyPrintConnector();
  21. $printer = new Printer($this -> outputConnector);
  22. $this -> buffer = $printer -> getPrintBuffer();
  23. }
  24. protected function checkOutput($expected = null)
  25. {
  26. /* Check those output strings */
  27. $outp = $this -> outputConnector -> getData();
  28. if ($expected === null) {
  29. echo "\nOutput was:\n\"" . friendlyBinary($outp) . "\"\n";
  30. }
  31. $this -> assertEquals($expected, $outp);
  32. }
  33. protected function tearDown()
  34. {
  35. $this -> outputConnector -> finalize();
  36. }
  37. public function testNotUtf8() {
  38. $this -> expectException(Exception::class);
  39. $this -> buffer -> writeText("Not valid UTF-8 can it be printed? \xc3\x28");
  40. }
  41. public function testRawTextNonprintable()
  42. {
  43. $this -> buffer -> writeTextRaw("Test" . Printer::ESC . "v1\n");
  44. $this -> checkOutput("\x1b@Test?v1\x0a"); // ASCII ESC character is substituted for '?'
  45. }
  46. public function testDanish()
  47. {
  48. $this -> buffer -> writeText("Quizdeltagerne spiste jordbær med fløde, mens cirkusklovnen Wolther spillede på xylofon.\n");
  49. $this -> checkOutput("\x1b@Quizdeltagerne spiste jordb\x91r med fl\x1bt\x02\x9bde, mens cirkusklovnen Wolther spillede p\x86 xylofon.\x0a");
  50. }
  51. public function testGerman()
  52. {
  53. $this -> buffer -> writeText("Falsches Üben von Xylophonmusik quält jeden größeren Zwerg.\n");
  54. $this -> checkOutput("\x1b@Falsches \x9aben von Xylophonmusik qu\x84lt jeden gr\x94\xe1eren Zwerg.\x0a");
  55. }
  56. public function testGreek()
  57. {
  58. $this -> buffer -> writeText("Ξεσκεπάζω την ψυχοφθόρα βδελυγμία");
  59. $this -> checkOutput("\x1b@\x1bt\x0e\x8d\x9c\xa9\xa1\x9c\xa7\xe1\x9d\xe0 \xab\x9e\xa4 \xaf\xac\xae\xa6\xad\x9f\xe6\xa8\x98 \x99\x9b\x9c\xa2\xac\x9a\xa3\xe5\x98");
  60. }
  61. public function testGreekWithDiacritics()
  62. {
  63. // This is a string which is known to be un-printable in ESC/POS (the grave-accented letters are not in any code page),
  64. // so we are checking the substitution '?' for unknown characters.
  65. $this -> buffer -> writeText("Γαζέες καὶ μυρτιὲς δὲν θὰ βρῶ πιὰ στὸ χρυσαφὶ ξέφωτο.\n");
  66. $this -> checkOutput("\x1b@\xe2\xe0\x1bt\x0e\x9d\xe2\x9c\xaa \xa1\x98? \xa3\xac\xa8\xab\xa0?\xaa \x9b?\xa4 \x9f? \x99\xa8? \xa7\xa0? \xa9\xab? \xae\xa8\xac\xa9\x98\xad? \xa5\xe2\xad\xe0\xab\xa6.\x0a");
  67. }
  68. public function testEnglish()
  69. {
  70. $this -> buffer -> writeText("The quick brown fox jumps over the lazy dog.\n");
  71. $this -> checkOutput("\x1b@The quick brown fox jumps over the lazy dog.\n");
  72. }
  73. public function testSpanish()
  74. {
  75. // This one does not require changing code-pages at all, so characters are just converted from Unicode to CP437.
  76. $this -> buffer -> writeText("El pingüino Wenceslao hizo kilómetros bajo exhaustiva lluvia y frío, añoraba a su querido cachorro.\n");
  77. $this -> checkOutput("\x1b@El ping\x81ino Wenceslao hizo kil\xa2metros bajo exhaustiva lluvia y fr\xa1o, a\xa4oraba a su querido cachorro.\x0a");
  78. }
  79. public function testFrench()
  80. {
  81. $this -> buffer -> writeText("Le cœur déçu mais l'âme plutôt naïve, Louÿs rêva de crapaüter en canoë au delà des îles, près du mälström où brûlent les novæ.\n");
  82. $this -> checkOutput("\x1b@Le c\x1bt\x10\x9cur d\xe9\xe7u mais l'\xe2me plut\xf4t na\xefve, Lou\xffs r\xeava de crapa\xfcter en cano\xeb au del\xe0 des \xeeles, pr\xe8s du m\xe4lstr\xf6m o\xf9 br\xfblent les nov\xe6.\x0a");
  83. }
  84. public function testIrishGaelic()
  85. {
  86. // Note that some letters with diacritics cannot be printed for Irish Gaelic text, so text may need to be simplified.
  87. $this -> buffer -> writeText("D'fhuascail Íosa, Úrmhac na hÓighe Beannaithe, pór Éava agus Ádhaimh.\n");
  88. $this -> checkOutput("\x1b@D'fhuascail \x1bt\x02\xd6osa, \xe9rmhac na h\xe0ighe Beannaithe, p\xa2r \x90ava agus \xb5dhaimh.\x0a");
  89. }
  90. public function testHungarian()
  91. {
  92. $this -> buffer -> writeText("Árvíztűrő tükörfúrógép.\n");
  93. $this -> checkOutput("\x1b@\x1bt\x02\xb5rv\xa1zt\x1bt\x12\xfbr\x8b t\x81k\x94rf\xa3r\xa2g\x82p.\x0a");
  94. }
  95. public function testIcelandic()
  96. {
  97. $this -> buffer -> writeText("Kæmi ný öxi hér ykist þjófum nú bæði víl og ádrepa.");
  98. $this -> checkOutput("\x1b@K\x91mi n\x1bt\x02\xec \x94xi h\x82r ykist \xe7j\xa2fum n\xa3 b\x91\xd0i v\xa1l og \xa0drepa.");
  99. }
  100. public function testJapaneseHiragana()
  101. {
  102. $this -> markTestIncomplete("Non-ASCII character sets not yet supported.");
  103. $this -> buffer -> writeText(implode("\n", array("いろはにほへとちりぬるを", " わかよたれそつねならむ", "うゐのおくやまけふこえて", "あさきゆめみしゑひもせす")) . "\n");
  104. $this -> checkOutput();
  105. }
  106. public function testJapaneseKatakana()
  107. {
  108. $this -> markTestIncomplete("Non-ASCII character sets not yet supported.");
  109. $this -> buffer -> writeText(implode("\n", array("イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム", "ウヰノオクヤマ ケフコエテ アサキユメミシ ヱヒモセスン")) . "\n");
  110. $this -> checkOutput("\x1b@\x1bt\x01\xb2\xdb\xca\xc6\xce\xcd\xc4 \xc1\xd8\xc7\xd9\xa6 \xdc\xb6\xd6\xc0\xda\xbf \xc2\xc8\xc5\xd7\xd1\x0a\xb3\xb2\xc9\xb5\xb8\xd4\xcf \xb9\xcc\xba\xb4\xc3 \xb1\xbb\xb7\xd5\xd2\xd0\xbc \xb4\xcb\xd3\xbe\xbd\xdd\x0a");
  111. }
  112. public function testJapaneseKataKanaHalfWidth()
  113. {
  114. $this -> buffer -> writeText(implode("\n", array("イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム", "ウイノオクヤマ ケフコエテ アサキユメミシ エヒモセスン")) . "\n");
  115. $this -> checkOutput("\x1b@\x1bt\x01\xb2\xdb\xca\xc6\xce\xcd\xc4 \xc1\xd8\xc7\xd9\xa6 \xdc\xb6\xd6\xc0\xda\xbf \xc2\xc8\xc5\xd7\xd1\x0a\xb3\xb2\xc9\xb5\xb8\xd4\xcf \xb9\xcc\xba\xb4\xc3 \xb1\xbb\xb7\xd5\xd2\xd0\xbc \xb4\xcb\xd3\xbe\xbd\xdd\x0a");
  116. }
  117. public function testLatvian()
  118. {
  119. $this -> buffer -> writeText("Glāžšķūņa rūķīši dzērumā čiepj Baha koncertflīģeļu vākus.\n");
  120. $this -> checkOutput("\x1b@Gl\x1bt!\x83\xd8\xd5\xe9\xd7\xeca r\xd7\xe9\x8c\xd5i dz\x89rum\x83 \xd1iepj Baha koncertfl\x8c\x85e\xebu v\x83kus.\x0a");
  121. }
  122. public function testPolish()
  123. {
  124. $this -> buffer -> writeText("Pchnąć w tę łódź jeża lub ośm skrzyń fig.\n");
  125. $this -> checkOutput("\x1b@Pchn\x1bt\x12\xa5\x86 w t\xa9 \x88\xa2d\xab je\xbea lub o\x98m skrzy\xe4 fig.\x0a");
  126. }
  127. public function testRussian()
  128. {
  129. $this -> buffer -> writeText("В чащах юга жил бы цитрус? Да, но фальшивый экземпляр!\n");
  130. $this -> checkOutput("\x1b@\x1bt\x11\x82 \xe7\xa0\xe9\xa0\xe5 \xee\xa3\xa0 \xa6\xa8\xab \xa1\xeb \xe6\xa8\xe2\xe0\xe3\xe1? \x84\xa0, \xad\xae \xe4\xa0\xab\xec\xe8\xa8\xa2\xeb\xa9 \xed\xaa\xa7\xa5\xac\xaf\xab\xef\xe0!\x0a");
  131. }
  132. public function testThai()
  133. {
  134. $this -> markTestIncomplete("Non-ASCII character sets not yet supported.");
  135. $this -> buffer -> writeText("นายสังฆภัณฑ์ เฮงพิทักษ์ฝั่ง ผู้เฒ่าซึ่งมีอาชีพเป็นฅนขายฃวด ถูกตำรวจปฏิบัติการจับฟ้องศาล ฐานลักนาฬิกาคุณหญิงฉัตรชฎา ฌานสมาธิ\n"); // Quotation from Wikipedia
  136. $this -> checkOutput();
  137. }
  138. public function testTurkish()
  139. {
  140. $this -> buffer -> writeText("Pijamalı hasta, yağız şoföre çabucak güvendi.\n");
  141. $this -> checkOutput("\x1b@Pijamal\x1bt\x02\xd5 hasta, ya\x1bt\x0d\xa7\x8dz \x9fof\x94re \x87abucak g\x81vendi.\x0a");
  142. }
  143. public function testArabic()
  144. {
  145. $this -> markTestIncomplete("Right-to-left text not yet supported.");
  146. $this -> buffer -> writeText("صِف خَلقَ خَودِ كَمِثلِ الشَمسِ إِذ بَزَغَت — يَحظى الضَجيعُ بِها نَجلاءَ مِعطارِ" . "\n"); // Quotation from Wikipedia
  147. $this -> checkOutput();
  148. }
  149. public function testHebrew()
  150. {
  151. // RTL text is more complex than the above.
  152. $this -> markTestIncomplete("Right-to-left text not yet supported.");
  153. $this -> buffer -> writeText("דג סקרן שט בים מאוכזב ולפתע מצא לו חברה איך הקליטה" . "\n");
  154. $this -> checkOutput();
  155. }
  156. public function testVietnamese() {
  157. $this -> buffer -> writeText("Tiếng Việt, còn gọi tiếng Việt Nam hay Việt ngữ, là ngôn ngữ của người Việt (người Kinh) và là ngôn ngữ chính thức tại Việt Nam.\n");
  158. $this -> checkOutput("\x1b@Ti\x1bt\x1e\xd5ng Vi\xd6t, c\xdfn g\xe4i ti\xd5ng Vi\xd6t Nam hay Vi\xd6t ng\xf7, l\xb5 ng\xabn ng\xf7 c\xf1a ng\xad\xeai Vi\xd6t (ng\xad\xeai Kinh) v\xb5 l\xb5 ng\xabn ng\xf7 ch\xddnh th\xf8c t\xb9i Vi\xd6t Nam.\x0a");
  159. }
  160. public function testWindowsLineEndings() {
  161. $this -> buffer -> writeText("Hello World!\r\n");
  162. $this -> checkOutput("\x1b@Hello World!\x0a");
  163. }
  164. public function testWindowsLineEndingsRaw() {
  165. $this -> buffer -> writeTextRaw("Hello World!\r\n");
  166. $this -> checkOutput("\x1b@Hello World!\x0a");
  167. }
  168. }