Skip to content

Commit

Permalink
Merge pull request #16 from tobias-nitsche/master
Browse files Browse the repository at this point in the history
Guzzle Client injected
  • Loading branch information
eko committed Jan 31, 2017
2 parents 622195c + 056bbff commit 67f57ea
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 35 deletions.
11 changes: 11 additions & 0 deletions Resources/config/methods.xml
Expand Up @@ -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">

<parameters>
<parameter key="eko.guzzle_client.class">GuzzleHttp\Client</parameter>
</parameters>

<services>
<!-- Manager -->

Expand All @@ -21,19 +25,26 @@
<tag name="eko.google_translate.method" />

<argument>%eko_google_translate.api_key%</argument>
<argument type="service" id="eko.guzzle_client" />
</service>

<service id="eko.google_translate.languages" class="Eko\GoogleTranslateBundle\Translate\Method\Languages">
<tag name="eko.google_translate.method" />

<argument>%eko_google_translate.api_key%</argument>
<argument type="service" id="eko.guzzle_client" />
</service>

<service id="eko.google_translate.translator" class="Eko\GoogleTranslateBundle\Translate\Method\Translator">
<tag name="eko.google_translate.method" />

<argument>%eko_google_translate.api_key%</argument>
<argument type="service" id="eko.guzzle_client" />
<argument type="service" id="eko.google_translate.detector" />
</service>

<service id="eko.guzzle_client" class="%eko.guzzle_client.class%">

</service>
</services>
</container>
8 changes: 2 additions & 6 deletions Tests/Method/DetectorTest.php
Expand Up @@ -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));
}

/**
Expand Down
8 changes: 2 additions & 6 deletions Tests/Method/LanguagesTest.php
Expand Up @@ -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));
}

/**
Expand Down
11 changes: 3 additions & 8 deletions Tests/Method/TranslatorTest.php
Expand Up @@ -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));
}

/**
Expand Down Expand Up @@ -70,8 +66,7 @@ public function testPlainTextTranslate()
// Then
$this->assertEquals($value, "J'ai", 'Should return "J\'ai"');
}



/**
* Test multiple translate method using an array.
*/
Expand Down
20 changes: 10 additions & 10 deletions Translate/Method.php
Expand Up @@ -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;

Expand All @@ -29,7 +29,7 @@ class Method
protected $apiKey = null;

/**
* @var GuzzleClient A Guzzle client instance
* @var ClientInterface A Guzzle client instance
*/
protected $client;

Expand All @@ -54,23 +54,23 @@ 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 ClientInterface $client
* @param Stopwatch $stopwatch Symfony profiler Stopwatch service
*/
public function __construct($apiKey, Stopwatch $stopwatch = null)
public function __construct($apiKey, ClientInterface $client, Stopwatch $stopwatch = null)
{
$this->apiKey = $apiKey;
$this->client = new GuzzleClient();

$this->apiKey = $apiKey;
$this->client = $client;
$this->stopwatch = $stopwatch;
}

/**
* Returns Guzzle HTTP client instance.
*
* @return GuzzleClient
* @return ClientInterface
*/
public function getClient()
{
Expand Down
12 changes: 7 additions & 5 deletions Translate/Method/Translator.php
Expand Up @@ -12,6 +12,7 @@

use Eko\GoogleTranslateBundle\Translate\Method;
use Eko\GoogleTranslateBundle\Translate\MethodInterface;
use GuzzleHttp\ClientInterface;
use Symfony\Component\Stopwatch\Stopwatch;

/**
Expand Down Expand Up @@ -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 ClientInterface $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, ClientInterface $client, Detector $detector, Stopwatch $stopwatch = null)
{
$this->detector = $detector;

return parent::__construct($apiKey, $stopwatch);
return parent::__construct($apiKey, $client, $stopwatch);
}

/**
Expand Down

0 comments on commit 67f57ea

Please sign in to comment.