FriendlyErrors.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php namespace thiagoalessio\TesseractOCR\Tests\EndToEnd;
  2. use thiagoalessio\TesseractOCR\Tests\Common\TestCase;
  3. use thiagoalessio\TesseractOCR\TesseractOCR;
  4. use thiagoalessio\TesseractOCR\Tests\Unit\TestableCommand;
  5. use thiagoalessio\TesseractOCR\ImageNotFoundException;
  6. use thiagoalessio\TesseractOCR\TesseractNotFoundException;
  7. use thiagoalessio\TesseractOCR\UnsuccessfulCommandException;
  8. class FriendlyErrors extends TestCase
  9. {
  10. public function testImageNotFound()
  11. {
  12. $currentDir = realpath(
  13. join(DIRECTORY_SEPARATOR, [__DIR__, '..', '..', 'src'])
  14. );
  15. $expected = array();
  16. $expected[] = 'Error! The image "/invalid/image.png" was not found.';
  17. $expected[] = '';
  18. $expected[] = "The current __DIR__ is $currentDir";
  19. $expected = join(PHP_EOL, $expected);
  20. try {
  21. (new TesseractOCR('/invalid/image.png'))->run();
  22. throw new \Exception('ImageNotFoundException not thrown');
  23. } catch (\Exception $e) {
  24. $this->assertEquals($expected, $e->getMessage());
  25. }
  26. }
  27. public function testExecutableNotFound()
  28. {
  29. $currentPath = getenv('PATH');
  30. $expected = array();
  31. $expected[] = 'Error! The command "/nowhere/tesseract" was not found.';
  32. $expected[] = '';
  33. $expected[] = 'Make sure you have Tesseract OCR installed on your system:';
  34. $expected[] = 'https://github.com/tesseract-ocr/tesseract';
  35. $expected[] = '';
  36. $expected[] = "The current \$PATH is $currentPath";
  37. $expected = join(PHP_EOL, $expected);
  38. try {
  39. (new TesseractOCR('./tests/EndToEnd/images/text.png'))
  40. ->executable('/nowhere/tesseract')
  41. ->run();
  42. throw new \Exception('TesseractNotFoundException not thrown');
  43. } catch (TesseractNotFoundException $e) {
  44. $this->assertEquals($expected, $e->getMessage());
  45. }
  46. }
  47. public function testExecutableNotFoundWithVersionCheckingOptions()
  48. {
  49. # Issue #210, reported by @samwilson
  50. $currentPath = getenv('PATH');
  51. $expected = array();
  52. $expected[] = 'Error! The command "/nowhere/tesseract" was not found.';
  53. $expected[] = '';
  54. $expected[] = 'Make sure you have Tesseract OCR installed on your system:';
  55. $expected[] = 'https://github.com/tesseract-ocr/tesseract';
  56. $expected[] = '';
  57. $expected[] = "The current \$PATH is $currentPath";
  58. $expected = join(PHP_EOL, $expected);
  59. try {
  60. (new TesseractOCR())
  61. ->executable('/nowhere/tesseract')
  62. ->imageData('irrelevant', 1234)
  63. ->withoutTempFiles()
  64. ->run();
  65. throw new \Exception('TesseractNotFoundException not thrown');
  66. } catch (TesseractNotFoundException $e) {
  67. $this->assertEquals($expected, $e->getMessage());
  68. }
  69. }
  70. public function testUnsuccessfulCommand()
  71. {
  72. $expected = array();
  73. $expected[] = 'Error! The command did not produce any output.';
  74. $expected[] = '';
  75. $expected[] = 'Generated command:';
  76. $expected[] = '"tesseract" "./tests/EndToEnd/images/not-an-image.txt" "tmpfile" quiet';
  77. $expected[] = '';
  78. $expected[] = 'Returned message:';
  79. switch (true) {
  80. case ($this->isVersion('3.02')):
  81. $expected[] = 'Error in pixReadStream: Unknown format: no pix returned';
  82. $expected[] = 'Error in pixRead: pix not read';
  83. $expected[] = 'Unsupported image type.';
  84. break;
  85. case ($this->isVersion('3.03')):
  86. $expected[] = 'Tesseract Open Source OCR Engine v3.03 with Leptonica';
  87. $expected[] = 'Error in pixReadStream: Unknown format: no pix returned';
  88. $expected[] = 'Error in pixRead: pix not read';
  89. $expected[] = 'Error in pixGetInputFormat: pix not defined';
  90. $expected[] = 'Error in fopenReadStream: file not found';
  91. $expected[] = 'Error in pixRead: image file not found: not an image';
  92. $expected[] = 'Error during processing.';
  93. break;
  94. case ($this->isVersion('3.04.01')):
  95. $expected[] = 'Tesseract Open Source OCR Engine v3.04.01 with Leptonica';
  96. $expected[] = 'Error in fopenReadStream: file not found';
  97. $expected[] = 'Error in pixRead: image file not found: not an image';
  98. $expected[] = 'Error during processing.';
  99. break;
  100. case ($this->isVersion('3.05.00dev')):
  101. $expected[] = 'Tesseract Open Source OCR Engine v3.05.00dev with Leptonica';
  102. $expected[] = 'read_params_file: Can\'t open quiet';
  103. $expected[] = 'Image file not an image cannot be read!';
  104. $expected[] = 'Error during processing.';
  105. break;
  106. default:
  107. $expected[] = 'Error in fopenReadStream: file not found';
  108. $expected[] = 'Error in pixRead: image file not found: not an image';
  109. $expected[] = 'Error during processing.';
  110. }
  111. $expected = join(PHP_EOL, $expected);
  112. $cmd = new TestableCommand();
  113. try {
  114. (new TesseractOCR('./tests/EndToEnd/images/not-an-image.txt', $cmd))
  115. ->quiet()
  116. ->run();
  117. throw new \Exception('UnsuccessfulCommandException not thrown');
  118. } catch (UnsuccessfulCommandException $e) {
  119. $this->assertEquals($expected, $e->getMessage());
  120. }
  121. }
  122. protected function isVersion($version)
  123. {
  124. exec('tesseract --version 2>&1', $output);
  125. return strpos($output[0], "tesseract $version") !== false;
  126. }
  127. }