WindowsPrintConnectorTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
  3. class WindowsPrintConnectorTest extends PHPUnit\Framework\TestCase
  4. {
  5. private $connector;
  6. public function testLptWindows()
  7. {
  8. // Should attempt to send data to the local printer by writing to it
  9. $connector = $this -> getMockConnector("LPT1", WindowsPrintConnector::PLATFORM_WIN);
  10. $connector -> expects($this -> once())
  11. -> method('runWrite')
  12. -> with($this -> equalTo(''), $this -> equalTo("LPT1"));
  13. $connector -> expects($this -> exactly(0))
  14. -> method('runCommand');
  15. $connector -> expects($this -> exactly(0))
  16. -> method('runCopy');
  17. $connector -> finalize();
  18. }
  19. public function testLptMac()
  20. {
  21. // Cannot print to local printer on Mac with this connector
  22. $this -> expectException(BadMethodCallException::class);
  23. $connector = $this -> getMockConnector("LPT1", WindowsPrintConnector::PLATFORM_MAC);
  24. $connector -> expects($this -> exactly(0))
  25. -> method('runWrite');
  26. $connector -> expects($this -> exactly(0))
  27. -> method('runCommand');
  28. $connector -> expects($this -> exactly(0))
  29. -> method('runCopy');
  30. $connector -> finalize();
  31. }
  32. public function testLptLinux()
  33. {
  34. // Cannot print to local printer on Linux with this connector
  35. $this -> expectException(BadMethodCallException::class);
  36. $connector = $this -> getMockConnector("LPT1", WindowsPrintConnector::PLATFORM_LINUX);
  37. $connector -> expects($this -> exactly(0))
  38. -> method('runWrite');
  39. $connector -> expects($this -> exactly(0))
  40. -> method('runCommand');
  41. $connector -> expects($this -> exactly(0))
  42. -> method('runCopy');
  43. $connector -> finalize();
  44. }
  45. public function testComWindows()
  46. {
  47. // Simple file write
  48. $connector = $this -> getMockConnector("COM1", WindowsPrintConnector::PLATFORM_WIN);
  49. $connector -> expects($this -> once())
  50. -> method('runWrite')
  51. -> with($this -> equalTo(''), $this -> equalTo("COM1"));
  52. $connector -> expects($this -> exactly(0))
  53. -> method('runCommand');
  54. $connector -> expects($this -> exactly(0))
  55. -> method('runCopy');
  56. $connector -> finalize();
  57. }
  58. public function testComMac()
  59. {
  60. // Cannot print to local printer on Mac with this connector
  61. $this -> expectException(BadMethodCallException::class);
  62. $connector = $this -> getMockConnector("COM1", WindowsPrintConnector::PLATFORM_MAC);
  63. $connector -> expects($this -> exactly(0))
  64. -> method('runWrite');
  65. $connector -> expects($this -> exactly(0))
  66. -> method('runCommand');
  67. $connector -> expects($this -> exactly(0))
  68. -> method('runCopy');
  69. $connector -> finalize();
  70. }
  71. public function testComLinux()
  72. {
  73. // Cannot print to local printer on Linux with this connector
  74. $this -> expectException(BadMethodCallException::class);
  75. $connector = $this -> getMockConnector("COM1", WindowsPrintConnector::PLATFORM_LINUX);
  76. $connector -> expects($this -> exactly(0))
  77. -> method('runWrite');
  78. $connector -> expects($this -> exactly(0))
  79. -> method('runCommand');
  80. $connector -> expects($this -> exactly(0))
  81. -> method('runCopy');
  82. $connector -> finalize();
  83. }
  84. public function testLocalShareWindows()
  85. {
  86. $connector = $this -> getMockConnector("Printer", WindowsPrintConnector::PLATFORM_WIN);
  87. $connector -> expects($this -> exactly(0))
  88. -> method('runCommand');
  89. $connector -> expects($this -> exactly(0))
  90. -> method('runWrite');
  91. $connector -> expects($this -> once())
  92. -> method('runCopy')
  93. -> with($this -> anything(), $this -> stringContains('\\Printer'));
  94. $connector -> finalize();
  95. }
  96. public function testSharedPrinterWindows()
  97. {
  98. $connector = $this -> getMockConnector("smb://example-pc/Printer", WindowsPrintConnector::PLATFORM_WIN);
  99. $connector -> expects($this -> exactly(0))
  100. -> method('runCommand');
  101. $connector -> expects($this -> exactly(0))
  102. -> method('runWrite');
  103. $connector -> expects($this -> once())
  104. -> method('runCopy')
  105. -> with($this -> anything(), $this -> equalTo('\\\\example-pc\\Printer'));
  106. $connector -> finalize();
  107. }
  108. public function testSharedPrinterWindowsUsername()
  109. {
  110. $connector = $this -> getMockConnector("smb://bob@example-pc/Printer", WindowsPrintConnector::PLATFORM_WIN);
  111. $connector -> expects($this -> once())
  112. -> method('runCommand')
  113. -> with($this -> equalTo('net use \'\\\\example-pc\\Printer\' \'/user:bob\''));
  114. $connector -> expects($this -> exactly(0))
  115. -> method('runWrite');
  116. $connector -> expects($this -> once())
  117. -> method('runCopy')
  118. -> with($this -> anything(), $this -> equalTo('\\\\example-pc\\Printer'));
  119. $connector -> finalize();
  120. }
  121. public function testSharedPrinterWindowsUsernameDomain()
  122. {
  123. $connector = $this -> getMockConnector("smb://bob@example-pc/home/Printer", WindowsPrintConnector::PLATFORM_WIN);
  124. $connector -> expects($this -> once())
  125. -> method('runCommand')
  126. -> with($this -> equalTo('net use \'\\\\example-pc\\Printer\' \'/user:home\\bob\''));
  127. $connector -> expects($this -> exactly(0))
  128. -> method('runWrite');
  129. $connector -> expects($this -> once())
  130. -> method('runCopy')
  131. -> with($this -> anything(), $this -> equalTo('\\\\example-pc\\Printer'));
  132. $connector -> finalize();
  133. }
  134. public function testSharedPrinterWindowsUsernamePassword()
  135. {
  136. $connector = $this -> getMockConnector("smb://bob:secret@example-pc/Printer", WindowsPrintConnector::PLATFORM_WIN);
  137. $connector -> expects($this -> once())
  138. -> method('runCommand')
  139. -> with($this -> equalTo('net use \'\\\\example-pc\\Printer\' \'/user:bob\' \'secret\''));
  140. $connector -> expects($this -> exactly(0))
  141. -> method('runWrite');
  142. $connector -> expects($this -> once())
  143. -> method('runCopy')
  144. -> with($this -> anything(), $this -> equalTo('\\\\example-pc\\Printer'));
  145. $connector -> finalize();
  146. }
  147. public function testSharedPrinterMac()
  148. {
  149. // Not implemented
  150. $this -> expectException(Exception::class);
  151. $connector = $this -> getMockConnector("smb://Guest@example-pc/Printer", WindowsPrintConnector::PLATFORM_MAC);
  152. $connector -> expects($this -> exactly(0))
  153. -> method('runWrite');
  154. $connector -> expects($this -> exactly(0))
  155. -> method('runCommand');
  156. $connector -> expects($this -> exactly(0))
  157. -> method('runCopy');
  158. $connector -> finalize();
  159. }
  160. public function testSharedPrinterLinux()
  161. {
  162. $connector = $this -> getMockConnector("smb://example-pc/Printer", WindowsPrintConnector::PLATFORM_LINUX);
  163. $connector -> expects($this -> once())
  164. -> method('runCommand')
  165. -> with($this -> equalTo('smbclient \'//example-pc/Printer\' -c \'print -\' -N -m SMB2'));
  166. $connector -> expects($this -> exactly(0))
  167. -> method('runCopy');
  168. $connector -> expects($this -> exactly(0))
  169. -> method('runWrite');
  170. $connector -> finalize();
  171. }
  172. public function testSharedPrinterLinuxUsername()
  173. {
  174. $connector = $this -> getMockConnector("smb://bob@example-pc/Printer", WindowsPrintConnector::PLATFORM_LINUX);
  175. $connector -> expects($this -> once())
  176. -> method('runCommand')
  177. -> with($this -> equalTo('smbclient \'//example-pc/Printer\' -U \'bob\' -c \'print -\' -N -m SMB2'));
  178. $connector -> expects($this -> exactly(0))
  179. -> method('runCopy');
  180. $connector -> expects($this -> exactly(0))
  181. -> method('runWrite');
  182. $connector -> finalize();
  183. }
  184. public function testSharedPrinterLinuxUsernameDomain()
  185. {
  186. $connector = $this -> getMockConnector("smb://bob@example-pc/home/Printer", WindowsPrintConnector::PLATFORM_LINUX);
  187. $connector -> expects($this -> once())
  188. -> method('runCommand')
  189. -> with($this -> equalTo('smbclient \'//example-pc/Printer\' -U \'home\\bob\' -c \'print -\' -N -m SMB2'));
  190. $connector -> expects($this -> exactly(0))
  191. -> method('runCopy');
  192. $connector -> expects($this -> exactly(0))
  193. -> method('runWrite');
  194. $connector -> finalize();
  195. }
  196. public function testSharedPrinterLinuxUsernamePassword()
  197. {
  198. $connector = $this -> getMockConnector("smb://bob:secret@example-pc/Printer", WindowsPrintConnector::PLATFORM_LINUX);
  199. $connector -> expects($this -> once())
  200. -> method('runCommand')
  201. -> with($this -> equalTo('smbclient \'//example-pc/Printer\' \'secret\' -U \'bob\' -c \'print -\' -m SMB2'));
  202. $connector -> expects($this -> exactly(0))
  203. -> method('runCopy');
  204. $connector -> expects($this -> exactly(0))
  205. -> method('runWrite');
  206. $connector -> finalize();
  207. }
  208. private function getMockConnector($path, $platform)
  209. {
  210. $stub = $this -> getMockBuilder('Mike42\Escpos\PrintConnectors\WindowsPrintConnector')
  211. -> setMethods(array('runCopy', 'runCommand', 'getCurrentPlatform', 'runWrite'))
  212. -> disableOriginalConstructor()
  213. -> getMock();
  214. $stub -> method('runCommand')
  215. -> willReturn(0);
  216. $stub -> method('runCopy')
  217. -> willReturn(true);
  218. $stub -> method('runWrite')
  219. -> willReturn(true);
  220. $stub -> method('getCurrentPlatform')
  221. -> willReturn($platform);
  222. $stub -> __construct($path);
  223. return $stub;
  224. }
  225. /**
  226. * Test for correct identification of bogus or non-supported Samba strings.
  227. */
  228. public function testSambaRegex()
  229. {
  230. $good = array("smb://foo/bar",
  231. "smb://foo/bar baz",
  232. "smb://bob@foo/bar",
  233. "smb://bob:secret@foo/bar",
  234. "smb://foo-computer/FooPrinter",
  235. "smb://foo-computer/workgroup/FooPrinter",
  236. "smb://foo-computer/Foo-Printer",
  237. "smb://foo-computer/workgroup/Foo-Printer",
  238. "smb://foo-computer/Foo Printer",
  239. "smb://foo-computer.local/Foo Printer",
  240. "smb://127.0.0.1/abcd"
  241. );
  242. $bad = array("",
  243. "http://google.com",
  244. "smb:/foo/bar",
  245. "smb://",
  246. "smb:///bar",
  247. "smb://@foo/bar",
  248. "smb://bob:@foo/bar",
  249. "smb://:secret@foo/bar",
  250. "smb://foo/bar/baz/quux",
  251. "smb://foo-computer//FooPrinter");
  252. foreach ($good as $item) {
  253. $this -> assertTrue(preg_match(WindowsPrintConnector::REGEX_SMB, $item) == 1, "Windows samba regex should pass '$item'.");
  254. }
  255. foreach ($bad as $item) {
  256. $this -> assertTrue(preg_match(WindowsPrintConnector::REGEX_SMB, $item) != 1, "Windows samba regex should fail '$item'.");
  257. }
  258. }
  259. public function testPrinterNameRegex()
  260. {
  261. $good = array("a",
  262. "ab",
  263. "a b",
  264. "a-b",
  265. "Abcd Efg-",
  266. "-a",
  267. "OK1"
  268. );
  269. $bad = array("",
  270. " ",
  271. "a ",
  272. " a",
  273. " a ",
  274. "a/B",
  275. "A:b"
  276. );
  277. foreach ($good as $item) {
  278. $this -> assertTrue(preg_match(WindowsPrintConnector::REGEX_PRINTERNAME, $item) == 1, "Windows printer name regex should pass '$item'.");
  279. }
  280. foreach ($bad as $item) {
  281. $this -> assertTrue(preg_match(WindowsPrintConnector::REGEX_PRINTERNAME, $item) != 1, "Windows printer name regex should fail '$item'.");
  282. }
  283. }
  284. }