<commit_before><commit_msg>Added tests for the exception handler<commit_after><?php

/*
 * This file is part of Laravel Exceptions.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace GrahamCampbell\Tests\Exceptions;

use Exception;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\GoneHttpException;

/**
 * This is the exception handler test class.
 *
 * @author <NAME> <<EMAIL>>
 */
class ExceptionHandlerTest extends AbstractTestCase
{
    public function testBasicRender()
    {
        $handler = $this->app->make('GrahamCampbell\Exceptions\ExceptionHandler');
        $response = $handler->render($this->app->request, new Exception('Foo Bar.'));

        $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
        $this->assertSame(500, $response->getStatusCode());
        $this->assertTrue(str_contains($response->getContent(), 'Foo Bar.'));
        $this->assertSame('text/html', $response->headers->get('Content-Type'));
    }

    public function testNotFoundRender()
    {
        $handler = $this->app->make('GrahamCampbell\Exceptions\ExceptionHandler');
        $response = $handler->render($this->app->request, new NotFoundHttpException());

        $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
        $this->assertSame(404, $response->getStatusCode());
        $this->assertTrue(str_contains($response->getContent(), 'Not Found'));
        $this->assertSame('text/html', $response->headers->get('Content-Type'));
    }

    public function testJsonRender()
    {
        $this->app->request->headers->set('accept', 'application/json');

        $handler = $this->app->make('GrahamCampbell\Exceptions\ExceptionHandler');
        $response = $handler->render($this->app->request, new GoneHttpException());

        $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
        $this->assertSame(410, $response->getStatusCode());
        $this->assertSame('{"success":false,"code":410,"msg":"Houston, We Have A Problem"}', $response->getContent());
        $this->assertSame('application/json', $response->headers->get('Content-Type'));
    }

    public function testTextRender()
    {
        $this->app->request->headers->set('accept', 'not/acceptable');

        $handler = $this->app->make('GrahamCampbell\Exceptions\ExceptionHandler');
        $response = $handler->render($this->app->request, new NotFoundHttpException());

        $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
        $this->assertSame(500, $response->getStatusCode());
        $this->assertSame('An error has occurred and this resource cannot be displayed.', $response->getContent());
        $this->assertSame('text/plain', $response->headers->get('Content-Type'));
    }
}
{'fraction_non_alphanumeric': 0.16616212579885636, 'fraction_numerical': 0.005045408678102927, 'mean_word_length': 4.254416961130742, 'pattern_counts': {'":': 3, '<': 10, '<?xml version=': 0, '>': 69, 'https://': 0, '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': '7125678', 'n_tokens_mistral': 814, 'n_tokens_neox': 796, 'n_words': 146}