Home » Php » php – How to test a REST API with Behat and Mink in Symfony 2

php – How to test a REST API with Behat and Mink in Symfony 2

Posted by: admin July 12, 2020 Leave a comment

Questions:

I’m builing a REST API in Symfony and I’d like to test it with Behat (using Mink and the behat extension for symfony2). There’s no problem for the GET methods, I just “mock” some database objects, use the “I am on ” step definition and check the response.

But when it comes to test if the post of a certain element works I don’t know how to send post params with Mink. I know it could be done with i.e. Guzzle but I think it would be much better doing it through Mink and the Symfony extension.

What I’m looking for is the way to define a step such as

When I POST to <url> the following data:
| field1 | field2 | field3 |
| value1 | value2 | value3 |

Is there any easy way to send this using Mink? Thanks!

How to&Answers:

You should do it like this:

$session->getDriver()->getClient()->request ('POST', $url, $postdata);

This is what mink uses for its visit method only using get instead of post