Sending a POST request with PHPUnit

17,741

Solution 1

$_POST is a variable filled by PHP and the symfony request is only created from this globals if called directly over http. The symfony crawler doesn't make a real request, it creates a request from the parameters supplied in your $client->request and executes it. You need to access this stuff via the Request object. Never use $_POST, $_GET, etc. directly.

use Symfony\Component\HttpFoundation\Request;

class HomeController extends CoralBaseController
{
    public function indexAction(Request $request)
    {

        var_dump($request->request->all());
        die;
        return $this->render('CoralWalletBundle:Home:index.html.twig');
    }

}

use $request->request->all() to get all POST parameters in an array. To get only a specific parameter you can use $request->request->get('my_param'). If you ever need to acces GET parameters you can use $request->query->get('my_param'), but better set query parameters already in the routing pattern.

Solution 2

I think you're trying to do this:

$client = static::createClient();
    $client->request($method, $url, [], [], [], json_encode($content));
    $this->assertEquals(
        200,
        $client->getResponse()
            ->getStatusCode()
    );

You're putting your data (content) in as the params array but you want to put it in as the raw body content which is a JSON encoded string.

Share:
17,741
Enrique Moreno Tent
Author by

Enrique Moreno Tent

I am a full-stack web developer. Born in Spain, living in Germany currently. Always interested in learning more and improving the quality of my work. Also, very glad to be a part of this community where we all help each other. Let's break some eggs!

Updated on June 09, 2022

Comments

  • Enrique Moreno Tent
    Enrique Moreno Tent almost 2 years

    I have a symfony website, and Im trying to do some unit testing. I have this kind of test where I try to submit something:

    <?php
    
    namespace Acme\AcmeBundle\Tests\Controller;
    
    use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
    
    class HomeControllerTest extends WebTestCase {
    
        public function testrandomeThings() {
    
            $client = static::createClient();
            $crawler = $client->request(
                'POST',
                '/',
                array(
                    "shopNumber"        => 0099,
                    "cardNumber"        => 231,
                    "cardPIN"           => "adasd"),
                array(),
                array());
            }
    

    but I dont think that the data Im sending is being received in the controler:

    class HomeController extends Controller
    {
        public function indexAction()
        {
    
            var_dump($_POST);
            die;
            return $this->render('AcmeBundle:Home:index.html.twig');
        }
    
    }
    

    the var_dump is actually returning me an empty array.

    What am I missing to send information through my POST request?