Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from einorler/remainingTests
Document and Command tests
- Loading branch information
Showing
5 changed files
with
192 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the ONGR package. | ||
* | ||
* (c) NFQ Technologies UAB <info@nfq.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace ONGR\CurrencyExchangeBundle\Tests\Functional\Command; | ||
|
||
use ONGR\CurrencyExchangeBundle\Command\UpdateEsRatesCommand; | ||
use ONGR\CurrencyExchangeBundle\Driver\OpenExchangeRatesDriver; | ||
use ONGR\CurrencyExchangeBundle\Service\CurrencyRatesService; | ||
use ONGR\ElasticsearchBundle\Test\AbstractElasticsearchTestCase; | ||
use Symfony\Component\Console\Application; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Handler\MockHandler; | ||
use GuzzleHttp\HandlerStack; | ||
use GuzzleHttp\Psr7\Response; | ||
|
||
class UpdateEsRatesCommandTest extends AbstractElasticsearchTestCase | ||
{ | ||
/** | ||
* Tests the UpdateEsRates command | ||
*/ | ||
public function testUpdateEsRatesCommand() | ||
{ | ||
$this->setUpConfiguration(true); | ||
$app = new Application(); | ||
$command = new UpdateEsRatesCommand(); | ||
$command->setContainer($this->getContainer()); | ||
$app->add($command); | ||
$command = $app->find('ongr:currency:update'); | ||
$tester = new CommandTester($command); | ||
$tester->execute( | ||
[ | ||
'command' => $command->getName(), | ||
] | ||
); | ||
|
||
$this->assertContains( | ||
'Currency rates updated', | ||
$tester->getDisplay() | ||
); | ||
$this->assertEquals(0, $tester->getStatusCode(), 'Status code should be zero.'); | ||
} | ||
|
||
/** | ||
* Tests the UpdateEsRates command | ||
*/ | ||
public function testUpdateEsRatesCommandException() | ||
{ | ||
$this->setUpConfiguration(false); | ||
$app = new Application(); | ||
$command = new UpdateEsRatesCommand(); | ||
$command->setContainer($this->getContainer()); | ||
$app->add($command); | ||
$command = $app->find('ongr:currency:update'); | ||
$tester = new CommandTester($command); | ||
$tester->execute( | ||
[ | ||
'command' => $command->getName(), | ||
] | ||
); | ||
$this->assertContains( | ||
'Error ocurred during update.', | ||
$tester->getDisplay() | ||
); | ||
} | ||
|
||
/** | ||
* mocks http client to set a service to the container | ||
* | ||
* @param bool $loadInfo determines if the request will provide a response | ||
*/ | ||
public function setUpConfiguration($loadInfo) | ||
{ | ||
$response = [ | ||
'disclaimer' => 'https://openexchangerates.org/terms/', | ||
'license' => 'https://openexchangerates.org/license/', | ||
'timestamp' => 1449877801, | ||
'base' => 'USD', | ||
'rates' => [ | ||
'EUR' => 0.935485, | ||
'AFN' => 66.809999, | ||
'ALL' => 125.716501, | ||
'AMD' => 484.902502, | ||
'ANG' => 1.788575, | ||
'AOA' => 135.295998, | ||
'ARS' => 9.750101, | ||
'AUD' => 1.390866 | ||
] | ||
]; | ||
if ($loadInfo) { | ||
$response_json = json_encode($response); | ||
$mock = new MockHandler([ | ||
new Response(200, [], $response_json) | ||
]); | ||
} else { | ||
$mock = new MockHandler([new Response(400, [])]); | ||
} | ||
$handler = HandlerStack::create($mock); | ||
$client = new Client(['handler' => $handler]); | ||
|
||
$driver = new OpenExchangeRatesDriver( | ||
$client, | ||
'http://openexchangerates.org/api/latest.json' | ||
); | ||
$item = $this->getMock('Stash\Interfaces\ItemInterface'); | ||
$item->expects($this->any())->method('set'); | ||
$pool = $this->getMock('Stash\Interfaces\PoolInterface'); | ||
$pool->expects($this->any())->method('getItem')->with('ongr_currency')->willReturn($item); | ||
$currencyRateService = new CurrencyRatesService( | ||
$driver, | ||
$this->getManager(), | ||
$pool | ||
); | ||
$this->getContainer()->set('ongr_currency_exchange.currency_rates_service', $currencyRateService); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the ONGR package. | ||
* | ||
* (c) NFQ Technologies UAB <info@nfq.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace ONGR\CurrencyExchangeBundle\Tests\Unit\Document; | ||
|
||
use ONGR\CurrencyExchangeBundle\Document\CurrencyDocument; | ||
use ONGR\CurrencyExchangeBundle\Document\RatesObject; | ||
|
||
class CurrencyDocumentTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* Test getters and setters | ||
*/ | ||
public function testGettersAndSetters() | ||
{ | ||
$time = time(); | ||
$rates = new RatesObject(); | ||
$currency = new CurrencyDocument(); | ||
$currency->setCreatedAt($time); | ||
$currency->setRates($rates); | ||
$this->assertEquals($time, $currency->getCreatedAt()); | ||
$this->assertEquals($rates, $currency->getRates()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the ONGR package. | ||
* | ||
* (c) NFQ Technologies UAB <info@nfq.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace ONGR\CurrencyExchangeBundle\Tests\Unit\Document; | ||
|
||
use ONGR\CurrencyExchangeBundle\Document\RatesObject; | ||
|
||
class RatesObjectTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* Test getters and setters | ||
*/ | ||
public function testGettersAndSetters() | ||
{ | ||
$name = 'USD'; | ||
$value = 1.2543; | ||
$rate = new RatesObject(); | ||
$rate->setName($name); | ||
$rate->setValue($value); | ||
$this->assertEquals($name, $rate->getName()); | ||
$this->assertEquals($value, $rate->getValue()); | ||
} | ||
} |