donatj / mock-webserver

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

Load responses from JSON object or file #9

Closed stevenrombauts closed 6 years ago

stevenrombauts commented 6 years ago

Purpose

This PR makes it possible to add paths and their responses through a JSON file or JSON object. It adds the MockWebServer::load($json) method and the static ResponseInterface::create() function in order to do so.

Example

<?php
$server = new MockWebServer;
$server->start();

$json = <<<EOF
{
    "/foo/bar": {
        "GET": {
            "body": {
                "bar": "foo"
            }
        }
    }
}
EOF;

$server->load($json);

For a complete example, please see the tests/deserialize.php example.

Tests

New tests are added in the DeserializationTest file.

Misc

If there are any changes you'd like to see to this PR, just let me know. Thanks again :)

donatj commented 6 years ago

While I honestly appreciate the the time and effort that went into this, I do not believe this is functionality I am interested in having or maintaining.

Additionally I feel this functionality would be better implemented as a system of Builders and Factories external to the classes themselves - as they do not need private access to anything within the classes themselves. Single responsibility principle and what not.

stevenrombauts commented 6 years ago

No problem, @donatj, thank you for considering it. I can put this logic in my own code as well. Using a Builder would be a clean solution to this problem, I might give that another try in my own code then.