CodePageTest.php 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. use Mike42\Escpos\CodePage;
  3. class CodePageTest extends PHPUnit\Framework\TestCase
  4. {
  5. public function testDataGenerated()
  6. {
  7. // Set up CP437
  8. $cp = new CodePage("CP437", array(
  9. "name" => "CP437",
  10. "iconv" => "CP437"
  11. ));
  12. $dataArray = $cp->getDataArray();
  13. $this->assertEquals(128, count($dataArray));
  14. $expected = "ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧ƒáíóúñѪº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀αßΓπΣσμτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■ ";
  15. $this->assertEquals($expected, self::dataArrayToString($dataArray));
  16. }
  17. public function testDataGenerateFailed()
  18. {
  19. // No errors raised, you just get an empty list of supported characters if you try to compute a fake code page
  20. $cp = new CodePage("foo", array(
  21. "name" => "foo",
  22. "iconv" => "foo"
  23. ));
  24. $this->assertTrue($cp->isEncodable());
  25. $this->assertEquals($cp->getIconv(), "foo");
  26. $this->assertEquals($cp->getName(), "foo");
  27. $this->assertEquals($cp->getId(), "foo");
  28. $this->assertEquals($cp->getNotes(), null);
  29. $dataArray = $cp->getDataArray();
  30. $expected = str_repeat(" ", 128);
  31. $this->assertEquals($expected, self::dataArrayToString($dataArray));
  32. // Do this twice (caching behaviour)
  33. $dataArray = $cp->getDataArray();
  34. $this->assertEquals($expected, self::dataArrayToString($dataArray));
  35. }
  36. public function testDataDefined()
  37. {
  38. // A made up code page called "baz", which is the same as CP437 but with some unmapped values at the start.
  39. $cp = new CodePage("baz", array(
  40. "name" => "baz",
  41. "iconv" => "baz",
  42. "data" => [
  43. " âäàåçêëèïîìÄÅ",
  44. "ÉæÆôöòûùÿÖÜ¢£¥₧ƒ",
  45. "áíóúñѪº¿⌐¬½¼¡«»",
  46. "░▒▓│┤╡╢╖╕╣║╗╝╜╛┐",
  47. "└┴┬├─┼╞╟╚╔╩╦╠═╬╧",
  48. "╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀",
  49. "αßΓπΣσμτΦΘΩδ∞φε∩",
  50. "≡±≥≤⌠⌡÷≈°∙·√ⁿ²■ "]
  51. ));
  52. $dataArray = $cp->getDataArray();
  53. $this->assertEquals(128, count($dataArray));
  54. $expected = " âäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧ƒáíóúñѪº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀αßΓπΣσμτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■ ";
  55. $this->assertEquals($expected, self::dataArrayToString($dataArray));
  56. }
  57. public function testDataCannotEncode()
  58. {
  59. $this->expectException(InvalidArgumentException::class);
  60. $cp = new CodePage("foo", array(
  61. "name" => "foo"
  62. ));
  63. $this->assertFalse($cp->isEncodable());
  64. $cp->getDataArray();
  65. }
  66. private static function dataArrayToString(array $codePoints) : string
  67. {
  68. // Assemble into character string so that the assertion is more compact
  69. return implode(array_map("IntlChar::chr", $codePoints));
  70. }
  71. }