<commit_before><?php declare(strict_types=1);

namespace Symplify\PackageBuilder\Tests\Console;

use Exception;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\StreamOutput;
use Symplify\PackageBuilder\Console\ExceptionRenderer;

final class ExceptionRendererTest extends TestCase
{
    /**
     * @var resource
     */
    private $tempFile;

    /**
     * @see https://phpunit.readthedocs.io/en/latest/assertions.html#assertstringmatchesformat
     */
    public function test(): void
    {
        $exceptionRenderer = new ExceptionRenderer($this->createStreamOutput());
        $exception = new Exception('Random message');
        $exceptionRenderer->render($exception);

        $this->assertStringMatchesFormat(
            '%wIn ExceptionRendererTest.php line %d:%wRandom message%w',
            $this->getTestErrorOutput()
        );
    }

    public function testNestedException(): void
    {
        $exceptionRenderer = new ExceptionRenderer($this->createStreamOutput());
        $exception = new Exception('Random message', 404, new Exception('Parent message'));
        $exceptionRenderer->render($exception);

        $this->assertStringMatchesFormat(
            '%wIn ExceptionRendererTest.php line %d:%wRandom message%w' .
            'In ExceptionRendererTest.php line %d:%wParent message%w',
            $this->getTestErrorOutput()
        );
    }

    public function testExceptionWithVerbosity(): void
    {
        $arrayInput = new ArrayInput(['v' => true]);
        $exceptionRenderer = new ExceptionRenderer($this->createStreamOutput(), $arrayInput);

        $exception = new Exception('Random message');
        $exceptionRenderer->render($exception);

        $this->assertStringMatchesFormat(
            '%wIn ExceptionRendererTest.php line %d:%w[Exception]%wRandom message%wException trace:%a',
            $this->getTestErrorOutput()
        );
    }

    /**
     * Inspired by http://alexandre-salome.fr/blog/Test-your-commands-in-Symfony2
     */
    private function getTestErrorOutput(): string
    {
        fseek($this->tempFile, 0);
        $output = fread($this->tempFile, 4096);
        fclose($this->tempFile);

        return $output;
    }

    private function createStreamOutput(): StreamOutput
    {
        $this->tempFile = tmpfile();
        $streamOutput = new StreamOutput($this->tempFile);

        return $streamOutput;
    }
}<commit_msg>shorten code<commit_after><?php declare(strict_types=1);

namespace Symplify\PackageBuilder\Tests\Console;

use Exception;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\StreamOutput;
use Symplify\PackageBuilder\Console\ExceptionRenderer;

final class ExceptionRendererTest extends TestCase
{
    /**
     * @var resource
     */
    private $tempFile;

    /**
     * @see https://phpunit.readthedocs.io/en/latest/assertions.html#assertstringmatchesformat
     */
    public function test(): void
    {
        $exceptionRenderer = new ExceptionRenderer($this->createStreamOutput());
        $exceptionRenderer->render(new Exception('Random message'));

        $this->assertStringMatchesFormat(
            '%wIn ExceptionRendererTest.php line %d:%wRandom message%w',
            $this->getTestErrorOutput()
        );
    }

    public function testNestedException(): void
    {
        $exceptionRenderer = new ExceptionRenderer($this->createStreamOutput());
        $exceptionRenderer->render(new Exception('Random message', 404, new Exception('Parent message')));

        $this->assertStringMatchesFormat(
            '%wIn ExceptionRendererTest.php line %d:%wRandom message%w' .
            'In ExceptionRendererTest.php line %d:%wParent message%w',
            $this->getTestErrorOutput()
        );
    }

    public function testExceptionWithVerbosity(): void
    {
        $arrayInput = new ArrayInput(['v' => true]);
        $exceptionRenderer = new ExceptionRenderer($this->createStreamOutput(), $arrayInput);

        $exceptionRenderer->render(new Exception('Random message'));

        $this->assertStringMatchesFormat(
            '%wIn ExceptionRendererTest.php line %d:%w[Exception]%wRandom message%wException trace:%a',
            $this->getTestErrorOutput()
        );
    }

    /**
     * Inspired by http://alexandre-salome.fr/blog/Test-your-commands-in-Symfony2
     */
    private function getTestErrorOutput(): string
    {
        fseek($this->tempFile, 0);
        $output = fread($this->tempFile, 4096);
        fclose($this->tempFile);

        return $output;
    }

    private function createStreamOutput(): StreamOutput
    {
        $this->tempFile = tmpfile();
        $streamOutput = new StreamOutput($this->tempFile);

        return $streamOutput;
    }
}
{'fraction_non_alphanumeric': 0.12412081092263136, 'fraction_numerical': 0.004137360364087712, 'mean_word_length': 3.080168776371308, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 39, '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': '1462345', 'n_tokens_mistral': 1317, 'n_tokens_neox': 1279, 'n_words': 241}