From c1c344b0cf52e2cc6dd086eaf1d52636efecbf5b Mon Sep 17 00:00:00 2001 From: ksdev Date: Sat, 4 Jul 2015 23:45:39 +0200 Subject: [PATCH] Tests --- tests/NBPCurrencyConverterTest.php | 141 +++++++++++++++++-- tests/{exchange_rates.xml => test_rates.xml} | 0 2 files changed, 131 insertions(+), 10 deletions(-) rename tests/{exchange_rates.xml => test_rates.xml} (100%) diff --git a/tests/NBPCurrencyConverterTest.php b/tests/NBPCurrencyConverterTest.php index 4328c51..b2aaf4c 100644 --- a/tests/NBPCurrencyConverterTest.php +++ b/tests/NBPCurrencyConverterTest.php @@ -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']); @@ -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(); diff --git a/tests/exchange_rates.xml b/tests/test_rates.xml similarity index 100% rename from tests/exchange_rates.xml rename to tests/test_rates.xml