This repository has been archived by the owner on Jul 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
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 #1 from Superbalist/add-unit-tests
add unit tests
- Loading branch information
Showing
10 changed files
with
1,812 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,5 +20,8 @@ | |
"branch-alias": { | ||
"dev-master": "1.0-dev" | ||
} | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "~4.0" | ||
} | ||
} |
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,3 @@ | ||
<?php | ||
|
||
include __DIR__.'/vendor/autoload.php'; |
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,29 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit backupGlobals="false" | ||
backupStaticAttributes="false" | ||
bootstrap="./phpunit.php" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false" | ||
syntaxCheck="true" | ||
verbose="true" | ||
> | ||
<testsuites> | ||
<testsuite name="php-money/tests"> | ||
<directory suffix=".php">./tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
<whitelist> | ||
<directory suffix=".php">./src/</directory> | ||
</whitelist> | ||
</filter> | ||
<logging> | ||
<log type="coverage-text" target="php://stdout" showUncoveredFiles="true"/> | ||
<log type="coverage-html" target="coverage" showUncoveredFiles="true"/> | ||
<log type="coverage-clover" target="coverage.xml" showUncoveredFiles="true"/> | ||
</logging> | ||
</phpunit> |
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 namespace Superbalist\Money; | ||
|
||
class MockCurrencyConversionService extends BaseCurrencyConversionService { | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getName() | ||
{ | ||
return 'Mock'; | ||
} | ||
|
||
/** | ||
* @param Currency $currency | ||
* @return array | ||
*/ | ||
public function getConversionRatesTable(Currency $currency) | ||
{ | ||
switch ($currency->getCode()) { | ||
case 'USD': | ||
return array( | ||
'USD' => '1', | ||
'ZAR' => '12.07682', | ||
); | ||
default: // ZAR | ||
return array( | ||
'USD' => '0.082776', | ||
'ZAR' => '1', | ||
); | ||
} | ||
} | ||
} |
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,67 @@ | ||
<?php | ||
|
||
class CurrencyConversionTests extends PHPUnit_Framework_TestCase { | ||
|
||
/** | ||
* @return \Superbalist\Money\MockCurrencyConversionService | ||
*/ | ||
protected function makeMockService() | ||
{ | ||
return new \Superbalist\Money\MockCurrencyConversionService(); | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
public function testFactoryMakeCurrencyConversionService() | ||
{ | ||
$service = \Superbalist\Money\CurrencyConversionServiceFactory::make('OPENEXCHANGERATES'); | ||
$this->assertInstanceOf('\Superbalist\Money\CurrencyConversionServiceInterface', $service); | ||
$this->assertInstanceOf('\Superbalist\Money\OpenExchangeRatesCurrencyConversionService', $service); | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
public function testFactoryMakeDefaultCurrencyConversionService() | ||
{ | ||
$service = \Superbalist\Money\CurrencyConversionServiceFactory::makeDefault(); | ||
$this->assertInstanceOf('\Superbalist\Money\CurrencyConversionServiceInterface', $service); | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
public function testGetConversionRatesTable() | ||
{ | ||
$service = $this->makeMockService(); | ||
$currency = \Superbalist\Money\CurrencyFactory::make('ZAR'); | ||
$rates = $service->getConversionRatesTable($currency); | ||
$this->assertArrayHasKey('ZAR', $rates); | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
public function testGetConversionMultiplier() | ||
{ | ||
$service = $this->makeMockService(); | ||
$from = \Superbalist\Money\CurrencyFactory::make('ZAR'); | ||
$to = \Superbalist\Money\CurrencyFactory::make('USD'); | ||
$multiplier = $service->getConversionMultiplier($from, $to); | ||
$this->assertSame('0.082776', $multiplier); | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
public function testConvert() | ||
{ | ||
$service = $this->makeMockService(); | ||
$from = \Superbalist\Money\CurrencyFactory::make('ZAR'); | ||
$to = \Superbalist\Money\CurrencyFactory::make('USD'); | ||
|
||
$actual = $service->convert('1', $from, $to); | ||
$this->assertSame('0.082776', $actual); | ||
} | ||
} |
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,87 @@ | ||
<?php | ||
|
||
class CurrencyTests extends PHPUnit_Framework_TestCase { | ||
|
||
/** | ||
* | ||
*/ | ||
public function testFactoryMakeCurrency() | ||
{ | ||
$currency1 = \Superbalist\Money\CurrencyFactory::make('ZAR'); | ||
$this->assertInstanceOf('\Superbalist\Money\Currency', $currency1); | ||
$this->assertSame('ZAR', $currency1->getCode()); | ||
|
||
$currency2 = \Superbalist\Money\CurrencyFactory::make('USD'); | ||
$this->assertInstanceOf('\Superbalist\Money\Currency', $currency2); | ||
$this->assertSame('USD', $currency2->getCode()); | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
public function testFactoryMakeDefaultCurrency() | ||
{ | ||
$currency = \Superbalist\Money\CurrencyFactory::makeDefault(); | ||
$this->assertInstanceOf('\Superbalist\Money\Currency', $currency); | ||
$this->assertSame('ZAR', $currency->getCode()); | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
public function testFactoryMakeInvalidCurrency() | ||
{ | ||
$this->setExpectedException('\Superbalist\Money\CurrencyNotSupportedException'); | ||
\Superbalist\Money\CurrencyFactory::make('LOREM'); | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
public function testGetSupportedCurrencies() | ||
{ | ||
$currencies = \Superbalist\Money\CurrencyFactory::getAllSupported(); | ||
$this->assertArrayHasKey('ZAR', $currencies); | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
public function testEquals() | ||
{ | ||
$currency1 = \Superbalist\Money\CurrencyFactory::make('ZAR'); | ||
$currency2 = \Superbalist\Money\CurrencyFactory::make('ZAR'); | ||
$this->assertTrue($currency1->equals($currency2)); | ||
|
||
$currency3 = \Superbalist\Money\CurrencyFactory::make('USD'); | ||
$this->assertFalse($currency1->equals($currency3)); | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
public function testDisplay() | ||
{ | ||
$currency = \Superbalist\Money\CurrencyFactory::make('ZAR'); | ||
$actual = $currency->display('100'); | ||
$this->assertSame('R100.00', $actual); | ||
|
||
$currency = \Superbalist\Money\CurrencyFactory::make('USD'); | ||
$actual = $currency->display('55.67'); | ||
$this->assertSame('$55.67', $actual); | ||
|
||
$currency = \Superbalist\Money\CurrencyFactory::make('ZAR'); | ||
$actual = $currency->display('-123.67', 0); | ||
$this->assertSame('-R123', $actual); | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
public function testToString() | ||
{ | ||
$currency = \Superbalist\Money\CurrencyFactory::make('ZAR'); | ||
$str = (string) $currency; | ||
$this->assertSame('ZAR', $str); | ||
} | ||
} |
Oops, something went wrong.