ExampleTest.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. use Mike42\Escpos\EscposImage;
  3. class ExampleTest extends PHPUnit\Framework\TestCase
  4. {
  5. /* Verify that the examples don't fizzle out with fatal errors */
  6. private $exampleDir;
  7. public function setup()
  8. {
  9. $this -> exampleDir = dirname(__FILE__) . "/../../example/";
  10. }
  11. /**
  12. * @medium
  13. */
  14. public function testBitImage()
  15. {
  16. $this->markTestSkipped('Not repeatable on Travis CI.');
  17. $this -> requireGraphicsLibrary();
  18. $outp = $this -> runExample("bit-image.php");
  19. $this -> outpTest($outp, "bit-image.bin");
  20. }
  21. /**
  22. * @medium
  23. */
  24. public function testCharacterEncodings()
  25. {
  26. $outp = $this -> runExample("character-encodings.php");
  27. $this -> outpTest($outp, "character-encodings.bin");
  28. }
  29. /**
  30. * @medium
  31. */
  32. public function testCharacterTables()
  33. {
  34. $outp = $this -> runExample("character-tables.php");
  35. $this -> outpTest($outp, "character-tables.bin");
  36. }
  37. private function outpTest($outp, $fn)
  38. {
  39. $file = dirname(__FILE__) . "/resources/output/".$fn;
  40. if (!file_exists($file)) {
  41. file_put_contents($file, $outp);
  42. }
  43. $this -> assertEquals($outp, file_get_contents($file));
  44. }
  45. /**
  46. * @medium
  47. */
  48. public function testDemo()
  49. {
  50. $this->markTestSkipped('Not repeatable on Travis CI.');
  51. $this -> requireGraphicsLibrary();
  52. $outp = $this -> runExample("demo.php");
  53. $this -> outpTest($outp, "demo.bin");
  54. }
  55. /**
  56. * @medium
  57. */
  58. public function testGraphics()
  59. {
  60. $this->markTestSkipped('Not repeatable on Travis CI.');
  61. $this -> requireGraphicsLibrary();
  62. $outp = $this -> runExample("graphics.php");
  63. $this -> outpTest($outp, "graphics.bin");
  64. }
  65. /**
  66. * @medium
  67. */
  68. public function testReceiptWithLogo()
  69. {
  70. $this->markTestSkipped('Not repeatable on Travis CI.');
  71. $this -> requireGraphicsLibrary();
  72. $outp = $this -> runExample("receipt-with-logo.php");
  73. $this -> outpTest($outp, "receipt-with-logo.bin");
  74. }
  75. /**
  76. * @medium
  77. */
  78. public function testQrCode()
  79. {
  80. $outp = $this -> runExample("qr-code.php");
  81. $this -> outpTest($outp, "qr-code.bin");
  82. }
  83. /**
  84. * @medium
  85. */
  86. public function testBarcode()
  87. {
  88. $outp = $this -> runExample("barcode.php");
  89. $this -> outpTest($outp, "barcode.bin");
  90. }
  91. /**
  92. * @medium
  93. */
  94. public function testTextSize()
  95. {
  96. $outp = $this -> runExample("text-size.php");
  97. $this -> outpTest($outp, "text-size.bin");
  98. }
  99. /**
  100. * @medium
  101. */
  102. public function testMarginsAndSpacing()
  103. {
  104. $outp = $this -> runExample("margins-and-spacing.php");
  105. $this -> outpTest($outp, "margins-and-spacing.bin");
  106. }
  107. /**
  108. * @medium
  109. */
  110. public function testPdf417Code()
  111. {
  112. $outp = $this -> runExample("pdf417-code.php");
  113. $this -> outpTest($outp, "pdf417-code.bin");
  114. }
  115. /**
  116. * @medium
  117. */
  118. public function testUnifontPrintBuffer()
  119. {
  120. $this->markTestSkipped('Not repeatable on Travis CI.');
  121. if(!file_exists("/usr/share/unifont/unifont.hex")) {
  122. $this -> markTestSkipped("Test only repeatable w/ unifont installed");
  123. }
  124. $outp = $this -> runExample("unifont-print-buffer.php");
  125. $this -> outpTest($outp, "unifont-print-buffer.bin");
  126. }
  127. public function testInterfaceCups()
  128. {
  129. $outp = $this -> runSyntaxCheck("interface/cups.php");
  130. }
  131. public function testInterfaceEthernet()
  132. {
  133. $outp = $this -> runSyntaxCheck("interface/ethernet.php");
  134. }
  135. public function testInterfaceLinuxUSB()
  136. {
  137. $outp = $this -> runSyntaxCheck("interface/linux-usb.php");
  138. }
  139. public function testInterfaceWindowsUSB()
  140. {
  141. $outp = $this -> runSyntaxCheck("interface/windows-usb.php");
  142. }
  143. public function testInterfaceSMB()
  144. {
  145. $outp = $this -> runSyntaxCheck("interface/smb.php");
  146. }
  147. public function testInterfaceWindowsLPT()
  148. {
  149. $outp = $this -> runSyntaxCheck("interface/windows-lpt.php");
  150. }
  151. private function runSyntaxCheck($fn)
  152. {
  153. $this -> runExample($fn, true);
  154. }
  155. private function runExample($fn, $syntaxCheck = false)
  156. {
  157. // Change directory and check script
  158. chdir($this -> exampleDir);
  159. $this -> assertTrue(file_exists($fn), "Script $fn not found.");
  160. // Run command and save output
  161. $php = "php" . ($syntaxCheck ? " -l" : "");
  162. ob_start();
  163. passthru($php . " " . escapeshellarg($fn), $retval);
  164. $outp = ob_get_contents();
  165. ob_end_clean();
  166. // Check return value
  167. $this -> assertEquals(0, $retval, "Example $fn exited with status $retval");
  168. return $outp;
  169. }
  170. protected function requireGraphicsLibrary()
  171. {
  172. if (!EscposImage::isGdLoaded() && !EscposImage::isImagickLoaded()) {
  173. $this -> markTestSkipped("This test requires a graphics library.");
  174. }
  175. }
  176. }