CommandTest.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php namespace thiagoalessio\TesseractOCR\Tests\Unit;
  2. use thiagoalessio\TesseractOCR\Tests\Common\TestCase;
  3. use thiagoalessio\TesseractOCR\Tests\Unit\TestableCommand;
  4. use thiagoalessio\TesseractOCR\Command;
  5. use thiagoalessio\TesseractOCR\Option;
  6. class CommandTest extends TestCase
  7. {
  8. public function setUp()
  9. {
  10. $this->customTempDir = __DIR__.DIRECTORY_SEPARATOR.'custom-temp-dir';
  11. mkdir($this->customTempDir);
  12. }
  13. public function tearDown()
  14. {
  15. $files = glob(join(DIRECTORY_SEPARATOR, array($this->customTempDir, '*')));
  16. array_map('unlink', $files);
  17. rmdir($this->customTempDir);
  18. }
  19. public function testSimplestCommand()
  20. {
  21. $cmd = new TestableCommand('image.png');
  22. $expected = '"tesseract" "image.png" "tmpfile"';
  23. $this->assertEquals("$expected", "$cmd");
  24. }
  25. public function testCommandWithOption()
  26. {
  27. $cmd = new TestableCommand('image.png');
  28. $cmd->options[] = Option::lang('eng');
  29. $expected = '"tesseract" "image.png" "tmpfile" -l eng';
  30. $this->assertEquals("$expected", "$cmd");
  31. }
  32. public function testWithConfigFile()
  33. {
  34. $cmd = new TestableCommand('image.png');
  35. $cmd->configFile = 'hocr';
  36. $expected = '"tesseract" "image.png" "tmpfile" hocr';
  37. $this->assertEquals("$expected", "$cmd");
  38. }
  39. public function testCustomTempDir()
  40. {
  41. if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') $this->skip();
  42. $cmd = new Command('image.png');
  43. $cmd->tempDir = $this->customTempDir;
  44. $expected = "\"tesseract\" \"image.png\" \"{$this->customTempDir}";
  45. $actual = substr("$cmd", 0, strlen($expected));
  46. $this->assertEquals("$expected", "$actual");
  47. }
  48. public function testCustomTempDirWindows()
  49. {
  50. if (strtoupper(substr(PHP_OS, 0, 3)) != 'WIN') $this->skip();
  51. $customTempDir = 'C:\Users\Foo Bar\Temp\Dir';
  52. if (!file_exists($customTempDir)) mkdir($customTempDir, null, true);
  53. $cmd = new Command('image.png');
  54. $cmd->tempDir = $customTempDir;
  55. $expected = '"tesseract" "image.png" "C:\Users\Foo Bar\Temp\Dir';
  56. $actual = substr("$cmd", 0, strlen($expected));
  57. $this->assertEquals("$expected", "$actual");
  58. }
  59. public function testCommandWithThreadLimit()
  60. {
  61. $cmd = new TestableCommand('image.png');
  62. $cmd->threadLimit = 2;
  63. $expected = 'OMP_THREAD_LIMIT=2 "tesseract" "image.png" "tmpfile"';
  64. $this->assertEquals("$expected", "$cmd");
  65. }
  66. public function testEscapeSpecialCharactersOnFilename()
  67. {
  68. $cmd = new TestableCommand('$@ ! ? "#\'_`foo.png');
  69. $expected = '"tesseract" "\$@ ! ? \\"#\'_\`foo.png" "tmpfile"';
  70. $this->assertEquals("$expected", "$cmd");
  71. }
  72. }