Skip to content

Firefly III: a personal finances manager

Attempt to cover all cases in amount converter, this fixes #840

James Cole2dfe2ddaf311
diff --git a/app/Import/Converter/Amount.php b/app/Import/Converter/Amount.php
index bf1f23fdf9..89481e9347 100644
--- a/app/Import/Converter/Amount.php
+++ b/app/Import/Converter/Amount.php
@@ -36,13 +36,14 @@ class Amount implements ConverterInterface
*/
public function convert($value): string
{
- if(is_null($value)) {
+ if (is_null($value)) {
return '0';
}
$value = strval($value);
Log::debug(sprintf('Start with amount "%s"', $value));
$len = strlen($value);
$decimalPosition = $len - 3;
+ $altPosition = $len - 2;
$decimal = null;
if (($len > 2 && $value{$decimalPosition} === '.') || ($len > 2 && strpos($value, '.') > $decimalPosition)) {
@@ -53,6 +54,11 @@ class Amount implements ConverterInterface
$decimal = ',';
Log::debug(sprintf('Decimal character in "%s" seems to be a comma.', $value));
}
+ // decimal character is null? find out if "0.1" or ".1" or "0,1" or ",1"
+ if ($len > 1 && ($value{$altPosition} === '.' || $value{$altPosition} === ',')) {
+ $decimal = $value{$altPosition};
+ Log::debug(sprintf('Alternate search resulted in "%s" for decimal sign.', $decimal));
+ }
// if decimal is dot, replace all comma's and spaces with nothing. then parse as float (round to 4 pos)
if ($decimal === '.') {
diff --git a/app/Models/Transaction.php b/app/Models/Transaction.php
index 45462db855..2b11be8d60 100644
--- a/app/Models/Transaction.php
+++ b/app/Models/Transaction.php
@@ -32,13 +32,13 @@ use Watson\Validating\ValidatingTrait;
* @property int $foreign_currency_dp
*
* @property int $account_id
- * @property-read string $account_name
+ * @property string $account_name
* @property string $account_iban
* @property string $account_number
* @property string $account_bic
* @property string $account_currency_code
*
- * @property-read int $opposing_account_id
+ * @property int $opposing_account_id
* @property string $opposing_account_name
* @property string $opposing_account_iban
* @property string $opposing_account_number
@@ -46,10 +46,10 @@ use Watson\Validating\ValidatingTrait;
* @property string $opposing_currency_code
*
*
- * @property-read int $transaction_budget_id
- * @property-read string $transaction_budget_name
- * @property-read int $transaction_journal_budget_id
- * @property-read string $transaction_journal_budget_name
+ * @property int $transaction_budget_id
+ * @property string $transaction_budget_name
+ * @property int $transaction_journal_budget_id
+ * @property string $transaction_journal_budget_name
*
* @property-read int $transaction_category_id
* @property-read string $transaction_category_name
diff --git a/phpunit.coverage.specific.xml b/phpunit.coverage.specific.xml
new file mode 100644
index 0000000000..7bcd1ffa62
--- /dev/null
+++ b/phpunit.coverage.specific.xml
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<phpunit backupGlobals="false"
+ backupStaticAttributes="false"
+ bootstrap="vendor/autoload.php"
+ colors="true"
+ convertErrorsToExceptions="true"
+ convertNoticesToExceptions="true"
+ convertWarningsToExceptions="true"
+ processIsolation="false"
+ stopOnFailure="true">
+ <testsuites>
+ <testsuite name="Feature">
+ <directory suffix="Test.php">./tests/Feature</directory>
+ </testsuite>
+
+ <testsuite name="Unit">
+ <directory suffix="Test.php">./tests/Unit</directory>
+ </testsuite>
+ </testsuites>
+ <filter>
+ <whitelist processUncoveredFilesFromWhitelist="true">
+ <directory suffix=".php">./app</directory>
+ <exclude>
+ <file>app/Http/breadcrumbs.php</file>
+ </exclude>
+ </whitelist>
+ <blacklist>
+ <directory>vendor/</directory>
+ </blacklist>
+ </filter>
+ <logging>
+ <log type="coverage-clover" target="./storage/build/clover-specific.xml" charset="UTF-8"/>
+ </logging>
+ <php>
+ <env name="APP_ENV" value="testing"/>
+ <env name="CACHE_DRIVER" value="array"/>
+ <env name="SESSION_DRIVER" value="array"/>
+ <env name="QUEUE_DRIVER" value="sync"/>
+ </php>
+</phpunit>
diff --git a/tests/Unit/Import/Converter/AmountTest.php b/tests/Unit/Import/Converter/AmountTest.php
new file mode 100644
index 0000000000..1fbc3584a9
--- /dev/null
+++ b/tests/Unit/Import/Converter/AmountTest.php
@@ -0,0 +1,99 @@
+<?php
+/**
+ * AmountTest.php
+ * Copyright (c) 2017 [email protected]
+ * This software may be modified and distributed under the terms of the
+ * Creative Commons Attribution-ShareAlike 4.0 International License.
+ *
+ * See the LICENSE file for details.
+ */
+
+declare(strict_types=1);
+
+namespace Tests\Unit\Import\Converter;
+
+use FireflyIII\Import\Converter\Amount;
+use Tests\TestCase;
+
+/**
+ * Class AmountTest
+ *
+ * 0, 0.0, 0.1, 0.01
+ * 1, 1.0, 1.1, 1.12, 1.10
+ * 12, 12.3, 12.34
+ * 123, 123.4, 123.45
+ * 1234, 1234.5, 1234.56
+ * 1 234, 1 234.5, 1 234.56
+ * 1.234, 1.234.5, 1.234.56
+ *
+ *
+ *
+ *
+ *
+ * @package Tests\Unit\Import\Converter
+ */
+class AmountTest extends TestCase
+{
+ /**
+ *
+ */
+ public function testConvert()
+ {
+ $values = [
+ '0' => '0',
+ '0.0' => '0',
+ '0.1' => '0.1',
+ '.2' => '0.2',
+ '0.01' => '0.01',
+ '1' => '1',
+ '1.0' => '1',
+ '1.1' => '1.1',
+ '1.12' => '1.12',
+ '1.10' => '1.1',
+ '12' => '12',
+ '12.3' => '12.3',
+ '12.34' => '12.34',
+ '123' => '123',
+ '123.4' => '123.4',
+ '123.45' => '123.45',
+ '1234' => '1234',
+ '1234.5' => '1234.5',
+ '1234.56' => '1234.56',
+ '1 234' => '1234',
+ '1 234.5' => '1234.5',
+ '1 234.56' => '1234.56',
+ '1,234' => '1234',
+ '1,234.5' => '1234.5',
+ '1,234.56' => '1234.56',
+ '0,0' => '0',
+ '0,1' => '0.1',
+ ',2' => '0.2',
+ '0,01' => '0.01',
+ '1,0' => '1',
+ '1,1' => '1.1',
+ '1,12' => '1.12',
+ '1,10' => '1.1',
+ '12,3' => '12.3',
+ '12,34' => '12.34',
+ '123,4' => '123.4',
+ '123,45' => '123.45',
+ '1234,5' => '1234.5',
+ '1234,56' => '1234.56',
+ '1 234,5' => '1234.5',
+ '1 234,56' => '1234.56',
+ '1.234' => '1234',
+ '1.234,5' => '1234.5',
+ '1.234,56' => '1234.56',
+
+ ];
+ foreach ($values as $value => $expected) {
+ $converter = new Amount;
+ $result = $converter->convert($value);
+ $this->assertEquals($expected, $result);
+ }
+
+
+ }
+
+
+}
\ No newline at end of file