Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/tests #1

Merged
merged 8 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ on:

jobs:
testsuite:
runs-on: ubuntu-18.04
runs-on: ubuntu-22.04
strategy:
fail-fast: false
matrix:
php-version: ['7.2', '7.3', '7.4', '8.0']
php-version: ['7.4', '8.0', '8.1', '8.2', '8.3']
db-type: [sqlite, mysql, pgsql]
prefer-lowest: ['']

Expand Down Expand Up @@ -66,7 +66,7 @@ jobs:
if [[ ${{ matrix.db-type }} == 'mysql' ]]; then export DB_URL='mysql://root:[email protected]/cakephp'; fi
if [[ ${{ matrix.db-type }} == 'pgsql' ]]; then export DB_URL='postgres://postgres:[email protected]/postgres'; fi
if [[ ${{ matrix.php-version }} == '7.4' ]]; then
export CODECOVERAGE=1 && vendor/bin/phpunit --verbose --coverage-clover=coverage.xml
export CODECOVERAGE=1 && vendor/bin/phpunit --coverage-clover=coverage.xml
else
vendor/bin/phpunit
fi
Expand All @@ -77,7 +77,7 @@ jobs:

cs-stan:
name: Coding Standard & Static Analysis
runs-on: ubuntu-18.04
runs-on: ubuntu-22.04

steps:
- uses: actions/checkout@v2
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
/config/Migrations/schema-dump-default.lock
/vendor/
/.idea/
/.phpunit.cache
42 changes: 21 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CakePHP Money Plugin
===================
# CakePHP Money Plugin

<p>
<a href="LICENSE" target="_blank">
<img alt="Software License" src="https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square">
Expand All @@ -22,45 +22,45 @@ It covers the following features:
* Store Money objects onto database (as strings)
* Marshal money objects into CakePHP entities.

Requirements
------------
## Requirements

* CakePHP 4.0.0+
* PHP 7.2+

Versions and branches
---------------------
## Versions and branches

| CakePHP | CakeDC Money Plugin | Notes |
| :-------------: | :------------------------: | :---- |
| ^4.5 | [1.x](https://github.com/cakedc/money/tree/1.next-cake4) | stable |
| ^4.0 | [0.0.1](https://github.com/cakedc/money/tree/0.0.1) | deprecated |

## Installation

| CakePHP | CakeDC Money Plugin | Tag | Notes |
| :-------------: | :------------------------: | :--: | :---- |
| ^4.0 | [0.1](https://github.com/cakedc/money/tree/1.next-cake4) | 0.0.1 | stable |
You can install this plugin into your CakePHP application using [composer](http://getcomposer.org).

The recommended way to install composer packages is:

Documentation
-------------
```
composer require cakedc/cakephp-money
```

## Documentation

For documentation, as well as tutorials, see the [Docs](Docs/Home.md) directory of this repository.

Support
-------
## Support

For bugs and feature requests, please use the [issues](https://github.com/CakeDC/money/issues) section of this repository.

Commercial support is also available, [contact us](https://www.cakedc.com/contact) for more information.

Contributing
------------
## Contributing

This repository follows the [CakeDC Plugin Standard](https://www.cakedc.com/plugin-standard).
If you'd like to contribute new features, enhancements or bug fixes to the plugin, please read our [Contribution Guidelines](https://www.cakedc.com/contribution-guidelines) for detailed instructions.

License
-------
## License

Copyright 2021 Cake Development Corporation (CakeDC). All rights reserved.

Licensed under the [MIT](http://www.opensource.org/licenses/mit-license.php) License. Redistributions of the source code included in this repository must retain the copyright notice found in each file.

## To Do

* Add Unit Tests
53 changes: 20 additions & 33 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,35 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
colors="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="tests/bootstrap.php"
>
<php>
<ini name="memory_limit" value="-1"/>
<ini name="apc.enable_cli" value="1"/>
</php>

<!-- Add any additional test suites you want to run here -->
<testsuites>
<testsuite name="CakeDC/Money">
<directory>tests/TestCase/</directory>
</testsuite>
</testsuites>

<!-- Setup a listener for fixtures -->
<listeners>
<listener class="Cake\TestSuite\Fixture\FixtureInjector">
<arguments>
<object class="Cake\TestSuite\Fixture\FixtureManager"/>
</arguments>
</listener>
</listeners>

<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" processIsolation="false" stopOnFailure="false" bootstrap="tests/bootstrap.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">src/</directory>
</include>
</coverage>
<php>
<ini name="memory_limit" value="-1"/>
<ini name="apc.enable_cli" value="1"/>
</php>
<!-- Add any additional test suites you want to run here -->
<testsuites>
<testsuite name="CakeDC/Money">
<directory>tests/TestCase/</directory>
</testsuite>
</testsuites>
<!-- Setup a listener for fixtures -->
<extensions>
<extension class="\Cake\TestSuite\Fixture\PHPUnitExtension"/>
</extensions>
</phpunit>
19 changes: 19 additions & 0 deletions src/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@
* @param $arguments
* @return false|mixed
*/
public function __call($name, $arguments)

Check failure on line 50 in src/Money.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

Method CakeDC\Money\Money::__call() has parameter $arguments with no typehint specified.

Check failure on line 50 in src/Money.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

Method CakeDC\Money\Money::__call() has parameter $name with no typehint specified.

Check failure on line 50 in src/Money.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

PHPDoc tag @param has invalid value ($arguments): Unexpected token "$arguments", expected type at offset 38

Check failure on line 50 in src/Money.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

PHPDoc tag @param has invalid value ($name): Unexpected token "$name", expected type at offset 18
{
$arguments = self::processArguments($arguments);

return call_user_func_array([$this->_money, $name], $arguments);
}

Expand All @@ -57,8 +59,10 @@
* @param $arguments
* @return false|mixed
*/
public static function __callStatic($name, $arguments)

Check failure on line 62 in src/Money.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

Method CakeDC\Money\Money::__callStatic() has parameter $arguments with no typehint specified.

Check failure on line 62 in src/Money.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

Method CakeDC\Money\Money::__callStatic() has parameter $name with no typehint specified.

Check failure on line 62 in src/Money.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

PHPDoc tag @param has invalid value ($arguments): Unexpected token "$arguments", expected type at offset 38

Check failure on line 62 in src/Money.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

PHPDoc tag @param has invalid value ($name): Unexpected token "$name", expected type at offset 18
{
$arguments = self::processArguments($arguments);

return new self(forward_static_call_array([MoneyPHP::class, $name], $arguments));
}

Expand All @@ -69,4 +73,19 @@
{
return MoneyUtil::format($this);
}

/**
* @param array $arguments
* @return array
*/
protected static function processArguments($arguments = [])
{
for ($i=0; $i < count($arguments); $i++) {
if ($arguments[$i] instanceof Money) {
$arguments[$i] = $arguments[$i]->getMoney();
}
}

return $arguments;
}
}
16 changes: 8 additions & 8 deletions src/Utility/MoneyUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,10 @@
namespace CakeDC\Money\Utility;

use Cake\Core\Configure;
use Cake\Error\Debugger;
use CakeDC\Accounting\Database\Type\MoneyType;
use Money\Currencies\BitcoinCurrencies;
use Money\Currencies\ISOCurrencies;
use Money\Currency;
use Money\Formatter\BitcoinMoneyFormatter;
use Money\Formatter\DecimalMoneyFormatter;
use Money\Formatter\IntlMoneyFormatter;
use CakeDC\Money\Money;
use Money\MoneyFormatter;
Expand All @@ -31,7 +28,9 @@
*/
class MoneyUtil
{
/** @var MoneyFormatter */
/**
* @var MoneyFormatter
*/
protected static $_moneyFormatters = [];

/**
Expand All @@ -56,16 +55,17 @@ public static function money($value, $fromDb = false) : ?Money
if (!isset($parts[1])) {
$parts[1] = '00';
}
$decimalLength = strlen($parts[1] ?? '') ;
$decimalLength = strlen($parts[1] ?? '');

if ($decimalLength == 1) {
$parts[1] = $parts[1] . '0';
}

$value = ltrim($parts[0] . $parts[1], '0');
}

return Money::USD(!empty($value) ? str_replace(',', '', $value) : 0);

$currency = Configure::read('Money.currency', 'USD');
return Money::{$currency}(!empty($value) ? str_replace(',', '', $value) : 0);
}

/**
Expand Down Expand Up @@ -108,7 +108,7 @@ protected static function _loadMoneyFormatter(Currency $currency) : MoneyFormatt
} elseif ($currency->isAvailableWithin($bitcoin)) {
$moneyFormatter = new BitcoinMoneyFormatter(7, $bitcoin);
} else {
throw new \RuntimeException(sprintf('Cannot format currency \'%s\'. Only ISO currencies and Bitcoin are allowed.'));
throw new \RuntimeException(sprintf('Cannot format currency \'%s\'. Only ISO currencies and Bitcoin are allowed.', $currency));
}
static::$_moneyFormatters[$currency->getCode()] = $moneyFormatter;
return static::$_moneyFormatters[$currency->getCode()];
Expand Down
10 changes: 6 additions & 4 deletions src/View/Helper/MoneyHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,16 @@ public function currency($value): string
{
$class = '';
if ($value instanceof Money) {
$value = MoneyUtil::format($value);
$output = MoneyUtil::format($value);
} else {
$value = $this->Number->currency($value);
$output = $this->Number->currency($value);
}
if ($value < 0) {
if ((is_numeric($value) && $value < 0) ||
($value instanceof Money && MoneyUtil::lessThanZero($value))
) {
$class = 'negative-balance';
}

return $this->Html->tag('span', $value, ['class' => $class]);
return $this->Html->tag('span', $output, ['class' => $class]);
}
}
72 changes: 72 additions & 0 deletions tests/TestCase/Database/Type/MoneyTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
declare(strict_types=1);

namespace CakeDC\Money\Test\TestCase\Utility;

use Cake\Core\Configure;
use Cake\TestSuite\TestCase;
use CakeDC\Money\Database\Type\MoneyType;
use CakeDC\Money\Utility\MoneyUtil;
use Exception;
use PDO;
use RuntimeException;

/**
* CakeDC\Money\Database\Type\MoneyTypeTest Test Case
*/
class MoneyTypeTest extends TestCase
{
/**
* setUp method
*
* @return void
*/
public function setUp(): void
{
$this->moneyType = new MoneyType();
$this->driver = $this->getMockBuilder('Cake\Database\Driver')->getMock();
parent::setUp();
}

/**
* tearDown method
*
* @return void
*/
public function tearDown(): void
{
parent::tearDown();
}

public function testToPhp()
{
$this->assertNull($this->moneyType->toPHP(null, $this->driver));
$this->assertInstanceOf(\CakeDC\Money\Money::class, $this->moneyType->toPHP(100, $this->driver));
}

public function testMarshal()
{
$this->assertNull($this->moneyType->marshal(null));
$this->assertInstanceOf(\CakeDC\Money\Money::class, $this->moneyType->marshal(100));
}

public function testToDatabase()
{
$this->assertNull($this->moneyType->toDatabase(null, $this->driver));
$this->assertEquals('10000', $this->moneyType->toDatabase(MoneyUtil::money(100), $this->driver));
}

public function testToDatabaseInvalidArgument()
{
$this->expectException(\InvalidArgumentException::class);
$this->moneyType->toDatabase(100, $this->driver);
}

public function testToStatement()
{
$this->assertEquals(PDO::PARAM_NULL, $this->moneyType->toStatement(null, $this->driver));
$this->assertEquals(PDO::PARAM_INT, $this->moneyType->toStatement(100, $this->driver));
}


}
Loading
Loading