Skip to content
This repository has been archived by the owner on Jul 19, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1 from Superbalist/add-unit-tests
Browse files Browse the repository at this point in the history
add unit tests
  • Loading branch information
matthewgoslett committed Jan 27, 2016
2 parents e52f254 + 4ce7cad commit af2c24d
Show file tree
Hide file tree
Showing 10 changed files with 1,812 additions and 0 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.0.2 - 2016-01-27

* Add unit tests

## 1.0.1 - 2016-01-27

* Renamed library from money-php to php-money
Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@
"branch-alias": {
"dev-master": "1.0-dev"
}
},
"require-dev": {
"phpunit/phpunit": "~4.0"
}
}
3 changes: 3 additions & 0 deletions phpunit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

include __DIR__.'/vendor/autoload.php';
29 changes: 29 additions & 0 deletions phpunit.xml
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>
32 changes: 32 additions & 0 deletions src/MockCurrencyConversionService.php
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',
);
}
}
}
67 changes: 67 additions & 0 deletions tests/CurrencyConversionTests.php
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);
}
}
87 changes: 87 additions & 0 deletions tests/CurrencyTests.php
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);
}
}
Loading

0 comments on commit af2c24d

Please sign in to comment.