If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.
With a Subscription, click any sentence in the script to jump to that part of the video!
Login SubscribeWe might also want to test that we get the correct fields in the response for each item. Can we do that with JMESPath? Sure! The assertJsonMatches()
method is really handy. And actually, if you hold command or control and click into it, when we call assertJsonMatches()
, behind the scenes, it calls $this->json()
. This creates a Json
object... which has even more useful methods. The Browser
instance itself gives us access to assertJsonMatches()
. But if we want to use any of its other methods, we need to do a bit more work.
The first way to use the Json
object is via Browser's use()
method. Pass this a callback with a Json $json
argument:
... lines 1 - 6 | |
use Zenstruck\Browser\Json; | |
... lines 8 - 10 | |
class DragonTreasureResourceTest extends KernelTestCase | |
{ | |
... lines 13 - 15 | |
public function testGetCollectionOfTreasures(): void | |
{ | |
... lines 18 - 19 | |
$this->browser() | |
... lines 21 - 24 | |
->use(function(Json $json) { | |
... line 26 | |
}) | |
; | |
} | |
} |
This is a magic feature of browser: it reads the type-hint of the argument, and knows to pass us the Json
object. You could also type-hint a CookieJar
object, Crawler
or a few other things.
The point is: because we type-hinted the argument with Json
, it will grab the Json
object for the last response and pass it to us. Let's use it to do some experimenting. We want to check what the keys are for the first item inside of hydra:member
. To help figure the expression we need, let's use a method called search()
. This allows us to use a JMESPath
expression and get back the result. Do double quotes then hydra:member
to see what it returns. And... remove the other dump:
... lines 1 - 10 | |
class DragonTreasureResourceTest extends KernelTestCase | |
{ | |
... lines 13 - 15 | |
public function testGetCollectionOfTreasures(): void | |
{ | |
... lines 18 - 19 | |
$this->browser() | |
... lines 21 - 24 | |
->use(function(Json $json) { | |
dump($json->search('"hydra:member"')); | |
}) | |
; | |
} | |
} |
Ok! Run that test again:
symfony php bin/phpunit
It passes... but more importantly, look at the dump! It's the array of 5 items. Ok... let's grab the 0
index. After the hydra:member
double quotes, add [0]
. Then surround the entire thing with a keys()
function from JMESPath:
... lines 1 - 10 | |
class DragonTreasureResourceTest extends KernelTestCase | |
{ | |
... lines 13 - 15 | |
public function testGetCollectionOfTreasures(): void | |
{ | |
... lines 18 - 19 | |
$this->browser() | |
... lines 21 - 24 | |
->use(function(Json $json) { | |
dump($json->search('keys("hydra:member"[0])')); | |
}) | |
; | |
} | |
} |
Try that now:
symfony php bin/phpunit
Oh that's lovely. And it's probably one of the more complex things that you'll do. Now that we've got the path right, turn that into an assertion. You can do that by setting this to a variable - like $keys
- and using a normal assertion. Or you can change search
to assertMatches()
and pass a second argument: the array of the expected fields:
... lines 1 - 10 | |
class DragonTreasureResourceTest extends KernelTestCase | |
{ | |
... lines 13 - 15 | |
public function testGetCollectionOfTreasures(): void | |
{ | |
... lines 18 - 19 | |
$this->browser() | |
... lines 21 - 24 | |
->use(function(Json $json) { | |
$json->assertMatches('keys("hydra:member"[0])', [ | |
'@id', | |
'@type', | |
'name', | |
'description', | |
'value', | |
'coolFactor', | |
'owner', | |
'shortDescription', | |
'plunderedAtAgo', | |
]); | |
}) | |
; | |
} | |
} |
We should be good! Try it:
symfony php bin/phpunit
It passes! And yes, we could now remove the use()
method and move this to a normal ->assertJsonMatches()
call.
As cool as this JMESPath stuff is, it is another thing to learn and it can get complex. So what's the alternative?
Assign the entire $browser
chain to a new $json
variable and then add ->json()
to the end. Most methods on Browser
return... a Browser
, which let's us do all the fun chaining. But a few, like ->json()
let us "break out" of browser so we can do something custom.
This allows us to remove the use()
function here and replace the assertions with more traditional PHPUnit code:
... lines 1 - 10 | |
class DragonTreasureResourceTest extends KernelTestCase | |
{ | |
... lines 13 - 15 | |
public function testGetCollectionOfTreasures(): void | |
{ | |
DragonTreasureFactory::createMany(5); | |
$json = $this->browser() | |
->get('/api/treasures') | |
->assertJson() | |
->assertJsonMatches('"hydra:totalItems"', 5) | |
->assertJsonMatches('length("hydra:member")', 5) | |
->json() | |
; | |
$json->assertMatches('keys("hydra:member"[0])', [ | |
'@id', | |
'@type', | |
'name', | |
'description', | |
'value', | |
'coolFactor', | |
'owner', | |
'shortDescription', | |
'plunderedAtAgo', | |
]); | |
} | |
} |
We could still use the Json
object directly... that passes... or to remove all fanciness, change to $this->assertSame()
that $json->decoded()['hydra:member'][0]
- array_keys()
around everything - matches our array:
... lines 1 - 10 | |
class DragonTreasureResourceTest extends KernelTestCase | |
{ | |
... lines 13 - 15 | |
public function testGetCollectionOfTreasures(): void | |
{ | |
DragonTreasureFactory::createMany(5); | |
$json = $this->browser() | |
->get('/api/treasures') | |
->assertJson() | |
->assertJsonMatches('"hydra:totalItems"', 5) | |
->assertJsonMatches('length("hydra:member")', 5) | |
->json() | |
; | |
$this->assertSame(array_keys($json->decoded()['hydra:member'][0]), [ | |
'@id', | |
'@type', | |
'name', | |
'description', | |
'value', | |
'coolFactor', | |
'owner', | |
'shortDescription', | |
'plunderedAtAgo', | |
]); | |
} | |
} |
And of course... that passes to!
So, a lot of power... but also a lot of flexibility to write tests how you want.
Next, let's add tests for authentication: both logging in via our login form and via an API token.
"Houston: no signs of life"
Start the conversation!
// composer.json
{
"require": {
"php": ">=8.1",
"ext-ctype": "*",
"ext-iconv": "*",
"api-platform/core": "^3.0", // v3.1.2
"doctrine/annotations": "^2.0", // 2.0.1
"doctrine/doctrine-bundle": "^2.8", // 2.8.3
"doctrine/doctrine-migrations-bundle": "^3.2", // 3.2.2
"doctrine/orm": "^2.14", // 2.14.1
"nelmio/cors-bundle": "^2.2", // 2.2.0
"nesbot/carbon": "^2.64", // 2.66.0
"phpdocumentor/reflection-docblock": "^5.3", // 5.3.0
"phpstan/phpdoc-parser": "^1.15", // 1.16.1
"symfony/asset": "6.2.*", // v6.2.5
"symfony/console": "6.2.*", // v6.2.5
"symfony/dotenv": "6.2.*", // v6.2.5
"symfony/expression-language": "6.2.*", // v6.2.5
"symfony/flex": "^2", // v2.2.4
"symfony/framework-bundle": "6.2.*", // v6.2.5
"symfony/property-access": "6.2.*", // v6.2.5
"symfony/property-info": "6.2.*", // v6.2.5
"symfony/runtime": "6.2.*", // v6.2.5
"symfony/security-bundle": "6.2.*", // v6.2.6
"symfony/serializer": "6.2.*", // v6.2.5
"symfony/twig-bundle": "6.2.*", // v6.2.5
"symfony/ux-react": "^2.6", // v2.7.1
"symfony/ux-vue": "^2.7", // v2.7.1
"symfony/validator": "6.2.*", // v6.2.5
"symfony/webpack-encore-bundle": "^1.16", // v1.16.1
"symfony/yaml": "6.2.*" // v6.2.5
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.4", // 3.4.2
"mtdowling/jmespath.php": "^2.6", // 2.6.1
"phpunit/phpunit": "^9.5", // 9.6.3
"symfony/browser-kit": "6.2.*", // v6.2.5
"symfony/css-selector": "6.2.*", // v6.2.5
"symfony/debug-bundle": "6.2.*", // v6.2.5
"symfony/maker-bundle": "^1.48", // v1.48.0
"symfony/monolog-bundle": "^3.0", // v3.8.0
"symfony/phpunit-bridge": "^6.2", // v6.2.5
"symfony/stopwatch": "6.2.*", // v6.2.5
"symfony/web-profiler-bundle": "6.2.*", // v6.2.5
"zenstruck/browser": "^1.2", // v1.2.0
"zenstruck/foundry": "^1.26" // v1.28.0
}
}