0

I'm attempting to write a symfony 2 command which can basically read in a number of routes (either through a yml file or through arguments) and can go and get the response for each of these pages so I can report back whether they came back as 200/404/502 etc.. The routes are relative so would be routes such as '/' and '/news'.

Can't seem to work out how to send these requests through to get a real response, I can use Request::create() to create a request, but this doesn't seem to work how I want it to.

Do I have to go through the Kernel even though its a command? Any help would be appreciated.

What I have so far:

$request = Request::create('/news', 'GET');
$response = new Response();
$response->prepare($request);
$res = $response->send();
var_dump($res->getContent());

This comes back with an empty string all the time.

Also tried the following:

$client = new Client(
    new HttpKernel(new EventDispatcher(), new ControllerResolver())
);
$client->request('GET', '/news');
var_dump($client->getResponse());

Which comes back with a route is wrongly configured error

Thanks Kevin

Kevin
  • 383
  • 5
  • 16
  • Why don't you use [tests](http://symfony.com/doc/current/best_practices/tests.html) in order to check the status codes? You can use [`$client->getResponse()->getStatusCode()`](http://symfony.com/doc/current/book/testing.html#index-3). – A.L Jun 16 '16 at 10:12
  • Is your goal to write a Symfony command or to check some URLs? Using some other tools may be simpler if you want to check *real* URLs: http://stackoverflow.com/questions/6136022/script-to-get-the-http-status-code-of-a-list-of-urls/9443278#9443278 – A.L Jun 16 '16 at 13:09

2 Answers2

1

The best way to do it is using an http client to which transfer the responsibility to create a request object, perform the request, and return a response.

One of the most used is the guzzle http client (http://docs.guzzlephp.org/en/latest/)

So in your command you can get the router from the container, generate the url for that, and perform the request with guzzle client.

EDIT after comments:

To reach that goal to perform request without actually have a server running, you can use \Symfony\Bundle\FrameworkBundle\Client object that uses the kernel to perform request-response ( you have of course to inject the current kernel of your command). Pretty much as the WebTestCase class does.

http://api.symfony.com/2.8/Symfony/Bundle/FrameworkBundle/Client.html

Hope this will help ! (I will update with some code later)

  • I need to use relative paths, so /news rather than www.website.com/news can guzzle handle this? Can you provide a simple example? – Kevin Jun 16 '16 at 08:45
  • Basically im testing the code base to ensure its all set up ok before I release it to the public, so I want to simulate a client request to the code and ensure I get a 200 response back, if a 404 came back then I know there is a problem so to pause the release. I can't use the full URL as that will be the URL of the main site which is up and running, I need to use relative so that it uses the code base that i'm currently in. Hope that makes sense. – Kevin Jun 16 '16 at 08:51
1

According to your comment:

Basically im testing the code base to ensure its all set up ok before I release it to the public, so I want to simulate a client request to the code and ensure I get a 200 response back, if a 404 came back then I know there is a problem so to pause the release.

This is easy to do with a test:

<?php
// tests/AppBundle/Controller/PostControllerTest.php
namespace Tests\AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class PostControllerTest extends WebTestCase
{
    public function testNews()
    {
        $client = static::createClient();

        $crawler = $client->request('GET', '/news');

        // Assert a specific 200 status code
        $this->assertEquals(
            200, // or Symfony\Component\HttpFoundation\Response::HTTP_OK
            $client->getResponse()->getStatusCode()
        );
    }
}

You also need to install PHPUnit:

composer require --dev "phpunit/phpunit=5.4.*"

Then you can launch the tests:

php ./vendor/bin/phpunit -c app/phpunit.xml.dist

You'll have a result like this:

PHPUnit 5.4.6 by Sebastian Bergmann and contributors.

.....................                                             21 / 21 (100%)

Time: 5.29 seconds, Memory: 58.00MB

OK (21 tests, 149 assertions)
Community
  • 1
  • 1
A.L
  • 10,259
  • 10
  • 67
  • 98
  • except it needs to be available via a command as thats how we need to be able to run it. – Kevin Jun 16 '16 at 12:54
  • @Kevin Tests are launched by command line through PHPUnit. I have edited my question in order to explain it. – A.L Jun 16 '16 at 13:11