commit 97eff2e1f23e7e9e3ff7fa207d0bc6f90193c157 Author: Alex Wright Date: Wed Aug 19 17:27:24 2020 +0200 Initial commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..5d6c654 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# A PHP Client for MockServer + +Connect to and control a [MockServer](https://www.mock-server.com/) instance to test your API clients and integrations. \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..74e256e --- /dev/null +++ b/composer.json @@ -0,0 +1,22 @@ +{ + "name": "xeen/mock-server-client", + "description": "A MockServer client", + "type": "library", + "authors": [ + { + "name": "Alex Wright", + "email": "alex@xeen.uk" + } + ], + "require": { + "guzzlehttp/guzzle": "^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^9.3" + }, + "autoload": { + "psr-4": { + "Xeen\\MockServerClient\\": "src/" + } + } +} \ No newline at end of file diff --git a/src/Client.php b/src/Client.php new file mode 100644 index 0000000..93c5ffe --- /dev/null +++ b/src/Client.php @@ -0,0 +1,52 @@ +uri = Uri::fromParts(parse_url($config['uri'])); + } else { + $this->uri = Uri::fromParts($config); + } + $this->apiUri = $this->uri->withPath('/mockserver/'); + $this->client = new HttpClient([ + 'base_uri' => $this->apiUri, + 'http_errors' => false, + ]); + } + + public function addExpectation($expectation) + { + $uri = 'expectation'; + return json_decode(static::handleResponse($this->client->put($uri, ['json' => $expectation]))->getBody()->getContents()); + } + + /** + * Reset the mock server. Returns true on success. + */ + public function reset(): bool + { + $uri = 'reset'; + return $this->client->put($uri)->getStatusCode() == 200; + } + + private static function handleResponse(Response $response) + { + if ($response->getStatusCode() >= 400) { + var_dump($response->getBody()->getContents()); + exit; + } + return $response; + } +} \ No newline at end of file diff --git a/src/Traits/MockServerTestCase.php b/src/Traits/MockServerTestCase.php new file mode 100644 index 0000000..ff1f04e --- /dev/null +++ b/src/Traits/MockServerTestCase.php @@ -0,0 +1,36 @@ +mockServerClient = new MockServerClient($this->mockServerConfig()); + } + + protected function mockServerConfig() + { + if (isset(self::$config) && isset(self::$config['mockserver'])) { + return self::$config['mockserver']; + } + throw new RuntimeException("No config for mockserver"); + } + + protected function setMockServerExpectation($expectaion) + { + return $this->mockServerClient->addExpectation($expectaion); + } + + protected function clearMockServerExpectaions() + { + $this->mockServerClient->reset(); + } +} \ No newline at end of file