Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ksdev-pl committed Jul 4, 2015
1 parent c525236 commit c1c344b
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 10 deletions.
141 changes: 131 additions & 10 deletions tests/NBPCurrencyConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,24 @@

class NBPCurrencyConverterTest extends \PHPUnit_Framework_TestCase
{
public function testConvert()
public function guzzleMock()
{
$rawContent = file_get_contents(__DIR__ . '/exchange_rates.xml');
$rawContent = file_get_contents(__DIR__ . '/test_rates.xml');
$guzzleMock = Mockery::mock('GuzzleHttp\Client')
->shouldReceive('get')
->andReturn(Mockery::self())
->shouldReceive('getStatusCode')
->andReturn(200)
->shouldReceive('getBody')
->andReturn($rawContent)
->mock();

$guzzleMock = Mockery::mock('GuzzleHttp\Client');
$guzzleMock->shouldReceive('get')
->andReturn(Mockery::self());
$guzzleMock->shouldReceive('getStatusCode')
->andReturn(200);
$guzzleMock->shouldReceive('getBody')
->andReturn($rawContent);
return $guzzleMock;
}

$converter = new NBPCurrencyConverter($guzzleMock, null, 0);
public function testConvert()
{
$converter = new NBPCurrencyConverter($this->guzzleMock(), null, 0);

$this->assertEquals('123.4567', $converter->convert('123.4567', 'PLN', 'PLN')['amount']);
$this->assertEquals('32.7246', $converter->convert('123.4567', 'PLN', 'USD')['amount']);
Expand All @@ -35,6 +40,122 @@ public function testConvert()
$this->assertEquals('123.4567', $converter->convert('123.4567', 'JPY', 'JPY')['amount']);
}

public function testInvalidFormatOfAmount()
{
$converter = new NBPCurrencyConverter($this->guzzleMock(), null, 0);

$numExceptions = 0;
try {
$converter->convert('123.45', 'PLN', 'USD');
} catch (\Exception $e) {
$this->assertEquals('Invalid format of amount', $e->getMessage());
$numExceptions++;
}
try {
$converter->convert('123,4567', 'PLN', 'USD');
} catch (\Exception $e) {
$this->assertEquals('Invalid format of amount', $e->getMessage());
$numExceptions++;
}
try {
$converter->convert('.4567', 'PLN', 'USD');
} catch (\Exception $e) {
$this->assertEquals('Invalid format of amount', $e->getMessage());
$numExceptions++;
}
try {
$converter->convert('1234567', 'PLN', 'USD');
} catch (\Exception $e) {
$this->assertEquals('Invalid format of amount', $e->getMessage());
$numExceptions++;
}

$this->assertEquals(4, $numExceptions);
}

/**
* @expectedException \Exception
* @expectedExceptionMessage Invalid currency code
*/
public function testInvalidCurrencyCode()
{
$converter = new NBPCurrencyConverter($this->guzzleMock(), null, 0);
$converter->convert('123.4567', 'ABC', 'USD');
}

/**
* @expectedException \Exception
* @expectedExceptionMessage Invalid cache path
*/
public function testInvalidCachePath()
{
new NBPCurrencyConverter($this->guzzleMock(), '/this/folder/doesnotexist');
}

/**
* @expectedException \Exception
* @expectedExceptionMessage String could not be parsed as XML
*/
public function testInvalidXml()
{
$guzzleMock = Mockery::mock('GuzzleHttp\Client')
->shouldReceive('get')
->andReturn(Mockery::self())
->shouldReceive('getStatusCode')
->andReturn(200)
->shouldReceive('getBody')
->andReturn('Hello! I\'m XML')
->mock();

$converter = new NBPCurrencyConverter($guzzleMock, null, 0);
$converter->convert('123.4567', 'PLN', 'USD');
}

/**
* @expectedException \Exception
* @expectedExceptionMessage Invalid response status code: 500 A good reason
*/
public function testInvalidResponseStatusCode()
{
$guzzleMock = Mockery::mock('GuzzleHttp\Client')
->shouldReceive('get')
->andReturn(Mockery::self())
->shouldReceive('getStatusCode')
->andReturn(500)
->shouldReceive('getReasonPhrase')
->andReturn('A good reason')
->mock();

$converter = new NBPCurrencyConverter($guzzleMock, null, 0);
$converter->convert('123.4567', 'PLN', 'USD');
}

public function testCache()
{
$rawContent = file_get_contents(__DIR__ . '/test_rates.xml');
$guzzleMock = Mockery::mock('GuzzleHttp\Client')
->shouldReceive('get')
->once()
->andReturn(Mockery::self())
->shouldReceive('getStatusCode')
->once()
->andReturn(200)
->shouldReceive('getBody')
->once()
->andReturn($rawContent)
->mock();

$converter = new NBPCurrencyConverter($guzzleMock, __DIR__);
$converter->convert('123.4567', 'PLN', 'USD');

$pathToFile = __DIR__ . '/exchange_rates.xml';
$this->assertFileExists($pathToFile);

$converter->convert('123.4567', 'PLN', 'USD');

unlink($pathToFile);
}

public function tearDown()
{
Mockery::close();
Expand Down
File renamed without changes.

0 comments on commit c1c344b

Please sign in to comment.