Skip to content
This repository has been archived by the owner on Dec 28, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1 from TomA-R/bcmath
Browse files Browse the repository at this point in the history
Use BC Math functions and upgrade PHPUnit
  • Loading branch information
TomA-R authored Oct 10, 2022
2 parents 906b4d4 + 3c6d605 commit 3baa263
Show file tree
Hide file tree
Showing 21 changed files with 102 additions and 252 deletions.
1 change: 0 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ jobs:
- run:
name: Run tests
command: |
sudo docker-php-ext-install bcmath
sudo vendor/bin/phpunit -c tests/phpunit.xml.dist
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,19 @@
},

"require": {
"php": ">=5.5.0"
"php": ">=7.0.0",
"ext-bcmath": "*"
},

"require-dev": {
"phpunit/phpunit": "4.8.*",
"phpunit/phpunit": "9.5.*",
"squizlabs/php_codesniffer": "2.2.*"
},

"replace": {
"triplepoint/php-units-of-measure": "*"
},

"autoload": {
"psr-4": {
"PhpUnitsOfMeasure\\": "source/"
Expand Down
4 changes: 2 additions & 2 deletions source/AbstractPhysicalQuantity.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public static function isUnitDefined($name)
}
return false;
}

/**
* @see \PhpUnitsOfMeasure\PhysicalQuantityInterface::listAllUnits
*/
Expand All @@ -262,7 +262,7 @@ public static function listAllUnits()

/**
* Get the unit definition array
* @return Array $unitDefinitions
* @return array $unitDefinitions
*/
public static function getUnitDefinitions()
{
Expand Down
20 changes: 0 additions & 20 deletions source/HasSIUnitsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,26 +132,6 @@ protected static function addMissingSIPrefixedUnits(
'long_prefix' => 'pico',
'factor' => 1e-12
],
[
'abbr_prefix' => 'f',
'long_prefix' => 'femto',
'factor' => 1e-15
],
[
'abbr_prefix' => 'a',
'long_prefix' => 'atto',
'factor' => 1e-18
],
[
'abbr_prefix' => 'z',
'long_prefix' => 'zepto',
'factor' => 1e-21
],
[
'abbr_prefix' => 'y',
'long_prefix' => 'yocto',
'factor' => 1e-24
],
];

// Determine the conversion factor from the no-prefix SI unit to the physical quantity's native unit
Expand Down
34 changes: 31 additions & 3 deletions source/UnitOfMeasure.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,39 @@ public static function linearUnitFactory($name, $toNativeUnitFactor)
{
return new static(
$name,
function ($valueInNativeUnit) use ($toNativeUnitFactor) {
return $valueInNativeUnit / $toNativeUnitFactor;
function ($valueInNativeUnit) use ($toNativeUnitFactor, $name) {
return (float)bcdiv(
(string)number_format(
$valueInNativeUnit,
16,
'.',
''
),
(string)number_format(
$toNativeUnitFactor,
16,
'.',
''
),
16
);
},
function ($valueInThisUnit) use ($toNativeUnitFactor) {
return $valueInThisUnit * $toNativeUnitFactor;
return (float)bcmul(
(string)number_format(
$valueInThisUnit,
16,
'.',
''
),
(string)number_format(
$toNativeUnitFactor,
16,
'.',
''
),
16
);
}
);
}
Expand Down
41 changes: 28 additions & 13 deletions tests/AbstractPhysicalQuantityTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

declare(strict_types=1);

namespace PhpUnitsOfMeasureTest;

use PHPUnit_Framework_TestCase;
use PHPUnit\Framework\TestCase;
use PhpUnitsOfMeasure\AbstractPhysicalQuantity;
use PhpUnitsOfMeasure\UnitOfMeasureInterface;
use PhpUnitsOfMeasure\Exception\PhysicalQuantityMismatch;
Expand All @@ -20,7 +22,7 @@
*
* @runTestsInSeparateProcesses
*/
class AbstractPhysicalQuantityTest extends PHPUnit_Framework_TestCase
class AbstractPhysicalQuantityTest extends TestCase
{
protected function getTestUnitOfMeasure($name, $aliases = [])
{
Expand All @@ -45,6 +47,8 @@ function ($value) use ($aliases) {
*/
public function testAddUnit()
{
$this->expectNotToPerformAssertions();

$newUnit = $this->getTestUnitOfMeasure('noconflict', ['definitelynoconflict']);

Wonkicity::addUnit($newUnit);
Expand All @@ -53,10 +57,11 @@ public function testAddUnit()
/**
* @dataProvider exceptionProducingUnitsProvider
* @covers \PhpUnitsOfMeasure\AbstractPhysicalQuantity::addUnit
* @expectedException \PhpUnitsOfMeasure\Exception\DuplicateUnitNameOrAlias
*/
public function testAddUnitFailsOnNameCollision($unitName, $unitAliases)
{
$this->expectException(\PhpUnitsOfMeasure\Exception\DuplicateUnitNameOrAlias::class);

$newUnit = $this->getTestUnitOfMeasure($unitName, $unitAliases);

Wonkicity::addUnit($newUnit);
Expand All @@ -78,6 +83,8 @@ public function testGetUnit()
*/
public function testGetUnitFailsOnUnknownUnit()
{
$this->expectException(\PhpUnitsOfMeasure\Exception\UnknownUnitOfMeasure::class);

Wonkicity::getUnit('someUnknownUnit');
}

Expand All @@ -86,6 +93,8 @@ public function testGetUnitFailsOnUnknownUnit()
*/
public function testInstantiateNewUnit()
{
$this->expectNotToPerformAssertions();

$value = new Wonkicity(1.234, 'quatloos');
}

Expand All @@ -95,28 +104,32 @@ public function testInstantiateNewUnit()
*/
public function testInstantiateNewUnitNonNumericValue()
{
$this->expectException(\PhpUnitsOfMeasure\Exception\NonNumericValue::class);

$value = new Wonkicity('string', 'quatloos');
}

/**
* @covers \PhpUnitsOfMeasure\AbstractPhysicalQuantity::__construct
* @expectedException \PhpUnitsOfMeasure\Exception\NonStringUnitName
*/
public function testInstantiateNewUnitNonStringUnit()
{
$this->expectException(\PhpUnitsOfMeasure\Exception\NonStringUnitName::class);

$value = new Wonkicity(1.234, 42);
}

/**
* @dataProvider quantityConversionsProvider
* @covers \PhpUnitsOfMeasure\AbstractPhysicalQuantity::toUnit
* @expectedException \PhpUnitsOfMeasure\Exception\UnknownUnitOfMeasure
*/
public function testConvertToUnknownUnitThrowsException(
AbstractPhysicalQuantity $value,
$arbitraryUnit,
$valueInArbitraryUnit
) {
$this->expectException(\PhpUnitsOfMeasure\Exception\UnknownUnitOfMeasure::class);

$value->toUnit('someUnknownUnit');
}

Expand All @@ -129,7 +142,7 @@ public function testUnitConvertsToArbitraryUnit(
$arbitraryUnit,
$valueInArbitraryUnit
) {
$this->assertSame($valueInArbitraryUnit, $value->toUnit($arbitraryUnit));
$this->assertEquals($valueInArbitraryUnit, $value->toUnit($arbitraryUnit));
}

/**
Expand All @@ -150,6 +163,8 @@ public function testSerialize(
$arbitraryUnit,
$valueInArbitraryUnit
) {
$this->expectNotToPerformAssertions();

serialize($value);
}

Expand All @@ -163,7 +178,7 @@ public function testUnserialize(
) {
$unserializedValue = unserialize(serialize($value));

$this->assertSame($valueInArbitraryUnit, $unserializedValue->toUnit($arbitraryUnit));
$this->assertEquals($valueInArbitraryUnit, $unserializedValue->toUnit($arbitraryUnit));
}

/**
Expand All @@ -178,7 +193,7 @@ public function testAdd(
$diffString
) {
if ($shouldThrowException) {
$this->setExpectedException('PhpUnitsOfMeasure\Exception\PhysicalQuantityMismatch');
$this->expectException(\PhpUnitsOfMeasure\Exception\PhysicalQuantityMismatch::class);
}

$sum = $firstValue->add($secondValue);
Expand All @@ -200,7 +215,7 @@ public function testSubtract(
$diffString
) {
if ($shouldThrowException) {
$this->setExpectedException('PhpUnitsOfMeasure\Exception\PhysicalQuantityMismatch');
$this->expectException(\PhpUnitsOfMeasure\Exception\PhysicalQuantityMismatch::class);
}

$difference = $firstValue->subtract($secondValue);
Expand Down Expand Up @@ -230,26 +245,26 @@ public function testIsUnitDefined()
{
$newUnit = $this->getTestUnitOfMeasure('noconflict', ['definitelynoconflict_1', 'definitelynoconflict_2']);
Wonkicity::addUnit($newUnit);

$someExistingUnits = ['u', 'uvees', 'v', 'vorp', 'noconflict', 'definitelynoconflict_1', 'definitelynoconflict_2'];
$unexistingUnits = ['kg', 'l', 'definitelynoconflict_'];

foreach ($someExistingUnits as $someExistingUnit) {
$this->assertTrue(Wonkicity::isUnitDefined($someExistingUnit), "$someExistingUnit is not defined");
}
foreach ($unexistingUnits as $unexistingUnit) {
$this->assertFalse(Wonkicity::isUnitDefined($unexistingUnit), "$unexistingUnit is not defined");
}
}

/**
* @covers \PhpUnitsOfMeasure\AbstractPhysicalQuantity::listAllUnits
*/
public function testListAllUnits()
{
$newUnit = $this->getTestUnitOfMeasure('noconflict', ['definitelynoconflict_1', 'definitelynoconflict_2']);
Wonkicity::addUnit($newUnit);

$allUnits = Wonkicity::listAllUnits();
$expected = [];
$expected['u'] = ['uvee', 'uvees'];
Expand Down
3 changes: 2 additions & 1 deletion tests/DemonstrationTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PhpUnitsOfMeasureTest;

use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_TestCase;

use PhpUnitsOfMeasure\UnitOfMeasure;
Expand Down Expand Up @@ -32,7 +33,7 @@
*
* @runTestsInSeparateProcesses
*/
class DemonstrationTests extends PHPUnit_Framework_TestCase
class DemonstrationTests extends TestCase
{
public function testInstantiatingQuantities()
{
Expand Down
9 changes: 7 additions & 2 deletions tests/PhysicalQuantity/AbstractPhysicalQuantityTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

namespace PhpUnitsOfMeasureTest\PhysicalQuantity;

use PHPUnit_Framework_TestCase;
use PHPUnit\Framework\TestCase;
use PhpUnitsOfMeasure\PhysicalQuantityInterface;

/**
* This is a parent class for all the PhysicalQuantity childrens' test cases.
*/
abstract class AbstractPhysicalQuantityTestCase extends PHPUnit_Framework_TestCase
abstract class AbstractPhysicalQuantityTestCase extends TestCase
{
protected $supportedUnitsWithAliases = [];

Expand All @@ -16,6 +17,8 @@ abstract class AbstractPhysicalQuantityTestCase extends PHPUnit_Framework_TestCa
*/
public function testConstructorSucceeds()
{
$this->expectNotToPerformAssertions();

$this->instantiateTestQuantity();
}

Expand All @@ -25,6 +28,8 @@ public function testConstructorSucceeds()
*/
public function testSupportedUnits()
{
$this->expectNotToPerformAssertions();

$quantity = $this->instantiateTestQuantity();

foreach ($this->supportedUnitsWithAliases as $unit) {
Expand Down
Loading

0 comments on commit 3baa263

Please sign in to comment.