<commit_before><?php namespace thiagoalessio\TesseractOCR\Tests\EndToEnd;

use thiagoalessio\TesseractOCR\Tests\Common\TestCase;
use thiagoalessio\TesseractOCR\TesseractOCR;
use thiagoalessio\TesseractOCR\Tests\Unit\TestableCommand;

use thiagoalessio\TesseractOCR\ImageNotFoundException;
use thiagoalessio\TesseractOCR\TesseractNotFoundException;
use thiagoalessio\TesseractOCR\UnsuccessfulCommandException;

class FriendlyErrors extends TestCase
{
	public function testImageNotFound()
	{
		$currentDir = realpath(
			join(DIRECTORY_SEPARATOR, [__DIR__, '..', '..', 'src'])
		);

		$expected = array();
		$expected[] = 'Error! The image "/invalid/image.png" was not found.';
		$expected[] = '';
		$expected[] = "The current __DIR__ is $currentDir";
		$expected = join(PHP_EOL, $expected);

		try {
			(new TesseractOCR('/invalid/image.png'))->run();
			throw new \Exception('ImageNotFoundException not thrown');
		} catch (\Exception $e) {
			$this->assertEquals($expected, $e->getMessage());
		}
	}

	public function testExecutableNotFound()
	{
		$currentPath = getenv('PATH');

		$expected = array();
		$expected[] = 'Error! The command "/nowhere/tesseract" was not found.';
		$expected[] = '';
		$expected[] = 'Make sure you have Tesseract OCR installed on your system:';
		$expected[] = 'https://github.com/tesseract-ocr/tesseract';
		$expected[] = '';
		$expected[] = "The current \$PATH is $currentPath";
		$expected = join(PHP_EOL, $expected);

		try {
			(new TesseractOCR('./tests/EndToEnd/images/text.png'))
				->executable('/nowhere/tesseract')
				->run();
			throw new \Exception('TesseractNotFoundException not thrown');
		} catch (TesseractNotFoundException $e) {
			$this->assertEquals($expected, $e->getMessage());
		}
	}

	public function testUnsuccessfulCommand()
	{
		$expected = array();
		$expected[] = 'Error! The command did not produce any output.';
		$expected[] = '';
		$expected[] = 'Generated command:';
		$expected[] = '"tesseract" "./tests/EndToEnd/images/not-an-image.txt" "tmpfile" quiet';
		$expected[] = '';
		$expected[] = 'Returned message:';

		switch (true) {

			case ($this->isVersion('3.02')):
				$expected[] = 'read_params_file: Can\'t open quiet';
				$expected[] = 'Error in pixReadStream: Unknown format: no pix returned';
				$expected[] = 'Error in pixRead: pix not read';<commit_msg>Expected error message is different on fedora's build<commit_after><?php namespace thiagoalessio\TesseractOCR\Tests\EndToEnd;

use thiagoalessio\TesseractOCR\Tests\Common\TestCase;
use thiagoalessio\TesseractOCR\TesseractOCR;
use thiagoalessio\TesseractOCR\Tests\Unit\TestableCommand;

use thiagoalessio\TesseractOCR\ImageNotFoundException;
use thiagoalessio\TesseractOCR\TesseractNotFoundException;
use thiagoalessio\TesseractOCR\UnsuccessfulCommandException;

class FriendlyErrors extends TestCase
{
	public function testImageNotFound()
	{
		$currentDir = realpath(
			join(DIRECTORY_SEPARATOR, [__DIR__, '..', '..', 'src'])
		);

		$expected = array();
		$expected[] = 'Error! The image "/invalid/image.png" was not found.';
		$expected[] = '';
		$expected[] = "The current __DIR__ is $currentDir";
		$expected = join(PHP_EOL, $expected);

		try {
			(new TesseractOCR('/invalid/image.png'))->run();
			throw new \Exception('ImageNotFoundException not thrown');
		} catch (\Exception $e) {
			$this->assertEquals($expected, $e->getMessage());
		}
	}

	public function testExecutableNotFound()
	{
		$currentPath = getenv('PATH');

		$expected = array();
		$expected[] = 'Error! The command "/nowhere/tesseract" was not found.';
		$expected[] = '';
		$expected[] = 'Make sure you have Tesseract OCR installed on your system:';
		$expected[] = 'https://github.com/tesseract-ocr/tesseract';
		$expected[] = '';
		$expected[] = "The current \$PATH is $currentPath";
		$expected = join(PHP_EOL, $expected);

		try {
			(new TesseractOCR('./tests/EndToEnd/images/text.png'))
				->executable('/nowhere/tesseract')
				->run();
			throw new \Exception('TesseractNotFoundException not thrown');
		} catch (TesseractNotFoundException $e) {
			$this->assertEquals($expected, $e->getMessage());
		}
	}

	public function testUnsuccessfulCommand()
	{
		$expected = array();
		$expected[] = 'Error! The command did not produce any output.';
		$expected[] = '';
		$expected[] = 'Generated command:';
		$expected[] = '"tesseract" "./tests/EndToEnd/images/not-an-image.txt" "tmpfile" quiet';
		$expected[] = '';
		$expected[] = 'Returned message:';

		switch (true) {

			case ($this->isVersion('3.02')):
				$expected[] = 'Error in pixReadStream: Unknown format: no pix returned';
				$expected[] = 'Error in pixRead: pix not read';
				$expected[] = 'Unsupported image type.';
{'fraction_non_alphanumeric': 0.16283524904214558, 'fraction_numerical': 0.001277139208173691, 'mean_word_length': 4.7374847374847375, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6324599', 'n_tokens_mistral': 1683, 'n_tokens_neox': 1576, 'n_words': 337}