Transform¶
If you need to transform the data during the test you can call the transform method with a callable as the first argument for the transformation. It will return a new Tester with the transformed data to test.
Let’s you have a json string as data, that you want to test the content, it will look like this:
(new Tester('{"key":"value"}'))
->transform('json_decode')
->path('key')->assertSame('value');
Ideally you should test your data before transforming it:
(new Tester('{"key":"value"}'))
->assertJson()
->transform('json_decode')
->path('key')->assertSame('value');
If you would like to transform the data but not with the default values of callable you can simply create a custom callable with the appropriate option. Let say you want json_decode with a associative array:
(new Tester('{"key":"value"}'))
->assertJson()
->transform(
function ($data) {
return json_decode($data, true);
}
)->path('[key]')->assertSame('value');
Take a note that since it’s a associative array the path must be change from key to [key].