Download the PHP package lequipefr/mockserver-behat-context without Composer
On this page you can find all versions of the php package lequipefr/mockserver-behat-context. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download lequipefr/mockserver-behat-context
More information about lequipefr/mockserver-behat-context
Files in lequipefr/mockserver-behat-context
Package mockserver-behat-context
Short Description Behat context for MockServer.
License MIT
Informations about the package mockserver-behat-context
MockServer Behat context
Php client and behat context for MockServer.
Install
Then add a context in your behat.yml
, with the url to your local MockServer instance:
Now, in your behat tests, you should be able to mock the webservices your project depends to, with:
Usage
Behat context
See all available behat phrases you can use.
PHP client
You can also use this library as a simple PHP client, and send your expectations as raw arrays, as defined in mockserver swagger api:
Builder
Instead of raw arrays, you can use the expectation builder and let your IDE autocomplete:
Configuration example
You may want to configure your project so that in test environment, external API are replaced by MockServer.
For example, you may have this kind of configuration:
.env
: points to real api
.env.test
: points to MockServer instance
Multiple services
If you have multiple apis or microservices and want to make sure a request is sent to the expected service, you can use one of these following trick:
Custom path prefix
.env
:
.env.test
:
This way, when running behat tests, your application will hit mockserver in any case, but with a different path prefix.
Then, your tests will looks like:
Custom domain name
One drawback of the solution above is that it can be difficult to concatenate a base host with a relative path if you didn't handled this case since the beginning, and always assumed that you hit your api at the root.
So in this case you can use different hosts name:
.env
:
.env.test
:
If you do the necessary in your hosts name resolution to make
any *.mockserver
requests will hit your localhost,
your mockserver instance will receive all requests with a different hostname
that you can match with the httpRequest.headers[name = 'Host']
parameter in expectation.
FAQ
I am still getting 404's when my application under test is trying to query mocked external service
For all trouble you get between your application and mockserver, you should use MockServer UI.
It is already installed and accessible here: http://127.0.0.1:1080/mockserver/dashboard.
If not, check MockServer UI documentation.
The UI displays useful information:
- Active expectations, the mocks received,
- Received Requests, the requests your tested application have sent to mockserver instead of the real external service
- Log messages, all mocks, requests received, did the mock have been sent, if not, why...
If you get 404's, you may check all this points:
- Did mockserver have received the mock ?
In "Active Expectations" you must see the mock you defined in your behat test.
If not, check if the mockserver url in behat.yml
valid and used:
- Did the appplication called mockserver
In "Received Requests", you must see the request sent by your tested application. If not, check where is sent the request then. You should configure your application to use mockserver as base host instead the external service, something like:
- Did the request matched the mock
If "Active Expectations" and "Received Requests" contains your mock and a received request, did the request matched the mock ?
Mockserver can contains multiple mocks and will response with the mock only if path, headers... matches.
To check that, in "Log Messages" section, you should see MATCHED_EXPECTATION
.
If not, check the log line about the received request, it will tell you with the request didn't matched the mock.
Sometimes it didn't match just because slash in path is like api/...
instead of /api/...
.
Contributing
See CONTRIBUTING.md.
License
This library is under MIT license.
All versions of mockserver-behat-context with dependencies
behat/behat Version ^3.0
behat/gherkin Version ^4.0
psr/http-client Version ^1.0
psr/http-message Version ^1.0|^2.0
php-http/discovery Version ^1.14
guzzlehttp/psr7 Version ^2.4