Skip to content

Commit 6ebbf20

Browse files
committed
Merge branch 'master' of github.com:defrag/JsonMatcher
* 'master' of github.com:defrag/JsonMatcher: Update README.md
2 parents 4de8857 + d200358 commit 6ebbf20

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,60 @@ require: {
1414
}
1515
```
1616

17+
### Ways of testing
18+
Common way of testing api responses with Symfony2 WebTestCase
19+
20+
```php
21+
public function testGetToys()
22+
{
23+
$this->setUpFixtures();
24+
$this->setUpOath();
25+
$this->client->request('GET', '/api/toys');
26+
$response = $this->client->getResponse();
27+
$this->assertJsonResponse($response, 200);
28+
$content = $response->getContent();
29+
$decoded = json_decode($content, true);
30+
$this->assertTrue(isset($decoded[0]['id']));
31+
$this->assertTrue(isset($decoded[1]['id']));
32+
$this->assertTrue(isset($decoded[2]['id']));
33+
$this->assertEquals($decoded[0]['name'], 'Barbie'));
34+
$this->assertEquals($decoded[1]['name'], 'GI Joe'));
35+
$this->assertEquals($decoded[2]['name'], 'Optimus Prime'));
36+
}
37+
```
38+
With php-matcher, you can make it more readable to the person reading the test:
39+
```php
40+
public function testGetToys()
41+
{
42+
$this->setUpFixtures();
43+
$this->setUpOath();
44+
$this->client->request('GET', '/api/toys');
45+
$response = $this->client->getResponse();
46+
$this->assertJsonResponse($response, 200);
47+
$content = $response->getContent();
48+
$pattern = '[
49+
{
50+
"id": "@string@",
51+
"name": "Barbie",
52+
"_links: "@*@"
53+
},
54+
{
55+
"id": "@string@",
56+
"name": "GI Joe",
57+
"_links": "@*@"
58+
},
59+
{
60+
"id": "@string@",
61+
"name": "Optimus Prime",
62+
"_links": "@*@"
63+
}
64+
]
65+
';
66+
$this->assertEquals(match($content, $pattern));
67+
}
68+
```
69+
70+
1771
From now you should be able to use global function ``match($value, $pattern)``
1872

1973
##Example usage

0 commit comments

Comments
 (0)