Step 1: Modify the setResponseOfPath method in MockWebServer.php
Modify the setResponseOfPath method in the MockWebServer 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:
Check if the $path parameter contains a wildcard character.
If it does, replace the wildcard character with a regular expression that matches any sequence of characters.
Use this regular expression when storing the path alias.
Make sure to escape any other special characters in the path so that they are treated as literal characters and not as part of the regular expression.
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 the InternalServer 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:
Modify the part of the method that gets a reference for the request URI to also match paths with wildcard characters.
Use a regular expression to match the request URI to the mocked paths.
If a match is found, get the reference for the matched path and use it to get a response.
Here is a code snippet to illustrate this:
$ref = $this->getRefForUri($this->request->getParsedUri()['path']);
if ($ref === null) {
foreach ($this->mockedPaths as $mockedPath) {
if (preg_match('/' . $mockedPath . '/', $this->request->getParsedUri()['path'])) {
$ref = $this->getRefForUri($mockedPath);
break;
}
}
}
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:
Add a test that checks if a response is correctly returned for a path with a wildcard character.
In this test, use the setResponseOfPath method to set a response for a path with a wildcard character.
Then, make a request to a path that matches the path with the wildcard character and check if the correct response is returned.
Add a test that checks if a response is correctly returned for multiple paths with the same wildcard character.
In this test, use the setResponseOfPath method to set different responses for different paths with the same wildcard character.
Then, make requests to these paths and check if the correct responses are returned.
Here is a code snippet to illustrate this:
public function testWildcard() : void {
$url = self::$server->setResponseOfPath('/api/endpoint/product/*', new Response('abc'));
$content = file_get_contents($url . '/1');
$this->assertEquals('abc', $content);
}
public function testMultipleWildcards() : void {
$url1 = self::$server->setResponseOfPath('/api/endpoint/product/*', new Response('abc'));
$url2 = self::$server->setResponseOfPath('/api/endpoint/user/*', new Response('def'));
$content1 = file_get_contents($url1 . '/1');
$content2 = file_get_contents($url2 . '/2');
$this->assertEquals('abc', $content1);
$this->assertEquals('def', $content2);
}
These tests check if the correct response is returned for paths with wildcard characters.
Report:
Step
Result
Details
1
✅
Modify the setResponseOfPath method in MockWebServer.phpModified the setResponseOfPath method in the MockWebServer 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 the preg_quote function.
2
❌
Modify the __invoke method in InternalServer.phpNo result
3
❌
Add unit tests for the new wildcard functionalityNo result
Something 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.
:warning: Sorry, I timed out before I could complete this PR, so I've submitted my work in progress.
Summary:
Issue: resolves https://github.com/donatj/mock-webserver/issues/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.
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.