Markdown

[symfony] get a jsonResponse and do json_decode in PHPUnit (1)

Controller
---
2373         $content = $request->getContent();~
2374 ~
2375         $data = json_decode($content, true);~
|376 ~
|377         $id = $data['id'];~
|378         $u = $data['userName'];~
|379         $m = $data['msg'];~
|380 ~
|381         $message->setUserName($u);~
|382 ~
|383         $message->setMsg($m);~
|384 ~
|385         $message->setupdatedAt(new \DateTime('now', new \DateTimeZone('Asia/Taipei')));~
|386 ~
-387         $response = new Response();~
2388 ~
2389         $encoders = array(new XmlEncoder(), new JsonEncoder());~
2390 ~
2391         $normalizers = array(new ObjectNormalizer());~
|392 ~
|393         $serializer = new Serializer($normalizers, $encoders);~
|394 ~
|395         $json = $serializer->serialize($message, 'json');~
|396 ~
|397         $dejson = json_decode($json, true);~
|398 ~
|399         $json = $serializer->serialize($dejson, 'json');~
|400 ~
-401         $response->setContent($json);
---

PHPUnit
   1 <?php~                                                                                                                         
   2 ~
   3 namespace Tests\AppBundle\Controller;~
   4 namespace AppBundle\Entity;~
   5 ~
   6 use Doctrine\ORM\EntityRepository;~
   7 use AppBundle\Entity\Message;~
   8 use Doctrine\ORM\Mapping as ORM;~
   9 ~
  10 use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;~
  11 ~
  12 class DefaultControllerTest extends WebTestCase~
  13 {~
- 14     public function testIndex()~
| 15     {~
- 16         $client = static::createClient();~
2 17 ~
2 18         $crawler = $client->request('GET', '/api/list');~
2 19         $this->assertEquals(200, $client->getResponse()->getStatusCode());~
2 20 ~
2 21         $content = $client->getResponse()->getcontent();~
2 22 ~
2 23         $data = json_decode($content,true);~
2 24         dump($data,count($data));~
2 25 ~
2 26         $this->assertCount(5, $data);~
2 27         $this->assertNotCount(0, $data);~
2 28 ~
| 39     }~

---

run test & dump

[eric_tu@localhost eric_tu]$ vendor/bin/phpunit PHPUnit 5.7.20 by Sebastian Bergmann and contributors. ..... 5 / 5 (100%) Time: 388 ms, Memory: 32.50MB OK (5 tests, 10 assertions) DefaultControllerTest.php on line 24: array:5 [ 0 => array:5 [ "updatedAt" => "2017-06-02 07:43:31" "publishedAt" => "2017-06-02 04:33:10" "id" => 96 "userName" => "test" "msg" => "test" ] 1 => array:5 [ "updatedAt" => "2017-06-02 05:34:04" "publishedAt" => "2017-06-02 05:34:04" "id" => 99 "userName" => "test" "msg" => "test" ] 2 => array:5 [ "updatedAt" => "2017-06-02 04:39:38" "publishedAt" => "2017-06-02 04:39:38" "id" => 97 "userName" => "test" "msg" => "test" ] 3 => array:5 [ "updatedAt" => "2017-06-02 04:30:32" "publishedAt" => "2017-06-02 04:30:32" "id" => 95 "userName" => "test" "msg" => "test" ] 4 => array:5 [ "updatedAt" => "2017-06-02 04:30:31" "publishedAt" => "2017-06-02 04:30:31" "id" => 94 "userName" => "test" "msg" => "test" ] ] DefaultControllerTest.php on line 24: 5

留言