How to pass JSON in POST method using PhpUnit Testing?

I am using symfony 3.0 with phpUnit 3.7.18 framework

Unit Test. abcControllerTest.php

namespace AbcBundle\Tests\Controller; use Symfony\Component\HttpFoundation\Request; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; use Symfony\Component\HttpFoundation\Response; class AbcControllerTest extends WebTestCase { public function testWithParams() { $Params = array("params" => array("page_no" => 5)); $expectedData = $this->listData($Params); print_r($expectedData); } private function listData($Params) { $client = static::createClient(); $server = array('HTTP_CONTENT_TYPE' => 'application/json', 'HTTP_ACCEPT' => 'application/json'); $crawler = $client->request('POST', $GLOBALS['host'] . '/abc/list', $Params,array(), $server); $response = $client->getResponse(); $this->assertSame('text/html; charset=UTF-8', $response->headers->get('Content-Type')); $expectedData = json_decode($response->getContent()); return $expectedData; } } 

Action: abc / list

abcController.php

 public function listAction(Request $request) { $Params = json_decode($request->getContent(), true); } 

The code works fine, but does not get the expected result. Since I'm trying to pass the json parameter from the phpunit file abcControllerTest.php to the abcController.php file. Anyone can offer me how I can achieve the same.

+5
source share
1 answer

I prefer to use GuzzleHttp for external requests:

 use GuzzleHttp\Client; $client = new Client(); $response = $client->post($url, [ GuzzleHttp\RequestOptions::JSON => ['title' => 'title', 'body' => 'body'] ]); 

Note: GuzzleHttp must be an installer version, for example. using composer.

But you can always use the client bundled with Symfony :

 public function testJsonPostPageAction() { $this->client = static::createClient(); $this->client->request( 'POST', '/api/v1/pages.json', array(), array(), array('CONTENT_TYPE' => 'application/json'), '[{"title":"title1","body":"body1"},{"title":"title2","body":"body2"}]' ); $this->assertJsonResponse($this->client->getResponse(), 201, false); } protected function assertJsonResponse($response, $statusCode = 200) { $this->assertEquals( $statusCode, $response->getStatusCode(), $response->getContent() ); $this->assertTrue( $response->headers->contains('Content-Type', 'application/json'), $response->headers ); } 
+6
source

Source: https://habr.com/ru/post/1262976/


All Articles