UriPrintConnectorTest.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. use Mike42\Escpos\PrintConnectors\UriPrintConnector;
  3. use PHPUnit\Framework\Error\Notice;
  4. class UriPrintConnectorTest extends PHPUnit\Framework\TestCase
  5. {
  6. public function testFile()
  7. {
  8. $filename = tempnam(sys_get_temp_dir(), "escpos-php-");
  9. // Make connector, write some data
  10. $connector = UriPrintConnector::get("file://" . $filename);
  11. $connector -> write("AAA");
  12. $connector -> finalize();
  13. $this -> assertEquals("AAA", file_get_contents($filename));
  14. $this -> assertEquals('Mike42\Escpos\PrintConnectors\FilePrintConnector', get_class($connector));
  15. unlink($filename);
  16. }
  17. public function testSmb()
  18. {
  19. $this->expectNotice();
  20. $this->expectNoticeMessage("not finalized");
  21. $connector = UriPrintConnector::get("smb://windows/printer");
  22. $this -> assertEquals('Mike42\Escpos\PrintConnectors\WindowsPrintConnector', get_class($connector));
  23. // We expect that this will throw an exception, we can't
  24. // realistically print to a real printer in this test though... :)
  25. $connector -> __destruct();
  26. }
  27. public function testBadUri()
  28. {
  29. $this->expectException(InvalidArgumentException::class);
  30. $this->expectExceptionMessage("Malformed connector URI");
  31. $connector = UriPrintConnector::get("foooooo");
  32. }
  33. public function testNetwork()
  34. {
  35. $this->expectExceptionMessage("Connection refused");
  36. $this->expectException(Exception::class);
  37. // Port should be closed so we can catch an error and move on
  38. $connector = UriPrintConnector::get("tcp://localhost:45987/");
  39. }
  40. public function testUnsupportedUri()
  41. {
  42. $this->expectExceptionMessage("URI sheme is not supported: ldap://");
  43. $this->expectException(InvalidArgumentException::class);
  44. // Try to print to something silly
  45. $connector = UriPrintConnector::get("ldap://host:1234/");
  46. }
  47. }