[PHPUnit] API 與 TEST 的規劃
之前做的 Create API 測試都是呼叫API完,跑去首頁撈結果比對。
所以之前的Create API設計就只有單向,不return response也沒關西。
但這樣等於是測試一個API 卻用到另一隻顯示首頁的 list API ,也就是跨了兩支API。
這樣顯然不符合針對Create API 做測試的目的,
所以現在要在後端API 收完request 且寫入完成的時候,
發一個 response 將寫入的資料結果用json_encode 丟出來。
然後再Test 這邊再去吃對API 做 post request 之後 ,API 給的 response。
並做 json_decode處理 ,再去比對 test 先前發出的 post request 內容是否一致。
---
先看TEST的部分
/** * @group aa */ public function testCreateAction() { $client = static::createClient(); $postData = [ 'userName' => 'testPHPUnit'. mt_rand(), 'msg' => 'test' ]; $crawler = $client->request( 'POST', '/api/add', array(), array(), array('CONTENT_TYPE' => 'application/json'), json_encode($postData) ); $this->assertEquals(200, $client->getResponse()->getStatusCode()); $content = $client->getResponse()->getcontent(); $responseCheck = json_decode($content, true); dump(gettype($responseCheck), $responseCheck["userName"]); $this->assertContains($postData['userName'], (string)$content); $this->assertEquals($postData['userName'], $responseCheck["userName"]); // $client->followRedirects(); /* $crawler = $client->request('GET', '/add'); // $postLink = $crawler->filter('body > ul > li > a')->link(); $content = $crawler->getResponse()->getContent(); */ }
---
再來看API的部分
/** * Add New Message * @Route("api/add", name="apiAdd") * @Method("POST") */ public function createActionAPI(Request $request) { $message = new Message(); $content = $request->getContent(); $data = json_decode($content, true); $u = $data['userName']; $m = $data['msg']; $publishAt = new \DateTime('now', new \DateTimeZone('Asia/Taipei')); $message = new Message(); $message->setUserName($u); $message->setMsg($m); $message->setPublishedAt($publishAt); $encoders = array(new XmlEncoder(), new JsonEncoder()); $normalizers = array(new ObjectNormalizer()); $serializer = new Serializer($normalizers, $encoders); $json = $serializer->serialize($message, 'json'); $response = new Response(); // $response->setContent("content-type: " . gettype($content) . " \$content: " .$content . " userName " . $u); $response->setContent($json); $response->headers->set('Content-Type', 'application/json'); $response->headers->set('Access-Control-Allow-Origin', '*'); $em = $this->getDoctrine()->getManager(); $em->persist($message); $em->flush(); $redirectArray = ['id' => $message->getId()]; //return new JasonResponse(); return $response; // return $this->redirectToRoute('list', $redirectArray); }
所以之前的Create API設計就只有單向,不return response也沒關西。
但這樣等於是測試一個API 卻用到另一隻顯示首頁的 list API ,也就是跨了兩支API。
這樣顯然不符合針對Create API 做測試的目的,
所以現在要在後端API 收完request 且寫入完成的時候,
發一個 response 將寫入的資料結果用json_encode 丟出來。
然後再Test 這邊再去吃對API 做 post request 之後 ,API 給的 response。
並做 json_decode處理 ,再去比對 test 先前發出的 post request 內容是否一致。
---
先看TEST的部分
/** * @group aa */ public function testCreateAction() { $client = static::createClient(); $postData = [ 'userName' => 'testPHPUnit'. mt_rand(), 'msg' => 'test' ]; $crawler = $client->request( 'POST', '/api/add', array(), array(), array('CONTENT_TYPE' => 'application/json'), json_encode($postData) ); $this->assertEquals(200, $client->getResponse()->getStatusCode()); $content = $client->getResponse()->getcontent(); $responseCheck = json_decode($content, true); dump(gettype($responseCheck), $responseCheck["userName"]); $this->assertContains($postData['userName'], (string)$content); $this->assertEquals($postData['userName'], $responseCheck["userName"]); // $client->followRedirects(); /* $crawler = $client->request('GET', '/add'); // $postLink = $crawler->filter('body > ul > li > a')->link(); $content = $crawler->getResponse()->getContent(); */ }
---
再來看API的部分
/** * Add New Message * @Route("api/add", name="apiAdd") * @Method("POST") */ public function createActionAPI(Request $request) { $message = new Message(); $content = $request->getContent(); $data = json_decode($content, true); $u = $data['userName']; $m = $data['msg']; $publishAt = new \DateTime('now', new \DateTimeZone('Asia/Taipei')); $message = new Message(); $message->setUserName($u); $message->setMsg($m); $message->setPublishedAt($publishAt); $encoders = array(new XmlEncoder(), new JsonEncoder()); $normalizers = array(new ObjectNormalizer()); $serializer = new Serializer($normalizers, $encoders); $json = $serializer->serialize($message, 'json'); $response = new Response(); // $response->setContent("content-type: " . gettype($content) . " \$content: " .$content . " userName " . $u); $response->setContent($json); $response->headers->set('Content-Type', 'application/json'); $response->headers->set('Access-Control-Allow-Origin', '*'); $em = $this->getDoctrine()->getManager(); $em->persist($message); $em->flush(); $redirectArray = ['id' => $message->getId()]; //return new JasonResponse(); return $response; // return $this->redirectToRoute('list', $redirectArray); }
留言
張貼留言