2

Im testing this function

/**
* @Route("/list", name="_clients")
* @Method("GET")
*/
public function ClientsAction()
{
   $em = $this->getDoctrine()->getManager();
   $data = $em->getRepository('InvoiceBundle:Clients')->findByUser($this->user());
   if($data){
     $Clients = array();
     foreach($data as $v){
        if($v->getCompanyId() != 0 ) {
           $companyId = $v->getCompanyId();
        } else {
           $companyId = '';
        }

        if ($v->getClient() == 'person'){
           $company = $v->getName().' '.$v->getLname();
        } else {
           $company = $v->getCompany();
        }

        $Clients[] = array(
          'id' => $v->getId(),
          'settings' => $company,
          'companyId' => $companyId,
          'client' => $v->getClient(),
          'mobile' => $v->getMobile(),
          'email' => $v->getEmail(),
          'clientName' => $v->getClientName(),
          'delivery' => $v->getDelivery(),
          'ContactPerson' => $v->getContactPerson()
       );
     }
   } else {
      $Clients = array('data' => 'empty');
   }

   $response = new JsonResponse($Clients);
   return $response;
}

The function it self runs correctly , but then i want to check if my 'Content-Type' is Json with this function

public function testClients()
{
   $client = static::createClient();

   $client->request('GET', '/clients/list');
   $this->assertTrue(
     $client->getResponse()->headers->contains(
       'Content-Type',
       'application/json'
     )
   );
}

with this i get a FALSE value. Then i try to do a test for Status code

$this->assertSame(200, $client->getResponse()->getStatusCode()); 

With this i get error 500 instead of 200 OK I understand that is why i get a FALSE value in my 'Content-Type' test but i cant get why. Im doing all this according to the Symfony documentation. May be i'm doing something wrong or is it just that you cant check the 'Content-Type'?

Any help would be appreciated!

1
  • Stupid question: is the URL correct? Commented Nov 18, 2014 at 17:34

1 Answer 1

2

JsonResponse does add the Content-Type header (application/json) so this should not be an issue.

I think the main issue is that you are missing $ on the client->request() line.

Edit :

Before the declaration of your class, did you add @Route("/clients") ?

Or, maybe the data returned by findByUser is not what you expected and calls to $v fail.

Sign up to request clarification or add additional context in comments.

1 Comment

sorry copied it wrong it has $ but still same problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.