Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: [Ellipsis] Feature: Mock a dynamic url #72

Closed
wants to merge 1 commit into from

Conversation

ellipsis-dev[bot]
Copy link
Contributor

@ellipsis-dev ellipsis-dev bot commented May 9, 2024

⚠️ Sorry, I timed out before I could complete this PR, so I've submitted my work in progress.

Summary:

Issue: resolves #23

Implementation:

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.

Here is a code snippet to illustrate this:

if (strpos($path, '*') !== false) {
    $path = str_replace('*', '.*', preg_quote($path, '/'));
}
$aliasPath = InternalServer::aliasPath($this->tmpDir, $path);

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.

@donatj donatj closed this May 10, 2024
@donatj donatj deleted the ellipsis/wflow_OqL91JEHeL6A9x8J_yTJ4 branch May 10, 2024 14:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature: Mock a dynamic url
1 participant