|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2017-05-26 10:04 UTC] danack@php.net
-Status: Open
+Status: Not a bug
[2017-05-26 10:04 UTC] danack@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 18:00:01 2025 UTC |
Description: ------------ <?php declare(strict_types=1); namespace App\Traits; /** * Class SanitizerHelper * * @package App\Traits */ trait SanitizerHelperTrait { /** * @param array $data * @param array $fields * * @return array */ public function decimalFormat(array $data, array $fields) { foreach ($fields as $field) { $value = array_get($data, $field, null); if ($value) { $value = ((float) $value) * 100; $data[$field] = (int) $value; } } return $data; } } Test script: --------------- <?php namespace Tests\Unit\Traits; use App\SanitizerHelperTrait; /** * Class SanitizerHelperTest * * @package Tests\Unit\Traits */ class SanitizerHelperTest extends \PHPUnit_Framework_TestCase { use SanitizerHelperTrait; /** * @param $data * @param $expected * * @covers \App\Traits\SanitizerHelperTrait::decimalFormat() * @dataProvider getDecimalDataProvider */ public function test_Decimal_Format($data, $expected) { static::assertEquals($expected, $this->decimalFormat($data, array_keys($data))); } public function getDecimalDataProvider() { return [ '#1 decimal' => [ 'data' => [ 'discountAmount' => 17.99, ], 'expected' => [ 'discountAmount' => 1799, ], ], '#2 decimal' => [ 'data' => [ 'arAmount' => 10.02, ], 'expected' => [ 'arAmount' => 1002, ], ], '#3 decimal' => [ 'data' => [ 'gotAmount' => 10.02, ], 'expected' => [ 'gotAmount' => 1002, ], ], '#5 No decimal' => [ 'data' => [ 'arAmount' => 200, ], 'expected' => [ 'arAmount' => 20000, ], ], '#6 Null' => [ 'data' => [ 'arAmount' => null, ], 'expected' => [ 'arAmount' => 0, ], ], ]; } } Expected result: ---------------- 1799 Actual result: -------------- 1798