EscposTest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. <?php
  2. class EscposTest extends PHPUnit_Framework_TestCase {
  3. protected $printer;
  4. protected $outputConnector;
  5. protected function setup() {
  6. /* Print to nowhere- for testing which inputs are accepted */
  7. $this -> outputConnector = new DummyPrintConnector();
  8. $this -> printer = new Escpos($this -> outputConnector);
  9. }
  10. protected function checkOutput($expected = null) {
  11. /* Check those output strings */
  12. $outp = $this -> outputConnector -> getData();
  13. if($expected === null) {
  14. echo "\nOutput was:\n\"" . friendlyBinary($outp) . "\"\n";
  15. }
  16. $this -> assertEquals($expected, $outp);
  17. }
  18. protected function tearDown() {
  19. $this -> outputConnector -> finalize();
  20. }
  21. protected function requireGraphicsLibrary() {
  22. if(!EscposImage::isGdLoaded() && !EscposImage::isImagickLoaded()) {
  23. // If the test is about to do something which requires a library,
  24. // something must throw an exception.
  25. $this -> setExpectedException('Exception');
  26. }
  27. }
  28. public function testInitializeOutput() {
  29. $this -> checkOutput("\x1b\x40");
  30. }
  31. public function testTextStringOutput() {
  32. $this -> printer -> text("The quick brown fox jumps over the lazy dog\n");
  33. $this -> checkOutput("\x1b@The quick brown fox jumps over the lazy dog\n");
  34. }
  35. public function testTextDefault() {
  36. $this -> printer -> text();
  37. $this -> checkOutput("\x1b@");
  38. }
  39. public function testTextString() {
  40. $this -> printer -> text("String");
  41. $this -> printer -> text(123);
  42. $this -> printer -> text();
  43. $this -> printer -> text(null);
  44. $this -> printer -> text(1.2);
  45. $this -> printer -> text(new FooBar("FooBar"));
  46. $this -> checkOutput("\x1b@String1231.2FooBar");
  47. }
  48. public function testTextObject() {
  49. $this -> setExpectedException('InvalidArgumentException');
  50. $this -> printer -> text(new DateTime());
  51. }
  52. public function testFeedDefault() {
  53. $this -> printer -> feed();
  54. $this -> checkOutput("\x1b@\x0a");
  55. }
  56. public function testFeed3Lines() {
  57. $this -> printer -> feed(3);
  58. $this -> checkOutput("\x1b@\x1bd\x03");
  59. }
  60. public function testFeedZero() {
  61. $this -> setExpectedException('InvalidArgumentException');
  62. $this -> printer -> feed(0);
  63. }
  64. public function testFeedNonInteger() {
  65. $this -> setExpectedException('InvalidArgumentException');
  66. $this -> printer -> feed("ab");
  67. }
  68. public function testFeedTooLarge() {
  69. $this -> setExpectedException('InvalidArgumentException');
  70. $this -> printer -> feed(256);
  71. }
  72. /* Print mode */
  73. public function testSelectPrintModeDefault() {
  74. $this -> printer -> selectPrintMode();
  75. $this -> checkOutput("\x1b@\x1b!\x00");
  76. }
  77. public function testSelectPrintModeAcceptedValues() {
  78. /* This iterates over a bunch of numbers, figures out which
  79. ones contain invalid flags, and checks that the driver
  80. rejects those, but accepts the good inputs */
  81. for($i = -1; $i <= 256; $i++) {
  82. $invalid = ($i < 0) || ($i > 255) || (($i & 2) == 2) || (($i & 4) == 4) || (($i & 64) == 64);
  83. $failed = false;
  84. try {
  85. $this -> printer -> selectPrintMode($i);
  86. } catch(Exception $e) {
  87. $failed = true;
  88. }
  89. $this -> assertEquals($failed, $invalid);
  90. }
  91. }
  92. /* Underline */
  93. public function testSetUnderlineDefault() {
  94. $this -> printer -> setUnderline();
  95. $this -> checkOutput("\x1b@\x1b-\x01");
  96. }
  97. public function testSetUnderlineOff() {
  98. $this -> printer -> setUnderline(Escpos::UNDERLINE_NONE);
  99. $this -> checkOutput("\x1b@\x1b-\x00");
  100. }
  101. public function testSetUnderlineOn() {
  102. $this -> printer -> setUnderline(Escpos::UNDERLINE_SINGLE);
  103. $this -> checkOutput("\x1b@\x1b-\x01");
  104. }
  105. public function testSetUnderlineDbl() {
  106. $this -> printer -> setUnderline(Escpos::UNDERLINE_DOUBLE);
  107. $this -> checkOutput("\x1b@\x1b-\x02");
  108. }
  109. public function testSetUnderlineAcceptedValues() {
  110. $this -> printer -> setUnderline(0);
  111. $this -> printer -> setUnderline(1);
  112. $this -> printer -> setUnderline(2);
  113. /* These map to 0 & 1 for interchangeability with setEmphasis */
  114. $this -> printer -> setUnderline(true);
  115. $this -> printer -> setUnderline(false);
  116. $this -> checkOutput("\x1b@\x1b-\x00\x1b-\x01\x1b-\x02\x1b-\x01\x1b-\x00");
  117. }
  118. public function testSetUnderlineTooLarge() {
  119. $this -> setExpectedException('InvalidArgumentException');
  120. $this -> printer -> setUnderline(3);
  121. }
  122. public function testSetUnderlineNegative() {
  123. $this -> setExpectedException('InvalidArgumentException');
  124. $this -> printer -> setUnderline(-1);
  125. }
  126. public function testSetUnderlineNonInteger() {
  127. $this -> setExpectedException('InvalidArgumentException');
  128. $this -> printer -> setUnderline("Hello");
  129. }
  130. /* Emphasis */
  131. public function testSetEmphasisDefault() {
  132. $this -> printer -> setEmphasis();
  133. $this -> checkOutput("\x1b@\x1bE\x01");
  134. }
  135. public function testSetEmphasisOn() {
  136. $this -> printer -> setEmphasis(true);
  137. $this -> checkOutput("\x1b@\x1bE\x01");
  138. }
  139. public function testSetEmphasisOff() {
  140. $this -> printer -> setEmphasis(false);
  141. $this -> checkOutput("\x1b@\x1bE\x00");
  142. }
  143. public function testSetEmphasisNonBoolean() {
  144. $this -> setExpectedException('InvalidArgumentException');
  145. $this -> printer -> setEmphasis(7);
  146. }
  147. /* Double strike */
  148. public function testSetDoubleStrikeDefault() {
  149. $this -> printer -> setDoubleStrike();
  150. $this -> checkOutput("\x1b@\x1bG\x01");
  151. }
  152. public function testSetDoubleStrikeOn() {
  153. $this -> printer -> setDoubleStrike(true);
  154. $this -> checkOutput("\x1b@\x1bG\x01");
  155. }
  156. public function testSetDoubleStrikeOff() {
  157. $this -> printer -> setDoubleStrike(false);
  158. $this -> checkOutput("\x1b@\x1bG\x00");
  159. }
  160. public function testSetDoubleStrikeNonBoolean() {
  161. $this -> setExpectedException('InvalidArgumentException');
  162. $this -> printer -> setDoubleStrike(4);
  163. }
  164. /* Font */
  165. public function testSetFontDefault() {
  166. $this -> printer -> setFont();
  167. $this -> checkOutput("\x1b@\x1bM\x00");
  168. }
  169. public function testSetFontAcceptedValues() {
  170. $this -> printer -> setFont(Escpos::FONT_A);
  171. $this -> printer -> setFont(Escpos::FONT_B);
  172. $this -> printer -> setFont(Escpos::FONT_C);
  173. $this -> checkOutput("\x1b@\x1bM\x00\x1bM\x01\x1bM\x02");
  174. }
  175. public function testSetFontNegative() {
  176. $this -> setExpectedException('InvalidArgumentException');
  177. $this -> printer -> setFont(-1);
  178. }
  179. public function testSetFontTooLarge() {
  180. $this -> setExpectedException('InvalidArgumentException');
  181. $this -> printer -> setFont(3);
  182. }
  183. public function testSetFontNonInteger() {
  184. $this -> setExpectedException('InvalidArgumentException');
  185. $this -> printer -> setFont('hello');
  186. }
  187. /* Justification */
  188. public function testSetJustificationDefault() {
  189. $this -> printer -> setJustification();
  190. $this -> checkOutput("\x1b@\x1ba\x00");
  191. }
  192. public function testSetJustificationLeft() {
  193. $this -> printer -> setJustification(Escpos::JUSTIFY_LEFT);
  194. $this -> checkOutput("\x1b@\x1ba\x00");
  195. }
  196. public function testSetJustificationRight() {
  197. $this -> printer -> setJustification(Escpos::JUSTIFY_RIGHT);
  198. $this -> checkOutput("\x1b@\x1ba\x02");
  199. }
  200. public function testSetJustificationCenter() {
  201. $this -> printer -> setJustification(Escpos::JUSTIFY_CENTER);
  202. $this -> checkOutput("\x1b@\x1ba\x01");
  203. }
  204. public function testSetJustificationNegative() {
  205. $this -> setExpectedException('InvalidArgumentException');
  206. $this -> printer -> setJustification(-1);
  207. }
  208. public function testSetJustificationTooLarge() {
  209. $this -> setExpectedException('InvalidArgumentException');
  210. $this -> printer -> setFont(3);
  211. }
  212. public function testSetJustificationNonInteger() {
  213. $this -> setExpectedException('InvalidArgumentException');
  214. $this -> printer -> setJustification('hello');
  215. }
  216. /* Reverse feed */
  217. public function testFeedReverseDefault() {
  218. $this -> printer -> feedReverse();
  219. $this -> checkOutput("\x1b@\x1be\x01");
  220. }
  221. public function testFeedReverse3() {
  222. $this -> printer -> feedReverse(3);
  223. $this -> checkOutput("\x1b@\x1be\x03");
  224. }
  225. public function testFeedReverseNegative() {
  226. $this -> setExpectedException('InvalidArgumentException');
  227. $this -> printer -> feedReverse(-1);
  228. }
  229. public function testFeedReverseTooLarge() {
  230. $this -> setExpectedException('InvalidArgumentException');
  231. $this -> printer -> feedReverse(256);
  232. }
  233. public function testFeedReverseNonInteger() {
  234. $this -> setExpectedException('InvalidArgumentException');
  235. $this -> printer -> feedReverse('hello');
  236. }
  237. /* Cut */
  238. public function testCutDefault() {
  239. // TODO check what the accepted range of values should be for $line
  240. // cut($mode = self::CUT_FULL, $lines = 3)
  241. $this -> printer -> cut();
  242. $this -> checkOutput("\x1b@\x1dVA\x03");
  243. }
  244. /* Set barcode height */
  245. public function testSetBarcodeHeightDefault() {
  246. $this -> printer -> setBarcodeHeight();
  247. $this -> checkOutput("\x1b@\x1dh\x08");
  248. }
  249. public function testBarcodeHeight10() {
  250. $this -> printer -> setBarcodeHeight(10);
  251. $this -> checkOutput("\x1b@\x1dh\x0a");
  252. }
  253. public function testSetBarcodeHeightNegative() {
  254. $this -> setExpectedException('InvalidArgumentException');
  255. $this -> printer -> setBarcodeHeight(-1);
  256. }
  257. public function testSetBarcodeHeightTooLarge() {
  258. $this -> setExpectedException('InvalidArgumentException');
  259. $this -> printer -> setBarcodeHeight(256);
  260. }
  261. public function tesSetBarcodeHeightNonInteger() {
  262. $this -> setExpectedException('InvalidArgumentException');
  263. $this -> printer -> setBarcodeHeight('hello');
  264. }
  265. /* Barcode text position */
  266. public function testSetBarcodeTextPositionDefault() {
  267. $this -> printer -> setBarcodeTextPosition();
  268. $this -> checkOutput("\x1b@\x1dH\x00");
  269. }
  270. public function testSetBarcodeTextPositionBelow() {
  271. $this -> printer -> setBarcodeTextPosition(Escpos::BARCODE_TEXT_BELOW);
  272. $this -> checkOutput("\x1b@\x1dH\x02");
  273. }
  274. public function testSetBarcodeTextPositionBoth() {
  275. $this -> printer -> setBarcodeTextPosition(Escpos::BARCODE_TEXT_BELOW | Escpos::BARCODE_TEXT_ABOVE);
  276. $this -> checkOutput("\x1b@\x1dH\x03");
  277. }
  278. public function testSetBarcodeTextPositionNegative() {
  279. $this -> setExpectedException('InvalidArgumentException');
  280. $this -> printer -> setBarcodeTextPosition(-1);
  281. }
  282. public function testSetBarcodeTextPositionTooLarge() {
  283. $this -> setExpectedException('InvalidArgumentException');
  284. $this -> printer -> setBarcodeTextPosition(4);
  285. }
  286. public function tesSetBarcodeTextPositionNonInteger() {
  287. $this -> setExpectedException('InvalidArgumentException');
  288. $this -> printer -> setBarcodeTextPosition('hello');
  289. }
  290. /* Barcode - UPC-A */
  291. public function testBarcodeUpcaNumeric11Char() {
  292. $this -> printer -> barcode("01234567890", Escpos::BARCODE_UPCA);
  293. $this -> checkOutput("\x1b@\x1dkA\x0b01234567890");
  294. }
  295. public function testBarcodeUpcaNumeric12Char() {
  296. $this -> printer -> barcode("012345678901", Escpos::BARCODE_UPCA);
  297. $this -> checkOutput("\x1b@\x1dkA\x0c012345678901");
  298. }
  299. public function testBarcodeUpcaNumeric13Char() {
  300. $this -> setExpectedException('InvalidArgumentException');
  301. $this -> printer -> barcode("0123456789012", Escpos::BARCODE_UPCA);
  302. }
  303. public function testBarcodeUpcaNonNumeric12Char() {
  304. $this -> setExpectedException('InvalidArgumentException');
  305. $this -> printer -> barcode("A12345678901", Escpos::BARCODE_UPCA);
  306. }
  307. /* Barcode - UPC-E */
  308. public function testBarcodeUpceNumeric6Char() {
  309. $this -> printer -> barcode("123456", Escpos::BARCODE_UPCE);
  310. $this -> checkOutput("\x1b@\x1dkB\x06123456");
  311. }
  312. public function testBarcodeUpceNumeric7Char() {
  313. $this -> printer -> barcode("0123456", Escpos::BARCODE_UPCE);
  314. $this -> checkOutput("\x1b@\x1dkB\x070123456");
  315. }
  316. public function testBarcodeUpceNumeric8Char() {
  317. $this -> printer -> barcode("01234567", Escpos::BARCODE_UPCE);
  318. $this -> checkOutput("\x1b@\x1dkB\x0801234567");
  319. }
  320. public function testBarcodeUpceNumeric11Char() {
  321. $this -> printer -> barcode("01234567890", Escpos::BARCODE_UPCE);
  322. $this -> checkOutput("\x1b@\x1dkB\x0b01234567890");
  323. }
  324. public function testBarcodeUpceNumeric12Char() {
  325. $this -> printer -> barcode("012345678901", Escpos::BARCODE_UPCE);
  326. $this -> checkOutput("\x1b@\x1dkB\x0c012345678901");
  327. }
  328. public function testBarcodeUpceNumeric9Char() {
  329. $this -> setExpectedException('InvalidArgumentException');
  330. $this -> printer -> barcode("012345678", Escpos::BARCODE_UPCE);
  331. }
  332. public function testBarcodeUpceNonNumeric12Char() {
  333. $this -> setExpectedException('InvalidArgumentException');
  334. $this -> printer -> barcode("A12345678901", Escpos::BARCODE_UPCE);
  335. }
  336. /* Barcode - JAN13 */
  337. public function testBarcodeJan13Numeric12Char() {
  338. $this -> printer -> barcode("012345678901", Escpos::BARCODE_JAN13);
  339. $this -> checkOutput("\x1b@\x1dkC\x0c012345678901");
  340. }
  341. public function testBarcodeJan13Numeric13Char() {
  342. $this -> printer -> barcode("0123456789012", Escpos::BARCODE_JAN13);
  343. $this -> checkOutput("\x1b@\x1dkC\x0d0123456789012");
  344. }
  345. public function testBarcodeJan13Numeric11Char() {
  346. $this -> setExpectedException('InvalidArgumentException');
  347. $this -> printer -> barcode("01234567890", Escpos::BARCODE_JAN13);
  348. }
  349. public function testBarcodeJan13NonNumeric13Char() {
  350. $this -> setExpectedException('InvalidArgumentException');
  351. $this -> printer -> barcode("A123456789012", Escpos::BARCODE_JAN13);
  352. }
  353. /* Barcode - JAN8 */
  354. public function testBarcodeJan8Numeric7Char() {
  355. $this -> printer -> barcode("0123456", Escpos::BARCODE_JAN8);
  356. $this -> checkOutput("\x1b@\x1dkD\x070123456");
  357. }
  358. public function testBarcodeJan8Numeric8Char() {
  359. $this -> printer -> barcode("01234567", Escpos::BARCODE_JAN8);
  360. $this -> checkOutput("\x1b@\x1dkD\x0801234567");
  361. }
  362. public function testBarcodeJan8Numeric9Char() {
  363. $this -> setExpectedException('InvalidArgumentException');
  364. $this -> printer -> barcode("012345678", Escpos::BARCODE_JAN8);
  365. }
  366. public function testBarcodeJan8NonNumeric8Char() {
  367. $this -> setExpectedException('InvalidArgumentException');
  368. $this -> printer -> barcode("A1234567", Escpos::BARCODE_JAN8);
  369. }
  370. /* Barcode - Code39 */
  371. public function testBarcodeCode39AsDefault() {
  372. $this -> printer -> barcode("1234");
  373. $this -> checkOutput("\x1b@\x1dkE\x041234");
  374. }
  375. public function testBarcodeCode39Text() {
  376. $this -> printer -> barcode("ABC 012", Escpos::BARCODE_CODE39);
  377. $this -> checkOutput("\x1b@\x1dkE\x07ABC 012");
  378. }
  379. public function testBarcodeCode39SpecialChars() {
  380. $this -> printer -> barcode("$%+-./", Escpos::BARCODE_CODE39);
  381. $this -> checkOutput("\x1b@\x1dkE\x06$%+-./");
  382. }
  383. public function testBarcodeCode39Asterisks() {
  384. $this -> printer -> barcode("*TEXT*", Escpos::BARCODE_CODE39);
  385. $this -> checkOutput("\x1b@\x1dkE\x06*TEXT*");
  386. }
  387. public function testBarcodeCode39AsterisksUnmatched() {
  388. $this -> setExpectedException('InvalidArgumentException');
  389. $this -> printer -> barcode("*TEXT", Escpos::BARCODE_CODE39);
  390. }
  391. public function testBarcodeCode39AsteriskInText() {
  392. $this -> setExpectedException('InvalidArgumentException');
  393. $this -> printer -> barcode("12*34", Escpos::BARCODE_CODE39);
  394. }
  395. public function testBarcodeCode39Lowercase() {
  396. $this -> setExpectedException('InvalidArgumentException');
  397. $this -> printer -> barcode("abcd", Escpos::BARCODE_CODE39);
  398. }
  399. public function testBarcodeCode39Empty() {
  400. $this -> setExpectedException('InvalidArgumentException');
  401. $this -> printer -> barcode("**", Escpos::BARCODE_CODE39);
  402. }
  403. /* Barcode - ITF */
  404. public function testBarcodeItfNumericEven() {
  405. $this -> printer -> barcode("1234", Escpos::BARCODE_ITF);
  406. $this -> checkOutput("\x1b@\x1dkF\x041234");
  407. }
  408. public function testBarcodeItfNumericOdd() {
  409. $this -> setExpectedException('InvalidArgumentException');
  410. $this -> printer -> barcode("123", Escpos::BARCODE_ITF);
  411. }
  412. public function testBarcodeItfNonNumericEven() {
  413. $this -> setExpectedException('InvalidArgumentException');
  414. $this -> printer -> barcode("A234", Escpos::BARCODE_ITF);
  415. }
  416. /* Barcode - Codabar */
  417. public function testBarcodeCodabarNumeric() {
  418. $this -> printer -> barcode("A012345A", Escpos::BARCODE_CODABAR);
  419. $this -> checkOutput("\x1b@\x1dkG\x08A012345A");
  420. }
  421. public function testBarcodeCodabarSpecialChars() {
  422. $this -> printer -> barcode("A012$+-./:A", Escpos::BARCODE_CODABAR);
  423. $this -> checkOutput("\x1b@\x1dkG\x0bA012$+-./:A");
  424. }
  425. public function testBarcodeCodabarNotWrapped() {
  426. $this -> setExpectedException('InvalidArgumentException');
  427. $this -> printer -> barcode("012345", Escpos::BARCODE_CODABAR);
  428. }
  429. public function testBarcodeCodabarStartStopWrongPlace() {
  430. $this -> setExpectedException('InvalidArgumentException');
  431. $this -> printer -> barcode("012A45", Escpos::BARCODE_CODABAR);
  432. }
  433. /* Barcode - Code93 */
  434. public function testBarcodeCode93Valid() {
  435. $this -> printer -> barcode("012abcd", Escpos::BARCODE_CODE93);
  436. $this -> checkOutput("\x1b@\x1dkH\x07012abcd");
  437. }
  438. public function testBarcodeCode93Empty() {
  439. $this -> setExpectedException('InvalidArgumentException');
  440. $this -> printer -> barcode("", Escpos::BARCODE_CODE93);
  441. }
  442. /* Barcode - Code128 */
  443. public function testBarcodeCode128ValidA() {
  444. $this -> printer -> barcode("{A" . "012ABCD", Escpos::BARCODE_CODE128);
  445. $this -> checkOutput("\x1b@\x1dkI\x09{A012ABCD");
  446. }
  447. public function testBarcodeCode128ValidB() {
  448. $this -> printer -> barcode("{B" . "012ABCDabcd", Escpos::BARCODE_CODE128);
  449. $this -> checkOutput("\x1b@\x1dkI\x0d{B012ABCDabcd");
  450. }
  451. public function testBarcodeCode128ValidC() {
  452. $this -> printer -> barcode("{C" . chr ( 21 ) . chr ( 32 ) . chr ( 43 ), Escpos::BARCODE_CODE128);
  453. $this -> checkOutput("\x1b@\x1dkI\x05{C\x15 +");
  454. }
  455. public function testBarcodeCode128NoCodeSet() {
  456. $this -> setExpectedException('InvalidArgumentException');
  457. $this -> printer -> barcode("ABCD", Escpos::BARCODE_CODE128);
  458. }
  459. /* Pulse */
  460. function testPulseDefault() {
  461. $this -> printer -> pulse();
  462. $this -> checkOutput("\x1b@\x1bp0<x");
  463. }
  464. function testPulse1() {
  465. $this -> printer -> pulse(1);
  466. $this -> checkOutput("\x1b@\x1bp1<x");
  467. }
  468. function testPulseEvenMs() {
  469. $this -> printer -> pulse(0, 2, 2);
  470. $this -> checkOutput("\x1b@\x1bp0\x01\x01");
  471. }
  472. function testPulseOddMs() {
  473. $this -> printer -> pulse(0, 3, 3); // Should be rounded down and give same output
  474. $this -> checkOutput("\x1b@\x1bp0\x01\x01");
  475. }
  476. function testPulseTooHigh() {
  477. $this -> setExpectedException('InvalidArgumentException');
  478. $this -> printer -> pulse(0, 512, 2);
  479. }
  480. function testPulseTooLow() {
  481. $this -> setExpectedException('InvalidArgumentException');
  482. $this -> printer -> pulse(0, 0, 2);
  483. }
  484. function testPulseNotANumber() {
  485. $this -> setExpectedException('InvalidArgumentException');
  486. $this -> printer -> pulse("fish");
  487. }
  488. /* Set reverse */
  489. public function testSetReverseColorsDefault() {
  490. $this -> printer -> setReverseColors();
  491. $this -> checkOutput("\x1b@\x1dB\x01");
  492. }
  493. public function testSetReverseColorsOn() {
  494. $this -> printer -> setReverseColors(true);
  495. $this -> checkOutput("\x1b@\x1dB\x01");
  496. }
  497. public function testSetReverseColorsOff() {
  498. $this -> printer -> setReverseColors(false);
  499. $this -> checkOutput("\x1b@\x1dB\x00");
  500. }
  501. public function testSetReverseColorsNonBoolean() {
  502. $this -> setExpectedException('InvalidArgumentException');
  503. $this -> printer -> setReverseColors(7);
  504. }
  505. /* Bit image print */
  506. public function testBitImageBlack() {
  507. $this -> requireGraphicsLibrary();
  508. $img = new EscposImage(dirname(__FILE__)."/resources/canvas_black.png");
  509. $this -> printer -> bitImage($img);
  510. $this -> checkOutput("\x1b@\x1dv0\x00\x01\x00\x01\x00\x80");
  511. }
  512. public function testBitImageWhite() {
  513. $this -> requireGraphicsLibrary();
  514. $img = new EscposImage(dirname(__FILE__)."/resources/canvas_white.png");
  515. $this -> printer -> bitImage($img);
  516. $this -> checkOutput("\x1b@\x1dv0\x00\x01\x00\x01\x00\x00");
  517. }
  518. public function testBitImageBoth() {
  519. $this -> requireGraphicsLibrary();
  520. $img = new EscposImage(dirname(__FILE__)."/resources/black_white.png");
  521. $this -> printer -> bitImage($img);
  522. $this -> checkOutput("\x1b@\x1dv0\x00\x01\x00\x02\x00\xc0\x00");
  523. }
  524. public function testBitImageTransparent() {
  525. $this -> requireGraphicsLibrary();
  526. $img = new EscposImage(dirname(__FILE__)."/resources/black_transparent.png");
  527. $this -> printer -> bitImage($img);
  528. $this -> checkOutput("\x1b@\x1dv0\x00\x01\x00\x02\x00\xc0\x00");
  529. }
  530. /* Graphics print */
  531. public function testGraphicsWhite() {
  532. $this -> requireGraphicsLibrary();
  533. $img = new EscposImage(dirname(__FILE__)."/resources/canvas_white.png");
  534. $this -> printer -> graphics($img);
  535. $this -> checkOutput("\x1b@\x1d(L\x0b\x000p0\x01\x011\x01\x00\x01\x00\x00\x1d(L\x02\x0002");
  536. }
  537. public function testGraphicsBlack() {
  538. $this -> requireGraphicsLibrary();
  539. $img = new EscposImage(dirname(__FILE__)."/resources/canvas_black.png");
  540. $this -> printer -> graphics($img);
  541. $this -> checkOutput("\x1b@\x1d(L\x0b\x000p0\x01\x011\x01\x00\x01\x00\x80\x1d(L\x02\x0002");
  542. }
  543. public function testGraphicsBoth() {
  544. $this -> requireGraphicsLibrary();
  545. $img = new EscposImage(dirname(__FILE__)."/resources/black_white.png");
  546. $this -> printer -> graphics($img);
  547. $this -> checkOutput("\x1b@\x1d(L\x0c\x000p0\x01\x011\x02\x00\x02\x00\xc0\x00\x1d(L\x02\x0002");
  548. }
  549. public function testGraphicsTransparent() {
  550. $this -> requireGraphicsLibrary();
  551. $img = new EscposImage(dirname(__FILE__)."/resources/black_transparent.png");
  552. $this -> printer -> graphics($img);
  553. $this -> checkOutput("\x1b@\x1d(L\x0c\x000p0\x01\x011\x02\x00\x02\x00\xc0\x00\x1d(L\x02\x0002");
  554. }
  555. /* QR code */
  556. public function testQRCodeDefaults() {
  557. // Test will fail if default values change
  558. $this -> printer -> qrCode("1234");
  559. $this -> checkOutput("\x1b@\x1d(k\x04\x001A2\x00\x1d(k\x03\x001C\x03\x1d(k\x03\x001E0\x1d(k\x07\x001P01234\x1d(k\x03\x001Q0");
  560. }
  561. public function testQRCodeDefaultsAreCorrect() {
  562. // Below tests assume that defaults are as written here (output string should be same as above)
  563. $this -> printer -> qrCode("1234", Escpos::QR_ECLEVEL_L, 3, Escpos::QR_MODEL_2);
  564. $this -> checkOutput("\x1b@\x1d(k\x04\x001A2\x00\x1d(k\x03\x001C\x03\x1d(k\x03\x001E0\x1d(k\x07\x001P01234\x1d(k\x03\x001Q0");
  565. }
  566. public function testQRCodeEmpty() {
  567. $this -> printer -> qrCode('');
  568. $this -> checkOutput("\x1b@"); // No commands actually sent
  569. }
  570. public function testQRCodeChangeEC() {
  571. $this -> printer -> qrCode("1234", Escpos::QR_ECLEVEL_H);
  572. $this -> checkOutput("\x1b@\x1d(k\x04\x001A2\x00\x1d(k\x03\x001C\x03\x1d(k\x03\x001E3\x1d(k\x07\x001P01234\x1d(k\x03\x001Q0");
  573. }
  574. public function testQRCodeChangeSize() {
  575. $this -> printer -> qrCode("1234", Escpos::QR_ECLEVEL_L, 7);
  576. $this -> checkOutput("\x1b@\x1d(k\x04\x001A2\x00\x1d(k\x03\x001C\x07\x1d(k\x03\x001E0\x1d(k\x07\x001P01234\x1d(k\x03\x001Q0");
  577. }
  578. public function testQRCodeChangeModel() {
  579. $this -> printer -> qrCode("1234", Escpos::QR_ECLEVEL_L, 3, Escpos::QR_MODEL_1);
  580. $this -> checkOutput("\x1b@\x1d(k\x04\x001A1\x00\x1d(k\x03\x001C\x03\x1d(k\x03\x001E0\x1d(k\x07\x001P01234\x1d(k\x03\x001Q0");
  581. }
  582. /* Feed form - Required on page-mode only printers */
  583. public function testFeedForm() {
  584. $this -> printer -> feedForm();
  585. $this -> checkOutput("\x1b@\x0c");
  586. }
  587. /* Get status */
  588. public function testGetStatus() {
  589. $this -> markTestIncomplete("Status check test code not implemented.");
  590. // TODO some unit testing here on statuses
  591. // $a = $this -> printer -> getPrinterStatus(Escpos::STATUS_PRINTER);
  592. }
  593. /* Set text size */
  594. public function testSetTextSizeNormal() {
  595. $this -> printer -> setTextSize(1, 1);
  596. $this -> checkOutput("\x1b@\x1d!\x00");
  597. }
  598. public function testSetTextSizeWide() {
  599. $this -> printer -> setTextSize(4, 1);
  600. $this -> checkOutput("\x1b@\x1d!0");
  601. }
  602. public function testSetTextSizeNarrow() {
  603. $this -> printer -> setTextSize(1, 4);
  604. $this -> checkOutput("\x1b@\x1d!\x03");
  605. }
  606. public function testSetTextSizeLarge() {
  607. $this -> printer -> setTextSize(4, 4);
  608. $this -> checkOutput("\x1b@\x1d!3");
  609. }
  610. public function testSetTextSizeInvalid() {
  611. $this -> setExpectedException('InvalidArgumentException');
  612. $this -> printer -> setTextSize(0, 9);
  613. }
  614. }
  615. /*
  616. * For testing that string-castable objects are handled
  617. */
  618. class FooBar {
  619. private $foo;
  620. public function __construct($foo) {
  621. $this -> foo = $foo;
  622. }
  623. public function __toString() {
  624. return $this -> foo;
  625. }
  626. }
  627. ?>