OptionTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php namespace thiagoalessio\TesseractOCR\Tests\Unit;
  2. use thiagoalessio\TesseractOCR\Tests\Common\TestCase;
  3. use thiagoalessio\TesseractOCR\Option;
  4. class OptionTest extends TestCase
  5. {
  6. public function testPsm()
  7. {
  8. $psm = Option::psm(8);
  9. $this->assertEquals('-psm 8', $psm('3.05.01'));
  10. $this->assertEquals('--psm 8', $psm('4.0.0-beta.1'));
  11. $this->assertEquals('--psm 8', $psm('v4.0.0-beta.4.20180912'));
  12. }
  13. public function testOem()
  14. {
  15. $oem = Option::oem(2);
  16. $this->assertEquals('--oem 2', $oem('3.05.01'));
  17. try {
  18. $oem('3.04.01');
  19. throw new \Exception('Expected Exception to be thrown');
  20. } catch (\Exception $e) {
  21. $expected = 'oem option is only available on Tesseract 3.05 or later.';
  22. $expected.= PHP_EOL."Your version of Tesseract is 3.04.01";
  23. $this->assertEquals($expected, $e->getMessage());
  24. }
  25. }
  26. public function testDpi()
  27. {
  28. $dpi = Option::dpi(300);
  29. $this->assertEquals('--dpi 300', $dpi());
  30. }
  31. public function testUserWords()
  32. {
  33. $userWords = Option::userWords('/path/to/words');
  34. $this->assertEquals('--user-words "/path/to/words"', $userWords('3.04'));
  35. $userWords = Option::userWords('c:\path\to\words');
  36. $this->assertEquals('--user-words "c:\\\\path\\\\to\\\\words"', $userWords('3.04'));
  37. try {
  38. $userWords('3.03');
  39. throw new \Exception('Expected Exception to be thrown');
  40. } catch (\Exception $e) {
  41. $expected = 'user-words option is only available on Tesseract 3.04 or later.';
  42. $expected.= PHP_EOL."Your version of Tesseract is 3.03";
  43. $this->assertEquals($expected, $e->getMessage());
  44. }
  45. }
  46. public function testUserPatterns()
  47. {
  48. $userPatterns = Option::userPatterns('/path/to/patterns');
  49. $this->assertEquals('--user-patterns "/path/to/patterns"', $userPatterns('3.04'));
  50. $userPatterns = Option::userPatterns('c:\path\to\patterns');
  51. $this->assertEquals('--user-patterns "c:\\\\path\\\\to\\\\patterns"', $userPatterns('3.04'));
  52. try {
  53. $userPatterns('3.03');
  54. throw new \Exception('Expected Exception to be thrown');
  55. } catch (\Exception $e) {
  56. $expected = 'user-patterns option is only available on Tesseract 3.04 or later.';
  57. $expected.= PHP_EOL."Your version of Tesseract is 3.03";
  58. $this->assertEquals($expected, $e->getMessage());
  59. }
  60. }
  61. public function testTessdataDir()
  62. {
  63. $tessdataDir = Option::tessdataDir('/path/to/tessdata');
  64. $this->assertEquals('--tessdata-dir "/path/to/tessdata"', $tessdataDir());
  65. $tessdataDir = Option::tessdataDir('c:\path\to\tessdata');
  66. $this->assertEquals('--tessdata-dir "c:\\\\path\\\\to\\\\tessdata"', $tessdataDir());
  67. }
  68. public function testLang()
  69. {
  70. $lang = Option::lang('eng');
  71. $this->assertEquals('-l eng', $lang());
  72. $lang = Option::lang('eng', 'deu', 'jpn');
  73. $this->assertEquals('-l eng+deu+jpn', $lang());
  74. }
  75. public function testConfig()
  76. {
  77. $config = Option::config('var', 'value');
  78. $this->assertEquals('-c "var=value"', $config());
  79. $config = Option::config('chars', '\'"!$@%&?`');
  80. $this->assertEquals('-c "chars=\'\\"!$@%&?`"', $config());
  81. $config = Option::config('fooBarBazChunkyBacon', 'value');
  82. $this->assertEquals('-c "foo_bar_baz_chunky_bacon=value"', $config());
  83. }
  84. public function testCheckMinVersion()
  85. {
  86. Option::checkMinVersion('3.05', '4.0.0.20190314', 'option');
  87. Option::checkMinVersion('3.05', 'v4.0.0.20190314', 'option');
  88. }
  89. }