From 5435578cd2d0fe91321e55ff754471a283124261 Mon Sep 17 00:00:00 2001 From: Tobias Nitsche Date: Tue, 31 Jan 2017 09:24:07 +0100 Subject: [PATCH 1/3] Guzzle Client injected --- Resources/config/methods.xml | 7 +++++++ Translate/Method.php | 11 ++++++----- Translate/Method/Translator.php | 12 +++++++----- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/Resources/config/methods.xml b/Resources/config/methods.xml index 5e18893..2c58e69 100644 --- a/Resources/config/methods.xml +++ b/Resources/config/methods.xml @@ -21,19 +21,26 @@ %eko_google_translate.api_key% + %eko_google_translate.api_key% + %eko_google_translate.api_key% + + + + + diff --git a/Translate/Method.php b/Translate/Method.php index ff22a7a..b049864 100644 --- a/Translate/Method.php +++ b/Translate/Method.php @@ -54,15 +54,16 @@ class Method protected $counter = 1; /** - * Constructor. + * Method constructor. * - * @param string $apiKey API key retrieved from configuration - * @param Stopwatch $stopwatch Symfony profiler Stopwatch service + * @param string $apiKey API key retrieved from configuration + * @param GuzzleClient $client + * @param Stopwatch $stopwatch Symfony profiler Stopwatch service */ - public function __construct($apiKey, Stopwatch $stopwatch = null) + public function __construct($apiKey, GuzzleClient $client, Stopwatch $stopwatch = null) { $this->apiKey = $apiKey; - $this->client = new GuzzleClient(); + $this->client = $client; $this->stopwatch = $stopwatch; } diff --git a/Translate/Method/Translator.php b/Translate/Method/Translator.php index 60fcfb0..dc6f62d 100644 --- a/Translate/Method/Translator.php +++ b/Translate/Method/Translator.php @@ -12,6 +12,7 @@ use Eko\GoogleTranslateBundle\Translate\Method; use Eko\GoogleTranslateBundle\Translate\MethodInterface; +use GuzzleHttp\Client as GuzzleClient; use Symfony\Component\Stopwatch\Stopwatch; /** @@ -49,15 +50,16 @@ class Translator extends Method implements MethodInterface /** * Constructor. * - * @param string $apiKey Google Translate API key - * @param Detector $detector A Detector service - * @param Stopwatch $stopwatch Symfony profiler stopwatch service + * @param string $apiKey Google Translate API key + * @param GuzzleClient $client + * @param Detector $detector A Detector service + * @param Stopwatch $stopwatch Symfony profiler stopwatch service */ - public function __construct($apiKey, Detector $detector, Stopwatch $stopwatch = null) + public function __construct($apiKey, GuzzleClient $client, Detector $detector, Stopwatch $stopwatch = null) { $this->detector = $detector; - return parent::__construct($apiKey, $stopwatch); + return parent::__construct($apiKey, $client, $stopwatch); } /** From 7f77e535a46af1091a7fd8515210d919298dd83c Mon Sep 17 00:00:00 2001 From: Tobias Nitsche Date: Tue, 31 Jan 2017 12:58:35 +0100 Subject: [PATCH 2/3] Tests fixed, and using ClientInterface --- Tests/Method/DetectorTest.php | 8 ++------ Tests/Method/LanguagesTest.php | 8 ++------ Tests/Method/TranslatorTest.php | 11 +++-------- Translate/Method.php | 19 +++++++++---------- Translate/Method/Translator.php | 12 ++++++------ 5 files changed, 22 insertions(+), 36 deletions(-) diff --git a/Tests/Method/DetectorTest.php b/Tests/Method/DetectorTest.php index 0478554..b1feaf8 100644 --- a/Tests/Method/DetectorTest.php +++ b/Tests/Method/DetectorTest.php @@ -28,13 +28,9 @@ protected function setUp() { $this->detector = $this->getMock( 'Eko\GoogleTranslateBundle\Translate\Method\Detector', - ['getClient'], - ['fakeapikey'] + null, + ['fakeapikey', $this->getClientMock()] ); - - $clientMock = $this->getClientMock(); - - $this->detector->expects($this->any())->method('getClient')->will($this->returnValue($clientMock)); } /** diff --git a/Tests/Method/LanguagesTest.php b/Tests/Method/LanguagesTest.php index ae15c7d..75f0184 100644 --- a/Tests/Method/LanguagesTest.php +++ b/Tests/Method/LanguagesTest.php @@ -28,13 +28,9 @@ protected function setUp() { $this->languages = $this->getMock( 'Eko\GoogleTranslateBundle\Translate\Method\Languages', - ['getClient'], - ['fakeapikey'] + null, + ['fakeapikey', $this->getClientMock()] ); - - $clientMock = $this->getClientMock(); - - $this->languages->expects($this->any())->method('getClient')->will($this->returnValue($clientMock)); } /** diff --git a/Tests/Method/TranslatorTest.php b/Tests/Method/TranslatorTest.php index 6929412..8dca3e8 100644 --- a/Tests/Method/TranslatorTest.php +++ b/Tests/Method/TranslatorTest.php @@ -28,13 +28,9 @@ protected function setUp() { $this->translator = $this->getMock( 'Eko\GoogleTranslateBundle\Translate\Method\Translator', - ['getClient'], - ['fakeapikey', $this->getDetectorMock()] + null, + ['fakeapikey', $this->getClientMock(), $this->getDetectorMock()] ); - - $clientMock = $this->getClientMock(); - - $this->translator->expects($this->any())->method('getClient')->will($this->returnValue($clientMock)); } /** @@ -70,8 +66,7 @@ public function testPlainTextTranslate() // Then $this->assertEquals($value, "J'ai", 'Should return "J\'ai"'); } - - + /** * Test multiple translate method using an array. */ diff --git a/Translate/Method.php b/Translate/Method.php index b049864..69b23e3 100644 --- a/Translate/Method.php +++ b/Translate/Method.php @@ -10,7 +10,7 @@ namespace Eko\GoogleTranslateBundle\Translate; -use GuzzleHttp\Client as GuzzleClient; +use GuzzleHttp\ClientInterface; use Symfony\Component\Stopwatch\Stopwatch; use Symfony\Component\Stopwatch\StopwatchEvent; @@ -29,7 +29,7 @@ class Method protected $apiKey = null; /** - * @var GuzzleClient A Guzzle client instance + * @var ClientInterface A Guzzle client instance */ protected $client; @@ -56,22 +56,21 @@ class Method /** * Method constructor. * - * @param string $apiKey API key retrieved from configuration - * @param GuzzleClient $client - * @param Stopwatch $stopwatch Symfony profiler Stopwatch service + * @param string $apiKey API key retrieved from configuration + * @param ClientInterface $client + * @param Stopwatch $stopwatch Symfony profiler Stopwatch service */ - public function __construct($apiKey, GuzzleClient $client, Stopwatch $stopwatch = null) + public function __construct($apiKey, ClientInterface $client, Stopwatch $stopwatch = null) { - $this->apiKey = $apiKey; - $this->client = $client; - + $this->apiKey = $apiKey; + $this->client = $client; $this->stopwatch = $stopwatch; } /** * Returns Guzzle HTTP client instance. * - * @return GuzzleClient + * @return ClientInterface */ public function getClient() { diff --git a/Translate/Method/Translator.php b/Translate/Method/Translator.php index dc6f62d..66a8e6e 100644 --- a/Translate/Method/Translator.php +++ b/Translate/Method/Translator.php @@ -12,7 +12,7 @@ use Eko\GoogleTranslateBundle\Translate\Method; use Eko\GoogleTranslateBundle\Translate\MethodInterface; -use GuzzleHttp\Client as GuzzleClient; +use GuzzleHttp\ClientInterface; use Symfony\Component\Stopwatch\Stopwatch; /** @@ -50,12 +50,12 @@ class Translator extends Method implements MethodInterface /** * Constructor. * - * @param string $apiKey Google Translate API key - * @param GuzzleClient $client - * @param Detector $detector A Detector service - * @param Stopwatch $stopwatch Symfony profiler stopwatch service + * @param string $apiKey Google Translate API key + * @param ClientInterface $client + * @param Detector $detector A Detector service + * @param Stopwatch $stopwatch Symfony profiler stopwatch service */ - public function __construct($apiKey, GuzzleClient $client, Detector $detector, Stopwatch $stopwatch = null) + public function __construct($apiKey, ClientInterface $client, Detector $detector, Stopwatch $stopwatch = null) { $this->detector = $detector; From 056bbff880ef6a3469c134d5efaf6c25e60715fc Mon Sep 17 00:00:00 2001 From: Tobias Nitsche Date: Tue, 31 Jan 2017 13:30:48 +0100 Subject: [PATCH 3/3] GuzzleClass is now a parameter --- Resources/config/methods.xml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Resources/config/methods.xml b/Resources/config/methods.xml index 2c58e69..63835f3 100644 --- a/Resources/config/methods.xml +++ b/Resources/config/methods.xml @@ -4,6 +4,10 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> + + GuzzleHttp\Client + + @@ -39,7 +43,7 @@ - +