TestHttpKernel example

use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpKernel\HttpKernelBrowser;
use Symfony\Component\HttpKernel\Tests\Fixtures\TestClient;

/** * @group time-sensitive */
class HttpKernelBrowserTest extends TestCase
{
    public function testDoRequest()
    {
        $client = new HttpKernelBrowser(new TestHttpKernel());

        $client->request('GET', '/');
        $this->assertEquals('Request: /', $client->getResponse()->getContent(), '->doRequest() uses the request handler to make the request');
        $this->assertInstanceOf(\Symfony\Component\BrowserKit\Request::class$client->getInternalRequest());
        $this->assertInstanceOf(Request::class$client->getRequest());
        $this->assertInstanceOf(\Symfony\Component\BrowserKit\Response::class$client->getInternalResponse());
        $this->assertInstanceOf(Response::class$client->getResponse());

        $client->request('GET', 'http://www.example.com/');
        $this->assertEquals('Request: /', $client->getResponse()->getContent(), '->doRequest() uses the request handler to make the request');
        $this->assertEquals('www.example.com', $client->getRequest()->getHost(), '->doRequest() uses the request handler to make the request');

        
use Symfony\Component\HttpKernel\Tests\Fixtures\Attribute\Baz;
use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\AttributeController;
use Symfony\Component\HttpKernel\Tests\TestHttpKernel;

class ControllerEventTest extends TestCase
{
    /** * @dataProvider provideGetAttributes */
    public function testGetAttributes(callable $controller)
    {
        $event = new ControllerEvent(new TestHttpKernel()$controllernew Request(), HttpKernelInterface::MAIN_REQUEST);

        $expected = [
            Bar::class => [
                new Bar('class'),
                new Bar('method'),
            ],
            Baz::class => [
                new Baz(),
            ],
        ];

        
namespace Symfony\Component\HttpKernel\Tests\Event;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Tests\TestHttpKernel;

class ExceptionEventTest extends TestCase
{
    public function testAllowSuccessfulResponseIsFalseByDefault()
    {
        $event = new ExceptionEvent(new TestHttpKernel()new Request(), 1, new \Exception());

        $this->assertFalse($event->isAllowingCustomResponseCode());
    }
}
$values = [];
        foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(sys_get_temp_dir().'/http_cache/md', \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
            $values[] = file_get_contents($file);
        }

        return $values;
    }

    // A basic response with 200 status code and a tiny body.     public function setNextResponse($statusCode = 200, array $headers = []$body = 'Hello World', \Closure $customizer = null, EventDispatcher $eventDispatcher = null)
    {
        $this->kernel = new TestHttpKernel($body$statusCode$headers$customizer$eventDispatcher);
    }

    public function setNextResponses($responses)
    {
        $this->kernel = new TestMultipleHttpKernel($responses);
    }

    public function catchExceptions($catch = true)
    {
        $this->catch = $catch;
    }

    
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Tests\Fixtures\Attribute\Bar;
use Symfony\Component\HttpKernel\Tests\Fixtures\Attribute\Baz;
use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\AttributeController;
use Symfony\Component\HttpKernel\Tests\TestHttpKernel;

class ControllerArgumentsEventTest extends TestCase
{
    public function testControllerArgumentsEvent()
    {
        $event = new ControllerArgumentsEvent(new TestHttpKernel()function D) {}['test']new Request(), HttpKernelInterface::MAIN_REQUEST);
        $this->assertEquals($event->getArguments()['test']);
    }

    public function testSetAttributes()
    {
        $controller = function D) {};
        $event = new ControllerArgumentsEvent(new TestHttpKernel()$controller['test']new Request(), HttpKernelInterface::MAIN_REQUEST);
        $event->setController($controller[]);

        $this->assertSame([]$event->getAttributes());
    }

    
Home | Imprint | This part of the site doesn't use cookies.