<commit_before><?php
/**
 * Slim Framework (https://slimframework.com)
 *
 * @link      https://github.com/slimphp/Slim
 * @copyright Copyright (c) 2011-2017 <NAME>
 * @license   https://github.com/slimphp/Slim/blob/4.x/LICENSE.md (MIT License)
 */
namespace Slim\Tests\Error;

use PHPUnit\Framework\TestCase;
use Slim\Error\Renderers\HtmlErrorRenderer;
use Slim\Error\Renderers\JsonErrorRenderer;
use Slim\Error\Renderers\XmlErrorRenderer;
use Exception;
use ReflectionClass;
use RuntimeException;

class AbstractErrorRendererTest extends TestCase
{
    public function testHTMLErrorRendererDisplaysErrorDetails()
    {
        $exception = new RuntimeException('Oops..');
        $renderer = new HtmlErrorRenderer();
        $output = $renderer->render($exception, true);

        $this->assertRegExp('/.*The application could not run because of the following error:.*/', $output);
    }

    public function testHTMLErrorRendererRenderFragmentMethod()
    {
        $exception = new Exception('Oops..', 500);
        $renderer = new HtmlErrorRenderer();
        $reflectionRenderer = new ReflectionClass(HtmlErrorRenderer::class);

        $method = $reflectionRenderer->getMethod('renderExceptionFragment');
        $method->setAccessible(true);
        $output = $method->invoke($renderer, $exception);

        $this->assertRegExp('/.*Type:*/', $output);
        $this->assertRegExp('/.*Code:*/', $output);
        $this->assertRegExp('/.*Message*/', $output);
        $this->assertRegExp('/.*File*/', $output);
        $this->assertRegExp('/.*Line*/', $output);
    }

    public function testJSONErrorRendererDisplaysErrorDetails()
    {
        $exception = new Exception('Oops..');
        $renderer = new JsonErrorRenderer();
        $reflectionRenderer = new ReflectionClass(JsonErrorRenderer::class);

        $method = $reflectionRenderer->getMethod('formatExceptionFragment');
        $method->setAccessible(true);

        $fragment = $method->invoke($renderer, $exception);
        $output = json_encode(json_decode($renderer->render($exception, true)));
        $expectedString = json_encode(['message' => 'Oops..', 'exception' => [$fragment]]);

        $this->assertEquals($output, $expectedString);
    }

    public function testJSONErrorRendererDoesNotDisplayErrorDetails()
    {
        $exception = new Exception('Oops..');

        $renderer = new JsonErrorRenderer();
        $output = json_encode(json_decode($renderer->render($exception, false)));

        $this->assertEquals($output, json_encode(['message' => 'Oops..']));
    }

    public function testJSONErrorRendererDisplaysPreviousError()
    {
        $previousException = new Exception('Oh no!');
        $exception = new Exception('Oops..', 0, $previousException);

        $renderer = new JsonErrorRenderer();
        $reflectionRenderer = new ReflectionClass(JsonErrorRenderer::class);
        $method = $reflectionRenderer->getMethod('formatExceptionFragment');
        $method->setAccessible(true);

        $output = json_encode(json_decode($renderer->render($exception, true)));

        $fragments = [
            $method->invoke($renderer, $exception),
            $method->invoke($renderer, $previousException),
        ];

        $expectedString = json_encode(['message' => 'Oops..', 'exception' => $fragments]);

        $this->assertEquals($output, $expectedString);
    }

    public function testXMLErrorRendererDisplaysErrorDetails()
    {
        $previousException = new RuntimeException('Oops..');
        $exception = new Exception('Ooops...', 0, $previousException);

        $renderer = new XmlErrorRenderer();
        $output = simplexml_load_string($renderer->render($exception, true));

        $this->assertEquals($output->message[0], 'Ooops...');
        $this->assertEquals((string)$output->exception[0]->type, 'Exception');
        $this->assertEquals((string)$output->exception[1]->type, 'RuntimeException');
    }
}<commit_msg>Add test coverage for PlainTextErrorRenderer<commit_after><?php
/**
 * Slim Framework (https://slimframework.com)
 *
 * @link      https://github.com/slimphp/Slim
 * @copyright Copyright (c) 2011-2017 <NAME>
 * @license   https://github.com/slimphp/Slim/blob/4.x/LICENSE.md (MIT License)
 */
namespace Slim\Tests\Error;

use PHPUnit\Framework\TestCase;
use Slim\Error\Renderers\HtmlErrorRenderer;
use Slim\Error\Renderers\JsonErrorRenderer;
use Slim\Error\Renderers\PlainTextErrorRenderer;
use Slim\Error\Renderers\XmlErrorRenderer;
use Exception;
use ReflectionClass;
use RuntimeException;

class AbstractErrorRendererTest extends TestCase
{
    public function testHTMLErrorRendererDisplaysErrorDetails()
    {
        $exception = new RuntimeException('Oops..');
        $renderer = new HtmlErrorRenderer();
        $output = $renderer->render($exception, true);

        $this->assertRegExp('/.*The application could not run because of the following error:.*/', $output);
    }

    public function testHTMLErrorRendererRenderFragmentMethod()
    {
        $exception = new Exception('Oops..', 500);
        $renderer = new HtmlErrorRenderer();
        $reflectionRenderer = new ReflectionClass(HtmlErrorRenderer::class);

        $method = $reflectionRenderer->getMethod('renderExceptionFragment');
        $method->setAccessible(true);
        $output = $method->invoke($renderer, $exception);

        $this->assertRegExp('/.*Type:*/', $output);
        $this->assertRegExp('/.*Code:*/', $output);
        $this->assertRegExp('/.*Message*/', $output);
        $this->assertRegExp('/.*File*/', $output);
        $this->assertRegExp('/.*Line*/', $output);
    }

    public function testJSONErrorRendererDisplaysErrorDetails()
    {
        $exception = new Exception('Oops..');
        $renderer = new JsonErrorRenderer();
        $reflectionRenderer = new ReflectionClass(JsonErrorRenderer::class);

        $method = $reflectionRenderer->getMethod('formatExceptionFragment');
        $method->setAccessible(true);

        $fragment = $method->invoke($renderer, $exception);
        $output = json_encode(json_decode($renderer->render($exception, true)));
        $expectedString = json_encode(['message' => 'Oops..', 'exception' => [$fragment]]);

        $this->assertEquals($output, $expectedString);
    }

    public function testJSONErrorRendererDoesNotDisplayErrorDetails()
    {
        $exception = new Exception('Oops..');

        $renderer = new JsonErrorRenderer();
        $output = json_encode(json_decode($renderer->render($exception, false)));

        $this->assertEquals($output, json_encode(['message' => 'Oops..']));
    }

    public function testJSONErrorRendererDisplaysPreviousError()
    {
        $previousException = new Exception('Oh no!');
        $exception = new Exception('Oops..', 0, $previousException);

        $renderer = new JsonErrorRenderer();
        $reflectionRenderer = new ReflectionClass(JsonErrorRenderer::class);
        $method = $reflectionRenderer->getMethod('formatExceptionFragment');
        $method->setAccessible(true);

        $output = json_encode(json_decode($renderer->render($exception, true)));

        $fragments = [
            $method->invoke($renderer, $exception),
            $method->invoke($renderer, $previousException),
        ];

        $expectedString = json_encode(['message' => 'Oops..', 'exception' => $fragments]);

        $this->assertEquals($output, $expectedString);
    }

    public function testXMLErrorRendererDisplaysErrorDetails()
    {
        $previousException = new RuntimeException('Oops..');
        $exception = new Exception('Ooops...', 0, $previousException);

        $renderer = new XmlErrorRenderer();
        $output = simplexml_load_string($renderer->render($exception, true));

        $this->assertEquals($output->message[0], 'Ooops...');
        $this->assertEquals((string)$output->exception[0]->type, 'Exception');
        $this->assertEquals((string)$output->exception[1]->type, 'RuntimeException');
    }

    public function testPlainTextErrorRendererFormatFragmentMethod()
    {
        $exception = new Exception('Oops..', 500);
        $renderer = new PlainTextErrorRenderer();
        $reflectionRenderer = new ReflectionClass(PlainTextErrorRenderer::class);

        $method = $reflectionRenderer->getMethod('formatExceptionFragment');
        $method->setAccessible(true);
        $output = $method->invoke($renderer, $exception);

        $this->assertRegExp('/.*Type:*/', $output);
        $this->assertRegExp('/.*Code:*/', $output);
        $this->assertRegExp('/.*Message*/', $output);
        $this->assertRegExp('/.*File*/', $output);
        $this->assertRegExp('/.*Line*/', $output);
    }

    public function testPlainTextErrorRendererDisplaysErrorDetails()
    {
        $previousException = new RuntimeException('Oops..');
        $exception = new Exception('Ooops...', 0, $previousException);

        $renderer = new PlainTextErrorRenderer();
        $output = $renderer->render($exception, true);

        $this->assertRegExp('/Ooops.../', $output);
    }
}
{'fraction_non_alphanumeric': 0.1560393413636866, 'fraction_numerical': 0.004199359045198365, 'mean_word_length': 3.5845997973657546, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 89, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '2127763', 'n_tokens_mistral': 2497, 'n_tokens_neox': 2444, 'n_words': 432}