From 99ee7df2ab0dc6c54fb6b417f4a243ded9eaf7ec Mon Sep 17 00:00:00 2001 From: Krzysztof Piorkowski Date: Fri, 10 Nov 2023 18:08:42 +0100 Subject: [PATCH] Adding tests with MockServer --- composer.json | 1 + tests/Core/FunctionalRetrofitTest.php | 83 +++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 tests/Core/FunctionalRetrofitTest.php diff --git a/composer.json b/composer.json index 77b0481..b2cfc09 100644 --- a/composer.json +++ b/composer.json @@ -27,6 +27,7 @@ "symfony/serializer": "^5.4 || ^6.3" }, "require-dev": { + "donatj/mock-webserver": "^2.7", "phpunit/phpunit": "^10.2", "symfony/property-access": "^5.4 || ^6.3" }, diff --git a/tests/Core/FunctionalRetrofitTest.php b/tests/Core/FunctionalRetrofitTest.php new file mode 100644 index 0000000..7d3f70c --- /dev/null +++ b/tests/Core/FunctionalRetrofitTest.php @@ -0,0 +1,83 @@ +mockWebServer = new MockWebServer(); +// $this->mockWebServer->start(); + +// $this->fullyValidApi = $this->createFullyValidApiMock(); + } + + protected function tearDown(): void + { + parent::tearDown(); + + $this->mockWebServer->stop(); + } + + #[Test] + public function shouldGenerateProperResponse(): void + { + // given + $this->mockWebServer = new MockWebServer(); + $this->mockWebServer->start(); + $url = $this->mockWebServer->setResponseOfPath('/info/sample', new Response('foo bar content')); + + $retrofit = Retrofit::Builder() + ->baseUrl($url) + ->client(new Guzzle7HttpClient(new Client())) + ->addConverterFactory(new SymfonySerializerConverterFactory(new Serializer())) + ->build(); + + /** @var FullyValidApi $service */ + $this->fullyValidApi = $retrofit->create(FullyValidApi::class); + + $content = file_get_contents($url); + +// $this->fullyValidApi = $this->createFullyValidApiMock(); + + $call = $this->fullyValidApi->getInfo('sample-user'); + + // when + $result = $call->request(); + + // then + $body = $result->getBody(); + $this->assertEquals('foo bar content', $body->getContents()); + } + + private function createFullyValidApiMock(): FullyValidApi + { + $retrofit = Retrofit::Builder() + ->baseUrl($this->mockWebServer->getServerRoot()) + ->client(new Guzzle7HttpClient(new Client())) + ->addConverterFactory(new SymfonySerializerConverterFactory(new Serializer())) + ->build(); + + /** @var FullyValidApi $service */ + return $retrofit->create(FullyValidApi::class); + } +}