WIP: [Ellipsis] Feature: Mock a dynamic url #72
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary:
Issue: resolves #23
Implementation:
Step 1: Modify the setResponseOfPath method in MockWebServer.php
Modify the
setResponseOfPath
method in theMockWebServer
class to accept and handle wildcard characters. This method is currently used to set a specific response for a given path. You need to modify it so that it can also handle paths with wildcard characters. Here are the steps to do this:$path
parameter contains a wildcard character.Here is a code snippet to illustrate this:
This code replaces the wildcard character with a regular expression that matches any sequence of characters. The
preg_quote
function is used to escape any other special characters in the path.Step 2: Modify the __invoke method in InternalServer.php
Modify the
__invoke
method in theInternalServer
class to match the request path to the mocked paths with wildcard characters. This method currently uses the request URI to get a reference, and then uses this reference to get a response. If a response is found, it is sent. If no response is found, a default response is sent instead. Here are the steps to do this:Here is a code snippet to illustrate this:
This code tries to get a reference for the request URI as before. If no reference is found, it loops through the mocked paths and uses a regular expression to match the request URI to the mocked paths. If a match is found, it gets the reference for the matched path.
Step 3: Add unit tests for the new wildcard functionality
Add unit tests to ensure the new wildcard functionality works as expected. These tests should be added to the
MockWebServer_IntegrationTest.php
file. Here are the steps to do this:setResponseOfPath
method to set a response for a path with a wildcard character.setResponseOfPath
method to set different responses for different paths with the same wildcard character.Here is a code snippet to illustrate this:
These tests check if the correct response is returned for paths with wildcard characters.
Report:
Modify the setResponseOfPath method in MockWebServer.php
Modified thesetResponseOfPath
method in theMockWebServer
class to handle wildcard characters in the path. The method now checks if the$path
parameter contains a wildcard character, and if it does, it replaces the wildcard character with a regular expression that matches any sequence of characters, and escapes the rest of the path using thepreg_quote
function.Modify the __invoke method in InternalServer.php
No resultAdd unit tests for the new wildcard functionality
No resultSomething look wrong?: If this Pull Request doesn't contain the expected changes, add more information to #23. Then, try again. For more information, check the documentation.