From 157f5959cabf14caccf5bac7d9c988340c6ed2b8 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Thu, 26 Dec 2019 19:55:41 +0100 Subject: [PATCH] Add a symfony factory --- .travis.yml | 4 +- src/ClientFactory/SymfonyFactory.php | 46 +++++++++++++++++++ src/Resources/config/services.xml | 4 ++ .../Unit/ClientFactory/SymfonyFactoryTest.php | 30 ++++++++++++ 4 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 src/ClientFactory/SymfonyFactory.php create mode 100644 tests/Unit/ClientFactory/SymfonyFactoryTest.php diff --git a/.travis.yml b/.travis.yml index d924517d..0c4d931f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -39,9 +39,9 @@ matrix: - php: 7.3 env: SYMFONY_REQUIRE=4.3.* - php: 7.3 - env: SYMFONY_REQUIRE=4.4.* + env: SYMFONY_REQUIRE=4.4.* DEPENDENCIES="symfony/http-client:^4.4" - php: 7.3 - env: SYMFONY_REQUIRE=5.0.* + env: SYMFONY_REQUIRE=5.0.* DEPENDENCIES="symfony/http-client:^5.0" # Test with httplug 1.x clients - php: 7.2 diff --git a/src/ClientFactory/SymfonyFactory.php b/src/ClientFactory/SymfonyFactory.php new file mode 100644 index 00000000..14a26225 --- /dev/null +++ b/src/ClientFactory/SymfonyFactory.php @@ -0,0 +1,46 @@ + + */ +class SymfonyFactory implements ClientFactory +{ + /** + * @var ResponseFactoryInterface + */ + private $responseFactory; + + /** + * @var StreamFactoryInterface + */ + private $streamFactory; + + /** + * @param ResponseFactoryInterface $responseFactory + * @param StreamFactoryInterface $streamFactory + */ + public function __construct(ResponseFactoryInterface $responseFactory, StreamFactoryInterface $streamFactory) + { + $this->responseFactory = $responseFactory; + $this->streamFactory = $streamFactory; + } + + /** + * {@inheritdoc} + */ + public function createClient(array $config = []) + { + if (!class_exists(HttplugClient::class)) { + throw new \LogicException('To use the Symfony client you need to install the "symfony/http-client" package.'); + } + + return new HttplugClient(HttpClient::create($config), $this->responseFactory, $this->streamFactory); + } +} diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index d97d1215..a7d6929b 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -93,5 +93,9 @@ + + + + diff --git a/tests/Unit/ClientFactory/SymfonyFactoryTest.php b/tests/Unit/ClientFactory/SymfonyFactoryTest.php new file mode 100644 index 00000000..f6b726cf --- /dev/null +++ b/tests/Unit/ClientFactory/SymfonyFactoryTest.php @@ -0,0 +1,30 @@ + + */ +class SymfonyFactoryTest extends TestCase +{ + public function testCreateClient(): void + { + if (!class_exists(HttplugClient::class)) { + $this->markTestSkipped('Symfony Http client is not installed'); + } + + $factory = new SymfonyFactory( + $this->getMockBuilder(ResponseFactoryInterface::class)->getMock(), + $this->getMockBuilder(StreamFactoryInterface::class)->getMock() + ); + $client = $factory->createClient(); + + $this->assertInstanceOf(HttplugClient::class, $client); + } +}