How to test a REST API with Behat and Mink in Symfony 2
Asked Answered
H

1

7

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!

Hertz answered 26/10, 2015 at 20:34 Comment(2)
@CarlosGranados yep, I already read that article. The problem is that makes a simple post request and I'd like to make an internal request the way mink does with symfonyHertz
Api request response testing with behat v2 includes json, xml, html and cli and Api request response testing with behat v1Keener
N
13

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

Nit answered 27/10, 2015 at 8:28 Comment(3)
What is the format of the $postdata? Is it key=>value array?Padus
The $postdata signature is in the symfony/browser-kit/Client.php. Yes, $postdata is an associative array.Churl
@KonstantinPereiaslov BrowserKit driver also supports POST. github.com/minkphp/MinkBrowserKitDriver/blob/master/src/…Cent

© 2022 - 2024 — McMap. All rights reserved.