donatj / mock-webserver

Simple mock web server in PHP for unit testing.
MIT License
131 stars 21 forks source link

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

Closed ellipsis-dev[bot] closed 3 months ago

ellipsis-dev[bot] commented 3 months ago

: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 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:

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:

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:

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.