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

Commit 3baa263

Browse files
authored
Merge pull request #1 from TomA-R/bcmath
Use BC Math functions and upgrade PHPUnit
2 parents 906b4d4 + 3c6d605 commit 3baa263

21 files changed

+102
-252
lines changed

.circleci/config.yml

-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,4 @@ jobs:
1313
- run:
1414
name: Run tests
1515
command: |
16-
sudo docker-php-ext-install bcmath
1716
sudo vendor/bin/phpunit -c tests/phpunit.xml.dist

composer.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,19 @@
2727
},
2828

2929
"require": {
30-
"php": ">=5.5.0"
30+
"php": ">=7.0.0",
31+
"ext-bcmath": "*"
3132
},
3233

3334
"require-dev": {
34-
"phpunit/phpunit": "4.8.*",
35+
"phpunit/phpunit": "9.5.*",
3536
"squizlabs/php_codesniffer": "2.2.*"
3637
},
3738

3839
"replace": {
3940
"triplepoint/php-units-of-measure": "*"
4041
},
41-
42+
4243
"autoload": {
4344
"psr-4": {
4445
"PhpUnitsOfMeasure\\": "source/"

source/AbstractPhysicalQuantity.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ public static function isUnitDefined($name)
246246
}
247247
return false;
248248
}
249-
249+
250250
/**
251251
* @see \PhpUnitsOfMeasure\PhysicalQuantityInterface::listAllUnits
252252
*/
@@ -262,7 +262,7 @@ public static function listAllUnits()
262262

263263
/**
264264
* Get the unit definition array
265-
* @return Array $unitDefinitions
265+
* @return array $unitDefinitions
266266
*/
267267
public static function getUnitDefinitions()
268268
{

source/HasSIUnitsTrait.php

-20
Original file line numberDiff line numberDiff line change
@@ -132,26 +132,6 @@ protected static function addMissingSIPrefixedUnits(
132132
'long_prefix' => 'pico',
133133
'factor' => 1e-12
134134
],
135-
[
136-
'abbr_prefix' => 'f',
137-
'long_prefix' => 'femto',
138-
'factor' => 1e-15
139-
],
140-
[
141-
'abbr_prefix' => 'a',
142-
'long_prefix' => 'atto',
143-
'factor' => 1e-18
144-
],
145-
[
146-
'abbr_prefix' => 'z',
147-
'long_prefix' => 'zepto',
148-
'factor' => 1e-21
149-
],
150-
[
151-
'abbr_prefix' => 'y',
152-
'long_prefix' => 'yocto',
153-
'factor' => 1e-24
154-
],
155135
];
156136

157137
// Determine the conversion factor from the no-prefix SI unit to the physical quantity's native unit

source/UnitOfMeasure.php

+31-3
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,39 @@ public static function linearUnitFactory($name, $toNativeUnitFactor)
2929
{
3030
return new static(
3131
$name,
32-
function ($valueInNativeUnit) use ($toNativeUnitFactor) {
33-
return $valueInNativeUnit / $toNativeUnitFactor;
32+
function ($valueInNativeUnit) use ($toNativeUnitFactor, $name) {
33+
return (float)bcdiv(
34+
(string)number_format(
35+
$valueInNativeUnit,
36+
16,
37+
'.',
38+
''
39+
),
40+
(string)number_format(
41+
$toNativeUnitFactor,
42+
16,
43+
'.',
44+
''
45+
),
46+
16
47+
);
3448
},
3549
function ($valueInThisUnit) use ($toNativeUnitFactor) {
36-
return $valueInThisUnit * $toNativeUnitFactor;
50+
return (float)bcmul(
51+
(string)number_format(
52+
$valueInThisUnit,
53+
16,
54+
'.',
55+
''
56+
),
57+
(string)number_format(
58+
$toNativeUnitFactor,
59+
16,
60+
'.',
61+
''
62+
),
63+
16
64+
);
3765
}
3866
);
3967
}

tests/AbstractPhysicalQuantityTest.php

+28-13
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace PhpUnitsOfMeasureTest;
46

5-
use PHPUnit_Framework_TestCase;
7+
use PHPUnit\Framework\TestCase;
68
use PhpUnitsOfMeasure\AbstractPhysicalQuantity;
79
use PhpUnitsOfMeasure\UnitOfMeasureInterface;
810
use PhpUnitsOfMeasure\Exception\PhysicalQuantityMismatch;
@@ -20,7 +22,7 @@
2022
*
2123
* @runTestsInSeparateProcesses
2224
*/
23-
class AbstractPhysicalQuantityTest extends PHPUnit_Framework_TestCase
25+
class AbstractPhysicalQuantityTest extends TestCase
2426
{
2527
protected function getTestUnitOfMeasure($name, $aliases = [])
2628
{
@@ -45,6 +47,8 @@ function ($value) use ($aliases) {
4547
*/
4648
public function testAddUnit()
4749
{
50+
$this->expectNotToPerformAssertions();
51+
4852
$newUnit = $this->getTestUnitOfMeasure('noconflict', ['definitelynoconflict']);
4953

5054
Wonkicity::addUnit($newUnit);
@@ -53,10 +57,11 @@ public function testAddUnit()
5357
/**
5458
* @dataProvider exceptionProducingUnitsProvider
5559
* @covers \PhpUnitsOfMeasure\AbstractPhysicalQuantity::addUnit
56-
* @expectedException \PhpUnitsOfMeasure\Exception\DuplicateUnitNameOrAlias
5760
*/
5861
public function testAddUnitFailsOnNameCollision($unitName, $unitAliases)
5962
{
63+
$this->expectException(\PhpUnitsOfMeasure\Exception\DuplicateUnitNameOrAlias::class);
64+
6065
$newUnit = $this->getTestUnitOfMeasure($unitName, $unitAliases);
6166

6267
Wonkicity::addUnit($newUnit);
@@ -78,6 +83,8 @@ public function testGetUnit()
7883
*/
7984
public function testGetUnitFailsOnUnknownUnit()
8085
{
86+
$this->expectException(\PhpUnitsOfMeasure\Exception\UnknownUnitOfMeasure::class);
87+
8188
Wonkicity::getUnit('someUnknownUnit');
8289
}
8390

@@ -86,6 +93,8 @@ public function testGetUnitFailsOnUnknownUnit()
8693
*/
8794
public function testInstantiateNewUnit()
8895
{
96+
$this->expectNotToPerformAssertions();
97+
8998
$value = new Wonkicity(1.234, 'quatloos');
9099
}
91100

@@ -95,28 +104,32 @@ public function testInstantiateNewUnit()
95104
*/
96105
public function testInstantiateNewUnitNonNumericValue()
97106
{
107+
$this->expectException(\PhpUnitsOfMeasure\Exception\NonNumericValue::class);
108+
98109
$value = new Wonkicity('string', 'quatloos');
99110
}
100111

101112
/**
102113
* @covers \PhpUnitsOfMeasure\AbstractPhysicalQuantity::__construct
103-
* @expectedException \PhpUnitsOfMeasure\Exception\NonStringUnitName
104114
*/
105115
public function testInstantiateNewUnitNonStringUnit()
106116
{
117+
$this->expectException(\PhpUnitsOfMeasure\Exception\NonStringUnitName::class);
118+
107119
$value = new Wonkicity(1.234, 42);
108120
}
109121

110122
/**
111123
* @dataProvider quantityConversionsProvider
112124
* @covers \PhpUnitsOfMeasure\AbstractPhysicalQuantity::toUnit
113-
* @expectedException \PhpUnitsOfMeasure\Exception\UnknownUnitOfMeasure
114125
*/
115126
public function testConvertToUnknownUnitThrowsException(
116127
AbstractPhysicalQuantity $value,
117128
$arbitraryUnit,
118129
$valueInArbitraryUnit
119130
) {
131+
$this->expectException(\PhpUnitsOfMeasure\Exception\UnknownUnitOfMeasure::class);
132+
120133
$value->toUnit('someUnknownUnit');
121134
}
122135

@@ -129,7 +142,7 @@ public function testUnitConvertsToArbitraryUnit(
129142
$arbitraryUnit,
130143
$valueInArbitraryUnit
131144
) {
132-
$this->assertSame($valueInArbitraryUnit, $value->toUnit($arbitraryUnit));
145+
$this->assertEquals($valueInArbitraryUnit, $value->toUnit($arbitraryUnit));
133146
}
134147

135148
/**
@@ -150,6 +163,8 @@ public function testSerialize(
150163
$arbitraryUnit,
151164
$valueInArbitraryUnit
152165
) {
166+
$this->expectNotToPerformAssertions();
167+
153168
serialize($value);
154169
}
155170

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

166-
$this->assertSame($valueInArbitraryUnit, $unserializedValue->toUnit($arbitraryUnit));
181+
$this->assertEquals($valueInArbitraryUnit, $unserializedValue->toUnit($arbitraryUnit));
167182
}
168183

169184
/**
@@ -178,7 +193,7 @@ public function testAdd(
178193
$diffString
179194
) {
180195
if ($shouldThrowException) {
181-
$this->setExpectedException('PhpUnitsOfMeasure\Exception\PhysicalQuantityMismatch');
196+
$this->expectException(\PhpUnitsOfMeasure\Exception\PhysicalQuantityMismatch::class);
182197
}
183198

184199
$sum = $firstValue->add($secondValue);
@@ -200,7 +215,7 @@ public function testSubtract(
200215
$diffString
201216
) {
202217
if ($shouldThrowException) {
203-
$this->setExpectedException('PhpUnitsOfMeasure\Exception\PhysicalQuantityMismatch');
218+
$this->expectException(\PhpUnitsOfMeasure\Exception\PhysicalQuantityMismatch::class);
204219
}
205220

206221
$difference = $firstValue->subtract($secondValue);
@@ -230,26 +245,26 @@ public function testIsUnitDefined()
230245
{
231246
$newUnit = $this->getTestUnitOfMeasure('noconflict', ['definitelynoconflict_1', 'definitelynoconflict_2']);
232247
Wonkicity::addUnit($newUnit);
233-
248+
234249
$someExistingUnits = ['u', 'uvees', 'v', 'vorp', 'noconflict', 'definitelynoconflict_1', 'definitelynoconflict_2'];
235250
$unexistingUnits = ['kg', 'l', 'definitelynoconflict_'];
236-
251+
237252
foreach ($someExistingUnits as $someExistingUnit) {
238253
$this->assertTrue(Wonkicity::isUnitDefined($someExistingUnit), "$someExistingUnit is not defined");
239254
}
240255
foreach ($unexistingUnits as $unexistingUnit) {
241256
$this->assertFalse(Wonkicity::isUnitDefined($unexistingUnit), "$unexistingUnit is not defined");
242257
}
243258
}
244-
259+
245260
/**
246261
* @covers \PhpUnitsOfMeasure\AbstractPhysicalQuantity::listAllUnits
247262
*/
248263
public function testListAllUnits()
249264
{
250265
$newUnit = $this->getTestUnitOfMeasure('noconflict', ['definitelynoconflict_1', 'definitelynoconflict_2']);
251266
Wonkicity::addUnit($newUnit);
252-
267+
253268
$allUnits = Wonkicity::listAllUnits();
254269
$expected = [];
255270
$expected['u'] = ['uvee', 'uvees'];

tests/DemonstrationTests.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PhpUnitsOfMeasureTest;
44

5+
use PHPUnit\Framework\TestCase;
56
use PHPUnit_Framework_TestCase;
67

78
use PhpUnitsOfMeasure\UnitOfMeasure;
@@ -32,7 +33,7 @@
3233
*
3334
* @runTestsInSeparateProcesses
3435
*/
35-
class DemonstrationTests extends PHPUnit_Framework_TestCase
36+
class DemonstrationTests extends TestCase
3637
{
3738
public function testInstantiatingQuantities()
3839
{

tests/PhysicalQuantity/AbstractPhysicalQuantityTestCase.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
namespace PhpUnitsOfMeasureTest\PhysicalQuantity;
44

5-
use PHPUnit_Framework_TestCase;
5+
use PHPUnit\Framework\TestCase;
6+
use PhpUnitsOfMeasure\PhysicalQuantityInterface;
67

78
/**
89
* This is a parent class for all the PhysicalQuantity childrens' test cases.
910
*/
10-
abstract class AbstractPhysicalQuantityTestCase extends PHPUnit_Framework_TestCase
11+
abstract class AbstractPhysicalQuantityTestCase extends TestCase
1112
{
1213
protected $supportedUnitsWithAliases = [];
1314

@@ -16,6 +17,8 @@ abstract class AbstractPhysicalQuantityTestCase extends PHPUnit_Framework_TestCa
1617
*/
1718
public function testConstructorSucceeds()
1819
{
20+
$this->expectNotToPerformAssertions();
21+
1922
$this->instantiateTestQuantity();
2023
}
2124

@@ -25,6 +28,8 @@ public function testConstructorSucceeds()
2528
*/
2629
public function testSupportedUnits()
2730
{
31+
$this->expectNotToPerformAssertions();
32+
2833
$quantity = $this->instantiateTestQuantity();
2934

3035
foreach ($this->supportedUnitsWithAliases as $unit) {

0 commit comments

Comments
 (0)