Skip to content

Commit

Permalink
Adding tests with MockServer
Browse files Browse the repository at this point in the history
  • Loading branch information
krzycho1997 committed Nov 10, 2023
1 parent 3c50497 commit 99ee7df
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
83 changes: 83 additions & 0 deletions tests/Core/FunctionalRetrofitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);

namespace Retrofit\Tests\Core;

use donatj\MockWebServer\MockWebServer;
use donatj\MockWebServer\Response;
use GuzzleHttp\Client;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Retrofit\Client\Guzzle7\Guzzle7HttpClient;
use Retrofit\Converter\SymfonySerializer\SymfonySerializerConverterFactory;
use Retrofit\Core\Retrofit;
use Retrofit\Tests\Fixtures\Api\FullyValidApi;
use Symfony\Component\Serializer\Serializer;

class FunctionalRetrofitTest extends TestCase
{
private MockWebServer $mockWebServer;

private FullyValidApi $fullyValidApi;

protected function setUp(): void
{
parent::setUp();

// $this->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);
}
}

0 comments on commit 99ee7df

Please sign in to comment.