Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Silex & phpunit JSON response

I am trying to write some tests for Silex using phpunit.

I have a class Symfony\Component\BrowserKit\Client that generates a Crawler object.

This object expects the results of the client to be xhtml however my api that I am trying to test returns JSON and the crawler does not allow this.

Is there a built in class in either Silex or phpunit that will work with JSON or will I have to roll my own?

Cheers

like image 548
Lee Avatar asked Sep 18 '12 09:09

Lee


People also ask

What is the Silex?

Definition of silex : silica or a siliceous material (such as powdered tripoli) especially for use as a filler in paints or wood.

What is Silex used for?

Silex is indicated in the treatment of uncomplicated skin and skin structure infections caused by Staphylococcus aureus (methicillin-susceptible isolates only) or Streptococcus pyogenes.

What type of rock is Silex?

Chert or silex is a cryptocrystalline or microcrystalline sedimentary rock composed of quartz.

Is Silex A flint?

Silex is a type of sedimentary soil that is rich with flint. In fact, silex is the English word for flint. Silex is closely associated with limestone soils -- they can exist in close proximity. But flint is generally a harder stone and can have an almost metallic look.


1 Answers

There is nothing special for dealing with json, but you can use the client without using the crawler. Simply call getResponse() on the client to get the response, like this:

$client = $this->createClient();
$client->request('GET', '/');
$response = $client->getResponse();

$data = json_decode($response->getContent(), true);
$this->assertSame(array('id' => 1, 'name' => 'igorw'), $data['users'][0]);

I suggest you move this logic into a helper method on the test case and use that.

like image 187
igorw Avatar answered Oct 11 '22 02:10

igorw